stm32f4xx_hal_crc.c 11 KB

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