Last active
July 18, 2017 03:07
-
-
Save ryankurte/521297b74c4e81cda2e4d278dc27449b to your computer and use it in GitHub Desktop.
CRC Implementations
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
#include <stdint.h> | |
#include <stdbool.h> | |
uint16_t crc16_ccit_FFFF(uint32_t len, uint8_t* data) { | |
uint16_t crc = 0xFFFF; | |
for (uint32_t i=0; i<len; i++) { | |
uint16_t d = data[i]; | |
d ^= crc >> 8; | |
d ^= d >> 4; | |
crc = (crc << 8) ^ (d << 12) ^ (d << 5) ^ (d << 0); | |
} | |
return crc & 0xFFFF; | |
} | |
uint16_t crc16_ccit_xmodem(uint32_t len, uint8_t* data) { | |
uint16_t crc = 0x0000; | |
for (uint32_t i=0; i<len; i++) { | |
uint16_t d = data[i]; | |
d ^= crc >> 8; | |
d ^= d >> 4; | |
crc = (crc << 8) ^ (d << 12) ^ (d << 5) ^ (d << 0); | |
} | |
return crc & 0xFFFF; | |
} | |
uint16_t crc16_ccit_kermit(uint32_t len, uint8_t* data) { | |
uint16_t crc = 0x0000; | |
for (uint32_t i=0; i<len; i++) { | |
uint8_t d = data[i]; | |
uint16_t q; | |
q = (crc ^ d) & 0x0f; | |
crc = (crc >> 4) ^ (q * 0x1081); | |
q = (crc ^ (d >> 4)) & 0xf; | |
crc = (crc >> 4) ^ (q * 0x1081); | |
} | |
return (crc << 8) | (crc >> 8); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment