2017-12-13 21:57:38 +08:00
|
|
|
/*
|
2020-02-26 19:44:42 +08:00
|
|
|
* Copyright (c) 2020 Intel Corporation
|
2017-12-13 21:57:38 +08:00
|
|
|
* Copyright (c) 2017 Nordic Semiconductor ASA
|
|
|
|
* Copyright (c) 2015 Runtime Inc
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
|
|
|
|
2022-05-06 17:23:05 +08:00
|
|
|
#include <zephyr/sys/crc.h>
|
2017-12-13 21:57:38 +08:00
|
|
|
|
2020-05-28 00:26:57 +08:00
|
|
|
static const uint8_t crc8_ccitt_small_table[16] = {
|
2017-12-13 21:57:38 +08:00
|
|
|
0x00, 0x07, 0x0e, 0x09, 0x1c, 0x1b, 0x12, 0x15,
|
|
|
|
0x38, 0x3f, 0x36, 0x31, 0x24, 0x23, 0x2a, 0x2d
|
|
|
|
};
|
|
|
|
|
2020-05-28 00:26:57 +08:00
|
|
|
uint8_t crc8_ccitt(uint8_t val, const void *buf, size_t cnt)
|
2017-12-13 21:57:38 +08:00
|
|
|
{
|
2020-04-04 00:55:04 +08:00
|
|
|
size_t i;
|
2020-05-28 00:26:57 +08:00
|
|
|
const uint8_t *p = buf;
|
2017-12-13 21:57:38 +08:00
|
|
|
|
|
|
|
for (i = 0; i < cnt; i++) {
|
|
|
|
val ^= p[i];
|
|
|
|
val = (val << 4) ^ crc8_ccitt_small_table[val >> 4];
|
|
|
|
val = (val << 4) ^ crc8_ccitt_small_table[val >> 4];
|
|
|
|
}
|
|
|
|
return val;
|
|
|
|
}
|
2020-02-26 19:44:42 +08:00
|
|
|
|
2020-05-28 00:26:57 +08:00
|
|
|
uint8_t crc8(const uint8_t *src, size_t len, uint8_t polynomial, uint8_t initial_value,
|
2020-02-26 19:44:42 +08:00
|
|
|
bool reversed)
|
|
|
|
{
|
2020-05-28 00:26:57 +08:00
|
|
|
uint8_t crc = initial_value;
|
2020-02-26 19:44:42 +08:00
|
|
|
size_t i, j;
|
|
|
|
|
|
|
|
for (i = 0; i < len; i++) {
|
|
|
|
crc ^= src[i];
|
|
|
|
|
|
|
|
for (j = 0; j < 8; j++) {
|
|
|
|
if (reversed) {
|
|
|
|
if (crc & 0x01) {
|
|
|
|
crc = (crc >> 1) ^ polynomial;
|
|
|
|
} else {
|
|
|
|
crc >>= 1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (crc & 0x80) {
|
|
|
|
crc = (crc << 1) ^ polynomial;
|
|
|
|
} else {
|
|
|
|
crc <<= 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return crc;
|
|
|
|
}
|