2 ******************************************************************************
3 * @file stm32f3xx_hal_crc.c
4 * @author MCD Application Team
7 * @brief CRC HAL module driver.
9 * This file provides firmware functions to manage the following
10 * functionalities of the CRC peripheral:
11 * + Initialization and de-initialization functions
12 * + Peripheral Control functions
13 * + Peripheral State functions
16 ===============================================================================
17 ##### How to use this driver #####
18 ===============================================================================
20 (+) Enable CRC AHB clock using __CRC_CLK_ENABLE();
21 (+) Initialize CRC calculator
22 - specify generating polynomial (IP default or non-default one)
23 - specify initialization value (IP default or non-default one)
24 - specify input data format
25 - specify input or output data inversion mode if any
26 (+) Use HAL_CRC_Accumulate() function to compute the CRC value of the
27 input data buffer starting with the previously computed CRC as
29 (+) Use HAL_CRC_Calculate() function to compute the CRC value of the
30 input data buffer starting with the defined initialization value
31 (default or non-default) to initiate CRC calculation
34 ******************************************************************************
37 * <h2><center>© COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
39 * Redistribution and use in source and binary forms, with or without modification,
40 * are permitted provided that the following conditions are met:
41 * 1. Redistributions of source code must retain the above copyright notice,
42 * this list of conditions and the following disclaimer.
43 * 2. Redistributions in binary form must reproduce the above copyright notice,
44 * this list of conditions and the following disclaimer in the documentation
45 * and/or other materials provided with the distribution.
46 * 3. Neither the name of STMicroelectronics nor the names of its contributors
47 * may be used to endorse or promote products derived from this software
48 * without specific prior written permission.
50 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
51 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
53 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
54 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
55 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
56 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
57 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
58 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
59 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
61 ******************************************************************************
64 /* Includes ------------------------------------------------------------------*/
65 #include "stm32f3xx_hal.h"
67 /** @addtogroup STM32F3xx_HAL_Driver
71 /** @defgroup CRC CRC HAL module driver
72 * @brief CRC HAL module driver.
76 #ifdef HAL_CRC_MODULE_ENABLED
78 /* Private typedef -----------------------------------------------------------*/
79 /* Private define ------------------------------------------------------------*/
80 /* Private macro -------------------------------------------------------------*/
81 /* Private variables ---------------------------------------------------------*/
82 /* Private function prototypes -----------------------------------------------*/
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);
86 /* Exported functions --------------------------------------------------------*/
87 /** @defgroup CRC_Exported_Functions CRC Exported Functions
91 /** @defgroup CRC_Exported_Functions_Group1 Initialization and de-initialization functions
92 * @brief Initialization and Configuration functions.
95 ===============================================================================
96 ##### Initialization/de-initialization functions #####
97 ===============================================================================
98 [..] This section provides functions allowing to:
99 (+) Initialize the CRC according to the specified parameters
100 in the CRC_InitTypeDef and create the associated handle
101 (+) DeInitialize the CRC peripheral
102 (+) Initialize the CRC MSP
103 (+) DeInitialize CRC MSP
110 * @brief Initializes the CRC according to the specified
111 * parameters in the CRC_InitTypeDef and creates the associated handle.
112 * @param hcrc: CRC handle
115 HAL_StatusTypeDef HAL_CRC_Init(CRC_HandleTypeDef *hcrc)
117 /* Check the CRC handle allocation */
123 /* Check the parameters */
124 assert_param(IS_CRC_ALL_INSTANCE(hcrc->Instance));
126 if(hcrc->State == HAL_CRC_STATE_RESET)
128 /* Init the low level hardware */
129 HAL_CRC_MspInit(hcrc);
132 hcrc->State = HAL_CRC_STATE_BUSY;
134 /* check whether or not non-default generating polynomial has been
135 * picked up by user */
136 assert_param(IS_DEFAULT_POLYNOMIAL(hcrc->Init.DefaultPolynomialUse));
137 if (hcrc->Init.DefaultPolynomialUse == DEFAULT_POLYNOMIAL_ENABLE)
139 /* initialize IP with default generating polynomial */
140 WRITE_REG(hcrc->Instance->POL, DEFAULT_CRC32_POLY);
141 MODIFY_REG(hcrc->Instance->CR, CRC_CR_POLYSIZE, CRC_POLYLENGTH_32B);
145 /* initialize CRC IP with generating polynomial defined by user */
146 if (HAL_CRCEx_Polynomial_Set(hcrc, hcrc->Init.GeneratingPolynomial, hcrc->Init.CRCLength) != HAL_OK)
152 /* check whether or not non-default CRC initial value has been
153 * picked up by user */
154 assert_param(IS_DEFAULT_INIT_VALUE(hcrc->Init.DefaultInitValueUse));
155 if (hcrc->Init.DefaultInitValueUse == DEFAULT_INIT_VALUE_ENABLE)
157 WRITE_REG(hcrc->Instance->INIT, DEFAULT_CRC_INITVALUE);
161 WRITE_REG(hcrc->Instance->INIT, hcrc->Init.InitValue);
165 /* set input data inversion mode */
166 assert_param(IS_CRC_INPUTDATA_INVERSION_MODE(hcrc->Init.InputDataInversionMode));
167 MODIFY_REG(hcrc->Instance->CR, CRC_CR_REV_IN, hcrc->Init.InputDataInversionMode);
169 /* set output data inversion mode */
170 assert_param(IS_CRC_OUTPUTDATA_INVERSION_MODE(hcrc->Init.OutputDataInversionMode));
171 MODIFY_REG(hcrc->Instance->CR, CRC_CR_REV_OUT, hcrc->Init.OutputDataInversionMode);
173 /* makes sure the input data format (bytes, halfwords or words stream)
174 * is properly specified by user */
175 assert_param(IS_CRC_INPUTDATA_FORMAT(hcrc->InputDataFormat));
177 /* Change CRC peripheral state */
178 hcrc->State = HAL_CRC_STATE_READY;
180 /* Return function status */
185 * @brief DeInitializes the CRC peripheral.
186 * @param hcrc: CRC handle
189 HAL_StatusTypeDef HAL_CRC_DeInit(CRC_HandleTypeDef *hcrc)
191 /* Check the CRC handle allocation */
197 /* Check the parameters */
198 assert_param(IS_CRC_ALL_INSTANCE(hcrc->Instance));
200 /* Check the CRC peripheral state */
201 if(hcrc->State == HAL_CRC_STATE_BUSY)
206 /* Change CRC peripheral state */
207 hcrc->State = HAL_CRC_STATE_BUSY;
209 /* DeInit the low level hardware */
210 HAL_CRC_MspDeInit(hcrc);
212 /* Change CRC peripheral state */
213 hcrc->State = HAL_CRC_STATE_RESET;
215 /* Process unlocked */
218 /* Return function status */
223 * @brief Initializes the CRC MSP.
224 * @param hcrc: CRC handle
227 __weak void HAL_CRC_MspInit(CRC_HandleTypeDef *hcrc)
229 /* NOTE : This function should not be modified, when the callback is needed,
230 the HAL_CRC_MspInit can be implemented in the user file
235 * @brief DeInitializes the CRC MSP.
236 * @param hcrc: CRC handle
239 __weak void HAL_CRC_MspDeInit(CRC_HandleTypeDef *hcrc)
241 /* NOTE : This function should not be modified, when the callback is needed,
242 the HAL_CRC_MspDeInit can be implemented in the user file
250 /** @defgroup CRC_Exported_Functions_Group2 Peripheral Control functions
251 * @brief management functions.
254 ===============================================================================
255 ##### Peripheral Control functions #####
256 ===============================================================================
257 [..] This section provides functions allowing to:
258 (+) Compute the 7, 8, 16 or 32-bit CRC value of an 8, 16 or 32-bit data buffer
259 using combination of the previous CRC value and the new one.
263 (+) Compute the 7, 8, 16 or 32-bit CRC value of an 8, 16 or 32-bit data buffer
264 independently of the previous CRC value.
271 * @brief Compute the 7, 8, 16 or 32-bit CRC value of an 8, 16 or 32-bit data buffer
272 * starting with the previously computed CRC as initialization value.
273 * @param hcrc: CRC handle
274 * @param pBuffer: pointer to the input data buffer, exact input data format is
275 * provided by hcrc->InputDataFormat.
276 * @param BufferLength: input data buffer length
277 * @retval uint32_t CRC (returned value LSBs for CRC shorter than 32 bits)
279 uint32_t HAL_CRC_Accumulate(CRC_HandleTypeDef *hcrc, uint32_t pBuffer[], uint32_t BufferLength)
281 uint32_t index = 0; /* CRC input data buffer index */
282 uint32_t temp = 0; /* CRC output (read from hcrc->Instance->DR register) */
287 /* Change CRC peripheral state */
288 hcrc->State = HAL_CRC_STATE_BUSY;
290 switch (hcrc->InputDataFormat)
292 case CRC_INPUTDATA_FORMAT_WORDS:
293 /* Enter Data to the CRC calculator */
294 for(index = 0; index < BufferLength; index++)
296 hcrc->Instance->DR = pBuffer[index];
298 temp = hcrc->Instance->DR;
301 case CRC_INPUTDATA_FORMAT_BYTES:
302 temp = CRC_Handle_8(hcrc, (uint8_t*)pBuffer, BufferLength);
305 case CRC_INPUTDATA_FORMAT_HALFWORDS:
306 temp = CRC_Handle_16(hcrc, (uint16_t*)pBuffer, BufferLength);
310 /* Change CRC peripheral state */
311 hcrc->State = HAL_CRC_STATE_READY;
313 /* Process unlocked */
316 /* Return the CRC computed value */
322 * @brief Compute the 7, 8, 16 or 32-bit CRC value of an 8, 16 or 32-bit data buffer
323 * starting with hcrc->Instance->INIT as initialization value.
324 * @param hcrc: CRC handle
325 * @param pBuffer: pointer to the input data buffer, exact input data format is
326 * provided by hcrc->InputDataFormat.
327 * @param BufferLength: input data buffer length
328 * @retval uint32_t CRC (returned value LSBs for CRC shorter than 32 bits)
330 uint32_t HAL_CRC_Calculate(CRC_HandleTypeDef *hcrc, uint32_t pBuffer[], uint32_t BufferLength)
332 uint32_t index = 0; /* CRC input data buffer index */
333 uint32_t temp = 0; /* CRC output (read from hcrc->Instance->DR register) */
338 /* Change CRC peripheral state */
339 hcrc->State = HAL_CRC_STATE_BUSY;
341 /* Reset CRC Calculation Unit (hcrc->Instance->INIT is
342 * written in hcrc->Instance->DR) */
343 __HAL_CRC_DR_RESET(hcrc);
345 switch (hcrc->InputDataFormat)
347 case CRC_INPUTDATA_FORMAT_WORDS:
348 /* Enter 32-bit input data to the CRC calculator */
349 for(index = 0; index < BufferLength; index++)
351 hcrc->Instance->DR = pBuffer[index];
353 temp = hcrc->Instance->DR;
356 case CRC_INPUTDATA_FORMAT_BYTES:
357 /* Specific 8-bit input data handling */
358 temp = CRC_Handle_8(hcrc, (uint8_t*)pBuffer, BufferLength);
361 case CRC_INPUTDATA_FORMAT_HALFWORDS:
362 /* Specific 16-bit input data handling */
363 temp = CRC_Handle_16(hcrc, (uint16_t*)pBuffer, BufferLength);
367 /* Change CRC peripheral state */
368 hcrc->State = HAL_CRC_STATE_READY;
370 /* Process unlocked */
373 /* Return the CRC computed value */
381 /** @defgroup CRC_Exported_Functions_Group3 Peripheral State functions
382 * @brief Peripheral State functions.
385 ===============================================================================
386 ##### Peripheral State functions #####
387 ===============================================================================
389 This subsection permits to get in run-time the status of the peripheral
397 * @brief Returns the CRC state.
398 * @param hcrc: CRC handle
401 HAL_CRC_StateTypeDef HAL_CRC_GetState(CRC_HandleTypeDef *hcrc)
415 /** @defgroup CRC_Private_Functions CRC Private Functions
419 * @brief Enter 8-bit input data to the CRC calculator.
420 * Specific data handling to optimize processing time.
421 * @param hcrc: CRC handle
422 * @param pBuffer: pointer to the input data buffer
423 * @param BufferLength: input data buffer length
424 * @retval uint32_t CRC (returned value LSBs for CRC shorter than 32 bits)
426 static uint32_t CRC_Handle_8(CRC_HandleTypeDef *hcrc, uint8_t pBuffer[], uint32_t BufferLength)
428 uint32_t i = 0; /* input data buffer index */
430 /* Processing time optimization: 4 bytes are entered in a row with a single word write,
431 * last bytes must be carefully fed to the CRC calculator to ensure a correct type
432 * handling by the IP */
433 for(i = 0; i < (BufferLength/4); i++)
435 hcrc->Instance->DR = (pBuffer[4*i]<<24) | (pBuffer[4*i+1]<<16) | (pBuffer[4*i+2]<<8) | pBuffer[4*i+3];
437 /* last bytes specific handling */
438 if ((BufferLength%4) != 0)
440 if (BufferLength%4 == 1)
442 *(uint8_t*) (&hcrc->Instance->DR) = pBuffer[4*i];
444 if (BufferLength%4 == 2)
446 *(uint16_t*) (&hcrc->Instance->DR) = (pBuffer[4*i]<<8) | pBuffer[4*i+1];
448 if (BufferLength%4 == 3)
450 *(uint16_t*) (&hcrc->Instance->DR) = (pBuffer[4*i]<<8) | pBuffer[4*i+1];
451 *(uint8_t*) (&hcrc->Instance->DR) = pBuffer[4*i+2];
455 /* Return the CRC computed value */
456 return hcrc->Instance->DR;
462 * @brief Enter 16-bit input data to the CRC calculator.
463 * Specific data handling to optimize processing time.
464 * @param hcrc: CRC handle
465 * @param pBuffer: pointer to the input data buffer
466 * @param BufferLength: input data buffer length
467 * @retval uint32_t CRC (returned value LSBs for CRC shorter than 32 bits)
469 static uint32_t CRC_Handle_16(CRC_HandleTypeDef *hcrc, uint16_t pBuffer[], uint32_t BufferLength)
471 uint32_t i = 0; /* input data buffer index */
473 /* Processing time optimization: 2 HalfWords are entered in a row with a single word write,
474 * in case of odd length, last HalfWord must be carefully fed to the CRC calculator to ensure
475 * a correct type handling by the IP */
476 for(i = 0; i < (BufferLength/2); i++)
478 hcrc->Instance->DR = (pBuffer[2*i]<<16) | pBuffer[2*i+1];
480 if ((BufferLength%2) != 0)
482 *(uint16_t*) (&hcrc->Instance->DR) = pBuffer[2*i];
485 /* Return the CRC computed value */
486 return hcrc->Instance->DR;
493 #endif /* HAL_CRC_MODULE_ENABLED */
502 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/