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.
 
 
 

526 lines
17 KiB

  1. /**
  2. ******************************************************************************
  3. * @file stm32f7xx_hal_crc.c
  4. * @author MCD Application Team
  5. * @version V1.1.2
  6. * @date 23-September-2016
  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) 2016 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 Initialize the CRC according to the specified
  100. * parameters in the CRC_InitTypeDef and create 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 DeInitialize 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. /* Reset IDR register content */
  188. CLEAR_BIT(hcrc->Instance->IDR, CRC_IDR_IDR);
  189. /* DeInit the low level hardware */
  190. HAL_CRC_MspDeInit(hcrc);
  191. /* Change CRC peripheral state */
  192. hcrc->State = HAL_CRC_STATE_RESET;
  193. /* Process unlocked */
  194. __HAL_UNLOCK(hcrc);
  195. /* Return function status */
  196. return HAL_OK;
  197. }
  198. /**
  199. * @brief Initialize the CRC MSP.
  200. * @param hcrc: CRC handle
  201. * @retval None
  202. */
  203. __weak void HAL_CRC_MspInit(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_MspInit can be implemented in the user file
  209. */
  210. }
  211. /**
  212. * @brief DeInitialize the CRC MSP.
  213. * @param hcrc: CRC handle
  214. * @retval None
  215. */
  216. __weak void HAL_CRC_MspDeInit(CRC_HandleTypeDef *hcrc)
  217. {
  218. /* Prevent unused argument(s) compilation warning */
  219. UNUSED(hcrc);
  220. /* NOTE : This function should not be modified, when the callback is needed,
  221. the HAL_CRC_MspDeInit can be implemented in the user file
  222. */
  223. }
  224. /**
  225. * @}
  226. */
  227. /** @defgroup HAL_CRC_Group2 Peripheral Control functions
  228. * @brief Peripheral Control functions
  229. *
  230. @verbatim
  231. ==============================================================================
  232. ##### Peripheral Control functions #####
  233. ==============================================================================
  234. [..] This section provides functions allowing to:
  235. (+) Compute the 7, 8, 16 or 32-bit CRC value of an 8, 16 or 32-bit data buffer
  236. using combination of the previous CRC value and the new one.
  237. or
  238. (+) Compute the 7, 8, 16 or 32-bit CRC value of an 8, 16 or 32-bit data buffer
  239. independently of the previous CRC value.
  240. @endverbatim
  241. * @{
  242. */
  243. /**
  244. * @brief Compute the 7, 8, 16 or 32-bit CRC value of an 8, 16 or 32-bit data buffer
  245. * starting with the previously computed CRC as initialization value.
  246. * @param hcrc: CRC handle
  247. * @param pBuffer: pointer to the input data buffer, exact input data format is
  248. * provided by hcrc->InputDataFormat.
  249. * @param BufferLength: input data buffer length (number of bytes if pBuffer
  250. * type is * uint8_t, number of half-words if pBuffer type is * uint16_t,
  251. * number of words if pBuffer type is * uint32_t).
  252. * @note By default, the API expects a uint32_t pointer as input buffer parameter.
  253. * Input buffer pointers with other types simply need to be cast in uint32_t
  254. * and the API will internally adjust its input data processing based on the
  255. * handle field hcrc->InputDataFormat.
  256. * @retval uint32_t CRC (returned value LSBs for CRC shorter than 32 bits)
  257. */
  258. uint32_t HAL_CRC_Accumulate(CRC_HandleTypeDef *hcrc, uint32_t pBuffer[], uint32_t BufferLength)
  259. {
  260. uint32_t index = 0; /* CRC input data buffer index */
  261. uint32_t temp = 0; /* CRC output (read from hcrc->Instance->DR register) */
  262. /* Process locked */
  263. __HAL_LOCK(hcrc);
  264. /* Change CRC peripheral state */
  265. hcrc->State = HAL_CRC_STATE_BUSY;
  266. switch (hcrc->InputDataFormat)
  267. {
  268. case CRC_INPUTDATA_FORMAT_WORDS:
  269. /* Enter Data to the CRC calculator */
  270. for(index = 0; index < BufferLength; index++)
  271. {
  272. hcrc->Instance->DR = pBuffer[index];
  273. }
  274. temp = hcrc->Instance->DR;
  275. break;
  276. case CRC_INPUTDATA_FORMAT_BYTES:
  277. temp = CRC_Handle_8(hcrc, (uint8_t*)pBuffer, BufferLength);
  278. break;
  279. case CRC_INPUTDATA_FORMAT_HALFWORDS:
  280. temp = CRC_Handle_16(hcrc, (uint16_t*)pBuffer, BufferLength);
  281. break;
  282. default:
  283. break;
  284. }
  285. /* Change CRC peripheral state */
  286. hcrc->State = HAL_CRC_STATE_READY;
  287. /* Process unlocked */
  288. __HAL_UNLOCK(hcrc);
  289. /* Return the CRC computed value */
  290. return temp;
  291. }
  292. /**
  293. * @brief Compute the 7, 8, 16 or 32-bit CRC value of an 8, 16 or 32-bit data buffer
  294. * starting with hcrc->Instance->INIT as initialization value.
  295. * @param hcrc: CRC handle
  296. * @param pBuffer: pointer to the input data buffer, exact input data format is
  297. * provided by hcrc->InputDataFormat.
  298. * @param BufferLength: input data buffer length (number of bytes if pBuffer
  299. * type is * uint8_t, number of half-words if pBuffer type is * uint16_t,
  300. * number of words if pBuffer type is * uint32_t).
  301. * @note By default, the API expects a uint32_t pointer as input buffer parameter.
  302. * Input buffer pointers with other types simply need to be cast in uint32_t
  303. * and the API will internally adjust its input data processing based on the
  304. * handle field hcrc->InputDataFormat.
  305. * @retval uint32_t CRC (returned value LSBs for CRC shorter than 32 bits)
  306. */
  307. uint32_t HAL_CRC_Calculate(CRC_HandleTypeDef *hcrc, uint32_t pBuffer[], uint32_t BufferLength)
  308. {
  309. uint32_t index = 0; /* CRC input data buffer index */
  310. uint32_t temp = 0; /* CRC output (read from hcrc->Instance->DR register) */
  311. /* Process locked */
  312. __HAL_LOCK(hcrc);
  313. /* Change CRC peripheral state */
  314. hcrc->State = HAL_CRC_STATE_BUSY;
  315. /* Reset CRC Calculation Unit (hcrc->Instance->INIT is
  316. * written in hcrc->Instance->DR) */
  317. __HAL_CRC_DR_RESET(hcrc);
  318. switch (hcrc->InputDataFormat)
  319. {
  320. case CRC_INPUTDATA_FORMAT_WORDS:
  321. /* Enter 32-bit input data to the CRC calculator */
  322. for(index = 0; index < BufferLength; index++)
  323. {
  324. hcrc->Instance->DR = pBuffer[index];
  325. }
  326. temp = hcrc->Instance->DR;
  327. break;
  328. case CRC_INPUTDATA_FORMAT_BYTES:
  329. /* Specific 8-bit input data handling */
  330. temp = CRC_Handle_8(hcrc, (uint8_t*)pBuffer, BufferLength);
  331. break;
  332. case CRC_INPUTDATA_FORMAT_HALFWORDS:
  333. /* Specific 16-bit input data handling */
  334. temp = CRC_Handle_16(hcrc, (uint16_t*)pBuffer, BufferLength);
  335. break;
  336. default:
  337. break;
  338. }
  339. /* Change CRC peripheral state */
  340. hcrc->State = HAL_CRC_STATE_READY;
  341. /* Process unlocked */
  342. __HAL_UNLOCK(hcrc);
  343. /* Return the CRC computed value */
  344. return temp;
  345. }
  346. /**
  347. * @brief Enter 8-bit input data to the CRC calculator.
  348. * Specific data handling to optimize processing time.
  349. * @param hcrc: CRC handle
  350. * @param pBuffer: pointer to the input data buffer
  351. * @param BufferLength: input data buffer length
  352. * @retval uint32_t CRC (returned value LSBs for CRC shorter than 32 bits)
  353. */
  354. static uint32_t CRC_Handle_8(CRC_HandleTypeDef *hcrc, uint8_t pBuffer[], uint32_t BufferLength)
  355. {
  356. uint32_t i = 0; /* input data buffer index */
  357. /* Processing time optimization: 4 bytes are entered in a row with a single word write,
  358. * last bytes must be carefully fed to the CRC calculator to ensure a correct type
  359. * handling by the IP */
  360. for(i = 0; i < (BufferLength/4); i++)
  361. {
  362. 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]));
  363. }
  364. /* last bytes specific handling */
  365. if((BufferLength%4) != 0)
  366. {
  367. if(BufferLength%4 == 1)
  368. {
  369. *(__IO uint8_t*) (&hcrc->Instance->DR) = pBuffer[4*i];
  370. }
  371. if(BufferLength%4 == 2)
  372. {
  373. *(__IO uint16_t*) (&hcrc->Instance->DR) = (uint16_t)((uint16_t)((uint16_t)(pBuffer[4*i])<<8) | (uint16_t)(pBuffer[4*i+1]));
  374. }
  375. if(BufferLength%4 == 3)
  376. {
  377. *(__IO uint16_t*) (&hcrc->Instance->DR) = (uint16_t)((uint16_t)((uint16_t)(pBuffer[4*i])<<8) | (uint16_t)(pBuffer[4*i+1]));
  378. *(__IO uint8_t*) (&hcrc->Instance->DR) = pBuffer[4*i+2];
  379. }
  380. }
  381. /* Return the CRC computed value */
  382. return hcrc->Instance->DR;
  383. }
  384. /**
  385. * @brief Enter 16-bit input data to the CRC calculator.
  386. * Specific data handling to optimize processing time.
  387. * @param hcrc: CRC handle
  388. * @param pBuffer: pointer to the input data buffer
  389. * @param BufferLength: input data buffer length
  390. * @retval uint32_t CRC (returned value LSBs for CRC shorter than 32 bits)
  391. */
  392. static uint32_t CRC_Handle_16(CRC_HandleTypeDef *hcrc, uint16_t pBuffer[], uint32_t BufferLength)
  393. {
  394. uint32_t i = 0; /* input data buffer index */
  395. /* Processing time optimization: 2 HalfWords are entered in a row with a single word write,
  396. * in case of odd length, last HalfWord must be carefully fed to the CRC calculator to ensure
  397. * a correct type handling by the IP */
  398. for(i = 0; i < (BufferLength/2); i++)
  399. {
  400. hcrc->Instance->DR = (((uint32_t)(pBuffer[2*i])<<16) | (uint32_t)(pBuffer[2*i+1]));
  401. }
  402. if((BufferLength%2) != 0)
  403. {
  404. *(__IO uint16_t*) (&hcrc->Instance->DR) = pBuffer[2*i];
  405. }
  406. /* Return the CRC computed value */
  407. return hcrc->Instance->DR;
  408. }
  409. /**
  410. * @}
  411. */
  412. /** @defgroup HAL_CRC_Group3 Peripheral State functions
  413. * @brief Peripheral State functions.
  414. *
  415. @verbatim
  416. ==============================================================================
  417. ##### Peripheral State functions #####
  418. ==============================================================================
  419. [..]
  420. This subsection permits to get in run-time the status of the peripheral
  421. and the data flow.
  422. @endverbatim
  423. * @{
  424. */
  425. /**
  426. * @brief Return the CRC state.
  427. * @param hcrc: CRC handle
  428. * @retval HAL state
  429. */
  430. HAL_CRC_StateTypeDef HAL_CRC_GetState(CRC_HandleTypeDef *hcrc)
  431. {
  432. return hcrc->State;
  433. }
  434. /**
  435. * @}
  436. */
  437. /**
  438. * @}
  439. */
  440. #endif /* HAL_CRC_MODULE_ENABLED */
  441. /**
  442. * @}
  443. */
  444. /**
  445. * @}
  446. */
  447. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/