Arduino Core for STM32  1.0
HarwareCRCExample
  1. Basic example of CRC calculation using append
    #include "HardwareCRC.h"
    uint8_t testArray[] = {1,2,3,4,5};
    //This method have to be called in main function in main.c file.
    void app(void){
    crc.begin(); //Enable unit
    crc.reset(0x00); //Reset calculation and set init value to 0
    crc.setPolynomial8(CRC8_POLY); //Set default CRC-8 polynomial (X^8 + X^5 + X^4 + 1)
    // or use crc.setPolynomial8(CRC_X5 | CRC_X4 | CRC_X0); //(X^8 + X^5 + X^4 + 1)
    crc.setInputReverse(false); //Disable input reflection
    crc.setOutputReverse(false); //Disable output reflection
    //Bytes can be appended by one
    for(int i = 0; i < 4; i++){
    crc.append(testArray[i]);
    }
    uint32_t CRC_result = crc.append(testArray[4], 0xFF); //Append last byte and set finalXOR to 0xFF
    //Or whole array can be appended at once:
    /*
    uint32_t CRC_result = crc.append(testArray, 5, 0xFF);
    */
    //Infinite loop
    while(1){
    // Nothing here.
    }
    }
    *
  2. Basic example of CRC calculation using compute
    #include "HardwareCRC.h"
    uint8_t testArray[] = {1,2,3,4,5};
    //This method have to be called in main function in main.c file.
    void app(void){
    crc.begin(); //Enable unit
    //crc.reset(0x00); - don't need to be called when using compute
    crc.setPolynomial8(CRC8_POLY); //Set default CRC-8 polynomial (X^8 + X^5 + X^4 + 1)
    // or use crc.setPolynomial8(CRC_X5 | CRC_X4 | CRC_X0); //(X^8 + X^5 + X^4 + 1)
    crc.setInputReverse(false); //Disable input reflection
    crc.setOutputReverse(false); //Disable output reflection
    uint32_t CRC_result = crc.compute(testArray, 5, 0x00, 0xFF); //Compute CRC, set init value to 0x00 and finalXOR to 0xFF
    //Infinite loop
    while(1){
    // Nothing here.
    }
    }
  3. Basic example of CRC calculation using computeAndWriteToEnd
    #include "HardwareCRC.h"
    uint8_t testArray[6] = {1,2,3,4,5}; //We need to reserve last byte for CRC.
    //NOTE: When CRC-16 is used, 2 bytes has to be reserved and when CRC-32 is used, 4 bytes has to be reserved
    //This method have to be called in main function in main.c file.
    void app(void){
    crc.begin(); //Enable unit
    //crc.reset(0x00); - don't need to be called when using compute
    crc.setPolynomial8(CRC8_POLY); //Set default CRC-8 polynomial (X^8 + X^5 + X^4 + 1)
    // or use crc.setPolynomial8(CRC_X5 | CRC_X4 | CRC_X0); //(X^8 + X^5 + X^4 + 1)
    crc.setInputReverse(false); //Disable input reflection
    crc.setOutputReverse(false); //Disable output reflection
    uint32_t CRC_result = crc.computeAndWriteToEnd(testArray, 5, 0x00, 0xFF); //Compute CRC, set init value to 0x00 and finalXOR to 0xFF
    //CRC result is now attached at the end of the array
    uint32_t CRC_result2 = testArray[5];
    //CRC_result == CRC_result2
    //Infinite loop
    while(1){
    // Nothing here.
    }
    }
  4. Basic example of CRC check
    #include "HardwareCRC.h"
    uint8_t testArray[6] = {1,2,3,4,5}; //We need to reserve last byte for CRC.
    //NOTE: When CRC-16 is used, 2 bytes has to be reserved and when CRC-32 is used, 4 bytes has to be reserved
    //This method have to be called in main function in main.c file.
    void app(void){
    crc.begin(); //Enable unit
    //crc.reset(0x00); - don't need to be called when using compute
    crc.setPolynomial8(CRC8_POLY); //Set default CRC-8 polynomial (X^8 + X^5 + X^4 + 1)
    // or use crc.setPolynomial8(CRC_X5 | CRC_X4 | CRC_X0); //(X^8 + X^5 + X^4 + 1)
    crc.setInputReverse(false); //Disable input reflection
    crc.setOutputReverse(false); //Disable output reflection
    crc.computeAndWriteToEnd(testArray, 5, 0x00, 0xFF); //Compute CRC and append it at the end, set init value to 0x00 and finalXOR to 0xFF
    //Now we will check, if testArray is valid
    bool valid = crc.check(testArray, 5, 0x00, 0xFF);
    //Now we will check, if testArray is valid also after small change
    testArray[2] = 12;
    bool stillValid = crc.check(testArray, 5, 0x00, 0xFF);
    //Infinite loop
    while(1){
    // Nothing here.
    }
    }
  5. Example of CRC-32 calculation
    #include "HardwareCRC.h"
    uint8_t testArray[9] = {1,2,3,4,5}; //We need to reserve last 4 bytes for CRC.
    //This method have to be called in main function in main.c file.
    void app(void){
    crc.begin(); //Enable unit
    //crc.reset(0x00); - don't need to be called when using compute
    crc.setPolynomial32(); //Set default CRC-32 polynomial
    crc.setInputReverse(true); //Enable input reflection
    crc.setOutputReverse(true); //Enable output reflection
    crc.computeAndWriteToEnd(testArray, 5, 0xFFFFFFFFUL, 0xFFFFFFFFUL); //Compute CRC and append it at the end, set init value to 0xFFFFFFFFUL and finalXOR to 0xFFFFFFFFUL
    //Now we will check, if testArray is valid
    bool valid = crc.check(testArray, 5, 0xFFFFFFFFUL, 0xFFFFFFFFUL);
    //Now we will check, if testArray is valid also after small change
    testArray[2] = 12;
    bool stillValid = crc.check(testArray, 5, 0xFFFFFFFFUL, 0xFFFFFFFFUL);
    //Infinite loop
    while(1){
    // Nothing here.
    }
    }
Note
Those examples works only with c++, but STM32CubeIDE does not allow you to change main.c to main.cpp, so you have to create own c++ file, and include it in main.h. In your cpp file create function, that will be called in main() function. In this case, function name is app() and is called in main() function between tags USER CODE BEGIN 2 and USER CODE END 2.
See also
Hardware CRC documentation
HardwareCRC
HardwareCRC::setOutputReverse
bool setOutputReverse(bool reverse)
Sets, if output CRC data bits will be reversed(reflected) or not.
Definition: HardwareCRC.cpp:491
HardwareCRC::reset
void reset(uint32_t initValue=DEFAULT_HCRC_INITVAL)
Resets calculation and sets initial value.
Definition: HardwareCRC.cpp:92
HardwareCRC::compute
uint32_t compute(const uint8_t *data, uint32_t dataLength, uint32_t initValue=DEFAULT_HCRC_INITVAL, uint32_t finalXOR=0x00)
Resets and computes CRC value of the whole array.
Definition: HardwareCRC.cpp:255
HardwareCRC::setPolynomial32
bool setPolynomial32(uint32_t polynomial=CRC32_POLY)
Sets CRC-32 polynomial.
Definition: HardwareCRC.cpp:63
CRC8_POLY
#define CRC8_POLY
Definition: HardwareCRC.h:119
HardwareCRC::append
uint32_t append(const uint8_t data, uint32_t finalXOR=0x00)
Appends another data to previous CRC calculation and calculates new CRC value.
Definition: HardwareCRC.cpp:100
HardwareCRC::setPolynomial8
bool setPolynomial8(uint32_t polynomial=CRC8_POLY)
Sets CRC-8 polynomial.
Definition: HardwareCRC.cpp:37
HardwareCRC::setInputReverse
bool setInputReverse(bool reverse)
Sets, if input data bits will be reversed(reflected) or not.
Definition: HardwareCRC.cpp:481
HardwareCRC::computeAndWriteToEnd
uint32_t computeAndWriteToEnd(uint8_t *data, uint32_t dataLength, uint32_t initValue=DEFAULT_HCRC_INITVAL, uint32_t finalXOR=0x00)
Resets, computes CRC value of the whole array and attaches calculated CRC value at the end of the arr...
Definition: HardwareCRC.cpp:270
HardwareCRC.h
This file contains class, that provides hardware CRC calculation.
crc
HardwareCRC crc
Definition: HardwareCRC.cpp:539
HardwareCRC::check
bool check(const uint8_t *data, uint32_t dataLength, uint32_t initValue=DEFAULT_HCRC_INITVAL, uint32_t finalXOR=0x00)
Checks if the array (with CRC attached at the end) is valid.
Definition: HardwareCRC.cpp:354
HardwareCRC::begin
bool begin()
Enables CRC hardware calculation unit.
Definition: HardwareCRC.cpp:11