EasyManua.ls Logo

Infineon XENSIV TLI4971 - Code example CRC calculation

Infineon XENSIV TLI4971
31 pages
To Next Page IconTo Next Page
To Next Page IconTo Next Page
To Previous Page IconTo Previous Page
To Previous Page IconTo Previous Page
Loading...
Application Note 23 of 31 Rev. 1.30
2022-03-01
Current Sensor TLI4971
Programming Guide and User Manual
4.5 Code example CRC calculation
//CRC calculation example for Infineon TLI4971 current sensor
//CRC 8 (SAE - J1850) CRC polynomial: x^8 + x^4 + x^3 + x^2 + 1
// len = 18 (EEPROM line 0-17)
#define CRC_POLYNOMIAL 0x1D
#define CRC_SEED 0xAA
//check CRC
bool checkCRC (uint16_t* data, int len) {
uint8_t checkSum = data[2]&0xFF;//CRC lower byte in EEPROM line2
return checkSum == crcCalc(data, len);
}
//read data beginning in EEPROM line 3 to line 17, append line 0 to line 2
uint8_t crcCalc(uint16_t* data, int len) {
uint8_t crcData8[len*2];
for(int i = 0; i < len; i++) {
crcData8[i*2] = ( data[(i+3)%18] >>8 ) & 0xFF;//read upper 8 bit
crcData8[i*2+1] = ( data[(i+3)%18] ) & 0xFF;//read lower 8 bit
}
return crc8(crcData8, len*2-1); //do not include last byte (line 2 lower byte)
}
// CRC calculation
uint8_t crc8(uint8_t *data, uint8_t length) {
uint32_t crc;
int16_t i, bit;
crc = CRC_SEED;
for (i = 0; i < length; i++) {
crc ^= data[i];
for (bit = 0; bit < 8; bit++) {
if ((crc & 0x80) != 0) {
crc <<= 1;
crc ^= CRC_POLYNOMIAL;
} else {
crc <<= 1;
}
}
}
return ~crc; // ~crc = crc^0xFF;
}

Related product manuals