-
Notifications
You must be signed in to change notification settings - Fork 4
/
Crc8.cpp
59 lines (47 loc) · 1.27 KB
/
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
49
50
51
52
53
54
55
56
57
#include "Crc8.h"
#include "CrcParameters.h"
#include <iostream>
#include <iomanip>
#include <cstdint>
#include <array>
//*****************************************************************************
void Crc8::DisplayTable() const
{
int index = 0;
for (int i = 0; i < 16; ++i)
{
for (int j = 0; j < 16; ++j)
{
std::cout << "0x" << std::uppercase << std::hex << std::setfill('0') << std::setw(2) << int(crcTable[index++]) << ", ";
}
std::cout << "\n";
}
}
//*****************************************************************************
void Crc8::GenerateTable(uint8_t polynomial, bool reflectIn, bool reflectOut)
{
for (int byte = 0; byte < 256; ++byte)
{
uint8_t crc = (reflectIn ? Reverse(uint8_t(byte)) : byte);
for (int bit = 8; bit > 0; --bit)
{
if (crc & 0x80)
{
crc = (crc << 1) ^ polynomial;
}
else
{
crc <<= 1;
}
}
crcTable[byte] = (reflectOut ? Reverse(crc) : crc);
}
}
//*****************************************************************************
uint8_t Crc8::Reverse(uint8_t value) const
{
value = ((value & 0xAA) >> 1) | ((value & 0x55) << 1);
value = ((value & 0xCC) >> 2) | ((value & 0x33) << 2);
value = (value >> 4) | (value << 4);
return value;
}