Created
September 24, 2015 10:09
-
-
Save volca/88b8c11de05c2c8fabaf to your computer and use it in GitHub Desktop.
crc16
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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