stm32f0xx_hal_crc.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  1. /**
  2. ******************************************************************************
  3. * @file stm32f0xx_hal_crc.c
  4. * @author MCD Application Team
  5. * @brief CRC HAL module driver.
  6. * This file provides firmware functions to manage the following
  7. * functionalities of the Cyclic Redundancy Check (CRC) peripheral:
  8. * + Initialization and de-initialization functions
  9. * + Peripheral Control functions
  10. * + Peripheral State functions
  11. *
  12. ******************************************************************************
  13. * @attention
  14. *
  15. * Copyright (c) 2016 STMicroelectronics.
  16. * All rights reserved.
  17. *
  18. * This software is licensed under terms that can be found in the LICENSE file
  19. * in the root directory of this software component.
  20. * If no LICENSE file comes with this software, it is provided AS-IS.
  21. *
  22. ******************************************************************************
  23. @verbatim
  24. ===============================================================================
  25. ##### How to use this driver #####
  26. ===============================================================================
  27. [..]
  28. (+) Enable CRC AHB clock using __HAL_RCC_CRC_CLK_ENABLE();
  29. (+) Initialize CRC calculator
  30. (++) specify generating polynomial (peripheral default or non-default one)
  31. (++) specify initialization value (peripheral default or non-default one)
  32. (++) specify input data format
  33. (++) specify input or output data inversion mode if any
  34. (+) Use HAL_CRC_Accumulate() function to compute the CRC value of the
  35. input data buffer starting with the previously computed CRC as
  36. initialization value
  37. (+) Use HAL_CRC_Calculate() function to compute the CRC value of the
  38. input data buffer starting with the defined initialization value
  39. (default or non-default) to initiate CRC calculation
  40. @endverbatim
  41. ******************************************************************************
  42. */
  43. /* Includes ------------------------------------------------------------------*/
  44. #include "stm32f0xx_hal.h"
  45. /** @addtogroup STM32F0xx_HAL_Driver
  46. * @{
  47. */
  48. /** @defgroup CRC CRC
  49. * @brief CRC HAL module driver.
  50. * @{
  51. */
  52. #ifdef HAL_CRC_MODULE_ENABLED
  53. /* Private typedef -----------------------------------------------------------*/
  54. /* Private define ------------------------------------------------------------*/
  55. /* Private macro -------------------------------------------------------------*/
  56. /* Private variables ---------------------------------------------------------*/
  57. /* Private function prototypes -----------------------------------------------*/
  58. /** @defgroup CRC_Private_Functions CRC Private Functions
  59. * @{
  60. */
  61. static uint32_t CRC_Handle_8(CRC_HandleTypeDef *hcrc, uint8_t pBuffer[], uint32_t BufferLength);
  62. static uint32_t CRC_Handle_16(CRC_HandleTypeDef *hcrc, uint16_t pBuffer[], uint32_t BufferLength);
  63. /**
  64. * @}
  65. */
  66. /* Exported functions --------------------------------------------------------*/
  67. /** @defgroup CRC_Exported_Functions CRC Exported Functions
  68. * @{
  69. */
  70. /** @defgroup CRC_Exported_Functions_Group1 Initialization and de-initialization functions
  71. * @brief Initialization and Configuration functions.
  72. *
  73. @verbatim
  74. ===============================================================================
  75. ##### Initialization and de-initialization functions #####
  76. ===============================================================================
  77. [..] This section provides functions allowing to:
  78. (+) Initialize the CRC according to the specified parameters
  79. in the CRC_InitTypeDef and create the associated handle
  80. (+) DeInitialize the CRC peripheral
  81. (+) Initialize the CRC MSP (MCU Specific Package)
  82. (+) DeInitialize the CRC MSP
  83. @endverbatim
  84. * @{
  85. */
  86. /**
  87. * @brief Initialize the CRC according to the specified
  88. * parameters in the CRC_InitTypeDef and create the associated handle.
  89. * @param hcrc CRC handle
  90. * @retval HAL status
  91. */
  92. HAL_StatusTypeDef HAL_CRC_Init(CRC_HandleTypeDef *hcrc)
  93. {
  94. /* Check the CRC handle allocation */
  95. if (hcrc == NULL)
  96. {
  97. return HAL_ERROR;
  98. }
  99. /* Check the parameters */
  100. assert_param(IS_CRC_ALL_INSTANCE(hcrc->Instance));
  101. if (hcrc->State == HAL_CRC_STATE_RESET)
  102. {
  103. /* Allocate lock resource and initialize it */
  104. hcrc->Lock = HAL_UNLOCKED;
  105. /* Init the low level hardware */
  106. HAL_CRC_MspInit(hcrc);
  107. }
  108. hcrc->State = HAL_CRC_STATE_BUSY;
  109. #if defined(CRC_POL_POL)
  110. /* check whether or not non-default generating polynomial has been
  111. * picked up by user */
  112. assert_param(IS_DEFAULT_POLYNOMIAL(hcrc->Init.DefaultPolynomialUse));
  113. if (hcrc->Init.DefaultPolynomialUse == DEFAULT_POLYNOMIAL_ENABLE)
  114. {
  115. /* initialize peripheral with default generating polynomial */
  116. WRITE_REG(hcrc->Instance->POL, DEFAULT_CRC32_POLY);
  117. MODIFY_REG(hcrc->Instance->CR, CRC_CR_POLYSIZE, CRC_POLYLENGTH_32B);
  118. }
  119. else
  120. {
  121. /* initialize CRC peripheral with generating polynomial defined by user */
  122. if (HAL_CRCEx_Polynomial_Set(hcrc, hcrc->Init.GeneratingPolynomial, hcrc->Init.CRCLength) != HAL_OK)
  123. {
  124. return HAL_ERROR;
  125. }
  126. }
  127. #endif /* CRC_POL_POL */
  128. /* check whether or not non-default CRC initial value has been
  129. * picked up by user */
  130. assert_param(IS_DEFAULT_INIT_VALUE(hcrc->Init.DefaultInitValueUse));
  131. if (hcrc->Init.DefaultInitValueUse == DEFAULT_INIT_VALUE_ENABLE)
  132. {
  133. WRITE_REG(hcrc->Instance->INIT, DEFAULT_CRC_INITVALUE);
  134. }
  135. else
  136. {
  137. WRITE_REG(hcrc->Instance->INIT, hcrc->Init.InitValue);
  138. }
  139. /* set input data inversion mode */
  140. assert_param(IS_CRC_INPUTDATA_INVERSION_MODE(hcrc->Init.InputDataInversionMode));
  141. MODIFY_REG(hcrc->Instance->CR, CRC_CR_REV_IN, hcrc->Init.InputDataInversionMode);
  142. /* set output data inversion mode */
  143. assert_param(IS_CRC_OUTPUTDATA_INVERSION_MODE(hcrc->Init.OutputDataInversionMode));
  144. MODIFY_REG(hcrc->Instance->CR, CRC_CR_REV_OUT, hcrc->Init.OutputDataInversionMode);
  145. /* makes sure the input data format (bytes, halfwords or words stream)
  146. * is properly specified by user */
  147. assert_param(IS_CRC_INPUTDATA_FORMAT(hcrc->InputDataFormat));
  148. /* Change CRC peripheral state */
  149. hcrc->State = HAL_CRC_STATE_READY;
  150. /* Return function status */
  151. return HAL_OK;
  152. }
  153. /**
  154. * @brief DeInitialize the CRC peripheral.
  155. * @param hcrc CRC handle
  156. * @retval HAL status
  157. */
  158. HAL_StatusTypeDef HAL_CRC_DeInit(CRC_HandleTypeDef *hcrc)
  159. {
  160. /* Check the CRC handle allocation */
  161. if (hcrc == NULL)
  162. {
  163. return HAL_ERROR;
  164. }
  165. /* Check the parameters */
  166. assert_param(IS_CRC_ALL_INSTANCE(hcrc->Instance));
  167. /* Check the CRC peripheral state */
  168. if (hcrc->State == HAL_CRC_STATE_BUSY)
  169. {
  170. return HAL_BUSY;
  171. }
  172. /* Change CRC peripheral state */
  173. hcrc->State = HAL_CRC_STATE_BUSY;
  174. /* Reset CRC calculation unit */
  175. __HAL_CRC_DR_RESET(hcrc);
  176. /* Reset IDR register content */
  177. __HAL_CRC_SET_IDR(hcrc, 0);
  178. /* DeInit the low level hardware */
  179. HAL_CRC_MspDeInit(hcrc);
  180. /* Change CRC peripheral state */
  181. hcrc->State = HAL_CRC_STATE_RESET;
  182. /* Process unlocked */
  183. __HAL_UNLOCK(hcrc);
  184. /* Return function status */
  185. return HAL_OK;
  186. }
  187. /**
  188. * @brief Initializes the CRC MSP.
  189. * @param hcrc CRC handle
  190. * @retval None
  191. */
  192. __weak void HAL_CRC_MspInit(CRC_HandleTypeDef *hcrc)
  193. {
  194. /* Prevent unused argument(s) compilation warning */
  195. UNUSED(hcrc);
  196. /* NOTE : This function should not be modified, when the callback is needed,
  197. the HAL_CRC_MspInit can be implemented in the user file
  198. */
  199. }
  200. /**
  201. * @brief DeInitialize the CRC MSP.
  202. * @param hcrc CRC handle
  203. * @retval None
  204. */
  205. __weak void HAL_CRC_MspDeInit(CRC_HandleTypeDef *hcrc)
  206. {
  207. /* Prevent unused argument(s) compilation warning */
  208. UNUSED(hcrc);
  209. /* NOTE : This function should not be modified, when the callback is needed,
  210. the HAL_CRC_MspDeInit can be implemented in the user file
  211. */
  212. }
  213. /**
  214. * @}
  215. */
  216. /** @defgroup CRC_Exported_Functions_Group2 Peripheral Control functions
  217. * @brief management functions.
  218. *
  219. @verbatim
  220. ===============================================================================
  221. ##### Peripheral Control functions #####
  222. ===============================================================================
  223. [..] This section provides functions allowing to:
  224. (+) compute the 7, 8, 16 or 32-bit CRC value of an 8, 16 or 32-bit data buffer
  225. using combination of the previous CRC value and the new one.
  226. [..] or
  227. (+) compute the 7, 8, 16 or 32-bit CRC value of an 8, 16 or 32-bit data buffer
  228. independently of the previous CRC value.
  229. @endverbatim
  230. * @{
  231. */
  232. /**
  233. * @brief Compute the 7, 8, 16 or 32-bit CRC value of an 8, 16 or 32-bit data buffer
  234. * starting with the previously computed CRC as initialization value.
  235. * @param hcrc CRC handle
  236. * @param pBuffer pointer to the input data buffer, exact input data format is
  237. * provided by hcrc->InputDataFormat.
  238. * @param BufferLength input data buffer length (number of bytes if pBuffer
  239. * type is * uint8_t, number of half-words if pBuffer type is * uint16_t,
  240. * number of words if pBuffer type is * uint32_t).
  241. * @note By default, the API expects a uint32_t pointer as input buffer parameter.
  242. * Input buffer pointers with other types simply need to be cast in uint32_t
  243. * and the API will internally adjust its input data processing based on the
  244. * handle field hcrc->InputDataFormat.
  245. * @retval uint32_t CRC (returned value LSBs for CRC shorter than 32 bits)
  246. */
  247. uint32_t HAL_CRC_Accumulate(CRC_HandleTypeDef *hcrc, uint32_t pBuffer[], uint32_t BufferLength)
  248. {
  249. uint32_t index; /* CRC input data buffer index */
  250. uint32_t temp = 0U; /* CRC output (read from hcrc->Instance->DR register) */
  251. /* Change CRC peripheral state */
  252. hcrc->State = HAL_CRC_STATE_BUSY;
  253. switch (hcrc->InputDataFormat)
  254. {
  255. case CRC_INPUTDATA_FORMAT_WORDS:
  256. /* Enter Data to the CRC calculator */
  257. for (index = 0U; index < BufferLength; index++)
  258. {
  259. hcrc->Instance->DR = pBuffer[index];
  260. }
  261. temp = hcrc->Instance->DR;
  262. break;
  263. case CRC_INPUTDATA_FORMAT_BYTES:
  264. temp = CRC_Handle_8(hcrc, (uint8_t *)pBuffer, BufferLength);
  265. break;
  266. case CRC_INPUTDATA_FORMAT_HALFWORDS:
  267. temp = CRC_Handle_16(hcrc, (uint16_t *)(void *)pBuffer, BufferLength); /* Derogation MisraC2012 R.11.5 */
  268. break;
  269. default:
  270. break;
  271. }
  272. /* Change CRC peripheral state */
  273. hcrc->State = HAL_CRC_STATE_READY;
  274. /* Return the CRC computed value */
  275. return temp;
  276. }
  277. /**
  278. * @brief Compute the 7, 8, 16 or 32-bit CRC value of an 8, 16 or 32-bit data buffer
  279. * starting with hcrc->Instance->INIT as initialization value.
  280. * @param hcrc CRC handle
  281. * @param pBuffer pointer to the input data buffer, exact input data format is
  282. * provided by hcrc->InputDataFormat.
  283. * @param BufferLength input data buffer length (number of bytes if pBuffer
  284. * type is * uint8_t, number of half-words if pBuffer type is * uint16_t,
  285. * number of words if pBuffer type is * uint32_t).
  286. * @note By default, the API expects a uint32_t pointer as input buffer parameter.
  287. * Input buffer pointers with other types simply need to be cast in uint32_t
  288. * and the API will internally adjust its input data processing based on the
  289. * handle field hcrc->InputDataFormat.
  290. * @retval uint32_t CRC (returned value LSBs for CRC shorter than 32 bits)
  291. */
  292. uint32_t HAL_CRC_Calculate(CRC_HandleTypeDef *hcrc, uint32_t pBuffer[], uint32_t BufferLength)
  293. {
  294. uint32_t index; /* CRC input data buffer index */
  295. uint32_t temp = 0U; /* CRC output (read from hcrc->Instance->DR register) */
  296. /* Change CRC peripheral state */
  297. hcrc->State = HAL_CRC_STATE_BUSY;
  298. /* Reset CRC Calculation Unit (hcrc->Instance->INIT is
  299. * written in hcrc->Instance->DR) */
  300. __HAL_CRC_DR_RESET(hcrc);
  301. switch (hcrc->InputDataFormat)
  302. {
  303. case CRC_INPUTDATA_FORMAT_WORDS:
  304. /* Enter 32-bit input data to the CRC calculator */
  305. for (index = 0U; index < BufferLength; index++)
  306. {
  307. hcrc->Instance->DR = pBuffer[index];
  308. }
  309. temp = hcrc->Instance->DR;
  310. break;
  311. case CRC_INPUTDATA_FORMAT_BYTES:
  312. /* Specific 8-bit input data handling */
  313. temp = CRC_Handle_8(hcrc, (uint8_t *)pBuffer, BufferLength);
  314. break;
  315. case CRC_INPUTDATA_FORMAT_HALFWORDS:
  316. /* Specific 16-bit input data handling */
  317. temp = CRC_Handle_16(hcrc, (uint16_t *)(void *)pBuffer, BufferLength); /* Derogation MisraC2012 R.11.5 */
  318. break;
  319. default:
  320. break;
  321. }
  322. /* Change CRC peripheral state */
  323. hcrc->State = HAL_CRC_STATE_READY;
  324. /* Return the CRC computed value */
  325. return temp;
  326. }
  327. /**
  328. * @}
  329. */
  330. /** @defgroup CRC_Exported_Functions_Group3 Peripheral State functions
  331. * @brief Peripheral State functions.
  332. *
  333. @verbatim
  334. ===============================================================================
  335. ##### Peripheral State functions #####
  336. ===============================================================================
  337. [..]
  338. This subsection permits to get in run-time the status of the peripheral.
  339. @endverbatim
  340. * @{
  341. */
  342. /**
  343. * @brief Return the CRC handle state.
  344. * @param hcrc CRC handle
  345. * @retval HAL state
  346. */
  347. HAL_CRC_StateTypeDef HAL_CRC_GetState(CRC_HandleTypeDef *hcrc)
  348. {
  349. /* Return CRC handle state */
  350. return hcrc->State;
  351. }
  352. /**
  353. * @}
  354. */
  355. /**
  356. * @}
  357. */
  358. /** @addtogroup CRC_Private_Functions
  359. * @{
  360. */
  361. /**
  362. * @brief Enter 8-bit input data to the CRC calculator.
  363. * Specific data handling to optimize processing time.
  364. * @param hcrc CRC handle
  365. * @param pBuffer pointer to the input data buffer
  366. * @param BufferLength input data buffer length
  367. * @retval uint32_t CRC (returned value LSBs for CRC shorter than 32 bits)
  368. */
  369. static uint32_t CRC_Handle_8(CRC_HandleTypeDef *hcrc, uint8_t pBuffer[], uint32_t BufferLength)
  370. {
  371. uint32_t i; /* input data buffer index */
  372. uint16_t data;
  373. __IO uint16_t *pReg;
  374. /* Processing time optimization: 4 bytes are entered in a row with a single word write,
  375. * last bytes must be carefully fed to the CRC calculator to ensure a correct type
  376. * handling by the peripheral */
  377. for (i = 0U; i < (BufferLength / 4U); i++)
  378. {
  379. hcrc->Instance->DR = ((uint32_t)pBuffer[4U * i] << 24U) | \
  380. ((uint32_t)pBuffer[(4U * i) + 1U] << 16U) | \
  381. ((uint32_t)pBuffer[(4U * i) + 2U] << 8U) | \
  382. (uint32_t)pBuffer[(4U * i) + 3U];
  383. }
  384. /* last bytes specific handling */
  385. if ((BufferLength % 4U) != 0U)
  386. {
  387. if ((BufferLength % 4U) == 1U)
  388. {
  389. *(__IO uint8_t *)(__IO void *)(&hcrc->Instance->DR) = pBuffer[4U * i]; /* Derogation MisraC2012 R.11.5 */
  390. }
  391. if ((BufferLength % 4U) == 2U)
  392. {
  393. data = ((uint16_t)(pBuffer[4U * i]) << 8U) | (uint16_t)pBuffer[(4U * i) + 1U];
  394. pReg = (__IO uint16_t *)(__IO void *)(&hcrc->Instance->DR); /* Derogation MisraC2012 R.11.5 */
  395. *pReg = data;
  396. }
  397. if ((BufferLength % 4U) == 3U)
  398. {
  399. data = ((uint16_t)(pBuffer[4U * i]) << 8U) | (uint16_t)pBuffer[(4U * i) + 1U];
  400. pReg = (__IO uint16_t *)(__IO void *)(&hcrc->Instance->DR); /* Derogation MisraC2012 R.11.5 */
  401. *pReg = data;
  402. *(__IO uint8_t *)(__IO void *)(&hcrc->Instance->DR) = pBuffer[(4U * i) + 2U]; /* Derogation MisraC2012 R.11.5 */
  403. }
  404. }
  405. /* Return the CRC computed value */
  406. return hcrc->Instance->DR;
  407. }
  408. /**
  409. * @brief Enter 16-bit input data to the CRC calculator.
  410. * Specific data handling to optimize processing time.
  411. * @param hcrc CRC handle
  412. * @param pBuffer pointer to the input data buffer
  413. * @param BufferLength input data buffer length
  414. * @retval uint32_t CRC (returned value LSBs for CRC shorter than 32 bits)
  415. */
  416. static uint32_t CRC_Handle_16(CRC_HandleTypeDef *hcrc, uint16_t pBuffer[], uint32_t BufferLength)
  417. {
  418. uint32_t i; /* input data buffer index */
  419. __IO uint16_t *pReg;
  420. /* Processing time optimization: 2 HalfWords are entered in a row with a single word write,
  421. * in case of odd length, last HalfWord must be carefully fed to the CRC calculator to ensure
  422. * a correct type handling by the peripheral */
  423. for (i = 0U; i < (BufferLength / 2U); i++)
  424. {
  425. hcrc->Instance->DR = ((uint32_t)pBuffer[2U * i] << 16U) | (uint32_t)pBuffer[(2U * i) + 1U];
  426. }
  427. if ((BufferLength % 2U) != 0U)
  428. {
  429. pReg = (__IO uint16_t *)(__IO void *)(&hcrc->Instance->DR); /* Derogation MisraC2012 R.11.5 */
  430. *pReg = pBuffer[2U * i];
  431. }
  432. /* Return the CRC computed value */
  433. return hcrc->Instance->DR;
  434. }
  435. /**
  436. * @}
  437. */
  438. #endif /* HAL_CRC_MODULE_ENABLED */
  439. /**
  440. * @}
  441. */
  442. /**
  443. * @}
  444. */