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:
- Check if definitions for your MCU are included in this file.
- Call begin() method in your code to initialize and enable CRC unit.
- Calculate CRC.
- 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:
- Check if your MCU is not supported yet.
- Add elif macro to include part (#elif defined(YOUR_MCU) ...). It will mean, that you have revisited this file for your MCU.
- Set correct definitions values for your MCU from reference manual.
- 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