stm32f7xx_hal_crc.c 17 KB

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