gigasensore_UART/crc8.h

40 lines
545 B
C

/*
* File: crc8.h
* Author: Emanuele Trabattoni
*
* Created on January 6, 2021, 3:12 PM
*/
#ifndef CRC8_H
#define CRC8_H
uint8_t CRC8(const uint8_t *data, uint8_t len) {
uint8_t crc = 0x00;
while (len--) {
uint8_t extract = *data++;
for (uint8_t tempI = 8; tempI; tempI--) {
uint8_t sum = (crc ^ extract) & 0x01;
crc >>= 1;
if (sum) {
crc ^= 0x8C;
}
extract >>= 1;
}
}
return crc;
}
#ifdef __cplusplus
extern "C" {
#endif
#ifdef __cplusplus
}
#endif
#endif /* CRC8_H */