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.
 
 
 

532 lines
17 KiB

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