stm32f1xx_hal_crc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. /**
  2. ******************************************************************************
  3. * @file stm32f1xx_hal_crc.c
  4. * @author MCD Application Team
  5. * @version V1.1.1
  6. * @date 12-May-2017
  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) 2016 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 "stm32f1xx_hal.h"
  59. /** @addtogroup STM32F1xx_HAL_Driver
  60. * @{
  61. */
  62. /** @defgroup CRC CRC
  63. * @brief CRC HAL module driver.
  64. * @{
  65. */
  66. #ifdef HAL_CRC_MODULE_ENABLED
  67. /* Private typedef -----------------------------------------------------------*/
  68. /* Private define ------------------------------------------------------------*/
  69. /* Private macro -------------------------------------------------------------*/
  70. /* Private variables ---------------------------------------------------------*/
  71. /* Private function prototypes -----------------------------------------------*/
  72. /* Private functions ---------------------------------------------------------*/
  73. /** @defgroup CRC_Exported_Functions CRC Exported Functions
  74. * @{
  75. */
  76. /** @defgroup CRC_Exported_Functions_Group1 Initialization and de-initialization functions
  77. * @brief Initialization and Configuration 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_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. /* Resets the CRC calculation unit and sets the data register to 0xFFFF FFFF */
  140. __HAL_CRC_DR_RESET(hcrc);
  141. /* Reset IDR register content */
  142. CLEAR_BIT(hcrc->Instance->IDR, CRC_IDR_IDR);
  143. /* Change CRC peripheral state */
  144. hcrc->State = HAL_CRC_STATE_RESET;
  145. /* Release Lock */
  146. __HAL_UNLOCK(hcrc);
  147. /* Return function status */
  148. return HAL_OK;
  149. }
  150. /**
  151. * @brief Initializes the CRC MSP.
  152. * @param hcrc: pointer to a CRC_HandleTypeDef structure that contains
  153. * the configuration information for CRC
  154. * @retval None
  155. */
  156. __weak void HAL_CRC_MspInit(CRC_HandleTypeDef *hcrc)
  157. {
  158. /* Prevent unused argument(s) compilation warning */
  159. UNUSED(hcrc);
  160. /* NOTE : This function Should not be modified, when the callback is needed,
  161. the HAL_CRC_MspInit could be implemented in the user file
  162. */
  163. }
  164. /**
  165. * @brief DeInitializes the CRC MSP.
  166. * @param hcrc: pointer to a CRC_HandleTypeDef structure that contains
  167. * the configuration information for CRC
  168. * @retval None
  169. */
  170. __weak void HAL_CRC_MspDeInit(CRC_HandleTypeDef *hcrc)
  171. {
  172. /* Prevent unused argument(s) compilation warning */
  173. UNUSED(hcrc);
  174. /* NOTE : This function Should not be modified, when the callback is needed,
  175. the HAL_CRC_MspDeInit could be implemented in the user file
  176. */
  177. }
  178. /**
  179. * @}
  180. */
  181. /** @defgroup CRC_Exported_Functions_Group2 Peripheral Control functions
  182. * @brief management functions.
  183. *
  184. @verbatim
  185. ==============================================================================
  186. ##### Peripheral Control functions #####
  187. ==============================================================================
  188. [..] This section provides functions allowing to:
  189. (+) Compute the 32-bit CRC value of 32-bit data buffer,
  190. using combination of the previous CRC value and the new one.
  191. (+) Compute the 32-bit CRC value of 32-bit data buffer,
  192. independently of the previous CRC value.
  193. @endverbatim
  194. * @{
  195. */
  196. /**
  197. * @brief Computes the 32-bit CRC of 32-bit data buffer using combination
  198. * of the previous CRC value and the new one.
  199. * @param hcrc: pointer to a CRC_HandleTypeDef structure that contains
  200. * the configuration information for CRC
  201. * @param pBuffer: pointer to the buffer containing the data to be computed
  202. * @param BufferLength: length of the buffer to be computed (defined in word, 4 bytes)
  203. * @retval 32-bit CRC
  204. */
  205. uint32_t HAL_CRC_Accumulate(CRC_HandleTypeDef *hcrc, uint32_t pBuffer[], uint32_t BufferLength)
  206. {
  207. uint32_t index = 0U;
  208. /* Process Locked */
  209. __HAL_LOCK(hcrc);
  210. /* Change CRC peripheral state */
  211. hcrc->State = HAL_CRC_STATE_BUSY;
  212. /* Enter Data to the CRC calculator */
  213. for(index = 0U; index < BufferLength; index++)
  214. {
  215. hcrc->Instance->DR = pBuffer[index];
  216. }
  217. /* Change CRC peripheral state */
  218. hcrc->State = HAL_CRC_STATE_READY;
  219. /* Process Unlocked */
  220. __HAL_UNLOCK(hcrc);
  221. /* Return the CRC computed value */
  222. return hcrc->Instance->DR;
  223. }
  224. /**
  225. * @brief Computes the 32-bit CRC of 32-bit data buffer independently
  226. * of the previous CRC value.
  227. * @param hcrc: pointer to a CRC_HandleTypeDef structure that contains
  228. * the configuration information for CRC
  229. * @param pBuffer: Pointer to the buffer containing the data to be computed
  230. * @param BufferLength: Length of the buffer to be computed (defined in word, 4 bytes)
  231. * @retval 32-bit CRC
  232. */
  233. uint32_t HAL_CRC_Calculate(CRC_HandleTypeDef *hcrc, uint32_t pBuffer[], uint32_t BufferLength)
  234. {
  235. uint32_t index = 0U;
  236. /* Process Locked */
  237. __HAL_LOCK(hcrc);
  238. /* Change CRC peripheral state */
  239. hcrc->State = HAL_CRC_STATE_BUSY;
  240. /* Reset CRC Calculation Unit */
  241. __HAL_CRC_DR_RESET(hcrc);
  242. /* Enter Data to the CRC calculator */
  243. for(index = 0U; index < BufferLength; index++)
  244. {
  245. hcrc->Instance->DR = pBuffer[index];
  246. }
  247. /* Change CRC peripheral state */
  248. hcrc->State = HAL_CRC_STATE_READY;
  249. /* Process Unlocked */
  250. __HAL_UNLOCK(hcrc);
  251. /* Return the CRC computed value */
  252. return hcrc->Instance->DR;
  253. }
  254. /**
  255. * @}
  256. */
  257. /** @defgroup CRC_Exported_Functions_Group3 Peripheral State functions
  258. * @brief Peripheral State functions.
  259. *
  260. @verbatim
  261. ==============================================================================
  262. ##### Peripheral State functions #####
  263. ==============================================================================
  264. [..]
  265. This subsection permits to get in run-time the status of the peripheral.
  266. @endverbatim
  267. * @{
  268. */
  269. /**
  270. * @brief Returns the CRC state.
  271. * @param hcrc: pointer to a CRC_HandleTypeDef structure that contains
  272. * the configuration information for CRC
  273. * @retval HAL state
  274. */
  275. HAL_CRC_StateTypeDef HAL_CRC_GetState(CRC_HandleTypeDef *hcrc)
  276. {
  277. return hcrc->State;
  278. }
  279. /**
  280. * @}
  281. */
  282. /**
  283. * @}
  284. */
  285. #endif /* HAL_CRC_MODULE_ENABLED */
  286. /**
  287. * @}
  288. */
  289. /**
  290. * @}
  291. */
  292. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/