stm32l4xx_hal_crc.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  1. /**
  2. ******************************************************************************
  3. * @file stm32l4xx_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) 2017 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 "stm32l4xx_hal.h"
  45. /** @addtogroup STM32L4xx_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. /* check whether or not non-default generating polynomial has been
  110. * picked up by user */
  111. assert_param(IS_DEFAULT_POLYNOMIAL(hcrc->Init.DefaultPolynomialUse));
  112. if (hcrc->Init.DefaultPolynomialUse == DEFAULT_POLYNOMIAL_ENABLE)
  113. {
  114. /* initialize peripheral with default generating polynomial */
  115. WRITE_REG(hcrc->Instance->POL, DEFAULT_CRC32_POLY);
  116. MODIFY_REG(hcrc->Instance->CR, CRC_CR_POLYSIZE, CRC_POLYLENGTH_32B);
  117. }
  118. else
  119. {
  120. /* initialize CRC peripheral with generating polynomial defined by user */
  121. if (HAL_CRCEx_Polynomial_Set(hcrc, hcrc->Init.GeneratingPolynomial, hcrc->Init.CRCLength) != HAL_OK)
  122. {
  123. return HAL_ERROR;
  124. }
  125. }
  126. /* check whether or not non-default CRC initial value has been
  127. * picked up by user */
  128. assert_param(IS_DEFAULT_INIT_VALUE(hcrc->Init.DefaultInitValueUse));
  129. if (hcrc->Init.DefaultInitValueUse == DEFAULT_INIT_VALUE_ENABLE)
  130. {
  131. WRITE_REG(hcrc->Instance->INIT, DEFAULT_CRC_INITVALUE);
  132. }
  133. else
  134. {
  135. WRITE_REG(hcrc->Instance->INIT, hcrc->Init.InitValue);
  136. }
  137. /* set input data inversion mode */
  138. assert_param(IS_CRC_INPUTDATA_INVERSION_MODE(hcrc->Init.InputDataInversionMode));
  139. MODIFY_REG(hcrc->Instance->CR, CRC_CR_REV_IN, hcrc->Init.InputDataInversionMode);
  140. /* set output data inversion mode */
  141. assert_param(IS_CRC_OUTPUTDATA_INVERSION_MODE(hcrc->Init.OutputDataInversionMode));
  142. MODIFY_REG(hcrc->Instance->CR, CRC_CR_REV_OUT, hcrc->Init.OutputDataInversionMode);
  143. /* makes sure the input data format (bytes, halfwords or words stream)
  144. * is properly specified by user */
  145. assert_param(IS_CRC_INPUTDATA_FORMAT(hcrc->InputDataFormat));
  146. /* Change CRC peripheral state */
  147. hcrc->State = HAL_CRC_STATE_READY;
  148. /* Return function status */
  149. return HAL_OK;
  150. }
  151. /**
  152. * @brief DeInitialize the CRC peripheral.
  153. * @param hcrc CRC handle
  154. * @retval HAL status
  155. */
  156. HAL_StatusTypeDef HAL_CRC_DeInit(CRC_HandleTypeDef *hcrc)
  157. {
  158. /* Check the CRC handle allocation */
  159. if (hcrc == NULL)
  160. {
  161. return HAL_ERROR;
  162. }
  163. /* Check the parameters */
  164. assert_param(IS_CRC_ALL_INSTANCE(hcrc->Instance));
  165. /* Check the CRC peripheral state */
  166. if (hcrc->State == HAL_CRC_STATE_BUSY)
  167. {
  168. return HAL_BUSY;
  169. }
  170. /* Change CRC peripheral state */
  171. hcrc->State = HAL_CRC_STATE_BUSY;
  172. /* Reset CRC calculation unit */
  173. __HAL_CRC_DR_RESET(hcrc);
  174. #if defined(CRC_IDR32BITSLENGTH_SUPPORT)
  175. /* Reset IDR register content */
  176. __HAL_CRC_SET_IDR(hcrc, 0);
  177. #else
  178. /* Reset IDR register content */
  179. CLEAR_REG(hcrc->Instance->IDR);
  180. #endif /* CRC_IDR32BITSLENGTH_SUPPORT */
  181. /* DeInit the low level hardware */
  182. HAL_CRC_MspDeInit(hcrc);
  183. /* Change CRC peripheral state */
  184. hcrc->State = HAL_CRC_STATE_RESET;
  185. /* Process unlocked */
  186. __HAL_UNLOCK(hcrc);
  187. /* Return function status */
  188. return HAL_OK;
  189. }
  190. /**
  191. * @brief Initializes the CRC MSP.
  192. * @param hcrc CRC handle
  193. * @retval None
  194. */
  195. __weak void HAL_CRC_MspInit(CRC_HandleTypeDef *hcrc)
  196. {
  197. /* Prevent unused argument(s) compilation warning */
  198. UNUSED(hcrc);
  199. /* NOTE : This function should not be modified, when the callback is needed,
  200. the HAL_CRC_MspInit can be implemented in the user file
  201. */
  202. }
  203. /**
  204. * @brief DeInitialize the CRC MSP.
  205. * @param hcrc CRC handle
  206. * @retval None
  207. */
  208. __weak void HAL_CRC_MspDeInit(CRC_HandleTypeDef *hcrc)
  209. {
  210. /* Prevent unused argument(s) compilation warning */
  211. UNUSED(hcrc);
  212. /* NOTE : This function should not be modified, when the callback is needed,
  213. the HAL_CRC_MspDeInit can be implemented in the user file
  214. */
  215. }
  216. /**
  217. * @}
  218. */
  219. /** @defgroup CRC_Exported_Functions_Group2 Peripheral Control functions
  220. * @brief management functions.
  221. *
  222. @verbatim
  223. ===============================================================================
  224. ##### Peripheral Control functions #####
  225. ===============================================================================
  226. [..] This section provides functions allowing to:
  227. (+) compute the 7, 8, 16 or 32-bit CRC value of an 8, 16 or 32-bit data buffer
  228. using combination of the previous CRC value and the new one.
  229. [..] or
  230. (+) compute the 7, 8, 16 or 32-bit CRC value of an 8, 16 or 32-bit data buffer
  231. independently of the previous CRC value.
  232. @endverbatim
  233. * @{
  234. */
  235. /**
  236. * @brief Compute the 7, 8, 16 or 32-bit CRC value of an 8, 16 or 32-bit data buffer
  237. * starting with the previously computed CRC as initialization value.
  238. * @param hcrc CRC handle
  239. * @param pBuffer pointer to the input data buffer, exact input data format is
  240. * provided by hcrc->InputDataFormat.
  241. * @param BufferLength input data buffer length (number of bytes if pBuffer
  242. * type is * uint8_t, number of half-words if pBuffer type is * uint16_t,
  243. * number of words if pBuffer type is * uint32_t).
  244. * @note By default, the API expects a uint32_t pointer as input buffer parameter.
  245. * Input buffer pointers with other types simply need to be cast in uint32_t
  246. * and the API will internally adjust its input data processing based on the
  247. * handle field hcrc->InputDataFormat.
  248. * @retval uint32_t CRC (returned value LSBs for CRC shorter than 32 bits)
  249. */
  250. uint32_t HAL_CRC_Accumulate(CRC_HandleTypeDef *hcrc, uint32_t pBuffer[], uint32_t BufferLength)
  251. {
  252. uint32_t index; /* CRC input data buffer index */
  253. uint32_t temp = 0U; /* CRC output (read from hcrc->Instance->DR register) */
  254. /* Change CRC peripheral state */
  255. hcrc->State = HAL_CRC_STATE_BUSY;
  256. switch (hcrc->InputDataFormat)
  257. {
  258. case CRC_INPUTDATA_FORMAT_WORDS:
  259. /* Enter Data to the CRC calculator */
  260. for (index = 0U; index < BufferLength; index++)
  261. {
  262. hcrc->Instance->DR = pBuffer[index];
  263. }
  264. temp = hcrc->Instance->DR;
  265. break;
  266. case CRC_INPUTDATA_FORMAT_BYTES:
  267. temp = CRC_Handle_8(hcrc, (uint8_t *)pBuffer, BufferLength);
  268. break;
  269. case CRC_INPUTDATA_FORMAT_HALFWORDS:
  270. temp = CRC_Handle_16(hcrc, (uint16_t *)(void *)pBuffer, BufferLength); /* Derogation MisraC2012 R.11.5 */
  271. break;
  272. default:
  273. break;
  274. }
  275. /* Change CRC peripheral state */
  276. hcrc->State = HAL_CRC_STATE_READY;
  277. /* Return the CRC computed value */
  278. return temp;
  279. }
  280. /**
  281. * @brief Compute the 7, 8, 16 or 32-bit CRC value of an 8, 16 or 32-bit data buffer
  282. * starting with hcrc->Instance->INIT as initialization value.
  283. * @param hcrc CRC handle
  284. * @param pBuffer pointer to the input data buffer, exact input data format is
  285. * provided by hcrc->InputDataFormat.
  286. * @param BufferLength input data buffer length (number of bytes if pBuffer
  287. * type is * uint8_t, number of half-words if pBuffer type is * uint16_t,
  288. * number of words if pBuffer type is * uint32_t).
  289. * @note By default, the API expects a uint32_t pointer as input buffer parameter.
  290. * Input buffer pointers with other types simply need to be cast in uint32_t
  291. * and the API will internally adjust its input data processing based on the
  292. * handle field hcrc->InputDataFormat.
  293. * @retval uint32_t CRC (returned value LSBs for CRC shorter than 32 bits)
  294. */
  295. uint32_t HAL_CRC_Calculate(CRC_HandleTypeDef *hcrc, uint32_t pBuffer[], uint32_t BufferLength)
  296. {
  297. uint32_t index; /* CRC input data buffer index */
  298. uint32_t temp = 0U; /* CRC output (read from hcrc->Instance->DR register) */
  299. /* Change CRC peripheral state */
  300. hcrc->State = HAL_CRC_STATE_BUSY;
  301. /* Reset CRC Calculation Unit (hcrc->Instance->INIT is
  302. * written in hcrc->Instance->DR) */
  303. __HAL_CRC_DR_RESET(hcrc);
  304. switch (hcrc->InputDataFormat)
  305. {
  306. case CRC_INPUTDATA_FORMAT_WORDS:
  307. /* Enter 32-bit input data to the CRC calculator */
  308. for (index = 0U; index < BufferLength; index++)
  309. {
  310. hcrc->Instance->DR = pBuffer[index];
  311. }
  312. temp = hcrc->Instance->DR;
  313. break;
  314. case CRC_INPUTDATA_FORMAT_BYTES:
  315. /* Specific 8-bit input data handling */
  316. temp = CRC_Handle_8(hcrc, (uint8_t *)pBuffer, BufferLength);
  317. break;
  318. case CRC_INPUTDATA_FORMAT_HALFWORDS:
  319. /* Specific 16-bit input data handling */
  320. temp = CRC_Handle_16(hcrc, (uint16_t *)(void *)pBuffer, BufferLength); /* Derogation MisraC2012 R.11.5 */
  321. break;
  322. default:
  323. break;
  324. }
  325. /* Change CRC peripheral state */
  326. hcrc->State = HAL_CRC_STATE_READY;
  327. /* Return the CRC computed value */
  328. return temp;
  329. }
  330. /**
  331. * @}
  332. */
  333. /** @defgroup CRC_Exported_Functions_Group3 Peripheral State functions
  334. * @brief Peripheral State functions.
  335. *
  336. @verbatim
  337. ===============================================================================
  338. ##### Peripheral State functions #####
  339. ===============================================================================
  340. [..]
  341. This subsection permits to get in run-time the status of the peripheral.
  342. @endverbatim
  343. * @{
  344. */
  345. /**
  346. * @brief Return the CRC handle state.
  347. * @param hcrc CRC handle
  348. * @retval HAL state
  349. */
  350. HAL_CRC_StateTypeDef HAL_CRC_GetState(const CRC_HandleTypeDef *hcrc)
  351. {
  352. /* Return CRC handle state */
  353. return hcrc->State;
  354. }
  355. /**
  356. * @}
  357. */
  358. /**
  359. * @}
  360. */
  361. /** @addtogroup CRC_Private_Functions
  362. * @{
  363. */
  364. /**
  365. * @brief Enter 8-bit input data to the CRC calculator.
  366. * Specific data handling to optimize processing time.
  367. * @param hcrc CRC handle
  368. * @param pBuffer pointer to the input data buffer
  369. * @param BufferLength input data buffer length
  370. * @retval uint32_t CRC (returned value LSBs for CRC shorter than 32 bits)
  371. */
  372. static uint32_t CRC_Handle_8(CRC_HandleTypeDef *hcrc, uint8_t pBuffer[], uint32_t BufferLength)
  373. {
  374. uint32_t i; /* input data buffer index */
  375. uint16_t data;
  376. __IO uint16_t *pReg;
  377. /* Processing time optimization: 4 bytes are entered in a row with a single word write,
  378. * last bytes must be carefully fed to the CRC calculator to ensure a correct type
  379. * handling by the peripheral */
  380. for (i = 0U; i < (BufferLength / 4U); i++)
  381. {
  382. hcrc->Instance->DR = ((uint32_t)pBuffer[4U * i] << 24U) | \
  383. ((uint32_t)pBuffer[(4U * i) + 1U] << 16U) | \
  384. ((uint32_t)pBuffer[(4U * i) + 2U] << 8U) | \
  385. (uint32_t)pBuffer[(4U * i) + 3U];
  386. }
  387. /* last bytes specific handling */
  388. if ((BufferLength % 4U) != 0U)
  389. {
  390. if ((BufferLength % 4U) == 1U)
  391. {
  392. *(__IO uint8_t *)(__IO void *)(&hcrc->Instance->DR) = pBuffer[4U * i]; /* Derogation MisraC2012 R.11.5 */
  393. }
  394. if ((BufferLength % 4U) == 2U)
  395. {
  396. data = ((uint16_t)(pBuffer[4U * i]) << 8U) | (uint16_t)pBuffer[(4U * i) + 1U];
  397. pReg = (__IO uint16_t *)(__IO void *)(&hcrc->Instance->DR); /* Derogation MisraC2012 R.11.5 */
  398. *pReg = data;
  399. }
  400. if ((BufferLength % 4U) == 3U)
  401. {
  402. data = ((uint16_t)(pBuffer[4U * i]) << 8U) | (uint16_t)pBuffer[(4U * i) + 1U];
  403. pReg = (__IO uint16_t *)(__IO void *)(&hcrc->Instance->DR); /* Derogation MisraC2012 R.11.5 */
  404. *pReg = data;
  405. *(__IO uint8_t *)(__IO void *)(&hcrc->Instance->DR) = pBuffer[(4U * i) + 2U]; /* Derogation MisraC2012 R.11.5 */
  406. }
  407. }
  408. /* Return the CRC computed value */
  409. return hcrc->Instance->DR;
  410. }
  411. /**
  412. * @brief Enter 16-bit input data to the CRC calculator.
  413. * Specific data handling to optimize processing time.
  414. * @param hcrc CRC handle
  415. * @param pBuffer pointer to the input data buffer
  416. * @param BufferLength input data buffer length
  417. * @retval uint32_t CRC (returned value LSBs for CRC shorter than 32 bits)
  418. */
  419. static uint32_t CRC_Handle_16(CRC_HandleTypeDef *hcrc, uint16_t pBuffer[], uint32_t BufferLength)
  420. {
  421. uint32_t i; /* input data buffer index */
  422. __IO uint16_t *pReg;
  423. /* Processing time optimization: 2 HalfWords are entered in a row with a single word write,
  424. * in case of odd length, last HalfWord must be carefully fed to the CRC calculator to ensure
  425. * a correct type handling by the peripheral */
  426. for (i = 0U; i < (BufferLength / 2U); i++)
  427. {
  428. hcrc->Instance->DR = ((uint32_t)pBuffer[2U * i] << 16U) | (uint32_t)pBuffer[(2U * i) + 1U];
  429. }
  430. if ((BufferLength % 2U) != 0U)
  431. {
  432. pReg = (__IO uint16_t *)(__IO void *)(&hcrc->Instance->DR); /* Derogation MisraC2012 R.11.5 */
  433. *pReg = pBuffer[2U * i];
  434. }
  435. /* Return the CRC computed value */
  436. return hcrc->Instance->DR;
  437. }
  438. /**
  439. * @}
  440. */
  441. #endif /* HAL_CRC_MODULE_ENABLED */
  442. /**
  443. * @}
  444. */
  445. /**
  446. * @}
  447. */