stm32l4xx_hal_crc.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  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 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 (IP default or non-default one)
  20. (++) specify initialization value (IP 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) 2017 STMicroelectronics</center></h2>
  34. *
  35. * Redistribution and use in source and binary forms, with or without modification,
  36. * are permitted provided that the following conditions are met:
  37. * 1. Redistributions of source code must retain the above copyright notice,
  38. * this list of conditions and the following disclaimer.
  39. * 2. Redistributions in binary form must reproduce the above copyright notice,
  40. * this list of conditions and the following disclaimer in the documentation
  41. * and/or other materials provided with the distribution.
  42. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  43. * may be used to endorse or promote products derived from this software
  44. * without specific prior written permission.
  45. *
  46. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  47. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  48. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  49. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  50. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  51. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  52. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  53. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  54. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  55. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  56. *
  57. ******************************************************************************
  58. */
  59. /* Includes ------------------------------------------------------------------*/
  60. #include "stm32l4xx_hal.h"
  61. /** @addtogroup STM32L4xx_HAL_Driver
  62. * @{
  63. */
  64. /** @defgroup CRC CRC
  65. * @brief CRC HAL module driver.
  66. * @{
  67. */
  68. #ifdef HAL_CRC_MODULE_ENABLED
  69. /* Private typedef -----------------------------------------------------------*/
  70. /* Private define ------------------------------------------------------------*/
  71. /* Private macro -------------------------------------------------------------*/
  72. /* Private variables ---------------------------------------------------------*/
  73. /* Private function prototypes -----------------------------------------------*/
  74. /** @defgroup CRC_Private_Functions CRC Private Functions
  75. * @{
  76. */
  77. static uint32_t CRC_Handle_8(CRC_HandleTypeDef *hcrc, uint8_t pBuffer[], uint32_t BufferLength);
  78. static uint32_t CRC_Handle_16(CRC_HandleTypeDef *hcrc, uint16_t pBuffer[], uint32_t BufferLength);
  79. /**
  80. * @}
  81. */
  82. /* Exported functions --------------------------------------------------------*/
  83. /** @defgroup CRC_Exported_Functions CRC Exported Functions
  84. * @{
  85. */
  86. /** @defgroup CRC_Exported_Functions_Group1 Initialization and de-initialization functions
  87. * @brief Initialization and Configuration functions.
  88. *
  89. @verbatim
  90. ===============================================================================
  91. ##### Initialization and de-initialization functions #####
  92. ===============================================================================
  93. [..] This section provides functions allowing to:
  94. (+) Initialize the CRC according to the specified parameters
  95. in the CRC_InitTypeDef and create the associated handle
  96. (+) DeInitialize the CRC peripheral
  97. (+) Initialize the CRC MSP (MCU Specific Package)
  98. (+) DeInitialize the CRC MSP
  99. @endverbatim
  100. * @{
  101. */
  102. /**
  103. * @brief Initialize the CRC according to the specified
  104. * parameters in the CRC_InitTypeDef and create the associated handle.
  105. * @param hcrc: CRC handle
  106. * @retval HAL status
  107. */
  108. HAL_StatusTypeDef HAL_CRC_Init(CRC_HandleTypeDef *hcrc)
  109. {
  110. /* Check the CRC handle allocation */
  111. if(hcrc == NULL)
  112. {
  113. return HAL_ERROR;
  114. }
  115. /* Check the parameters */
  116. assert_param(IS_CRC_ALL_INSTANCE(hcrc->Instance));
  117. if(hcrc->State == HAL_CRC_STATE_RESET)
  118. {
  119. /* Allocate lock resource and initialize it */
  120. hcrc->Lock = HAL_UNLOCKED;
  121. /* Init the low level hardware */
  122. HAL_CRC_MspInit(hcrc);
  123. }
  124. hcrc->State = HAL_CRC_STATE_BUSY;
  125. /* check whether or not non-default generating polynomial has been
  126. * picked up by user */
  127. assert_param(IS_DEFAULT_POLYNOMIAL(hcrc->Init.DefaultPolynomialUse));
  128. if (hcrc->Init.DefaultPolynomialUse == DEFAULT_POLYNOMIAL_ENABLE)
  129. {
  130. /* initialize IP with default generating polynomial */
  131. WRITE_REG(hcrc->Instance->POL, DEFAULT_CRC32_POLY);
  132. MODIFY_REG(hcrc->Instance->CR, CRC_CR_POLYSIZE, CRC_POLYLENGTH_32B);
  133. }
  134. else
  135. {
  136. /* initialize CRC IP with generating polynomial defined by user */
  137. if (HAL_CRCEx_Polynomial_Set(hcrc, hcrc->Init.GeneratingPolynomial, hcrc->Init.CRCLength) != HAL_OK)
  138. {
  139. return HAL_ERROR;
  140. }
  141. }
  142. /* check whether or not non-default CRC initial value has been
  143. * picked up by user */
  144. assert_param(IS_DEFAULT_INIT_VALUE(hcrc->Init.DefaultInitValueUse));
  145. if (hcrc->Init.DefaultInitValueUse == DEFAULT_INIT_VALUE_ENABLE)
  146. {
  147. WRITE_REG(hcrc->Instance->INIT, DEFAULT_CRC_INITVALUE);
  148. }
  149. else
  150. {
  151. WRITE_REG(hcrc->Instance->INIT, hcrc->Init.InitValue);
  152. }
  153. /* set input data inversion mode */
  154. assert_param(IS_CRC_INPUTDATA_INVERSION_MODE(hcrc->Init.InputDataInversionMode));
  155. MODIFY_REG(hcrc->Instance->CR, CRC_CR_REV_IN, hcrc->Init.InputDataInversionMode);
  156. /* set output data inversion mode */
  157. assert_param(IS_CRC_OUTPUTDATA_INVERSION_MODE(hcrc->Init.OutputDataInversionMode));
  158. MODIFY_REG(hcrc->Instance->CR, CRC_CR_REV_OUT, hcrc->Init.OutputDataInversionMode);
  159. /* makes sure the input data format (bytes, halfwords or words stream)
  160. * is properly specified by user */
  161. assert_param(IS_CRC_INPUTDATA_FORMAT(hcrc->InputDataFormat));
  162. /* Change CRC peripheral state */
  163. hcrc->State = HAL_CRC_STATE_READY;
  164. /* Return function status */
  165. return HAL_OK;
  166. }
  167. /**
  168. * @brief DeInitialize the CRC peripheral.
  169. * @param hcrc: CRC handle
  170. * @retval HAL status
  171. */
  172. HAL_StatusTypeDef HAL_CRC_DeInit(CRC_HandleTypeDef *hcrc)
  173. {
  174. /* Check the CRC handle allocation */
  175. if(hcrc == NULL)
  176. {
  177. return HAL_ERROR;
  178. }
  179. /* Check the parameters */
  180. assert_param(IS_CRC_ALL_INSTANCE(hcrc->Instance));
  181. /* Check the CRC peripheral state */
  182. if(hcrc->State == HAL_CRC_STATE_BUSY)
  183. {
  184. return HAL_BUSY;
  185. }
  186. /* Change CRC peripheral state */
  187. hcrc->State = HAL_CRC_STATE_BUSY;
  188. /* Reset CRC calculation unit */
  189. __HAL_CRC_DR_RESET(hcrc);
  190. /* Reset IDR register content */
  191. CLEAR_BIT(hcrc->Instance->IDR, CRC_IDR_IDR) ;
  192. /* DeInit the low level hardware */
  193. HAL_CRC_MspDeInit(hcrc);
  194. /* Change CRC peripheral state */
  195. hcrc->State = HAL_CRC_STATE_RESET;
  196. /* Process unlocked */
  197. __HAL_UNLOCK(hcrc);
  198. /* Return function status */
  199. return HAL_OK;
  200. }
  201. /**
  202. * @brief Initializes the CRC MSP.
  203. * @param hcrc: CRC handle
  204. * @retval None
  205. */
  206. __weak void HAL_CRC_MspInit(CRC_HandleTypeDef *hcrc)
  207. {
  208. /* Prevent unused argument(s) compilation warning */
  209. UNUSED(hcrc);
  210. /* NOTE : This function should not be modified, when the callback is needed,
  211. the HAL_CRC_MspInit can be implemented in the user file
  212. */
  213. }
  214. /**
  215. * @brief DeInitialize the CRC MSP.
  216. * @param hcrc: CRC handle
  217. * @retval None
  218. */
  219. __weak void HAL_CRC_MspDeInit(CRC_HandleTypeDef *hcrc)
  220. {
  221. /* Prevent unused argument(s) compilation warning */
  222. UNUSED(hcrc);
  223. /* NOTE : This function should not be modified, when the callback is needed,
  224. the HAL_CRC_MspDeInit can be implemented in the user file
  225. */
  226. }
  227. /**
  228. * @}
  229. */
  230. /** @defgroup CRC_Exported_Functions_Group2 Peripheral Control functions
  231. * @brief management functions.
  232. *
  233. @verbatim
  234. ===============================================================================
  235. ##### Peripheral Control functions #####
  236. ===============================================================================
  237. [..] This section provides functions allowing to:
  238. (+) compute the 7, 8, 16 or 32-bit CRC value of an 8, 16 or 32-bit data buffer
  239. using the combination of the previous CRC value and the new one
  240. [..] or
  241. (+) compute the 7, 8, 16 or 32-bit CRC value of an 8, 16 or 32-bit data buffer
  242. independently of the previous CRC value.
  243. @endverbatim
  244. * @{
  245. */
  246. /**
  247. * @brief Compute the 7, 8, 16 or 32-bit CRC value of an 8, 16 or 32-bit data buffer
  248. * starting with the previously computed CRC as initialization value.
  249. * @param hcrc: CRC handle
  250. * @param pBuffer: pointer to the input data buffer, exact input data format is
  251. * provided by hcrc->InputDataFormat.
  252. * @param BufferLength: input data buffer length (number of bytes if pBuffer
  253. * type is * uint8_t, number of half-words if pBuffer type is * uint16_t,
  254. * number of words if pBuffer type is * uint32_t).
  255. * @note By default, the API expects a uint32_t pointer as input buffer parameter.
  256. * Input buffer pointers with other types simply need to be cast in uint32_t
  257. * and the API will internally adjust its input data processing based on the
  258. * handle field hcrc->InputDataFormat.
  259. * @retval uint32_t CRC (returned value LSBs for CRC shorter than 32 bits)
  260. */
  261. uint32_t HAL_CRC_Accumulate(CRC_HandleTypeDef *hcrc, uint32_t pBuffer[], uint32_t BufferLength)
  262. {
  263. uint32_t index = 0; /* CRC input data buffer index */
  264. uint32_t temp = 0; /* CRC output (read from hcrc->Instance->DR register) */
  265. /* Process locked */
  266. __HAL_LOCK(hcrc);
  267. /* Change CRC peripheral state */
  268. hcrc->State = HAL_CRC_STATE_BUSY;
  269. switch (hcrc->InputDataFormat)
  270. {
  271. case CRC_INPUTDATA_FORMAT_WORDS:
  272. /* Enter Data to the CRC calculator */
  273. for(index = 0; index < BufferLength; index++)
  274. {
  275. hcrc->Instance->DR = pBuffer[index];
  276. }
  277. temp = hcrc->Instance->DR;
  278. break;
  279. case CRC_INPUTDATA_FORMAT_BYTES:
  280. temp = CRC_Handle_8(hcrc, (uint8_t*)pBuffer, BufferLength);
  281. break;
  282. case CRC_INPUTDATA_FORMAT_HALFWORDS:
  283. temp = CRC_Handle_16(hcrc, (uint16_t*)pBuffer, BufferLength);
  284. break;
  285. default:
  286. break;
  287. }
  288. /* Change CRC peripheral state */
  289. hcrc->State = HAL_CRC_STATE_READY;
  290. /* Process unlocked */
  291. __HAL_UNLOCK(hcrc);
  292. /* Return the CRC computed value */
  293. return temp;
  294. }
  295. /**
  296. * @brief Compute the 7, 8, 16 or 32-bit CRC value of an 8, 16 or 32-bit data buffer
  297. * starting with hcrc->Instance->INIT as initialization value.
  298. * @param hcrc: CRC handle
  299. * @param pBuffer: pointer to the input data buffer, exact input data format is
  300. * provided by hcrc->InputDataFormat.
  301. * @param BufferLength: input data buffer length (number of bytes if pBuffer
  302. * type is * uint8_t, number of half-words if pBuffer type is * uint16_t,
  303. * number of words if pBuffer type is * uint32_t).
  304. * @note By default, the API expects a uint32_t pointer as input buffer parameter.
  305. * Input buffer pointers with other types simply need to be cast in uint32_t
  306. * and the API will internally adjust its input data processing based on the
  307. * handle field hcrc->InputDataFormat.
  308. * @retval uint32_t CRC (returned value LSBs for CRC shorter than 32 bits)
  309. */
  310. uint32_t HAL_CRC_Calculate(CRC_HandleTypeDef *hcrc, uint32_t pBuffer[], uint32_t BufferLength)
  311. {
  312. uint32_t index = 0; /* CRC input data buffer index */
  313. uint32_t temp = 0; /* CRC output (read from hcrc->Instance->DR register) */
  314. /* Process locked */
  315. __HAL_LOCK(hcrc);
  316. /* Change CRC peripheral state */
  317. hcrc->State = HAL_CRC_STATE_BUSY;
  318. /* Reset CRC Calculation Unit (hcrc->Instance->INIT is
  319. * written in hcrc->Instance->DR) */
  320. __HAL_CRC_DR_RESET(hcrc);
  321. switch (hcrc->InputDataFormat)
  322. {
  323. case CRC_INPUTDATA_FORMAT_WORDS:
  324. /* Enter 32-bit input data to the CRC calculator */
  325. for(index = 0; index < BufferLength; index++)
  326. {
  327. hcrc->Instance->DR = pBuffer[index];
  328. }
  329. temp = hcrc->Instance->DR;
  330. break;
  331. case CRC_INPUTDATA_FORMAT_BYTES:
  332. /* Specific 8-bit input data handling */
  333. temp = CRC_Handle_8(hcrc, (uint8_t*)pBuffer, BufferLength);
  334. break;
  335. case CRC_INPUTDATA_FORMAT_HALFWORDS:
  336. /* Specific 16-bit input data handling */
  337. temp = CRC_Handle_16(hcrc, (uint16_t*)pBuffer, BufferLength);
  338. break;
  339. default:
  340. break;
  341. }
  342. /* Change CRC peripheral state */
  343. hcrc->State = HAL_CRC_STATE_READY;
  344. /* Process unlocked */
  345. __HAL_UNLOCK(hcrc);
  346. /* Return the CRC computed value */
  347. return temp;
  348. }
  349. /**
  350. * @}
  351. */
  352. /** @defgroup CRC_Exported_Functions_Group3 Peripheral State functions
  353. * @brief Peripheral State functions.
  354. *
  355. @verbatim
  356. ===============================================================================
  357. ##### Peripheral State functions #####
  358. ===============================================================================
  359. [..]
  360. This subsection permits to get in run-time the status of the peripheral.
  361. @endverbatim
  362. * @{
  363. */
  364. /**
  365. * @brief Return the CRC handle state.
  366. * @param hcrc: CRC handle
  367. * @retval HAL state
  368. */
  369. HAL_CRC_StateTypeDef HAL_CRC_GetState(CRC_HandleTypeDef *hcrc)
  370. {
  371. /* Return CRC handle state */
  372. return hcrc->State;
  373. }
  374. /**
  375. * @}
  376. */
  377. /**
  378. * @}
  379. */
  380. /** @defgroup CRC_Private_Functions CRC Private Functions
  381. * @{
  382. */
  383. /**
  384. * @brief Enter 8-bit input data to the CRC calculator.
  385. * Specific data handling to optimize processing time.
  386. * @param hcrc: CRC handle
  387. * @param pBuffer: pointer to the input data buffer
  388. * @param BufferLength: input data buffer length
  389. * @retval uint32_t CRC (returned value LSBs for CRC shorter than 32 bits)
  390. */
  391. static uint32_t CRC_Handle_8(CRC_HandleTypeDef *hcrc, uint8_t pBuffer[], uint32_t BufferLength)
  392. {
  393. uint32_t i = 0; /* input data buffer index */
  394. uint16_t data;
  395. __IO uint16_t *pReg;
  396. /* Processing time optimization: 4 bytes are entered in a row with a single word write,
  397. * last bytes must be carefully fed to the CRC calculator to ensure a correct type
  398. * handling by the IP */
  399. for(i = 0; i < (BufferLength/4); i++)
  400. {
  401. hcrc->Instance->DR = ((uint32_t)pBuffer[4*i]<<24) | ((uint32_t)pBuffer[4*i+1]<<16) | ((uint32_t)pBuffer[4*i+2]<<8) | (uint32_t)pBuffer[4*i+3];
  402. }
  403. /* last bytes specific handling */
  404. if ((BufferLength%4) != 0)
  405. {
  406. if (BufferLength%4 == 1)
  407. {
  408. *(__IO uint8_t *)(__IO void *)(&hcrc->Instance->DR) = pBuffer[4*i];
  409. }
  410. if (BufferLength%4 == 2)
  411. {
  412. data = (uint16_t)(pBuffer[4*i]<<8) | (uint16_t)pBuffer[4*i+1];
  413. pReg = (__IO uint16_t *)(__IO void *)(&hcrc->Instance->DR);
  414. *pReg = data;
  415. }
  416. if (BufferLength%4 == 3)
  417. {
  418. data = (uint16_t)(pBuffer[4*i]<<8) | (uint16_t)pBuffer[4*i+1];
  419. pReg = (__IO uint16_t *)(__IO void *)(&hcrc->Instance->DR);
  420. *pReg = data;
  421. *(__IO uint8_t *)(__IO void *)(&hcrc->Instance->DR) = pBuffer[4*i+2];
  422. }
  423. }
  424. /* Return the CRC computed value */
  425. return hcrc->Instance->DR;
  426. }
  427. /**
  428. * @brief Enter 16-bit input data to the CRC calculator.
  429. * Specific data handling to optimize processing time.
  430. * @param hcrc: CRC handle
  431. * @param pBuffer: pointer to the input data buffer
  432. * @param BufferLength: input data buffer length
  433. * @retval uint32_t CRC (returned value LSBs for CRC shorter than 32 bits)
  434. */
  435. static uint32_t CRC_Handle_16(CRC_HandleTypeDef *hcrc, uint16_t pBuffer[], uint32_t BufferLength)
  436. {
  437. uint32_t i = 0; /* input data buffer index */
  438. __IO uint16_t *pReg;
  439. /* Processing time optimization: 2 HalfWords are entered in a row with a single word write,
  440. * in case of odd length, last HalfWord must be carefully fed to the CRC calculator to ensure
  441. * a correct type handling by the IP */
  442. for(i = 0; i < (BufferLength/2); i++)
  443. {
  444. hcrc->Instance->DR = ((uint32_t)pBuffer[2*i]<<16) | (uint32_t)pBuffer[2*i+1];
  445. }
  446. if ((BufferLength%2) != 0)
  447. {
  448. pReg = (__IO uint16_t *)(__IO void *)(&hcrc->Instance->DR);
  449. *pReg = pBuffer[2*i];
  450. }
  451. /* Return the CRC computed value */
  452. return hcrc->Instance->DR;
  453. }
  454. /**
  455. * @}
  456. */
  457. #endif /* HAL_CRC_MODULE_ENABLED */
  458. /**
  459. * @}
  460. */
  461. /**
  462. * @}
  463. */
  464. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/