Search code examples
c++embeddedpacketcrc16samd21

How to determine CRC16 initial checksum so resulting checksum is zero


Working on a SPI communication bus between an array of SAMD MCUs.

I have an incoming packet which is something like { 0x00, 0xFF, 0x00, 0xFF }. The receiver chip performs CRC16 check on the incoming packet.

Since I am expecting the exact same packet every time, I want to have zero CRC checksum when the packet is valid and not zero checksum when there is a transfer error.

I know that I can add the calculated CRC16 to the end of the packet when sending it and on the receiver side the CRC check will output 0, but in this case it is impossible to add a CRC16 checksum to the packet since the packet is constructed by multiple sender chips on the SPI line and each chip only fills its own two bytes from the entire packet.

I need to load an initial CRC checksum on the receiver side, so after the incoming packet is checked, the resulting CRC equals to zero (if packet is intact).

The answer here on SO is actually what I am looking for, but it is for CRC32 format and I don't actually understand the principle of the code, so I can't rewrite if for CRC16 format.

Any help would be greatly appreciated!

Regards, Niko


Solution

  • I found a solution, thanks to the advice of Bastian Molkenthin, who did this great online CRC calculator.

    He advised trying a brute force calculation of all the 2^16 values of a CRC16 initial value. Indeed, after a few lines of code and few microseconds later the SAMD51 found an initial value, which matches a zero CRC value for the given buffer.