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 ******************************************************************************
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
41 @endverbatim
42 ******************************************************************************
43 */
44
45 /* Includes ------------------------------------------------------------------*/
46 #include "stm32f4xx_hal.h"
47
48 /** @addtogroup STM32F4xx_HAL_Driver
49 * @{
50 */
51
52 /** @defgroup CRC CRC
53 * @brief CRC HAL module driver.
54 * @{
55 */
56
57 #ifdef HAL_CRC_MODULE_ENABLED
58
59 /* Private typedef -----------------------------------------------------------*/
60 /* Private define ------------------------------------------------------------*/
61 /* Private macro -------------------------------------------------------------*/
62 /* Private variables ---------------------------------------------------------*/
63 /* Private function prototypes -----------------------------------------------*/
64
65 /* Exported functions --------------------------------------------------------*/
66
67 /** @defgroup CRC_Exported_Functions CRC Exported Functions
68 * @{
69 */
70
71 /** @defgroup CRC_Exported_Functions_Group1 Initialization and de-initialization functions
72 * @brief Initialization and Configuration functions.
73 *
74 @verbatim
75 ===============================================================================
76 ##### Initialization and de-initialization functions #####
77 ===============================================================================
78 [..] This section provides functions allowing to:
79 (+) Initialize the CRC according to the specified parameters
80 in the CRC_InitTypeDef and create the associated handle
81 (+) DeInitialize the CRC peripheral
82 (+) Initialize the CRC MSP (MCU Specific Package)
83 (+) DeInitialize the CRC MSP
84
85 @endverbatim
86 * @{
87 */
88
89 /**
90 * @brief Initialize the CRC according to the specified
91 * parameters in the CRC_InitTypeDef and create the associated handle.
92 * @param hcrc CRC handle
93 * @retval HAL status
94 */
HAL_CRC_Init(CRC_HandleTypeDef * hcrc)95 HAL_StatusTypeDef HAL_CRC_Init(CRC_HandleTypeDef *hcrc)
96 {
97 /* Check the CRC handle allocation */
98 if (hcrc == NULL)
99 {
100 return HAL_ERROR;
101 }
102
103 /* Check the parameters */
104 assert_param(IS_CRC_ALL_INSTANCE(hcrc->Instance));
105
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
114 /* Change CRC peripheral state */
115 hcrc->State = HAL_CRC_STATE_READY;
116
117 /* Return function status */
118 return HAL_OK;
119 }
120
121 /**
122 * @brief DeInitialize the CRC peripheral.
123 * @param hcrc CRC handle
124 * @retval HAL status
125 */
HAL_CRC_DeInit(CRC_HandleTypeDef * hcrc)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
134 /* Check the parameters */
135 assert_param(IS_CRC_ALL_INSTANCE(hcrc->Instance));
136
137 /* Check the CRC peripheral state */
138 if (hcrc->State == HAL_CRC_STATE_BUSY)
139 {
140 return HAL_BUSY;
141 }
142
143 /* Change CRC peripheral state */
144 hcrc->State = HAL_CRC_STATE_BUSY;
145
146 /* Reset CRC calculation unit */
147 __HAL_CRC_DR_RESET(hcrc);
148
149 /* Reset IDR register content */
150 CLEAR_BIT(hcrc->Instance->IDR, CRC_IDR_IDR);
151
152 /* DeInit the low level hardware */
153 HAL_CRC_MspDeInit(hcrc);
154
155 /* Change CRC peripheral state */
156 hcrc->State = HAL_CRC_STATE_RESET;
157
158 /* Process unlocked */
159 __HAL_UNLOCK(hcrc);
160
161 /* Return function status */
162 return HAL_OK;
163 }
164
165 /**
166 * @brief Initializes the CRC MSP.
167 * @param hcrc CRC handle
168 * @retval None
169 */
HAL_CRC_MspInit(CRC_HandleTypeDef * hcrc)170 __weak void HAL_CRC_MspInit(CRC_HandleTypeDef *hcrc)
171 {
172 /* Prevent unused argument(s) compilation warning */
173 UNUSED(hcrc);
174
175 /* NOTE : This function should not be modified, when the callback is needed,
176 the HAL_CRC_MspInit can be implemented in the user file
177 */
178 }
179
180 /**
181 * @brief DeInitialize the CRC MSP.
182 * @param hcrc CRC handle
183 * @retval None
184 */
HAL_CRC_MspDeInit(CRC_HandleTypeDef * hcrc)185 __weak void HAL_CRC_MspDeInit(CRC_HandleTypeDef *hcrc)
186 {
187 /* Prevent unused argument(s) compilation warning */
188 UNUSED(hcrc);
189
190 /* NOTE : This function should not be modified, when the callback is needed,
191 the HAL_CRC_MspDeInit can be implemented in the user file
192 */
193 }
194
195 /**
196 * @}
197 */
198
199 /** @defgroup CRC_Exported_Functions_Group2 Peripheral Control functions
200 * @brief management functions.
201 *
202 @verbatim
203 ===============================================================================
204 ##### Peripheral Control functions #####
205 ===============================================================================
206 [..] This section provides functions allowing to:
207 (+) compute the 32-bit CRC value of a 32-bit data buffer
208 using combination of the previous CRC value and the new one.
209
210 [..] or
211
212 (+) compute the 32-bit CRC value of a 32-bit data buffer
213 independently of the previous CRC value.
214
215 @endverbatim
216 * @{
217 */
218
219 /**
220 * @brief Compute the 32-bit CRC value of a 32-bit data buffer
221 * starting with the previously computed CRC as initialization value.
222 * @param hcrc CRC handle
223 * @param pBuffer pointer to the input data buffer.
224 * @param BufferLength input data buffer length (number of uint32_t words).
225 * @retval uint32_t CRC (returned value LSBs for CRC shorter than 32 bits)
226 */
HAL_CRC_Accumulate(CRC_HandleTypeDef * hcrc,uint32_t pBuffer[],uint32_t BufferLength)227 uint32_t HAL_CRC_Accumulate(CRC_HandleTypeDef *hcrc, uint32_t pBuffer[], uint32_t BufferLength)
228 {
229 uint32_t index; /* CRC input data buffer index */
230 uint32_t temp = 0U; /* CRC output (read from hcrc->Instance->DR register) */
231
232 /* Change CRC peripheral state */
233 hcrc->State = HAL_CRC_STATE_BUSY;
234
235 /* Enter Data to the CRC calculator */
236 for (index = 0U; index < BufferLength; index++)
237 {
238 hcrc->Instance->DR = pBuffer[index];
239 }
240 temp = hcrc->Instance->DR;
241
242 /* Change CRC peripheral state */
243 hcrc->State = HAL_CRC_STATE_READY;
244
245 /* Return the CRC computed value */
246 return temp;
247 }
248
249 /**
250 * @brief Compute the 32-bit CRC value of a 32-bit data buffer
251 * starting with hcrc->Instance->INIT as initialization value.
252 * @param hcrc CRC handle
253 * @param pBuffer pointer to the input data buffer.
254 * @param BufferLength input data buffer length (number of uint32_t words).
255 * @retval uint32_t CRC (returned value LSBs for CRC shorter than 32 bits)
256 */
HAL_CRC_Calculate(CRC_HandleTypeDef * hcrc,uint32_t pBuffer[],uint32_t BufferLength)257 uint32_t HAL_CRC_Calculate(CRC_HandleTypeDef *hcrc, uint32_t pBuffer[], uint32_t BufferLength)
258 {
259 uint32_t index; /* CRC input data buffer index */
260 uint32_t temp = 0U; /* CRC output (read from hcrc->Instance->DR register) */
261
262 /* Change CRC peripheral state */
263 hcrc->State = HAL_CRC_STATE_BUSY;
264
265 /* Reset CRC Calculation Unit (hcrc->Instance->INIT is
266 * written in hcrc->Instance->DR) */
267 __HAL_CRC_DR_RESET(hcrc);
268
269 /* Enter 32-bit input data to the CRC calculator */
270 for (index = 0U; index < BufferLength; index++)
271 {
272 hcrc->Instance->DR = pBuffer[index];
273 }
274 temp = hcrc->Instance->DR;
275
276 /* Change CRC peripheral state */
277 hcrc->State = HAL_CRC_STATE_READY;
278
279 /* Return the CRC computed value */
280 return temp;
281 }
282
283 /**
284 * @}
285 */
286
287 /** @defgroup CRC_Exported_Functions_Group3 Peripheral State functions
288 * @brief Peripheral State functions.
289 *
290 @verbatim
291 ===============================================================================
292 ##### Peripheral State functions #####
293 ===============================================================================
294 [..]
295 This subsection permits to get in run-time the status of the peripheral.
296
297 @endverbatim
298 * @{
299 */
300
301 /**
302 * @brief Return the CRC handle state.
303 * @param hcrc CRC handle
304 * @retval HAL state
305 */
HAL_CRC_GetState(CRC_HandleTypeDef * hcrc)306 HAL_CRC_StateTypeDef HAL_CRC_GetState(CRC_HandleTypeDef *hcrc)
307 {
308 /* Return CRC handle state */
309 return hcrc->State;
310 }
311
312 /**
313 * @}
314 */
315
316 /**
317 * @}
318 */
319
320
321 #endif /* HAL_CRC_MODULE_ENABLED */
322 /**
323 * @}
324 */
325
326 /**
327 * @}
328 */
329