stm32f4xx_hal_crc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. /**
  2. ******************************************************************************
  3. * @file stm32f4xx_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. The CRC HAL driver can be used as follows:
  18. (#) Enable CRC AHB clock using __HAL_RCC_CRC_CLK_ENABLE();
  19. (#) Use HAL_CRC_Accumulate() function to compute the CRC value of
  20. a 32-bit data buffer using combination of the previous CRC value
  21. and the new one.
  22. (#) Use HAL_CRC_Calculate() function to compute the CRC Value of
  23. a new 32-bit data buffer. This function resets the CRC computation
  24. unit before starting the computation to avoid getting wrong CRC values.
  25. @endverbatim
  26. ******************************************************************************
  27. * @attention
  28. *
  29. * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
  30. *
  31. * Redistribution and use in source and binary forms, with or without modification,
  32. * are permitted provided that the following conditions are met:
  33. * 1. Redistributions of source code must retain the above copyright notice,
  34. * this list of conditions and the following disclaimer.
  35. * 2. Redistributions in binary form must reproduce the above copyright notice,
  36. * this list of conditions and the following disclaimer in the documentation
  37. * and/or other materials provided with the distribution.
  38. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  39. * may be used to endorse or promote products derived from this software
  40. * without specific prior written permission.
  41. *
  42. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  43. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  44. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  45. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  46. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  47. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  48. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  49. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  50. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  51. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  52. *
  53. ******************************************************************************
  54. */
  55. /* Includes ------------------------------------------------------------------*/
  56. #include "stm32f4xx_hal.h"
  57. /** @addtogroup STM32F4xx_HAL_Driver
  58. * @{
  59. */
  60. /** @addtogroup CRC
  61. * @{
  62. */
  63. #ifdef HAL_CRC_MODULE_ENABLED
  64. /* Private typedef -----------------------------------------------------------*/
  65. /* Private define ------------------------------------------------------------*/
  66. /* Private macro -------------------------------------------------------------*/
  67. /* Private variables ---------------------------------------------------------*/
  68. /* Private function prototypes -----------------------------------------------*/
  69. /* Private functions ---------------------------------------------------------*/
  70. /* Exported functions --------------------------------------------------------*/
  71. /** @addtogroup CRC_Exported_Functions
  72. * @{
  73. */
  74. /** @addtogroup CRC_Exported_Functions_Group1
  75. * @brief Initialization and de-initialization functions
  76. *
  77. @verbatim
  78. ==============================================================================
  79. ##### Initialization and de-initialization functions #####
  80. ==============================================================================
  81. [..] This section provides functions allowing to:
  82. (+) Initialize the CRC according to the specified parameters
  83. in the CRC_InitTypeDef and create the associated handle
  84. (+) DeInitialize the CRC peripheral
  85. (+) Initialize the CRC MSP
  86. (+) DeInitialize CRC MSP
  87. @endverbatim
  88. * @{
  89. */
  90. /**
  91. * @brief Initializes the CRC according to the specified
  92. * parameters in the CRC_InitTypeDef and creates the associated handle.
  93. * @param hcrc pointer to a CRC_HandleTypeDef structure that contains
  94. * the configuration information for CRC
  95. * @retval HAL status
  96. */
  97. HAL_StatusTypeDef HAL_CRC_Init(CRC_HandleTypeDef *hcrc)
  98. {
  99. /* Check the CRC handle allocation */
  100. if(hcrc == NULL)
  101. {
  102. return HAL_ERROR;
  103. }
  104. /* Check the parameters */
  105. assert_param(IS_CRC_ALL_INSTANCE(hcrc->Instance));
  106. if(hcrc->State == HAL_CRC_STATE_RESET)
  107. {
  108. /* Allocate lock resource and initialize it */
  109. hcrc->Lock = HAL_UNLOCKED;
  110. /* Init the low level hardware */
  111. HAL_CRC_MspInit(hcrc);
  112. }
  113. /* Change CRC peripheral state */
  114. hcrc->State = HAL_CRC_STATE_BUSY;
  115. /* Change CRC peripheral state */
  116. hcrc->State = HAL_CRC_STATE_READY;
  117. /* Return function status */
  118. return HAL_OK;
  119. }
  120. /**
  121. * @brief DeInitializes the CRC peripheral.
  122. * @param hcrc pointer to a CRC_HandleTypeDef structure that contains
  123. * the configuration information for CRC
  124. * @retval HAL status
  125. */
  126. HAL_StatusTypeDef HAL_CRC_DeInit(CRC_HandleTypeDef *hcrc)
  127. {
  128. /* Check the CRC handle allocation */
  129. if(hcrc == NULL)
  130. {
  131. return HAL_ERROR;
  132. }
  133. /* Check the parameters */
  134. assert_param(IS_CRC_ALL_INSTANCE(hcrc->Instance));
  135. /* Change CRC peripheral state */
  136. hcrc->State = HAL_CRC_STATE_BUSY;
  137. /* DeInit the low level hardware */
  138. HAL_CRC_MspDeInit(hcrc);
  139. /* Change CRC peripheral state */
  140. hcrc->State = HAL_CRC_STATE_RESET;
  141. /* Release Lock */
  142. __HAL_UNLOCK(hcrc);
  143. /* Return function status */
  144. return HAL_OK;
  145. }
  146. /**
  147. * @brief Initializes the CRC MSP.
  148. * @param hcrc pointer to a CRC_HandleTypeDef structure that contains
  149. * the configuration information for CRC
  150. * @retval None
  151. */
  152. __weak void HAL_CRC_MspInit(CRC_HandleTypeDef *hcrc)
  153. {
  154. /* Prevent unused argument(s) compilation warning */
  155. UNUSED(hcrc);
  156. /* NOTE : This function Should not be modified, when the callback is needed,
  157. the HAL_CRC_MspInit could be implemented in the user file
  158. */
  159. }
  160. /**
  161. * @brief DeInitializes the CRC MSP.
  162. * @param hcrc pointer to a CRC_HandleTypeDef structure that contains
  163. * the configuration information for CRC
  164. * @retval None
  165. */
  166. __weak void HAL_CRC_MspDeInit(CRC_HandleTypeDef *hcrc)
  167. {
  168. /* Prevent unused argument(s) compilation warning */
  169. UNUSED(hcrc);
  170. /* NOTE : This function Should not be modified, when the callback is needed,
  171. the HAL_CRC_MspDeInit could be implemented in the user file
  172. */
  173. }
  174. /**
  175. * @}
  176. */
  177. /** @addtogroup CRC_Exported_Functions_Group2
  178. * @brief Peripheral Control functions
  179. *
  180. @verbatim
  181. ==============================================================================
  182. ##### Peripheral Control functions #####
  183. ==============================================================================
  184. [..] This section provides functions allowing to:
  185. (+) Compute the 32-bit CRC value of 32-bit data buffer,
  186. using combination of the previous CRC value and the new one.
  187. (+) Compute the 32-bit CRC value of 32-bit data buffer,
  188. independently of the previous CRC value.
  189. @endverbatim
  190. * @{
  191. */
  192. /**
  193. * @brief Computes the 32-bit CRC of 32-bit data buffer using combination
  194. * of the previous CRC value and the new one.
  195. * @param hcrc pointer to a CRC_HandleTypeDef structure that contains
  196. * the configuration information for CRC
  197. * @param pBuffer pointer to the buffer containing the data to be computed
  198. * @param BufferLength length of the buffer to be computed
  199. * @retval 32-bit CRC
  200. */
  201. uint32_t HAL_CRC_Accumulate(CRC_HandleTypeDef *hcrc, uint32_t pBuffer[], uint32_t BufferLength)
  202. {
  203. uint32_t index = 0U;
  204. /* Process Locked */
  205. __HAL_LOCK(hcrc);
  206. /* Change CRC peripheral state */
  207. hcrc->State = HAL_CRC_STATE_BUSY;
  208. /* Enter Data to the CRC calculator */
  209. for(index = 0U; index < BufferLength; index++)
  210. {
  211. hcrc->Instance->DR = pBuffer[index];
  212. }
  213. /* Change CRC peripheral state */
  214. hcrc->State = HAL_CRC_STATE_READY;
  215. /* Process Unlocked */
  216. __HAL_UNLOCK(hcrc);
  217. /* Return the CRC computed value */
  218. return hcrc->Instance->DR;
  219. }
  220. /**
  221. * @brief Computes the 32-bit CRC of 32-bit data buffer independently
  222. * of the previous CRC value.
  223. * @param hcrc pointer to a CRC_HandleTypeDef structure that contains
  224. * the configuration information for CRC
  225. * @param pBuffer Pointer to the buffer containing the data to be computed
  226. * @param BufferLength Length of the buffer to be computed
  227. * @retval 32-bit CRC
  228. */
  229. uint32_t HAL_CRC_Calculate(CRC_HandleTypeDef *hcrc, uint32_t pBuffer[], uint32_t BufferLength)
  230. {
  231. uint32_t index = 0U;
  232. /* Process Locked */
  233. __HAL_LOCK(hcrc);
  234. /* Change CRC peripheral state */
  235. hcrc->State = HAL_CRC_STATE_BUSY;
  236. /* Reset CRC Calculation Unit */
  237. __HAL_CRC_DR_RESET(hcrc);
  238. /* Enter Data to the CRC calculator */
  239. for(index = 0U; index < BufferLength; index++)
  240. {
  241. hcrc->Instance->DR = pBuffer[index];
  242. }
  243. /* Change CRC peripheral state */
  244. hcrc->State = HAL_CRC_STATE_READY;
  245. /* Process Unlocked */
  246. __HAL_UNLOCK(hcrc);
  247. /* Return the CRC computed value */
  248. return hcrc->Instance->DR;
  249. }
  250. /**
  251. * @}
  252. */
  253. /** @addtogroup CRC_Exported_Functions_Group3
  254. * @brief Peripheral State functions
  255. *
  256. @verbatim
  257. ==============================================================================
  258. ##### Peripheral State functions #####
  259. ==============================================================================
  260. [..]
  261. This subsection permits to get in run-time the status of the peripheral
  262. and the data flow.
  263. @endverbatim
  264. * @{
  265. */
  266. /**
  267. * @brief Returns the CRC state.
  268. * @param hcrc pointer to a CRC_HandleTypeDef structure that contains
  269. * the configuration information for CRC
  270. * @retval HAL state
  271. */
  272. HAL_CRC_StateTypeDef HAL_CRC_GetState(CRC_HandleTypeDef *hcrc)
  273. {
  274. return hcrc->State;
  275. }
  276. /**
  277. * @}
  278. */
  279. /**
  280. * @}
  281. */
  282. #endif /* HAL_CRC_MODULE_ENABLED */
  283. /**
  284. * @}
  285. */
  286. /**
  287. * @}
  288. */
  289. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/