Arduino Core for STM32  1.0

Hardware CRC

Class documentation: HardwareCRC

Main features:

  • Supports hardware CRC calculation
  • Fully programmable polynomial with programmable size (7, 8, 16, 32 bits).
  • Handles 8-,16-, 32-bit data size.
  • Programmable CRC initial value.
  • Programmable final XOR value.
  • Hardware CRC computation is done in 4 AHB clock cycles (HCLK) for the 32-bit data size. 2 AHB clock cycles (HCLK) for the 16-bit data size. 1 AHB clock cycle (HCLK) for the 8-bit data size.
  • Supports input/output reflection.
  • Can be used without HAL CRC library. CRC don't need to be activated in .ioc file.

How to use:

  1. Check if definitions for your MCU are included in this file.
  2. Call begin() method in your code to initialize and enable CRC unit.
  3. Calculate CRC.
  4. Call end() method to save energy by turning off CRC unit.

What not to do:

  • It is not recommended to use CRC calulcation in interrupts, because it can change settings and values during another CRC calculation in main program.

Supported MCUs

Function F0xx F07x, F09x, L0xx, L4xx F1xx, F2xx, F4xx, L1xx
Polynome CRC-32 (0x4C11DB7) CUSTOM CRC-32 (0x4C11DB7)
In/Out data reversion (reflection) YES YES Forcibly reversed
Initial value CUSTOM CUSTOM 0xFFFFFFFF
Note
This code is only c++ compatible. If you want to use it, convert your project to c++.

How to add support for your MCU:

  1. Check if your MCU is not supported yet.
  2. Add elif macro to include part (#elif defined(YOUR_MCU) ...). It will mean, that you have revisited this file for your MCU.
  3. Set correct definitions values for your MCU from reference manual.
  4. Check if some functions or variables does not require specific code for your MCU.

Troubleshooting:

1. CRC is incorrect.

  • Check if you have called begin() method.
  • Check if you set correct polynomial, also check if custom polynomials are supported.
  • Check input / output reversion (reflection) settings, also check if they are supported.

Check if you called reset() method before new calculation. (Note: compute(), computeAndWriteToEnd(), check() methods call reset automatically)

  • Check if you set correct init value when calling reset() method.
  • Check if you set correct finalXOR value. To disable finalXOR use 0x00 value, to flip bit states use 0x7FUL(CRC-7), 0xFFUL(CRC-8), 0xFFFFUL(CRC-16), 0xFFFFFFFFUL(CRC-32). (Note: When using append() metod, finalXOR value has to be set only at the last appended byte/half-word/word).
  • Check if your input data has correct size. For byte (8-bits) size use uint8_t/int8_t, for half-word (2 bytes) size use uint16_t/int16_t, for word (4 bytes) size use uint32_t/int32_t. This is very important especially if input reverse (reflection) is enabled.
    • Check if CRC was not calculated in interrupt.
    • Check data endianess.

Credits

Author
Matej Fitoš
Date
Sept 27, 2021
See also
HardwareCRC
Default CRC polynomials
CRC polynomial terms

Examples:

HarwareCRCExample