[OpenTRV-dev] CRC computation
Damon Hart-Davis
EMAIL ADDRESS HIDDEN
Sun Apr 20 20:44:59 BST 2014
Duh! My brain hurts!
Can anyone confirm/deny if this, just written by me, is a correct implementation of CRC7 (0x5B polynomial) as given in the paper specified?
// Update 7-bit CRC with next byte.
// Polynomial 0x5B = (x+1)(x^6 + x^5 + x^3 +x^2 + 1).
// See: http://users.ece.cmu.edu/~koopman/roses/dsn04/koopman04_crc_poly_embedded.pdf
// For 2 or 3 byte payloads this should have a Hamming distance of 4 and be within a factor of 2 of optimum error detection.
uint8_t crc7_5B_update(uint8_t crc, const uint8_t datum)
{
crc ^= datum;
for(uint8_t i = 0; ++i <= 8; )
{
if(crc & 0x80)
{ crc = (crc << 1) ^ 0x5B; }
else
{ crc <<= 1; }
}
return(crc & 0x7f);
}
I’m after something with good behaviour on a 2-byte payload and need a 7-bit CRC in this case for packing efficiency.
Also, I assume that it’s customary to start with 0xff for the CRC before feeding it real bytes, unless there is a good reason otherwise?
Rgds
Damon
More information about the OpenTRV-dev
mailing list