CRC preset and residue
Asked Answered
L

3

6

I am working with a device that requires me to generate a 16 bit CRC.

The datasheet for the device says it needs the following CRC Definition:

CRC Type    Length    Polynomial           Direction  Preset      Residue
CRC-CCITT   16 bits   x16 + x12 + x5 + 1   Forward    FFFF (16)   1D0F (16)

where preset=FFFF (16 bit) and Residue=1D0F (16 bit)

I searched for a CRC algorithm and found this link: http://www.lammertbies.nl/comm/info/crc-calculation.html

It has both on it. CRC-CCITT (0xFFFF) CRC-CCITT (0x1D0F)

What's the difference between the preset and the residue?

Lunette answered 27/10, 2008 at 22:33 Comment(1)
Boost has a nice CRC implementation, if you are using C++.Swacked
T
6

You initialize the CRC register with the preset before feeding in your message.

The residue is what should be left in the CRC register after feeding through a message, plus its correct CRC.

If you just want to send a message, you won't see the residue value. But when the device runs your message+CRC through the CRC algorithm again, it'll see a final value of 0x1D0F if there were no transmission errors.


You can also demonstrate this to yourself without getting the device involved. This can help you confirm that your algorithm is doing something that, at least, resembles a CRC.

  • First, calculate the CRC for your message.
  • Append your message and that CRC, then pass the whole thing through a new CRC calculation (remember to reset to the preset value first.)
  • If all went well, your CRC register should contain the residue value.

The best CRC explanation I've ever found is here:

https://archive.org/stream/PainlessCRC/crc_v3.txt

Trebizond answered 28/10, 2008 at 3:33 Comment(1)
That reference archive.org/stream/PainlessCRC/crc_v3.txt is good, but it doesn't explain residue.Descendible
S
0

The difference is in what the algorithm does with the two values. I just looked at a CRC algorithm myself and it looks pretty simple.

Preset is the value it starts with and residue is XOR'd with the value at the end.

Now, the reason for choosing particular values for preset and residue, that I don't know.

Swacked answered 28/10, 2008 at 0:47 Comment(2)
The residue is not the same as the XOR value. It is the remainder which is left after running the entire data stream through the algorithm, including the received CRC. Because the XOR value added to the transmitted CRC goes into the CRC calculation on reception, that residue is the remainder of the XOR value multiplied by X^crc-length. Thus it is not chosen but can be computed from XOR value and CRC polynomial.Parade
So, why are there preset != 0 and XOR != 0 implementations at all? That's more electrical engineering than informatics or mathematics. preset = 0 will leave the CRC work register at 0 for all leading 0 bits. Thus a missed clock would not be detected in the initial section of a message. Similarly an XOR = 0 will cause CRC = 0 to indicate good blocks and thus a defective CRC engine which always outputs 0 would mark all blocks as good. Therefore electrical engineers like XOR != 0. The value ~0 is widey used, but other values are used to create distinct codes for different data sources.Parade
L
0

Something's not right here.

You're looking for a 16-bit CRC, but you've specified a 24-bit Preset and Residue. Post a link to the datasheet for the device you're looking at.

The best source for CRC information is, by the way, is Ross Williams' guide to CRC.

edit: Ah-hah, I see the "24-bit" preset was just the formatting of the table.

Laager answered 28/10, 2008 at 0:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.