0
votes

I have a protocol spec sheet for a TWI device which states:

The CRC bytes are an eight bit cyclic redundancy check using an initial seed of 0x5A and a polynomial XOR value of 0x18.

The spec gives some example commands:

enter image description here and also later an additional example with CRC 0x07 resulting from Command 0x15

It also states

Data are sent bit-serially with the most significant bit first.

I tried to calculate CRC values for the command examples given using http://www.sunshine2k.de/coding/javascript/crc/crc_js.html

enter image description here

playing with reflected and non-reflected inputs and results, but I can't get the calculated CRC to match the command examples.

Am I missing something obvious?

1

1 Answers

0
votes

The CRC is reflected, and the polynomial is x8+x5+x4+1. That polynomial reflected with the high power dropped is 0x8c. This C code calculates the CRC:

unsigned crc8twi(unsigned char const *data, size_t len) {
    unsigned crc = 0x5a;
    for (size_t i = 0; i < len; i++) {
        crc ^= data[i];
        for (int k = 0; k < 8; k++)
            crc = crc & 1 ? (crc >> 1) ^ 0x8c : crc >> 1;
    }
    return crc;
}

That gives the CRC 06 for 0c, 48 for 0c f0 64 b8 13, 5a for 11 03, a5 for 00, and 07 for 15.

I don't know how to get the 48 in the second example, but I also have no clue what the up and down arrows mean, nor what the blue and pink colors mean.

I also have no idea what is meant by the "polynomial XOR value of 0x18". The Koopman representation of the correct polynomial would 0x98, which is close but not the same.

Update:

Ok, the 0x18 comes from a truly tortuous approach to the CRC calculation, as shown in this example from a link in the OP's comment below:

tortuous assembly code

In that example the application of the polynomial occurs in two different places. The 0x18 is the polynomial shifted down by one, though not reflected since the code trudges through the data bits one at a time.

This was in fact noted in another answer of mine from a while back (that I forgot about).