21.4.1.2 CRC Calculation
The module can optionally calculate the CRC using the recommended method shown in Recommended J2716 CRC Implementation. The CRC is calculated when the CRCEN bit (SENTxCON1<8>) is set and the CRC[3:0] (SENTxDAT[3:0]) register bits become read-only. The hardware computed CRC value is indicated in the CRC[3:0] register bits when the calculation is finished.
When CRCEN = 0
, no CRC is computed in hardware and the CRC[3:0] bits
become writable by software. The application must compute a CRC value and write it to
CRC[3:0].
Recommended J2716 CRC Implementation
#define NUM_NIBBLES 6
// Array holding received nibbles
rec_data[NUM_NIBBLES];
// CRC lookup table
crc_table = {0,13,7,10,14,3,9,4,1,12,6,11,15,2,8,5};
// Initialize checksum to seed value
Checksum = 5;
// For each data nibble, bit-wise XOR with lookup value from table
for(i=0;i<NUM_NIBBLES;i++)
{
Checksum = rec_data[i] ^ crc_table[Checksum];
}
// Bit-wise XOR with additional 0 value
Checksum = 0 ^ crc_table[Checksum];