clean up target list order (#3040)
[ExpressLRS.git] / src / lib / CRC / crc.cpp
blob23b5d34317302ee8b0f75ee560a58d2a31f55f00
1 #include "crc.h"
3 GENERIC_CRC8::GENERIC_CRC8(uint8_t poly)
5 uint8_t crc;
7 for (uint16_t i = 0; i < crclen; i++)
9 crc = i;
10 for (uint8_t j = 0; j < 8; j++)
12 crc = (crc << 1) ^ ((crc & 0x80) ? poly : 0);
14 crc8tab[i] = crc & 0xFF;
18 uint8_t ICACHE_RAM_ATTR GENERIC_CRC8::calc(const uint8_t data)
20 return crc8tab[data];
23 uint8_t ICACHE_RAM_ATTR GENERIC_CRC8::calc(const uint8_t *data, uint16_t len, uint8_t crc)
25 while (len--)
27 crc = crc8tab[crc ^ *data++];
29 return crc;
32 void Crc2Byte::init(uint8_t bits, uint16_t poly)
34 if (bits == _bits && poly == _poly)
35 return;
36 _poly = poly;
37 _bits = bits;
38 _bitmask = (1 << _bits) - 1;
39 uint16_t highbit = 1 << (_bits - 1);
40 uint16_t crc;
41 for (uint16_t i = 0; i < crclen; i++)
43 crc = i << (bits - 8);
44 for (uint8_t j = 0; j < 8; j++)
46 crc = (crc << 1) ^ ((crc & highbit) ? poly : 0);
48 _crctab[i] = crc;
52 uint16_t ICACHE_RAM_ATTR Crc2Byte::calc(uint8_t *data, uint8_t len, uint16_t crc)
54 while (len--)
56 crc = (crc << 8) ^ _crctab[((crc >> (_bits - 8)) ^ (uint16_t) *data++) & 0x00FF];
58 return crc & _bitmask;