Skip to content

Instantly share code, notes, and snippets.

@volca
Created September 24, 2015 10:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save volca/88b8c11de05c2c8fabaf to your computer and use it in GitHub Desktop.
Save volca/88b8c11de05c2c8fabaf to your computer and use it in GitHub Desktop.
crc16
static uint16 crc16(uint16 crc, uint8 val) {
const uint16 poly = 0x1021;
uint8 cnt;
for (cnt = 0; cnt < 8; cnt++, val <<= 1)
{
uint8 msb = (crc & 0x8000) ? 1 : 0;
crc <<= 1;
if (val & 0x80) crc |= 0x0001;
if (msb) crc ^= poly;
}
return crc;
}
static uint16 crcCalc(uint8 *data, uint8 len) {
uint16 crc = 0;
for (uint8 idx = 0; idx < len; idx++) {
crc = crc16(crc, data[idx]);
}
// IAR note explains that poly must be run with value zero for each byte of crc.
crc = crc16(crc, 0);
crc = crc16(crc, 0);
return crc;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment