You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

519 lines
17 KiB

  1. /**
  2. ******************************************************************************
  3. * @file stm32wbxx_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. @verbatim
  13. ===============================================================================
  14. ##### How to use this driver #####
  15. ===============================================================================
  16. [..]
  17. (+) Enable CRC AHB clock using __HAL_RCC_CRC_CLK_ENABLE();
  18. (+) Initialize CRC calculator
  19. (++) specify generating polynomial (peripheral default or non-default one)
  20. (++) specify initialization value (peripheral default or non-default one)
  21. (++) specify input data format
  22. (++) specify input or output data inversion mode if any
  23. (+) Use HAL_CRC_Accumulate() function to compute the CRC value of the
  24. input data buffer starting with the previously computed CRC as
  25. initialization value
  26. (+) Use HAL_CRC_Calculate() function to compute the CRC value of the
  27. input data buffer starting with the defined initialization value
  28. (default or non-default) to initiate CRC calculation
  29. @endverbatim
  30. ******************************************************************************
  31. * @attention
  32. *
  33. * <h2><center>&copy; Copyright (c) 2019 STMicroelectronics.
  34. * All rights reserved.</center></h2>
  35. *
  36. * This software component is licensed by ST under BSD 3-Clause license,
  37. * the "License"; You may not use this file except in compliance with the
  38. * License. You may obtain a copy of the License at:
  39. * opensource.org/licenses/BSD-3-Clause
  40. *
  41. ******************************************************************************
  42. */
  43. /* Includes ------------------------------------------------------------------*/
  44. #include "stm32wbxx_hal.h"
  45. /** @addtogroup STM32WBxx_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. /* Reset IDR register content */
  175. CLEAR_BIT(hcrc->Instance->IDR, CRC_IDR_IDR);
  176. /* DeInit the low level hardware */
  177. HAL_CRC_MspDeInit(hcrc);
  178. /* Change CRC peripheral state */
  179. hcrc->State = HAL_CRC_STATE_RESET;
  180. /* Process unlocked */
  181. __HAL_UNLOCK(hcrc);
  182. /* Return function status */
  183. return HAL_OK;
  184. }
  185. /**
  186. * @brief Initializes the CRC MSP.
  187. * @param hcrc CRC handle
  188. * @retval None
  189. */
  190. __weak void HAL_CRC_MspInit(CRC_HandleTypeDef *hcrc)
  191. {
  192. /* Prevent unused argument(s) compilation warning */
  193. UNUSED(hcrc);
  194. /* NOTE : This function should not be modified, when the callback is needed,
  195. the HAL_CRC_MspInit can be implemented in the user file
  196. */
  197. }
  198. /**
  199. * @brief DeInitialize the CRC MSP.
  200. * @param hcrc CRC handle
  201. * @retval None
  202. */
  203. __weak void HAL_CRC_MspDeInit(CRC_HandleTypeDef *hcrc)
  204. {
  205. /* Prevent unused argument(s) compilation warning */
  206. UNUSED(hcrc);
  207. /* NOTE : This function should not be modified, when the callback is needed,
  208. the HAL_CRC_MspDeInit can be implemented in the user file
  209. */
  210. }
  211. /**
  212. * @}
  213. */
  214. /** @defgroup CRC_Exported_Functions_Group2 Peripheral Control functions
  215. * @brief management functions.
  216. *
  217. @verbatim
  218. ===============================================================================
  219. ##### Peripheral Control functions #####
  220. ===============================================================================
  221. [..] This section provides functions allowing to:
  222. (+) compute the 7, 8, 16 or 32-bit CRC value of an 8, 16 or 32-bit data buffer
  223. using combination of the previous CRC value and the new one.
  224. [..] or
  225. (+) compute the 7, 8, 16 or 32-bit CRC value of an 8, 16 or 32-bit data buffer
  226. independently of the previous CRC value.
  227. @endverbatim
  228. * @{
  229. */
  230. /**
  231. * @brief Compute the 7, 8, 16 or 32-bit CRC value of an 8, 16 or 32-bit data buffer
  232. * starting with the previously computed CRC as initialization value.
  233. * @param hcrc CRC handle
  234. * @param pBuffer pointer to the input data buffer, exact input data format is
  235. * provided by hcrc->InputDataFormat.
  236. * @param BufferLength input data buffer length (number of bytes if pBuffer
  237. * type is * uint8_t, number of half-words if pBuffer type is * uint16_t,
  238. * number of words if pBuffer type is * uint32_t).
  239. * @note By default, the API expects a uint32_t pointer as input buffer parameter.
  240. * Input buffer pointers with other types simply need to be cast in uint32_t
  241. * and the API will internally adjust its input data processing based on the
  242. * handle field hcrc->InputDataFormat.
  243. * @retval uint32_t CRC (returned value LSBs for CRC shorter than 32 bits)
  244. */
  245. uint32_t HAL_CRC_Accumulate(CRC_HandleTypeDef *hcrc, uint32_t pBuffer[], uint32_t BufferLength)
  246. {
  247. uint32_t index; /* CRC input data buffer index */
  248. uint32_t temp = 0U; /* CRC output (read from hcrc->Instance->DR register) */
  249. /* Change CRC peripheral state */
  250. hcrc->State = HAL_CRC_STATE_BUSY;
  251. switch (hcrc->InputDataFormat)
  252. {
  253. case CRC_INPUTDATA_FORMAT_WORDS:
  254. /* Enter Data to the CRC calculator */
  255. for (index = 0U; index < BufferLength; index++)
  256. {
  257. hcrc->Instance->DR = pBuffer[index];
  258. }
  259. temp = hcrc->Instance->DR;
  260. break;
  261. case CRC_INPUTDATA_FORMAT_BYTES:
  262. temp = CRC_Handle_8(hcrc, (uint8_t *)pBuffer, BufferLength);
  263. break;
  264. case CRC_INPUTDATA_FORMAT_HALFWORDS:
  265. temp = CRC_Handle_16(hcrc, (uint16_t *)(void *)pBuffer, BufferLength); /* Derogation MisraC2012 R.11.5 */
  266. break;
  267. default:
  268. break;
  269. }
  270. /* Change CRC peripheral state */
  271. hcrc->State = HAL_CRC_STATE_READY;
  272. /* Return the CRC computed value */
  273. return temp;
  274. }
  275. /**
  276. * @brief Compute the 7, 8, 16 or 32-bit CRC value of an 8, 16 or 32-bit data buffer
  277. * starting with hcrc->Instance->INIT as initialization value.
  278. * @param hcrc CRC handle
  279. * @param pBuffer pointer to the input data buffer, exact input data format is
  280. * provided by hcrc->InputDataFormat.
  281. * @param BufferLength input data buffer length (number of bytes if pBuffer
  282. * type is * uint8_t, number of half-words if pBuffer type is * uint16_t,
  283. * number of words if pBuffer type is * uint32_t).
  284. * @note By default, the API expects a uint32_t pointer as input buffer parameter.
  285. * Input buffer pointers with other types simply need to be cast in uint32_t
  286. * and the API will internally adjust its input data processing based on the
  287. * handle field hcrc->InputDataFormat.
  288. * @retval uint32_t CRC (returned value LSBs for CRC shorter than 32 bits)
  289. */
  290. uint32_t HAL_CRC_Calculate(CRC_HandleTypeDef *hcrc, uint32_t pBuffer[], uint32_t BufferLength)
  291. {
  292. uint32_t index; /* CRC input data buffer index */
  293. uint32_t temp = 0U; /* CRC output (read from hcrc->Instance->DR register) */
  294. /* Change CRC peripheral state */
  295. hcrc->State = HAL_CRC_STATE_BUSY;
  296. /* Reset CRC Calculation Unit (hcrc->Instance->INIT is
  297. * written in hcrc->Instance->DR) */
  298. __HAL_CRC_DR_RESET(hcrc);
  299. switch (hcrc->InputDataFormat)
  300. {
  301. case CRC_INPUTDATA_FORMAT_WORDS:
  302. /* Enter 32-bit input data to the CRC calculator */
  303. for (index = 0U; index < BufferLength; index++)
  304. {
  305. hcrc->Instance->DR = pBuffer[index];
  306. }
  307. temp = hcrc->Instance->DR;
  308. break;
  309. case CRC_INPUTDATA_FORMAT_BYTES:
  310. /* Specific 8-bit input data handling */
  311. temp = CRC_Handle_8(hcrc, (uint8_t *)pBuffer, BufferLength);
  312. break;
  313. case CRC_INPUTDATA_FORMAT_HALFWORDS:
  314. /* Specific 16-bit input data handling */
  315. temp = CRC_Handle_16(hcrc, (uint16_t *)(void *)pBuffer, BufferLength); /* Derogation MisraC2012 R.11.5 */
  316. break;
  317. default:
  318. break;
  319. }
  320. /* Change CRC peripheral state */
  321. hcrc->State = HAL_CRC_STATE_READY;
  322. /* Return the CRC computed value */
  323. return temp;
  324. }
  325. /**
  326. * @}
  327. */
  328. /** @defgroup CRC_Exported_Functions_Group3 Peripheral State functions
  329. * @brief Peripheral State functions.
  330. *
  331. @verbatim
  332. ===============================================================================
  333. ##### Peripheral State functions #####
  334. ===============================================================================
  335. [..]
  336. This subsection permits to get in run-time the status of the peripheral.
  337. @endverbatim
  338. * @{
  339. */
  340. /**
  341. * @brief Return the CRC handle state.
  342. * @param hcrc CRC handle
  343. * @retval HAL state
  344. */
  345. HAL_CRC_StateTypeDef HAL_CRC_GetState(CRC_HandleTypeDef *hcrc)
  346. {
  347. /* Return CRC handle state */
  348. return hcrc->State;
  349. }
  350. /**
  351. * @}
  352. */
  353. /**
  354. * @}
  355. */
  356. /** @addtogroup CRC_Private_Functions
  357. * @{
  358. */
  359. /**
  360. * @brief Enter 8-bit input data to the CRC calculator.
  361. * Specific data handling to optimize processing time.
  362. * @param hcrc CRC handle
  363. * @param pBuffer pointer to the input data buffer
  364. * @param BufferLength input data buffer length
  365. * @retval uint32_t CRC (returned value LSBs for CRC shorter than 32 bits)
  366. */
  367. static uint32_t CRC_Handle_8(CRC_HandleTypeDef *hcrc, uint8_t pBuffer[], uint32_t BufferLength)
  368. {
  369. uint32_t i; /* input data buffer index */
  370. uint16_t data;
  371. __IO uint16_t *pReg;
  372. /* Processing time optimization: 4 bytes are entered in a row with a single word write,
  373. * last bytes must be carefully fed to the CRC calculator to ensure a correct type
  374. * handling by the peripheral */
  375. for (i = 0U; i < (BufferLength / 4U); i++)
  376. {
  377. hcrc->Instance->DR = ((uint32_t)pBuffer[4U * i] << 24U) | \
  378. ((uint32_t)pBuffer[(4U * i) + 1U] << 16U) | \
  379. ((uint32_t)pBuffer[(4U * i) + 2U] << 8U) | \
  380. (uint32_t)pBuffer[(4U * i) + 3U];
  381. }
  382. /* last bytes specific handling */
  383. if ((BufferLength % 4U) != 0U)
  384. {
  385. if ((BufferLength % 4U) == 1U)
  386. {
  387. *(__IO uint8_t *)(__IO void *)(&hcrc->Instance->DR) = pBuffer[4U * i]; /* Derogation MisraC2012 R.11.5 */
  388. }
  389. if ((BufferLength % 4U) == 2U)
  390. {
  391. data = ((uint16_t)(pBuffer[4U * i]) << 8U) | (uint16_t)pBuffer[(4U * i) + 1U];
  392. pReg = (__IO uint16_t *)(__IO void *)(&hcrc->Instance->DR); /* Derogation MisraC2012 R.11.5 */
  393. *pReg = data;
  394. }
  395. if ((BufferLength % 4U) == 3U)
  396. {
  397. data = ((uint16_t)(pBuffer[4U * i]) << 8U) | (uint16_t)pBuffer[(4U * i) + 1U];
  398. pReg = (__IO uint16_t *)(__IO void *)(&hcrc->Instance->DR); /* Derogation MisraC2012 R.11.5 */
  399. *pReg = data;
  400. *(__IO uint8_t *)(__IO void *)(&hcrc->Instance->DR) = pBuffer[(4U * i) + 2U]; /* Derogation MisraC2012 R.11.5 */
  401. }
  402. }
  403. /* Return the CRC computed value */
  404. return hcrc->Instance->DR;
  405. }
  406. /**
  407. * @brief Enter 16-bit input data to the CRC calculator.
  408. * Specific data handling to optimize processing time.
  409. * @param hcrc CRC handle
  410. * @param pBuffer pointer to the input data buffer
  411. * @param BufferLength input data buffer length
  412. * @retval uint32_t CRC (returned value LSBs for CRC shorter than 32 bits)
  413. */
  414. static uint32_t CRC_Handle_16(CRC_HandleTypeDef *hcrc, uint16_t pBuffer[], uint32_t BufferLength)
  415. {
  416. uint32_t i; /* input data buffer index */
  417. __IO uint16_t *pReg;
  418. /* Processing time optimization: 2 HalfWords are entered in a row with a single word write,
  419. * in case of odd length, last HalfWord must be carefully fed to the CRC calculator to ensure
  420. * a correct type handling by the peripheral */
  421. for (i = 0U; i < (BufferLength / 2U); i++)
  422. {
  423. hcrc->Instance->DR = ((uint32_t)pBuffer[2U * i] << 16U) | (uint32_t)pBuffer[(2U * i) + 1U];
  424. }
  425. if ((BufferLength % 2U) != 0U)
  426. {
  427. pReg = (__IO uint16_t *)(__IO void *)(&hcrc->Instance->DR); /* Derogation MisraC2012 R.11.5 */
  428. *pReg = pBuffer[2U * i];
  429. }
  430. /* Return the CRC computed value */
  431. return hcrc->Instance->DR;
  432. }
  433. /**
  434. * @}
  435. */
  436. #endif /* HAL_CRC_MODULE_ENABLED */
  437. /**
  438. * @}
  439. */
  440. /**
  441. * @}
  442. */
  443. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/