2023-01-18 17:18:19 +00:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <stddef.h>
|
|
|
|
|
|
|
|
/* CRC8 with the specified initialization value 'init' and
|
|
|
|
* polynomial 'poly'. */
|
2023-01-18 19:25:39 +00:00
|
|
|
uint8_t crc8(const uint8_t* data, size_t len, uint8_t init, uint8_t poly) {
|
2023-01-18 17:18:19 +00:00
|
|
|
uint8_t crc = init;
|
|
|
|
size_t i, j;
|
2023-01-18 19:25:39 +00:00
|
|
|
for(i = 0; i < len; i++) {
|
2023-01-18 17:18:19 +00:00
|
|
|
crc ^= data[i];
|
2023-01-18 19:25:39 +00:00
|
|
|
for(j = 0; j < 8; j++) {
|
|
|
|
if((crc & 0x80) != 0)
|
2023-01-18 17:18:19 +00:00
|
|
|
crc = (uint8_t)((crc << 1) ^ poly);
|
|
|
|
else
|
|
|
|
crc <<= 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return crc;
|
|
|
|
}
|