
TLE5012B
Interfaces
User’s Manual
53
Rev. 1.2, 2018-02
CRC generation software code example
//“message” is the data transfer for which a CRC has to be calculated.
//A typical “message” consists of the status nibble, three data nibbles and the CRC
//nibble (the trigger nibble and the synchronisation nibble are not part of the CRC).
//“Length” is the number of nibbles in the “message”. A typical “message” has 5
//nibbles (the trigger nible and the synchronization nibble are not part of the CRC).
unsigned char CRC(unsigned char *message, unsigned char Length)
{
//“crc” defined as the 4-bits that will be generated through the message till the
//final “crc” is generated. In the example above this are the blue lines out of the
//XOR operation.
unsigned char crc;
//“Numnibbles” is a counter to compare the bits used for the CRC calculation and
//“Length”.
unsigned char Numnibbles, bitdata;
//Initially the CRC remainder has to be set with the original seed (0x05 for the
//TLE5012B).
crc = 0x05;
//For all the nibbles of the message.
for(Numnibbles=0; Numnibbles<Length; Num+)
{
//“crc” is calculated as the XOR operation from the previous “crc” and the “message”.
//“^” is the XOR operator.
crc ^= message[Numnibbles];
//For each bit position in a 4-bit nibble
for(bitdata=0; bitdata<4; +)
{
//If the MSB of the “crc” is 1 (with the &0x80 mask we get the MSB of the crc).
if((crc&0x08)!=0)
{
//“crc” advances on position (“crc” is moved left 1 bit: the MSB is deleted since it
//will be cancelled out with the first one of the generator polynomial and a new bit
//from the “message” is taken as LSB.)
crc <<=1;
//“crc” is calculated as the XOR operation from the previous “crc” and the generator
//polynomial (0x0D for TLE5012B). Be aware that here the x4 bit is not taken since
//the MSB of the “crc” already has been deleted in the previous step.
crc ^= 0x0D;
}
//In case the “crc” MSB is 0
else
//“crc” advances one position (this step is to ensure that the XOR operation is only
//done when the generator polynomial is aligned with a MSB of the message that is “1”.
crc <<= 1;
}
}