semf
Crc

General

The semf::app::Crc interface class is used for computing a of cyclic redundancy check (CRC) of the input data. The crc can be computed in one step using the calculate() function or it can be calculated in many steps where every time a certain chunk of data is fed to the function accumulate(). For computing the crc of new data one has to reset the semf::app::Crc object using the reset() function.

semf::CrcSoftware class inherits from semf::app::Crc interface and computes the crc of the input data using the main processing unit. If the hardware has a specific controller to compute the crc value a class that inherits from semf::app::Crc should be implemented and used instead.

In the following example we use the 32 bit software crc from the semf library.

Initialization

// buffer for storing some data, has to be filled somewhere in the code
uint8_t buff1[4*100];
// another buffer for storing some data, has to be filled somewhere in the code
uint8_t buff2[4*100];
// create a 'CrcSoftware' object with a specific
// datatype, polynomial, initial value, and final xor.
// or
// create a default/predefined 'CrcSoftware' object for uint32_t datatype
semf::Crc32Software crcSoftware2;
Template class for CRC generation in software.
Definition: crcsoftware.h:30

Usage

// compute the crc of the buff1 in one step
const uint32_t* crcOut1 = nullptr;
crcSoftware1.reset();
crcOut1 = reinterpret_cast<uint32_t*>(crcSoftware1.calculate(buff1, sizeof(buff1)));
// and check if the crc is as expected
const uint32_t expectedCrc = 0x12345678;
if (crcSoftware1.isEqual(&reinterpret_cast<uint8_t*>(&expectedCrc))
{
// Do something ...
}
// compute the crc of the buff2 in multiple steps
const uint32_t *crcOut2 = nullptr;
crcSoftware2.reset();
for (uint8_t i = 0; i < 100; i++)
{
// intermediate result of crc
crcOut2 = reinterpret_cast<uint32_t*>(crcSoftware2.accumlate(buff2[i*4], 4));
}
void reset()
Resets internal values to their initial values.
Definition: crcsoftware.h:75
const uint8_t * calculate(const uint8_t data[], size_t dataSize) override
Calculates the CRC of the input data.
Definition: crcsoftware.h:101
bool isEqual(const uint8_t data[]) override
Checks if previously calculated CRC is equal to the input value.
Definition: crcsoftware.h:110