stm32l4xx_hal_crc.c 18 KB

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