-
Notifications
You must be signed in to change notification settings - Fork 9
/
CRC8.cpp
48 lines (33 loc) · 890 Bytes
/
CRC8.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include "CRC8.h"
CRC8::CRC8(void) {
}
void CRC8::begin(void) {
crc remainder;
for (int dividend = 0; dividend < 256; ++dividend)
{
remainder = dividend << (WIDTH - 8);
for (uint8_t bit = 8; bit > 0; --bit)
{
if (remainder & TOPBIT)
{
remainder = (remainder << 1) ^ POLYNOMIAL;
}
else
{
remainder = (remainder << 1);
}
}
crcTable[dividend] = remainder;
}
}
crc CRC8::get_crc8(uint8_t const message[], int nBytes, uint8_t final) {
uint8_t data;
crc remainder = 0xFF;
for (int byte = 0; byte < nBytes; ++byte)
{
data = message[byte] ^ (remainder >> (WIDTH - 8));
remainder = crcTable[data] ^ (remainder << 8);
}
remainder = remainder^final;
return (remainder);
}