Post Go back to editing

# CRC implementation for the LTC3300 IC

Category: Software
Product Number: LTC3300-1

Dear Experts,

I apologize for this simple question. We have been attempting, without luck, to implement a CRC function for the LTC3300-1, as discussed on page 40 of the data sheet. However, we have not been successful in duplicating the example on page 40. And rather than continuing, I was hoping that there might be an appropriate published CRC implementation that we could use to go forward. If anyone knows of one, or is willing to share, it would be greatly appreciated.

// Eraticus

Parents
• Hi Eraticus,

No worries, it is a good question, the CRC algorithm mentioned on page 40 of the LTC3300-1 datasheet is a standard CRC-4 algorithm, so we can use the code below to implement it.

#include <stdio.h>

#include <stdint.h>

// CRC-4/

uint8_t calculate_crc4(const uint8_t *data, size_t length) {
uint8_t crc = 0; // Initialize CRC to zero
const uint8_t generator=0x3;
for (size_t i = 0; i < length; i++) {
uint8_t current_byte = data[i];
crc ^= current_byte; // XOR the current data byte with the CRC

// Perform 4 shifts
for (int j = 0; j < 4; j++) {
if((crc & 0x8)!=0)
{
crc = (uint8_t)((crc << 1) ^ generator);
}
else
{
crc <<= 1;
}
}
}
crc &= 0xF;
return crc;
}

int main() {
uint8_t data[] = {0xc, 0x1, 0x0}; // Example data bytes
size_t data_length = sizeof(data) / sizeof(data[0]);

uint8_t crc_result = calculate_crc4(data, data_length);
printf("CRC-4 checksum: 0x%X\n", crc_result);

return 0;
}

Compare to the example shown in Figure 16, the code returns the correct CRC result.

The algorithm above was quite inefficient as it works bit by bit. For larger input data, this could be quite slow, we can use the lookup table methods to accelerate.

#include <stdio.h>
#include <stdint.h>
uint8_t crc4_table[16];

// calculate_crc4 table/
uint8_t calculatetable_crc4() {
const uint8_t generator=0x3;

for (size_t i = 0; i < 16; i++) {
uint8_t current_byte = (uint8_t)i;
// Perform 4 shifts
for (int j = 0; j < 4; j++) {
if((current_byte & 0x8)!=0)
{
current_byte<<=1;
current_byte^=generator;
}
else
{
current_byte <<= 1;
}
}
crc4_table[i]=current_byte&= 0xF;
}

}

uint8_t compute_crc4(const uint8_t *data, size_t length) {
uint8_t crc = 0; // Initialize CRC to zero
for (size_t i = 0; i < length; i++) {
uint8_t index = (uint8_t)(data[i]^crc);
crc =(uint8_t)(crc4_table[index]) ;

}
crc &= 0xF;
return crc;
}

int main() {
calculatetable_crc4();
uint8_t data[] = {0xc, 0x1, 0x0}; // Example data bytes
size_t data_length = sizeof(data) / sizeof(data[0]);

uint8_t crc_result = compute_crc4(data, data_length);
printf("CRC-4 checksum: 0x%X\n", crc_result);

return 0;
}

Thanks and Best Regards,

Frank

Reply Children
No Data