This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

TMAG5170-CODE-EXAMPLE: CRC Calculation for TMAG5170

Part Number: TMAG5170-CODE-EXAMPLE

Tool/software:

Hi all, i'm new here to this forum, I have started working with the TMAG5170A1 EV Kit, I have some questions regarding the CRC calculation, I found some code in the Forum 

uint8_t calculateCRC( uint8_t data[] )
{
    int i = 0;
    uint8_t crc = 0x00;
    uint32_t n;

    // Build TX and RX byte array
    uint8_t d[32] = { 0 };

    n = (data[0] << 24)|(data[1] << 16)|(data[2] << 8)|(data[3]);
    Serial.print(n, DEC);
    Serial.println();
    while (n>0)
    {
        d[i] = n&1;
        n = n>>1;
        i++;
    }

    crc |= d[30] ^ d[26] ^ d[25] ^ d[24] ^ d[23] ^ d[21] ^ d[19] ^ d[18] ^ d[15] ^ d[11] ^ d[10] ^ d[9] ^ d[8] ^ d[6] ^ d[4] ^ d[3] ^ d[0] ^ 1;
    crc |= (d[31] ^ d[30] ^ d[27] ^ d[23] ^ d[22] ^ d[21] ^ d[20] ^ d[18] ^ d[16] ^ d[15] ^ d[12] ^ d[8] ^ d[7] ^ d[6] ^ d[5] ^ d[3] ^ d[1] ^ d[0] ^ 1 ^ 1) << 1;
    crc |= (d[31] ^ d[28] ^ d[24] ^ d[23] ^ d[22] ^ d[21] ^ d[19] ^ d[17] ^ d[16] ^ d[13] ^ d[9] ^ d[8] ^ d[7] ^ d[6] ^ d[4] ^ d[2] ^ d[1] ^ 1 ^ 1) << 2;
    crc |= (d[29] ^ d[25] ^ d[24] ^ d[23] ^ d[22] ^ d[20] ^ d[18] ^ d[17] ^ d[14] ^ d[10] ^ d[9] ^ d[8] ^ d[7] ^ d[5] ^ d[3] ^ d[2] ^ 1) << 3;
    Serial.print(crc);
    return crc;
}

When I run this code with the following byte array {0x40, 0x7d, 0x83, 0x1}
I get a CRC value of 3, however in this same thread here, it seems a TI engineer attached an excel sheet there to help calculate the CRC values, the problem is that when running the code above with the shown byte data i get one value for CRC and when using this spreadsheet I get a completely different CRC value of 5, So I need to figure out which one is correct, sorry if my post seems a little too vague, I'm not sure what more details to post. Any help will be greatly appreciated.

Regards,

  • Walter,

    Welcome to E2E. Thanks for reaching out.

    I'm looking through trying to make sense of this, and I think what might be happening is that you are only passing 28 bits to this function.  I believe your full 32bit input to the CRC should be 0x407d8310.  I am able to get the same code you reported (0x3) when I start the CRC with 0x407d8301.  For the command to work we need to send in zeroes for the CRC bits to pad the input to a full 32 bits.  

    Thanks,

    Scott

  • Hi Scott, thanks for the reply, I have actually tried sending the full 32 bits the way you suggested, in the spreadsheet it does not matter how you arrange the last byte, I have tried 0x1 and 0x01 and it still gives me the same crc value of 0x5, in my code, in My code if I send 0x407d8301 I get 0xD I am using the same code shown in the Arduino example.

  • Walter,

    As an 8-bit value 0x1 and 0x01 are equivalent.  I'm expecting that if you expand to binary in your compiler that you will find that they both translate to 0b00000001.  The last 8 bits you need to pass the function should be 0b00010000.

    Thanks,

    Scott