-
Notifications
You must be signed in to change notification settings - Fork 1
/
Crc16.h
201 lines (175 loc) · 5.92 KB
/
Crc16.h
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
//-------------------------------------------------------------------------------------
// CRC16 support class
// Based on various examples found on the web
// Copyright (C) 2014 Vincenzo Mennella (see license.txt)
// History
// 0.1.0 31/05/2014: First public code release
// 0.1.1 17/12/2014: Minor revision and commented code
//
// License
// "MIT Open Source Software License":
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in the
// Software without restriction, including without limitation the rights to use, copy,
// modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
// and to permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//-------------------------------------------------------------------------------------
#ifndef CRC16_H
#define CRC16_H
#define LIBRARY_VERSION_CRC16_H "0.1.1"
class Crc16 {
private:
//Crc parameters
uint16_t _msbMask;
uint16_t _mask;
uint16_t _xorIn;
uint16_t _xorOut;
uint16_t _polynomial;
uint8_t _reflectIn;
uint8_t _reflectOut;
//Crc value
uint16_t _crc;
uint8_t reflect(uint8_t data, uint8_t bits = 32);
public:
inline Crc16()
{
//Default to XModem parameters
_reflectIn = false;
_reflectOut = false;
_polynomial = 0x1021;
_xorIn = 0x0000;
_xorOut = 0x0000;
_msbMask = 0x8000;
_mask = 0xFFFF;
_crc = _xorIn;
}
inline Crc16(uint8_t reflectIn, uint8_t reflectOut, uint16_t polynomial, uint16_t xorIn, uint16_t xorOut, uint16_t msbMask, uint16_t mask)
{
_reflectIn = reflectIn;
_reflectOut = reflectOut;
_polynomial = polynomial;
_xorIn = xorIn;
_xorOut = xorOut;
_msbMask = msbMask;
_mask = mask;
_crc = _xorIn;
}
inline void clearCrc();
inline void updateCrc(uint8_t data);
inline uint16_t getCrc();
inline uint32_t fastCrc(uint8_t data[], uint8_t start, uint16_t length, uint8_t reflectIn, uint8_t reflectOut, uint16_t polynomial, uint16_t xorIn, uint16_t xorOut, uint16_t msbMask, uint16_t mask);
inline uint32_t XModemCrc(uint8_t data[], uint8_t start, uint16_t length)
{
// XModem parameters: poly=0x1021 init=0x0000 refin=false refout=false xorout=0x0000
return fastCrc(data, start, length, false, false, 0x1021, 0x0000, 0x0000, 0x8000, 0xffff);
}
};
//---------------------------------------------------
// Initialize crc calculation
//---------------------------------------------------
void Crc16::clearCrc()
{
_crc = _xorIn;
}
//---------------------------------------------------
// Update crc with new data
//---------------------------------------------------
void Crc16::updateCrc(uint8_t data)
{
if (_reflectIn != 0)
data = (uint8_t) reflect(data, 8);
int j = 0x80;
while (j > 0)
{
uint16_t bit = (uint16_t)(_crc & _msbMask);
_crc <<= 1;
if ((data & j) != 0)
{
bit = (uint16_t)(bit ^ _msbMask);
}
if (bit != 0)
{
_crc ^= _polynomial;
}
j >>= 1;
}
}
//---------------------------------------------------
// Get final crc value
//---------------------------------------------------
uint16_t Crc16::getCrc()
{
if (_reflectOut != 0)
_crc = (uint32_t)((reflect(_crc) ^ _xorOut) & _mask);
return _crc;
}
//---------------------------------------------------
// Calculate generic crc code on data array
// Examples of crc 16:
// Kermit: width=16 poly=0x1021 init=0x0000 refin=true refout=true xorout=0x0000 check=0x2189
// Modbus: width=16 poly=0x8005 init=0xffff refin=true refout=true xorout=0x0000 check=0x4b37
// XModem: width=16 poly=0x1021 init=0x0000 refin=false refout=false xorout=0x0000 check=0x31c3
// CCITT-False: width=16 poly=0x1021 init=0xffff refin=false refout=false xorout=0x0000 check=0x29b1
//---------------------------------------------------
uint32_t Crc16::fastCrc(uint8_t data[], uint8_t start, uint16_t length, uint8_t reflectIn, uint8_t reflectOut, uint16_t polynomial, uint16_t xorIn, uint16_t xorOut, uint16_t msbMask, uint16_t mask)
{
uint32_t crc = xorIn;
int j;
uint8_t c;
uint32_t bit;
if (length == 0) return crc;
for (int i = start; i < (start + length); i++)
{
c = data[i];
if (reflectIn != 0)
c = (uint8_t) reflect(c, 8);
j = 0x80;
while (j > 0)
{
bit = (uint32_t)(crc & msbMask);
crc <<= 1;
if ((c & j) != 0)
{
bit = (uint32_t)(bit ^ msbMask);
}
if (bit != 0)
{
crc ^= polynomial;
}
j >>= 1;
}
}
if (reflectOut != 0)
crc = (uint32_t)((reflect(crc) ^ xorOut) & mask);
return crc;
}
//-------------------------------------------------------
// Reflects bit in a uint8_t
//-------------------------------------------------------
uint8_t Crc16::reflect(uint8_t data, uint8_t bits)
{
unsigned long reflection = 0x00000000;
// Reflect the data about the center bit.
for (uint8_t bit = 0; bit < bits; bit++)
{
// If the LSB bit is set, set the reflection of it.
if ((data & 0x01) != 0)
{
reflection |= (unsigned long)(1 << ((bits - 1) - bit));
}
data = (uint8_t)(data >> 1);
}
return reflection;
}
#endif