2 ******************************************************************************
3 * @file stm32f0xx_hal_crc.c
4 * @author MCD Application Team
6 * @date 11-December-2014
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
15 ===============================================================================
16 ##### How to use this driver #####
17 ===============================================================================
19 (#) Enable CRC AHB clock using __CRC_CLK_ENABLE();
20 (#) Initialize CRC calculator
21 (++)specify generating polynomial (IP default or non-default one)
22 (++)specify initialization value (IP default or non-default one)
23 (++)specify input data format
24 (++)specify input or output data inversion mode if any
25 (#) Use HAL_CRC_Accumulate() function to compute the CRC value of the
26 input data buffer starting with the previously computed CRC as
28 (#) Use HAL_CRC_Calculate() function to compute the CRC value of the
29 input data buffer starting with the defined initialization value
30 (default or non-default) to initiate CRC calculation
33 ******************************************************************************
36 * <h2><center>© COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
38 * Redistribution and use in source and binary forms, with or without modification,
39 * are permitted provided that the following conditions are met:
40 * 1. Redistributions of source code must retain the above copyright notice,
41 * this list of conditions and the following disclaimer.
42 * 2. Redistributions in binary form must reproduce the above copyright notice,
43 * this list of conditions and the following disclaimer in the documentation
44 * and/or other materials provided with the distribution.
45 * 3. Neither the name of STMicroelectronics nor the names of its contributors
46 * may be used to endorse or promote products derived from this software
47 * without specific prior written permission.
49 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
50 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
52 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
55 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
56 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
57 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
58 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
60 ******************************************************************************
63 /* Includes ------------------------------------------------------------------*/
64 #include "stm32f0xx_hal.h"
66 /** @addtogroup STM32F0xx_HAL_Driver
70 /** @defgroup CRC CRC HAL module driver
71 * @brief CRC HAL module driver.
75 #ifdef HAL_CRC_MODULE_ENABLED
77 /* Private typedef -----------------------------------------------------------*/
78 /* Private define ------------------------------------------------------------*/
79 /* Private macro -------------------------------------------------------------*/
80 /* Private variables ---------------------------------------------------------*/
81 /* Private function prototypes -----------------------------------------------*/
82 /** @defgroup CRC_Private_Functions CRC Private Functions
85 static uint32_t CRC_Handle_8(CRC_HandleTypeDef
*hcrc
, uint8_t pBuffer
[], uint32_t BufferLength
);
86 static uint32_t CRC_Handle_16(CRC_HandleTypeDef
*hcrc
, uint16_t pBuffer
[], uint32_t BufferLength
);
91 /* Exported functions ---------------------------------------------------------*/
92 /** @defgroup CRC_Exported_Functions CRC Exported Functions
96 /** @defgroup CRC_Exported_Functions_Group1 Initialization/de-initialization functions
97 * @brief Initialization and Configuration functions.
100 ===============================================================================
101 ##### Initialization and Configuration functions #####
102 ===============================================================================
103 [..] This section provides functions allowing to:
104 (+) Initialize the CRC according to the specified parameters
105 in the CRC_InitTypeDef and create the associated handle
106 (+) DeInitialize the CRC peripheral
107 (+) Initialize the CRC MSP
108 (+) DeInitialize CRC MSP
115 * @brief Initializes the CRC according to the specified
116 * parameters in the CRC_InitTypeDef and creates the associated handle.
117 * @param hcrc: CRC handle
120 HAL_StatusTypeDef
HAL_CRC_Init(CRC_HandleTypeDef
*hcrc
)
122 /* Check the CRC handle allocation */
128 /* Check the parameters */
129 assert_param(IS_CRC_ALL_INSTANCE(hcrc
->Instance
));
131 if(hcrc
->State
== HAL_CRC_STATE_RESET
)
133 /* Init the low level hardware */
134 HAL_CRC_MspInit(hcrc
);
137 hcrc
->State
= HAL_CRC_STATE_BUSY
;
139 /* Extended initialization: if programmable polynomial feature is
140 applicable to device, set default or non-default generating
141 polynomial according to hcrc->Init parameters.
142 If feature is non-applicable to device in use, HAL_CRCEx_Init straight
143 away reports HAL_OK. */
144 if (HAL_CRCEx_Init(hcrc
) != HAL_OK
)
149 /* check whether or not non-default CRC initial value has been
150 * picked up by user */
151 assert_param(IS_DEFAULT_INIT_VALUE(hcrc
->Init
.DefaultInitValueUse
));
152 if (hcrc
->Init
.DefaultInitValueUse
== DEFAULT_INIT_VALUE_ENABLE
)
154 WRITE_REG(hcrc
->Instance
->INIT
, DEFAULT_CRC_INITVALUE
);
158 WRITE_REG(hcrc
->Instance
->INIT
, hcrc
->Init
.InitValue
);
162 /* set input data inversion mode */
163 assert_param(IS_CRC_INPUTDATA_INVERSION_MODE(hcrc
->Init
.InputDataInversionMode
));
164 MODIFY_REG(hcrc
->Instance
->CR
, CRC_CR_REV_IN
, hcrc
->Init
.InputDataInversionMode
);
166 /* set output data inversion mode */
167 assert_param(IS_CRC_OUTPUTDATA_INVERSION_MODE(hcrc
->Init
.OutputDataInversionMode
));
168 MODIFY_REG(hcrc
->Instance
->CR
, CRC_CR_REV_OUT
, hcrc
->Init
.OutputDataInversionMode
);
170 /* makes sure the input data format (bytes, halfwords or words stream)
171 * is properly specified by user */
172 assert_param(IS_CRC_INPUTDATA_FORMAT(hcrc
->InputDataFormat
));
174 /* Change CRC peripheral state */
175 hcrc
->State
= HAL_CRC_STATE_READY
;
177 /* Return function status */
182 * @brief DeInitializes the CRC peripheral.
183 * @param hcrc: CRC handle
186 HAL_StatusTypeDef
HAL_CRC_DeInit(CRC_HandleTypeDef
*hcrc
)
188 /* Check the CRC handle allocation */
194 /* Check the parameters */
195 assert_param(IS_CRC_ALL_INSTANCE(hcrc
->Instance
));
197 /* Check the CRC peripheral state */
198 if(hcrc
->State
== HAL_CRC_STATE_BUSY
)
203 /* Change CRC peripheral state */
204 hcrc
->State
= HAL_CRC_STATE_BUSY
;
206 /* DeInit the low level hardware */
207 HAL_CRC_MspDeInit(hcrc
);
209 /* Change CRC peripheral state */
210 hcrc
->State
= HAL_CRC_STATE_RESET
;
212 /* Process unlocked */
215 /* Return function status */
220 * @brief Initializes the CRC MSP.
221 * @param hcrc: CRC handle
224 __weak
void HAL_CRC_MspInit(CRC_HandleTypeDef
*hcrc
)
226 /* NOTE : This function should not be modified, when the callback is needed,
227 the HAL_CRC_MspInit can be implemented in the user file
232 * @brief DeInitializes the CRC MSP.
233 * @param hcrc: CRC handle
236 __weak
void HAL_CRC_MspDeInit(CRC_HandleTypeDef
*hcrc
)
238 /* NOTE : This function should not be modified, when the callback is needed,
239 the HAL_CRC_MspDeInit can be implemented in the user file
247 /** @defgroup CRC_Exported_Functions_Group2 Peripheral Control functions
248 * @brief management functions.
251 ===============================================================================
252 ##### Peripheral Control functions #####
253 ===============================================================================
254 [..] This section provides functions allowing to:
255 (+) Compute the 7, 8, 16 or 32-bit CRC value of an 8, 16 or 32-bit data buffer
256 using combination of the previous CRC value and the new one.
260 (+) Compute the 7, 8, 16 or 32-bit CRC value of an 8, 16 or 32-bit data buffer
261 independently of the previous CRC value.
268 * @brief Compute the 7, 8, 16 or 32-bit CRC value of an 8, 16 or 32-bit data buffer
269 * starting with the previously computed CRC as initialization value.
270 * @param hcrc: CRC handle
271 * @param pBuffer: pointer to the input data buffer, exact input data format is
272 * provided by hcrc->InputDataFormat.
273 * @param BufferLength: input data buffer length
274 * @retval uint32_t CRC (returned value LSBs for CRC shorter than 32 bits)
276 uint32_t HAL_CRC_Accumulate(CRC_HandleTypeDef
*hcrc
, uint32_t pBuffer
[], uint32_t BufferLength
)
278 uint32_t index
= 0; /* CRC input data buffer index */
279 uint32_t temp
= 0; /* CRC output (read from hcrc->Instance->DR register) */
284 /* Change CRC peripheral state */
285 hcrc
->State
= HAL_CRC_STATE_BUSY
;
287 switch (hcrc
->InputDataFormat
)
289 case CRC_INPUTDATA_FORMAT_WORDS
:
290 /* Enter Data to the CRC calculator */
291 for(index
= 0; index
< BufferLength
; index
++)
293 hcrc
->Instance
->DR
= pBuffer
[index
];
295 temp
= hcrc
->Instance
->DR
;
298 case CRC_INPUTDATA_FORMAT_BYTES
:
299 temp
= CRC_Handle_8(hcrc
, (uint8_t*)pBuffer
, BufferLength
);
302 case CRC_INPUTDATA_FORMAT_HALFWORDS
:
303 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
);
370 /* Change CRC peripheral state */
371 hcrc
->State
= HAL_CRC_STATE_READY
;
373 /* Process unlocked */
376 /* Return the CRC computed value */
383 /** @defgroup CRC_Exported_Functions_Group3 Peripheral State functions
384 * @brief Peripheral State functions.
387 ===============================================================================
388 ##### Peripheral State functions #####
389 ===============================================================================
391 This subsection permits to get in run-time the status of the peripheral
399 * @brief Returns the CRC state.
400 * @param hcrc: CRC handle
403 HAL_CRC_StateTypeDef
HAL_CRC_GetState(CRC_HandleTypeDef
*hcrc
)
416 /** @addtogroup CRC_Private_Functions CRC Private Functions
420 * @brief Enter 8-bit input data to the CRC calculator.
421 * Specific data handling to optimize processing time.
422 * @param hcrc: CRC handle
423 * @param pBuffer: pointer to the input data buffer
424 * @param BufferLength: input data buffer length
425 * @retval uint32_t CRC (returned value LSBs for CRC shorter than 32 bits)
427 static uint32_t CRC_Handle_8(CRC_HandleTypeDef
*hcrc
, uint8_t pBuffer
[], uint32_t BufferLength
)
429 uint32_t i
= 0; /* input data buffer index */
431 /* Processing time optimization: 4 bytes are entered in a row with a single word write,
432 * last bytes must be carefully fed to the CRC calculator to ensure a correct type
433 * handling by the IP */
434 for(i
= 0; i
< (BufferLength
/4); i
++)
436 hcrc
->Instance
->DR
= ((uint32_t)pBuffer
[4*i
]<<24) | ((uint32_t)pBuffer
[4*i
+1]<<16) | ((uint32_t)pBuffer
[4*i
+2]<<8) | (uint32_t)pBuffer
[4*i
+3];
438 /* last bytes specific handling */
439 if ((BufferLength
%4) != 0)
441 if (BufferLength
%4 == 1)
443 *(uint8_t*) (&hcrc
->Instance
->DR
) = pBuffer
[4*i
];
445 if (BufferLength
%4 == 2)
447 *(uint16_t*) (&hcrc
->Instance
->DR
) = ((uint16_t)pBuffer
[4*i
]<<8) | (uint16_t)pBuffer
[4*i
+1];
449 if (BufferLength
%4 == 3)
451 *(uint16_t*) (&hcrc
->Instance
->DR
) = ((uint16_t)pBuffer
[4*i
]<<8) | (uint16_t)pBuffer
[4*i
+1];
452 *(uint8_t*) (&hcrc
->Instance
->DR
) = pBuffer
[4*i
+2];
456 /* Return the CRC computed value */
457 return hcrc
->Instance
->DR
;
463 * @brief Enter 16-bit input data to the CRC calculator.
464 * Specific data handling to optimize processing time.
465 * @param hcrc: CRC handle
466 * @param pBuffer: pointer to the input data buffer
467 * @param BufferLength: input data buffer length
468 * @retval uint32_t CRC (returned value LSBs for CRC shorter than 32 bits)
470 static uint32_t CRC_Handle_16(CRC_HandleTypeDef
*hcrc
, uint16_t pBuffer
[], uint32_t BufferLength
)
472 uint32_t i
= 0; /* input data buffer index */
474 /* Processing time optimization: 2 HalfWords are entered in a row with a single word write,
475 * in case of odd length, last HalfWord must be carefully fed to the CRC calculator to ensure
476 * a correct type handling by the IP */
477 for(i
= 0; i
< (BufferLength
/2); i
++)
479 hcrc
->Instance
->DR
= (pBuffer
[2*i
]<<16) | pBuffer
[2*i
+1];
481 if ((BufferLength
%2) != 0)
483 *(uint16_t*) (&hcrc
->Instance
->DR
) = pBuffer
[2*i
];
486 /* Return the CRC computed value */
487 return hcrc
->Instance
->DR
;
497 #endif /* HAL_CRC_MODULE_ENABLED */
506 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/