How is CRC value calculated, how to determine CRC algorithm in embedded applications?
1. Cyclic Redundancy Check
Cyclic Redundancy Check (CRC) is an error detection method for digital data based on binary division.
CRC algorithm generates a fixed checksum code length.
2. How is CRC value calculated, how to determine CRC algorithm in embedded applications?
CRC algorithm embedded within the STM32 peripherals computes shift and XOR operations of a subsequent input data in functions of a Generator polynomial (POLY) and a programmable Initial CRC value.
Algorithm Input parameters:
- Input data also called “Dividend”. This is the data being transmitted or content of Flash for example.
- Generator polynomial or “Divisor”:
Algebraic polynomial represented as a bit pattern: the power of each term gives the position on the bit and the coefficient gives the value of the bit.
The order of the generator polynomial must not exceed the CRC length.
For a 32-bits CRC calculation, polynomial highest exponent must be 32.
By default, the standard generator polynomial used by the STM32 CRC peripheral is the Ethernet CRC-32 polynomial 0x04C11DB7. The mathematical representation according to this polynomial is x32 + x26 + x23 + x22 + x16 + x12 + x11 + x10 + x8 + x7 + x5 + x4 + x2 + x + 1. This presentation is obtained from the binary format of coefficients, where the 32-bit term is always present while the other terms are present only if the coefficient is equal to 1.
- Initial CRC value
Initial CRC value gives more security to CRC. It is fixed to 0xFFFFFFFF or programmable by user.
At start up, algorithm sets CRC to the initial CRC value XOR with the dividend.
Once CRC MSB is equal to one, algorithm shifts CRC one bit to the left and XORs it with the generator polynomial.
Otherwise, it only shifts CRC one bit to the left.
Figure below describes the algorithm used in STM32 MCUs.
You can refer to AN4187 CRC peripheral overview and its firmware to understand the computation process:
(https://www.st.com/content/st_com/en/products/embedded-software/mcu-mpu-embedded-software/stm32-embedded-software/stm32-standard-peripheral-library-expansion/stsw-stm32an4187.html#overview).
The equivalent embedded software implementation for the computation example of CRC is given in the function CrcSoftwareFunc below
We can also use the STM32Cube HAL library to compute CRC-32 using the STM32 CRC hardware using the following APIs
• HAL_CRC_Accumulate(): compute the 7, 8, 16 or 32-bit CRC value of an 8, 16 or 32-bit data buffer using combination of the previous CRC value and the new one.
• HAL_CRC_Calculate() compute the 7, 8, 16 or 32-bit CRC value of an 8, 16 or 32-bit data buffer independently of the previous CRC value.
To determine if your CRC is correct, you can calculate it with 3 different methods and compare the results.
There are different ways to compute CRC:
- With your own algorithm implementation like CrcSoftwareFunc function (FW embedded in user application code)
- With STM32Cube HAL library (STM32 CRC peripheral at user application level)
- With an online calculator to check flash data integrity during the link step[YL1]