• 欢迎访问吴爸爸的技术小木屋

stm32f103软硬件CRC

stm32 wutianjun5858@163.com 5年前 (2020-06-10) 1817次浏览 0个评论

1. h文件

  1. /******************************************************************************
  2. * 插座
  3. * (c) Copyright 2016, RD Department, SHFZWKEJI Industry Co.Ltd
  4. * All Rights Reserved
  5. *
  6. * FileName : CRC.h
  7. * Description :
  8. * Version : V1.0
  9. * Function List:
  10. * 1.CRC校验算法
  11. * 2:CRC多项式 X32+X26+X23+X22+X16+X12+X11+X10+X8+X7+X5+X4+X2+X+1
  12. * 3:CRC掩码 0x04C11DB7
  13. *------------------------------Revision History-------------------------------
  14. * No. Version Date Revised By Item Description
  15. * 1 V1.0 2017-03-08 Liu Qi Sen 插座 Original Version
  16. ******************************************************************************/
  17. /******************************************************************************
  18. * Multi-Include-Prevent *
  19. ******************************************************************************/
  20. #ifndef _CRC_H_
  21. #define _CRC_H_
  22. #ifdef __cplusplus
  23. extern "C"{
  24. #endif
  25. /******************************************************************************
  26. * Debug Switch Section *
  27. ******************************************************************************/
  28. #define DEBUG_CRC
  29. /******************************************************************************
  30. * Include File Section *
  31. ******************************************************************************/
  32. #include "typedef.h"
  33. /******************************************************************************
  34. * Pubilc Macro Define Section *
  35. ******************************************************************************/
  36. #ifndef _CRC_C_
  37. #define CRC_EXT extern
  38. #else
  39. #define CRC_EXT
  40. #endif
  41. /******************************************************************************
  42. * Struct Define Section *
  43. ******************************************************************************/
  44. /******************************************************************************
  45. * Global Variable Declare Section *
  46. ******************************************************************************/
  47. /******************************************************************************
  48. * Function Declare Section *
  49. ******************************************************************************/
  50. CRC_EXT void CRC_ModuleInit(void);
  51. CRC_EXT uint32 CRC_8BitHardwareCalc(uint8 *buf, uint32 len);
  52. CRC_EXT uint32 CRC_8BitHardwareCalc_ota(uint8 *buf, uint32 len);
  53. CRC_EXT uint32 CRC_32BitHardwareCalc(uint32 *buf, uint32 len);
  54. CRC_EXT void CRC_ResetValue(void);
  55. #ifdef __cplusplus
  56. }
  57. #endif
  58. #endif
  59. /*********** (C) COPYRIGHT 2016 SHFZWKEJI Industry ******** END OF FILE *******/

2. c文件

  1. /******************************************************************************
  2. * 插座
  3. * (c) Copyright 2017, RD Department, SHFZWKEJI Industry Co.Ltd
  4. * All Rights Reserved
  5. *
  6. * FileName : CRC.c
  7. * Description :
  8. * Version : V1.0
  9. * Function List:
  10. * 1.CRC校验算法
  11. * 2:CRC多项式 X32+X26+X23+X22+X16+X12+X11+X10+X8+X7+X5+X4+X2+X+1
  12. * 2:CRC掩码 0x04C11DB7
  13. *------------------------------Revision History-------------------------------
  14. * No. Version Date Revised By Item Description
  15. * 1 V1.0 2017-03-21 Liuqisen 插座 Original Version
  16. ******************************************************************************/
  17. /******************************************************************************
  18. * Include File Section *
  19. ******************************************************************************/
  20. #define _CRC_C_
  21. #include "CRC.h"
  22. #undef _CRC_C_
  23. /******************************************************************************
  24. * Private Macro Define Section *
  25. ******************************************************************************/
  26. /******************************************************************************
  27. * Struct Define Section *
  28. ******************************************************************************/
  29. /******************************************************************************
  30. * Prototype Declare Section *
  31. ******************************************************************************/
  32. /******************************************************************************
  33. * Global Variable Declare Section *
  34. ******************************************************************************/
  35. /******************************************************************************
  36. * File Static Variable Declare Section *
  37. ******************************************************************************/
  38. /******************************************************************************
  39. * Function Declare Section *
  40. ******************************************************************************/
  41. void CRC_ResetValue(void);
  42. /*============================== External Function ===========================*/
  43. /******************************************************************************
  44. * Function Name:
  45. * Decription :CRC 硬件模块初始化
  46. * Calls :
  47. * Called By :
  48. * Input :
  49. * Output :
  50. * Return Value :
  51. * Others :
  52. *-------------------------------- Revision History ---------------------------
  53. * No. Version Date Revised by Item Description
  54. * 1 V1.0 2017-03-21 Liu qi sen 插座 Original Version
  55. ******************************************************************************/
  56. void CRC_ModuleInit(void)
  57. {
  58. /* 打开CRC时钟 */
  59. RCC_AHBPeriphClockCmd(RCC_AHBPeriph_CRC, ENABLE);
  60. /* 复位CRC模块 */
  61. CRC_ResetValue();
  62. }
  63. /******************************************************************************
  64. * Function Name:
  65. * Decription :CRC 8bit硬件计算
  66. * Calls :
  67. * Called By :
  68. * Input :
  69. * Output :
  70. * Return Value :
  71. * Others :
  72. *-------------------------------- Revision History ---------------------------
  73. * No. Version Date Revised by Item Description
  74. * 1 V1.0 2017-03-21 Liu qi sen 插座 Original Version
  75. ******************************************************************************/
  76. #if 0
  77. uint32 CRC_8BitHardwareCalc(uint8 *buf, uint32 len)
  78. {
  79. uint32 loop;
  80. uint8 buff[4] = { 0xff,0xff,0xff,0xff };
  81. uint32 *p32 = (uint32 *)buff;
  82. CRC_ResetValue();
  83. for (loop = 0; loop < len; loop++)
  84. {
  85. buff[3] = buf[loop];
  86. CRC->DR = *p32;
  87. }
  88. return CRC->DR;
  89. }
  90. #endif
  91. uint32 CRC_8BitHardwareCalc(uint8 *buf, uint32 len)
  92. {
  93. uint32 index = 0;
  94. CRC_ResetValue();
  95. for (index = 0; index < len; index++)
  96. {
  97. CRC->DR = (uint32)buf[index];
  98. }
  99. return (CRC->DR);
  100. }
  101. //crc校验 wtj
  102. uint32 CRC_8BitHardwareCalc_ota(uint8 *buf, uint32 len)
  103. {
  104. uint32 index = 0;
  105. //CRC_ResetValue();
  106. for (index = 0; index < len; index++)
  107. {
  108. CRC->DR = (uint32)buf[index];
  109. }
  110. return (CRC->DR);
  111. }
  112. /******************************************************************************
  113. * Function Name:
  114. * Decription :CRC 32bit硬件计算
  115. * Calls :
  116. * Called By :
  117. * Input :
  118. * Output :
  119. * Return Value :
  120. * Others :
  121. *-------------------------------- Revision History ---------------------------
  122. * No. Version Date Revised by Item Description
  123. * 1 V1.0 2017-03-21 Liu qi sen 插座 Original Version
  124. ******************************************************************************/
  125. uint32 CRC_32BitHardwareCalc(uint32 *buf, uint32 len)
  126. {
  127. uint32 loop;
  128. CRC_ResetValue();
  129. for (loop = 0; loop < len; loop++)
  130. {
  131. CRC->DR = buf[loop];
  132. }
  133. return CRC->DR;
  134. }
  135. /*============================== Internal Function ===========================*/
  136. /******************************************************************************
  137. * Function Name:
  138. * Decription :CRC 硬件模块复位
  139. * Calls :
  140. * Called By :
  141. * Input :
  142. * Output :
  143. * Return Value :
  144. * Others :
  145. *-------------------------------- Revision History ---------------------------
  146. * No. Version Date Revised by Item Description
  147. * 1 V1.0 2017-03-21 Liu qi sen 插座 Original Version
  148. ******************************************************************************/
  149. void CRC_ResetValue(void)
  150. {
  151. CRC->CR = 1; //复位CRC寄存器
  152. }
  153. /******************************************************************************
  154. * Function Name:
  155. * Decription :软件CRC8bit计算
  156. * Calls :
  157. * Called By :
  158. * Input :
  159. * Output :
  160. * Return Value :
  161. * Others :
  162. *-------------------------------- Revision History ---------------------------
  163. * No. Version Date Revised by Item Description
  164. * 1 V1.0 2017-03-21 Liu qi sen 插座 Original Version
  165. ******************************************************************************/
  166. #if 0
  167. static uint32 CRC_8BitSoftwareCalc(uint8 *buf, uint32 len)
  168. {
  169. uint32 xbit;
  170. uint32 data;
  171. uint32 CRC32 = 0xFFFFFFFF; //初值
  172. uint32 bits;
  173. uint32 loop;
  174. uint32 buff[4] = { 0xff,0xff,0xff,0xff };
  175. uint32 *p32 = (u32 *)buff;
  176. const uint32 dwPolynomial = 0x04c11db7;
  177. for (loop = 0; loop < len; loop++)
  178. {
  179. xbit = 1 << 31;
  180. buff[3] = buf[i];
  181. data = *p32;
  182. for (bits = 0; bits < 32; bits++)
  183. {
  184. if (CRC32 & 0x80000000)
  185. {
  186. CRC32 <<= 1;
  187. CRC32 ^= dwPolynomial;
  188. }
  189. else
  190. {
  191. CRC32 <<= 1;
  192. }
  193. if (data & xbit)
  194. {
  195. CRC32 ^= dwPolynomial;
  196. }
  197. xbit >>= 1;
  198. }
  199. }
  200. return CRC32;
  201. }
  202. #endif
  203. /******************************************************************************
  204. * Function Name:
  205. * Decription :软件CRC32bit计算
  206. * Calls :
  207. * Called By :
  208. * Input :
  209. * Output :
  210. * Return Value :
  211. * Others :
  212. *-------------------------------- Revision History ---------------------------
  213. * No. Version Date Revised by Item Description
  214. * 1 V1.0 2017-03-21 Liu qi sen 插座 Original Version
  215. ******************************************************************************/
  216. #if 0
  217. static uint32 CRC_32BitSoftwareCalc(uint32 *buf, uint32 len)
  218. {
  219. uint32 xbit;
  220. uint32 data;
  221. uint32 CRC32 = 0xFFFFFFFF;
  222. uint32 bits;
  223. const u32 dwPolynomial = 0x04c11db7;
  224. uint32 loop;
  225. for (loop = 0; loop < len; loop++)
  226. {
  227. xbit = 1 << 31;
  228. data = buf[i];
  229. for (bits = 0; bits < 32; bits++)
  230. {
  231. if (CRC32 & 0x80000000)
  232. {
  233. CRC32 <<= 1;
  234. CRC32 ^= dwPolynomial;
  235. }
  236. else
  237. {
  238. CRC32 <<= 1;
  239. }
  240. if (data & xbit)
  241. {
  242. CRC32 ^= dwPolynomial;
  243. }
  244. xbit >>= 1;
  245. }
  246. }
  247. return CRC32;
  248. }
  249. #endif
  250. /*********** (C) COPYRIGHT 2017 SHFZWKEJI Industry ******** END OF FILE ***********/

吴爸爸的技术小木屋 , 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:stm32f103软硬件CRC
喜欢 (0)
发表我的评论
取消评论
表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址