stm32f1xx_hal_crc.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. /**
  2. ******************************************************************************
  3. * @file stm32f1xx_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. ******************************************************************************
  13. * @attention
  14. *
  15. * Copyright (c) 2016 STMicroelectronics.
  16. * All rights reserved.
  17. *
  18. * This software is licensed under terms that can be found in the LICENSE file
  19. * in the root directory of this software component.
  20. * If no LICENSE file comes with this software, it is provided AS-IS.
  21. *
  22. ******************************************************************************
  23. @verbatim
  24. ===============================================================================
  25. ##### How to use this driver #####
  26. ===============================================================================
  27. [..]
  28. (+) Enable CRC AHB clock using __HAL_RCC_CRC_CLK_ENABLE();
  29. (+) Initialize CRC calculator
  30. (++) specify generating polynomial (peripheral default or non-default one)
  31. (++) specify initialization value (peripheral default or non-default one)
  32. (++) specify input data format
  33. (++) specify input or output data inversion mode if any
  34. (+) Use HAL_CRC_Accumulate() function to compute the CRC value of the
  35. input data buffer starting with the previously computed CRC as
  36. initialization value
  37. (+) Use HAL_CRC_Calculate() function to compute the CRC value of the
  38. input data buffer starting with the defined initialization value
  39. (default or non-default) to initiate CRC calculation
  40. @endverbatim
  41. ******************************************************************************
  42. */
  43. /* Includes ------------------------------------------------------------------*/
  44. #include "stm32f1xx_hal.h"
  45. /** @addtogroup STM32F1xx_HAL_Driver
  46. * @{
  47. */
  48. /** @defgroup CRC CRC
  49. * @brief CRC HAL module driver.
  50. * @{
  51. */
  52. #ifdef HAL_CRC_MODULE_ENABLED
  53. /* Private typedef -----------------------------------------------------------*/
  54. /* Private define ------------------------------------------------------------*/
  55. /* Private macro -------------------------------------------------------------*/
  56. /* Private variables ---------------------------------------------------------*/
  57. /* Private function prototypes -----------------------------------------------*/
  58. /* Exported functions --------------------------------------------------------*/
  59. /** @defgroup CRC_Exported_Functions CRC Exported Functions
  60. * @{
  61. */
  62. /** @defgroup CRC_Exported_Functions_Group1 Initialization and de-initialization functions
  63. * @brief Initialization and Configuration functions.
  64. *
  65. @verbatim
  66. ===============================================================================
  67. ##### Initialization and de-initialization functions #####
  68. ===============================================================================
  69. [..] This section provides functions allowing to:
  70. (+) Initialize the CRC according to the specified parameters
  71. in the CRC_InitTypeDef and create the associated handle
  72. (+) DeInitialize the CRC peripheral
  73. (+) Initialize the CRC MSP (MCU Specific Package)
  74. (+) DeInitialize the CRC MSP
  75. @endverbatim
  76. * @{
  77. */
  78. /**
  79. * @brief Initialize the CRC according to the specified
  80. * parameters in the CRC_InitTypeDef and create the associated handle.
  81. * @param hcrc CRC handle
  82. * @retval HAL status
  83. */
  84. HAL_StatusTypeDef HAL_CRC_Init(CRC_HandleTypeDef *hcrc)
  85. {
  86. /* Check the CRC handle allocation */
  87. if (hcrc == NULL)
  88. {
  89. return HAL_ERROR;
  90. }
  91. /* Check the parameters */
  92. assert_param(IS_CRC_ALL_INSTANCE(hcrc->Instance));
  93. if (hcrc->State == HAL_CRC_STATE_RESET)
  94. {
  95. /* Allocate lock resource and initialize it */
  96. hcrc->Lock = HAL_UNLOCKED;
  97. /* Init the low level hardware */
  98. HAL_CRC_MspInit(hcrc);
  99. }
  100. /* Change CRC peripheral state */
  101. hcrc->State = HAL_CRC_STATE_READY;
  102. /* Return function status */
  103. return HAL_OK;
  104. }
  105. /**
  106. * @brief DeInitialize the CRC peripheral.
  107. * @param hcrc CRC handle
  108. * @retval HAL status
  109. */
  110. HAL_StatusTypeDef HAL_CRC_DeInit(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. /* Check the CRC peripheral state */
  120. if (hcrc->State == HAL_CRC_STATE_BUSY)
  121. {
  122. return HAL_BUSY;
  123. }
  124. /* Change CRC peripheral state */
  125. hcrc->State = HAL_CRC_STATE_BUSY;
  126. /* Reset CRC calculation unit */
  127. __HAL_CRC_DR_RESET(hcrc);
  128. /* Reset IDR register content */
  129. __HAL_CRC_SET_IDR(hcrc, 0);
  130. /* DeInit the low level hardware */
  131. HAL_CRC_MspDeInit(hcrc);
  132. /* Change CRC peripheral state */
  133. hcrc->State = HAL_CRC_STATE_RESET;
  134. /* Process unlocked */
  135. __HAL_UNLOCK(hcrc);
  136. /* Return function status */
  137. return HAL_OK;
  138. }
  139. /**
  140. * @brief Initializes the CRC MSP.
  141. * @param hcrc CRC handle
  142. * @retval None
  143. */
  144. __weak void HAL_CRC_MspInit(CRC_HandleTypeDef *hcrc)
  145. {
  146. /* Prevent unused argument(s) compilation warning */
  147. UNUSED(hcrc);
  148. /* NOTE : This function should not be modified, when the callback is needed,
  149. the HAL_CRC_MspInit can be implemented in the user file
  150. */
  151. }
  152. /**
  153. * @brief DeInitialize the CRC MSP.
  154. * @param hcrc CRC handle
  155. * @retval None
  156. */
  157. __weak void HAL_CRC_MspDeInit(CRC_HandleTypeDef *hcrc)
  158. {
  159. /* Prevent unused argument(s) compilation warning */
  160. UNUSED(hcrc);
  161. /* NOTE : This function should not be modified, when the callback is needed,
  162. the HAL_CRC_MspDeInit can be implemented in the user file
  163. */
  164. }
  165. /**
  166. * @}
  167. */
  168. /** @defgroup CRC_Exported_Functions_Group2 Peripheral Control functions
  169. * @brief management functions.
  170. *
  171. @verbatim
  172. ===============================================================================
  173. ##### Peripheral Control functions #####
  174. ===============================================================================
  175. [..] This section provides functions allowing to:
  176. (+) compute the 32-bit CRC value of a 32-bit data buffer
  177. using combination of the previous CRC value and the new one.
  178. [..] or
  179. (+) compute the 32-bit CRC value of a 32-bit data buffer
  180. independently of the previous CRC value.
  181. @endverbatim
  182. * @{
  183. */
  184. /**
  185. * @brief Compute the 32-bit CRC value of a 32-bit data buffer
  186. * starting with the previously computed CRC as initialization value.
  187. * @param hcrc CRC handle
  188. * @param pBuffer pointer to the input data buffer.
  189. * @param BufferLength input data buffer length (number of uint32_t words).
  190. * @retval uint32_t CRC (returned value LSBs for CRC shorter than 32 bits)
  191. */
  192. uint32_t HAL_CRC_Accumulate(CRC_HandleTypeDef *hcrc, uint32_t pBuffer[], uint32_t BufferLength)
  193. {
  194. uint32_t index; /* CRC input data buffer index */
  195. uint32_t temp = 0U; /* CRC output (read from hcrc->Instance->DR register) */
  196. /* Change CRC peripheral state */
  197. hcrc->State = HAL_CRC_STATE_BUSY;
  198. /* Enter Data to the CRC calculator */
  199. for (index = 0U; index < BufferLength; index++)
  200. {
  201. hcrc->Instance->DR = pBuffer[index];
  202. }
  203. temp = hcrc->Instance->DR;
  204. /* Change CRC peripheral state */
  205. hcrc->State = HAL_CRC_STATE_READY;
  206. /* Return the CRC computed value */
  207. return temp;
  208. }
  209. /**
  210. * @brief Compute the 32-bit CRC value of a 32-bit data buffer
  211. * starting with hcrc->Instance->INIT as initialization value.
  212. * @param hcrc CRC handle
  213. * @param pBuffer pointer to the input data buffer.
  214. * @param BufferLength input data buffer length (number of uint32_t words).
  215. * @retval uint32_t CRC (returned value LSBs for CRC shorter than 32 bits)
  216. */
  217. uint32_t HAL_CRC_Calculate(CRC_HandleTypeDef *hcrc, uint32_t pBuffer[], uint32_t BufferLength)
  218. {
  219. uint32_t index; /* CRC input data buffer index */
  220. uint32_t temp = 0U; /* CRC output (read from hcrc->Instance->DR register) */
  221. /* Change CRC peripheral state */
  222. hcrc->State = HAL_CRC_STATE_BUSY;
  223. /* Reset CRC Calculation Unit (hcrc->Instance->INIT is
  224. * written in hcrc->Instance->DR) */
  225. __HAL_CRC_DR_RESET(hcrc);
  226. /* Enter 32-bit input data to the CRC calculator */
  227. for (index = 0U; index < BufferLength; index++)
  228. {
  229. hcrc->Instance->DR = pBuffer[index];
  230. }
  231. temp = hcrc->Instance->DR;
  232. /* Change CRC peripheral state */
  233. hcrc->State = HAL_CRC_STATE_READY;
  234. /* Return the CRC computed value */
  235. return temp;
  236. }
  237. /**
  238. * @}
  239. */
  240. /** @defgroup CRC_Exported_Functions_Group3 Peripheral State functions
  241. * @brief Peripheral State functions.
  242. *
  243. @verbatim
  244. ===============================================================================
  245. ##### Peripheral State functions #####
  246. ===============================================================================
  247. [..]
  248. This subsection permits to get in run-time the status of the peripheral.
  249. @endverbatim
  250. * @{
  251. */
  252. /**
  253. * @brief Return the CRC handle state.
  254. * @param hcrc CRC handle
  255. * @retval HAL state
  256. */
  257. HAL_CRC_StateTypeDef HAL_CRC_GetState(const CRC_HandleTypeDef *hcrc)
  258. {
  259. /* Return CRC handle state */
  260. return hcrc->State;
  261. }
  262. /**
  263. * @}
  264. */
  265. /**
  266. * @}
  267. */
  268. #endif /* HAL_CRC_MODULE_ENABLED */
  269. /**
  270. * @}
  271. */
  272. /**
  273. * @}
  274. */