STM32 CRC Peripheral

From Stm32World Wiki
Jump to navigation Jump to search

Most, if not all, STM32 MCUs includes a built-in peripheral to calculate CRC values.

Tutorial Video

STM32CubeMX CRC Peripheral

In order to use the CRC peripheral, first step is to enable it in STM32CubeMX:

STM32CubeMX CRC Peripheral.png

There is no other configuration needed (or possible).

HAL Functions

The HAL library define two functions to interact with the CRC Peripheral:

/**
  * @brief  Compute the 32-bit CRC value of a 32-bit data buffer
  *         starting with hcrc->Instance->INIT as initialization value.
  * @param  hcrc CRC handle
  * @param  pBuffer pointer to the input data buffer.
  * @param  BufferLength input data buffer length (number of uint32_t words).
  * @retval uint32_t CRC (returned value LSBs for CRC shorter than 32 bits)
  */
uint32_t HAL_CRC_Calculate(CRC_HandleTypeDef *hcrc, uint32_t pBuffer[], uint32_t BufferLength)

and

/**
  * @brief  Compute the 32-bit CRC value of a 32-bit data buffer
  *         starting with the previously computed CRC as initialization value.
  * @param  hcrc CRC handle
  * @param  pBuffer pointer to the input data buffer.
  * @param  BufferLength input data buffer length (number of uint32_t words).
  * @retval uint32_t CRC (returned value LSBs for CRC shorter than 32 bits)
  */
uint32_t HAL_CRC_Accumulate(CRC_HandleTypeDef *hcrc, uint32_t pBuffer[], uint32_t BufferLength)

The HAL_CRC_Calculate will initiate a CRC calculation. More data can be added by using HAL_CRC_Accumulate.

Resetting (restarting) a new CRC Calculation

The CRC Peripheral can be reset by calling the HAL_CRC_Calculate with no data, like this:

HAL_CRC_Accumulate(&hcrc, (uint32_t*) NULL, 0);

Alternatively there is a macro defined for this exact purpose:

__HAL_CRC_DR_RESET(&hcrc);

Miscellaneous