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.

stm32l1xx_hal_crc.c 9.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. /**
  2. ******************************************************************************
  3. * @file stm32l1xx_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 (peripheral default or non-default one)
  20. (++) specify initialization value (peripheral 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.
  34. * All rights reserved.</center></h2>
  35. *
  36. * This software component is licensed by ST under BSD 3-Clause license,
  37. * the "License"; You may not use this file except in compliance with the
  38. * License. You may obtain a copy of the License at:
  39. * opensource.org/licenses/BSD-3-Clause
  40. *
  41. ******************************************************************************
  42. */
  43. /* Includes ------------------------------------------------------------------*/
  44. #include "stm32l1xx_hal.h"
  45. /** @addtogroup STM32L1xx_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. hcrc->State = HAL_CRC_STATE_BUSY;
  101. /* Change CRC peripheral state */
  102. hcrc->State = HAL_CRC_STATE_READY;
  103. /* Return function status */
  104. return HAL_OK;
  105. }
  106. /**
  107. * @brief DeInitialize the CRC peripheral.
  108. * @param hcrc CRC handle
  109. * @retval HAL status
  110. */
  111. HAL_StatusTypeDef HAL_CRC_DeInit(CRC_HandleTypeDef *hcrc)
  112. {
  113. /* Check the CRC handle allocation */
  114. if (hcrc == NULL)
  115. {
  116. return HAL_ERROR;
  117. }
  118. /* Check the parameters */
  119. assert_param(IS_CRC_ALL_INSTANCE(hcrc->Instance));
  120. /* Check the CRC peripheral state */
  121. if (hcrc->State == HAL_CRC_STATE_BUSY)
  122. {
  123. return HAL_BUSY;
  124. }
  125. /* Change CRC peripheral state */
  126. hcrc->State = HAL_CRC_STATE_BUSY;
  127. /* Reset CRC calculation unit */
  128. __HAL_CRC_DR_RESET(hcrc);
  129. /* Reset IDR register content */
  130. CLEAR_BIT(hcrc->Instance->IDR, CRC_IDR_IDR);
  131. /* DeInit the low level hardware */
  132. HAL_CRC_MspDeInit(hcrc);
  133. /* Change CRC peripheral state */
  134. hcrc->State = HAL_CRC_STATE_RESET;
  135. /* Process unlocked */
  136. __HAL_UNLOCK(hcrc);
  137. /* Return function status */
  138. return HAL_OK;
  139. }
  140. /**
  141. * @brief Initializes the CRC MSP.
  142. * @param hcrc CRC handle
  143. * @retval None
  144. */
  145. __weak void HAL_CRC_MspInit(CRC_HandleTypeDef *hcrc)
  146. {
  147. /* Prevent unused argument(s) compilation warning */
  148. UNUSED(hcrc);
  149. /* NOTE : This function should not be modified, when the callback is needed,
  150. the HAL_CRC_MspInit can be implemented in the user file
  151. */
  152. }
  153. /**
  154. * @brief DeInitialize the CRC MSP.
  155. * @param hcrc CRC handle
  156. * @retval None
  157. */
  158. __weak void HAL_CRC_MspDeInit(CRC_HandleTypeDef *hcrc)
  159. {
  160. /* Prevent unused argument(s) compilation warning */
  161. UNUSED(hcrc);
  162. /* NOTE : This function should not be modified, when the callback is needed,
  163. the HAL_CRC_MspDeInit can be implemented in the user file
  164. */
  165. }
  166. /**
  167. * @}
  168. */
  169. /** @defgroup CRC_Exported_Functions_Group2 Peripheral Control functions
  170. * @brief management functions.
  171. *
  172. @verbatim
  173. ===============================================================================
  174. ##### Peripheral Control functions #####
  175. ===============================================================================
  176. [..] This section provides functions allowing to:
  177. (+) compute the 32-bit CRC value of a 32-bit data buffer
  178. using combination of the previous CRC value and the new one.
  179. [..] or
  180. (+) compute the 32-bit CRC value of a 32-bit data buffer
  181. independently of the previous CRC value.
  182. @endverbatim
  183. * @{
  184. */
  185. /**
  186. * @brief Compute the 32-bit CRC value of a 32-bit data buffer
  187. * starting with the previously computed CRC as initialization value.
  188. * @param hcrc CRC handle
  189. * @param pBuffer pointer to the input data buffer.
  190. * @param BufferLength input data buffer length (number of uint32_t words).
  191. * @retval uint32_t CRC (returned value LSBs for CRC shorter than 32 bits)
  192. */
  193. uint32_t HAL_CRC_Accumulate(CRC_HandleTypeDef *hcrc, uint32_t pBuffer[], uint32_t BufferLength)
  194. {
  195. uint32_t index; /* CRC input data buffer index */
  196. uint32_t temp = 0U; /* CRC output (read from hcrc->Instance->DR register) */
  197. /* Change CRC peripheral state */
  198. hcrc->State = HAL_CRC_STATE_BUSY;
  199. /* Enter Data to the CRC calculator */
  200. for (index = 0U; index < BufferLength; index++)
  201. {
  202. hcrc->Instance->DR = pBuffer[index];
  203. }
  204. temp = hcrc->Instance->DR;
  205. /* Change CRC peripheral state */
  206. hcrc->State = HAL_CRC_STATE_READY;
  207. /* Return the CRC computed value */
  208. return temp;
  209. }
  210. /**
  211. * @brief Compute the 32-bit CRC value of a 32-bit data buffer
  212. * starting with hcrc->Instance->INIT as initialization value.
  213. * @param hcrc CRC handle
  214. * @param pBuffer pointer to the input data buffer.
  215. * @param BufferLength input data buffer length (number of uint32_t words).
  216. * @retval uint32_t CRC (returned value LSBs for CRC shorter than 32 bits)
  217. */
  218. uint32_t HAL_CRC_Calculate(CRC_HandleTypeDef *hcrc, uint32_t pBuffer[], uint32_t BufferLength)
  219. {
  220. uint32_t index; /* CRC input data buffer index */
  221. uint32_t temp = 0U; /* CRC output (read from hcrc->Instance->DR register) */
  222. /* Change CRC peripheral state */
  223. hcrc->State = HAL_CRC_STATE_BUSY;
  224. /* Reset CRC Calculation Unit (hcrc->Instance->INIT is
  225. * written in hcrc->Instance->DR) */
  226. __HAL_CRC_DR_RESET(hcrc);
  227. /* Enter 32-bit input data to the CRC calculator */
  228. for (index = 0U; index < BufferLength; index++)
  229. {
  230. hcrc->Instance->DR = pBuffer[index];
  231. }
  232. temp = hcrc->Instance->DR;
  233. /* Change CRC peripheral state */
  234. hcrc->State = HAL_CRC_STATE_READY;
  235. /* Return the CRC computed value */
  236. return temp;
  237. }
  238. /**
  239. * @}
  240. */
  241. /** @defgroup CRC_Exported_Functions_Group3 Peripheral State functions
  242. * @brief Peripheral State functions.
  243. *
  244. @verbatim
  245. ===============================================================================
  246. ##### Peripheral State functions #####
  247. ===============================================================================
  248. [..]
  249. This subsection permits to get in run-time the status of the peripheral.
  250. @endverbatim
  251. * @{
  252. */
  253. /**
  254. * @brief Return the CRC handle state.
  255. * @param hcrc CRC handle
  256. * @retval HAL state
  257. */
  258. HAL_CRC_StateTypeDef HAL_CRC_GetState(CRC_HandleTypeDef *hcrc)
  259. {
  260. /* Return CRC handle state */
  261. return hcrc->State;
  262. }
  263. /**
  264. * @}
  265. */
  266. /**
  267. * @}
  268. */
  269. #endif /* HAL_CRC_MODULE_ENABLED */
  270. /**
  271. * @}
  272. */
  273. /**
  274. * @}
  275. */
  276. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/