2017-03-30 17:54:29 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2017 Intel Corporation.
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
|
|
|
|
2022-05-06 17:23:05 +08:00
|
|
|
#include <zephyr/sys/crc.h>
|
2017-03-30 17:54:29 +08:00
|
|
|
|
2022-02-03 23:22:00 +08:00
|
|
|
uint16_t crc16(uint16_t poly, uint16_t seed, const uint8_t *src, size_t len)
|
2017-03-30 17:54:29 +08:00
|
|
|
{
|
2022-02-03 23:22:00 +08:00
|
|
|
uint16_t crc = seed;
|
|
|
|
size_t i, j;
|
2017-03-30 17:54:29 +08:00
|
|
|
|
2022-02-03 23:22:00 +08:00
|
|
|
for (i = 0; i < len; i++) {
|
|
|
|
crc ^= (uint16_t)(src[i] << 8U);
|
2017-03-30 17:54:29 +08:00
|
|
|
|
2022-02-03 23:22:00 +08:00
|
|
|
for (j = 0; j < 8; j++) {
|
|
|
|
if (crc & 0x8000UL) {
|
|
|
|
crc = (crc << 1U) ^ poly;
|
|
|
|
} else {
|
|
|
|
crc = crc << 1U;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-03-30 17:54:29 +08:00
|
|
|
|
|
|
|
|
2022-02-03 23:22:00 +08:00
|
|
|
return crc;
|
|
|
|
}
|
2017-03-30 17:54:29 +08:00
|
|
|
|
2022-02-03 23:22:00 +08:00
|
|
|
uint16_t crc16_reflect(uint16_t poly, uint16_t seed, const uint8_t *src, size_t len)
|
|
|
|
{
|
|
|
|
uint16_t crc = seed;
|
|
|
|
size_t i, j;
|
|
|
|
|
|
|
|
for (i = 0; i < len; i++) {
|
|
|
|
crc ^= (uint16_t)src[i];
|
|
|
|
|
|
|
|
for (j = 0; j < 8; j++) {
|
|
|
|
if (crc & 0x0001UL) {
|
|
|
|
crc = (crc >> 1U) ^ poly;
|
|
|
|
} else {
|
|
|
|
crc = crc >> 1U;
|
2017-03-30 17:54:29 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-03 23:22:00 +08:00
|
|
|
|
2017-03-30 17:54:29 +08:00
|
|
|
return crc;
|
|
|
|
}
|
2017-11-28 05:06:02 +08:00
|
|
|
|
2022-02-03 23:22:00 +08:00
|
|
|
|
2020-05-28 00:26:57 +08:00
|
|
|
uint16_t crc16_ccitt(uint16_t seed, const uint8_t *src, size_t len)
|
2017-11-28 05:06:02 +08:00
|
|
|
{
|
|
|
|
for (; len > 0; len--) {
|
2020-05-28 00:26:57 +08:00
|
|
|
uint8_t e, f;
|
2017-11-28 05:06:02 +08:00
|
|
|
|
|
|
|
e = seed ^ *src++;
|
|
|
|
f = e ^ (e << 4);
|
2021-06-10 15:50:18 +08:00
|
|
|
seed = (seed >> 8) ^ ((uint16_t)f << 8) ^ ((uint16_t)f << 3) ^ ((uint16_t)f >> 4);
|
2017-11-28 05:06:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return seed;
|
|
|
|
}
|
2018-02-02 04:37:24 +08:00
|
|
|
|
2020-05-28 00:26:57 +08:00
|
|
|
uint16_t crc16_itu_t(uint16_t seed, const uint8_t *src, size_t len)
|
2018-02-02 04:37:24 +08:00
|
|
|
{
|
|
|
|
for (; len > 0; len--) {
|
2019-03-15 02:55:21 +08:00
|
|
|
seed = (seed >> 8U) | (seed << 8U);
|
2018-02-02 04:37:24 +08:00
|
|
|
seed ^= *src++;
|
2019-03-15 02:55:21 +08:00
|
|
|
seed ^= (seed & 0xffU) >> 4U;
|
|
|
|
seed ^= seed << 12U;
|
|
|
|
seed ^= (seed & 0xffU) << 5U;
|
2018-02-02 04:37:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return seed;
|
|
|
|
}
|