2 ******************************************************************************
3 * @file stm32f3xx_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
13 ===============================================================================
14 ##### How to use this driver #####
15 ===============================================================================
17 (+) Enable CRC AHB clock using __HAL_RCC_CRC_CLK_ENABLE();
18 (+) Initialize CRC calculator
19 (++)specify generating polynomial (IP default or non-default one)
20 (++)specify initialization value (IP 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
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
31 ******************************************************************************
34 * <h2><center>© COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
36 * Redistribution and use in source and binary forms, with or without modification,
37 * are permitted provided that the following conditions are met:
38 * 1. Redistributions of source code must retain the above copyright notice,
39 * this list of conditions and the following disclaimer.
40 * 2. Redistributions in binary form must reproduce the above copyright notice,
41 * this list of conditions and the following disclaimer in the documentation
42 * and/or other materials provided with the distribution.
43 * 3. Neither the name of STMicroelectronics nor the names of its contributors
44 * may be used to endorse or promote products derived from this software
45 * without specific prior written permission.
47 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
48 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
49 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
50 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
51 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
52 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
53 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
54 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
55 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
56 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
58 ******************************************************************************
61 /* Includes ------------------------------------------------------------------*/
62 #include "stm32f3xx_hal.h"
64 /** @addtogroup STM32F3xx_HAL_Driver
69 * @brief CRC HAL module driver.
73 #ifdef HAL_CRC_MODULE_ENABLED
75 /* Private typedef -----------------------------------------------------------*/
76 /* Private define ------------------------------------------------------------*/
77 /* Private macro -------------------------------------------------------------*/
78 /* Private variables ---------------------------------------------------------*/
79 /* Private function prototypes -----------------------------------------------*/
80 /** @defgroup CRC_Private_Functions CRC Private Functions
83 static uint32_t CRC_Handle_8(CRC_HandleTypeDef
*hcrc
, uint8_t pBuffer
[], uint32_t BufferLength
);
84 static uint32_t CRC_Handle_16(CRC_HandleTypeDef
*hcrc
, uint16_t pBuffer
[], uint32_t BufferLength
);
89 /* Exported functions --------------------------------------------------------*/
91 /** @defgroup CRC_Exported_Functions CRC Exported Functions
95 /** @defgroup CRC_Exported_Functions_Group1 Initialization and de-initialization functions
96 * @brief Initialization and Configuration functions.
99 ===============================================================================
100 ##### Initialization and de-initialization functions #####
101 ===============================================================================
102 [..] This section provides functions allowing to:
103 (+) Initialize the CRC according to the specified parameters
104 in the CRC_InitTypeDef and create the associated handle
105 (+) DeInitialize the CRC peripheral
106 (+) Initialize the CRC MSP (MCU Specific Package)
107 (+) DeInitialize the CRC MSP
114 * @brief Initialize the CRC according to the specified
115 * parameters in the CRC_InitTypeDef and initialize the associated handle.
116 * @param hcrc CRC handle
119 HAL_StatusTypeDef
HAL_CRC_Init(CRC_HandleTypeDef
*hcrc
)
121 /* Check the CRC handle allocation */
127 /* Check the parameters */
128 assert_param(IS_CRC_ALL_INSTANCE(hcrc
->Instance
));
130 if(hcrc
->State
== HAL_CRC_STATE_RESET
)
132 /* Allocate lock resource and initialize it */
133 hcrc
->Lock
= HAL_UNLOCKED
;
135 /* Init the low level hardware */
136 HAL_CRC_MspInit(hcrc
);
139 hcrc
->State
= HAL_CRC_STATE_BUSY
;
141 /* check whether or not non-default generating polynomial has been
142 * picked up by user */
143 assert_param(IS_DEFAULT_POLYNOMIAL(hcrc
->Init
.DefaultPolynomialUse
));
144 if (hcrc
->Init
.DefaultPolynomialUse
== DEFAULT_POLYNOMIAL_ENABLE
)
146 /* initialize IP with default generating polynomial */
147 WRITE_REG(hcrc
->Instance
->POL
, DEFAULT_CRC32_POLY
);
148 MODIFY_REG(hcrc
->Instance
->CR
, CRC_CR_POLYSIZE
, CRC_POLYLENGTH_32B
);
152 /* initialize CRC IP with generating polynomial defined by user */
153 if (HAL_CRCEx_Polynomial_Set(hcrc
, hcrc
->Init
.GeneratingPolynomial
, hcrc
->Init
.CRCLength
) != HAL_OK
)
159 /* check whether or not non-default CRC initial value has been
160 * picked up by user */
161 assert_param(IS_DEFAULT_INIT_VALUE(hcrc
->Init
.DefaultInitValueUse
));
162 if (hcrc
->Init
.DefaultInitValueUse
== DEFAULT_INIT_VALUE_ENABLE
)
164 WRITE_REG(hcrc
->Instance
->INIT
, DEFAULT_CRC_INITVALUE
);
168 WRITE_REG(hcrc
->Instance
->INIT
, hcrc
->Init
.InitValue
);
172 /* set input data inversion mode */
173 assert_param(IS_CRC_INPUTDATA_INVERSION_MODE(hcrc
->Init
.InputDataInversionMode
));
174 MODIFY_REG(hcrc
->Instance
->CR
, CRC_CR_REV_IN
, hcrc
->Init
.InputDataInversionMode
);
176 /* set output data inversion mode */
177 assert_param(IS_CRC_OUTPUTDATA_INVERSION_MODE(hcrc
->Init
.OutputDataInversionMode
));
178 MODIFY_REG(hcrc
->Instance
->CR
, CRC_CR_REV_OUT
, hcrc
->Init
.OutputDataInversionMode
);
180 /* makes sure the input data format (bytes, halfwords or words stream)
181 * is properly specified by user */
182 assert_param(IS_CRC_INPUTDATA_FORMAT(hcrc
->InputDataFormat
));
184 /* Change CRC peripheral state */
185 hcrc
->State
= HAL_CRC_STATE_READY
;
187 /* Return function status */
192 * @brief DeInitialize the CRC peripheral.
193 * @param hcrc CRC handle
196 HAL_StatusTypeDef
HAL_CRC_DeInit(CRC_HandleTypeDef
*hcrc
)
198 /* Check the CRC handle allocation */
204 /* Check the parameters */
205 assert_param(IS_CRC_ALL_INSTANCE(hcrc
->Instance
));
207 /* Check the CRC peripheral state */
208 if(hcrc
->State
== HAL_CRC_STATE_BUSY
)
213 /* Change CRC peripheral state */
214 hcrc
->State
= HAL_CRC_STATE_BUSY
;
216 /* Reset CRC calculation unit */
217 __HAL_CRC_DR_RESET(hcrc
);
219 /* Reset IDR register content */
220 CLEAR_BIT(hcrc
->Instance
->IDR
, CRC_IDR_IDR
) ;
222 /* DeInit the low level hardware */
223 HAL_CRC_MspDeInit(hcrc
);
225 /* Change CRC peripheral state */
226 hcrc
->State
= HAL_CRC_STATE_RESET
;
228 /* Process unlocked */
231 /* Return function status */
236 * @brief Initializes the CRC MSP.
237 * @param hcrc CRC handle
240 __weak
void HAL_CRC_MspInit(CRC_HandleTypeDef
*hcrc
)
242 /* Prevent unused argument(s) compilation warning */
245 /* NOTE : This function should not be modified, when the callback is needed,
246 the HAL_CRC_MspInit can be implemented in the user file
251 * @brief DeInitialize the CRC MSP.
252 * @param hcrc CRC handle
255 __weak
void HAL_CRC_MspDeInit(CRC_HandleTypeDef
*hcrc
)
257 /* Prevent unused argument(s) compilation warning */
260 /* NOTE : This function should not be modified, when the callback is needed,
261 the HAL_CRC_MspDeInit can be implemented in the user file
269 /** @defgroup CRC_Exported_Functions_Group2 Peripheral Control functions
270 * @brief management functions.
273 ===============================================================================
274 ##### Peripheral Control functions #####
275 ===============================================================================
276 [..] This section provides functions allowing to:
277 (+) compute the 7U, 8U, 16 or 32-bit CRC value of an 8U, 16 or 32-bit data buffer
278 using the combination of the previous CRC value and the new one
282 (+) compute the 7U, 8U, 16 or 32-bit CRC value of an 8U, 16 or 32-bit data buffer
283 independently of the previous CRC value.
290 * @brief Compute the 7, 8, 16 or 32-bit CRC value of an 8, 16 or 32-bit data buffer
291 * starting with the previously computed CRC as initialization value.
292 * @param hcrc CRC handle
293 * @param pBuffer pointer to the input data buffer, exact input data format is
294 * provided by hcrc->InputDataFormat.
295 * @param BufferLength input data buffer length (number of bytes if pBuffer
296 * type is * uint8_t, number of half-words if pBuffer type is * uint16_t,
297 * number of words if pBuffer type is * uint32_t).
298 * @note By default, the API expects a uint32_t pointer as input buffer parameter.
299 * Input buffer pointers with other types simply need to be cast in uint32_t
300 * and the API will internally adjust its input data processing based on the
301 * handle field hcrc->InputDataFormat.
302 * @retval uint32_t CRC (returned value LSBs for CRC shorter than 32 bits)
304 uint32_t HAL_CRC_Accumulate(CRC_HandleTypeDef
*hcrc
, uint32_t pBuffer
[], uint32_t BufferLength
)
306 uint32_t index
= 0U; /* CRC input data buffer index */
307 uint32_t temp
= 0U; /* CRC output (read from hcrc->Instance->DR register) */
312 /* Change CRC peripheral state */
313 hcrc
->State
= HAL_CRC_STATE_BUSY
;
315 switch (hcrc
->InputDataFormat
)
317 case CRC_INPUTDATA_FORMAT_WORDS
:
318 /* Enter Data to the CRC calculator */
319 for(index
= 0U; index
< BufferLength
; index
++)
321 hcrc
->Instance
->DR
= pBuffer
[index
];
323 temp
= hcrc
->Instance
->DR
;
326 case CRC_INPUTDATA_FORMAT_BYTES
:
327 temp
= CRC_Handle_8(hcrc
, (uint8_t*)pBuffer
, BufferLength
);
330 case CRC_INPUTDATA_FORMAT_HALFWORDS
:
331 temp
= CRC_Handle_16(hcrc
, (uint16_t*)pBuffer
, BufferLength
);
338 /* Change CRC peripheral state */
339 hcrc
->State
= HAL_CRC_STATE_READY
;
341 /* Process unlocked */
344 /* Return the CRC computed value */
350 * @brief Compute the 7, 8, 16 or 32-bit CRC value of an 8, 16 or 32-bit data buffer
351 * starting with hcrc->Instance->INIT as initialization value.
352 * @param hcrc CRC handle
353 * @param pBuffer pointer to the input data buffer, exact input data format is
354 * provided by hcrc->InputDataFormat.
355 * @param BufferLength input data buffer length (number of bytes if pBuffer
356 * type is * uint8_t, number of half-words if pBuffer type is * uint16_t,
357 * number of words if pBuffer type is * uint32_t).
358 * @note By default, the API expects a uint32_t pointer as input buffer parameter.
359 * Input buffer pointers with other types simply need to be cast in uint32_t
360 * and the API will internally adjust its input data processing based on the
361 * handle field hcrc->InputDataFormat.
362 * @retval uint32_t CRC (returned value LSBs for CRC shorter than 32 bits)
364 uint32_t HAL_CRC_Calculate(CRC_HandleTypeDef
*hcrc
, uint32_t pBuffer
[], uint32_t BufferLength
)
366 uint32_t index
= 0U; /* CRC input data buffer index */
367 uint32_t temp
= 0U; /* CRC output (read from hcrc->Instance->DR register) */
372 /* Change CRC peripheral state */
373 hcrc
->State
= HAL_CRC_STATE_BUSY
;
375 /* Reset CRC Calculation Unit (hcrc->Instance->INIT is
376 * written in hcrc->Instance->DR) */
377 __HAL_CRC_DR_RESET(hcrc
);
379 switch (hcrc
->InputDataFormat
)
381 case CRC_INPUTDATA_FORMAT_WORDS
:
382 /* Enter 32-bit input data to the CRC calculator */
383 for(index
= 0U; index
< BufferLength
; index
++)
385 hcrc
->Instance
->DR
= pBuffer
[index
];
387 temp
= hcrc
->Instance
->DR
;
390 case CRC_INPUTDATA_FORMAT_BYTES
:
391 /* Specific 8-bit input data handling */
392 temp
= CRC_Handle_8(hcrc
, (uint8_t*)pBuffer
, BufferLength
);
395 case CRC_INPUTDATA_FORMAT_HALFWORDS
:
396 /* Specific 16-bit input data handling */
397 temp
= CRC_Handle_16(hcrc
, (uint16_t*)pBuffer
, BufferLength
);
404 /* Change CRC peripheral state */
405 hcrc
->State
= HAL_CRC_STATE_READY
;
407 /* Process unlocked */
410 /* Return the CRC computed value */
418 /** @defgroup CRC_Exported_Functions_Group3 Peripheral State functions
419 * @brief Peripheral State functions.
422 ===============================================================================
423 ##### Peripheral State functions #####
424 ===============================================================================
426 This subsection permits to get in run-time the status of the peripheral.
433 * @brief Return the CRC handle state.
434 * @param hcrc CRC handle
437 HAL_CRC_StateTypeDef
HAL_CRC_GetState(CRC_HandleTypeDef
*hcrc
)
439 /* Return CRC handle state */
451 /** @defgroup CRC_Private_Functions CRC Private Functions
456 * @brief Enter 8-bit input data to the CRC calculator.
457 * Specific data handling to optimize processing time.
458 * @param hcrc CRC handle
459 * @param pBuffer pointer to the input data buffer
460 * @param BufferLength input data buffer length
461 * @retval uint32_t CRC (returned value LSBs for CRC shorter than 32 bits)
463 static uint32_t CRC_Handle_8(CRC_HandleTypeDef
*hcrc
, uint8_t pBuffer
[], uint32_t BufferLength
)
465 uint32_t i
= 0U; /* input data buffer index */
467 /* Processing time optimization: 4 bytes are entered in a row with a single word write,
468 * last bytes must be carefully fed to the CRC calculator to ensure a correct type
469 * handling by the IP */
470 for(i
= 0U; i
< (BufferLength
/4U); i
++)
472 hcrc
->Instance
->DR
= ((uint32_t)pBuffer
[4U*i
]<<24U) | ((uint32_t)pBuffer
[4U*i
+1]<<16U) | ((uint32_t)pBuffer
[4U*i
+2]<<8U) | (uint32_t)pBuffer
[4U*i
+3];
474 /* last bytes specific handling */
475 if ((BufferLength
%4U) != 0U)
477 if (BufferLength
%4U == 1U)
479 *(uint8_t volatile*) (&hcrc
->Instance
->DR
) = pBuffer
[4*i
];
481 if (BufferLength
%4U == 2U)
483 *(uint16_t volatile*) (&hcrc
->Instance
->DR
) = ((uint32_t)pBuffer
[4*i
]<<8) | (uint32_t)pBuffer
[4*i
+1];
485 if (BufferLength
%4U == 3U)
487 *(uint16_t volatile*) (&hcrc
->Instance
->DR
) = ((uint32_t)pBuffer
[4*i
]<<8) | (uint32_t)pBuffer
[4*i
+1];
488 *(uint8_t volatile*) (&hcrc
->Instance
->DR
) = pBuffer
[4*i
+2];
492 /* Return the CRC computed value */
493 return hcrc
->Instance
->DR
;
499 * @brief Enter 16-bit input data to the CRC calculator.
500 * Specific data handling to optimize processing time.
501 * @param hcrc CRC handle
502 * @param pBuffer pointer to the input data buffer
503 * @param BufferLength input data buffer length
504 * @retval uint32_t CRC (returned value LSBs for CRC shorter than 32 bits)
506 static uint32_t CRC_Handle_16(CRC_HandleTypeDef
*hcrc
, uint16_t pBuffer
[], uint32_t BufferLength
)
508 uint32_t i
= 0U; /* input data buffer index */
510 /* Processing time optimization: 2 HalfWords are entered in a row with a single word write,
511 * in case of odd length, last HalfWord must be carefully fed to the CRC calculator to ensure
512 * a correct type handling by the IP */
513 for(i
= 0U; i
< (BufferLength
/2U); i
++)
515 hcrc
->Instance
->DR
= ((uint32_t)pBuffer
[2U*i
]<<16U) | (uint32_t)pBuffer
[2U*i
+1];
517 if ((BufferLength
%2U) != 0U)
519 *(uint16_t volatile*) (&hcrc
->Instance
->DR
) = pBuffer
[2*i
];
522 /* Return the CRC computed value */
523 return hcrc
->Instance
->DR
;
530 #endif /* HAL_CRC_MODULE_ENABLED */
539 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/