stm32f7xx_hal_crc.c 16 KB

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