Line data Source code
1 : /*
2 : This program is free software: you can redistribute it and/or modify
3 : it under the terms of the GNU General Public License as published by
4 : the Free Software Foundation, either version 3 of the License, or
5 : (at your option) any later version.
6 :
7 : This program is distributed in the hope that it will be useful,
8 : but WITHOUT ANY WARRANTY; without even the implied warranty of
9 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 : GNU General Public License for more details.
11 :
12 : You should have received a copy of the GNU General Public License
13 : along with this program. If not, see <http://www.gnu.org/licenses/>.
14 : */
15 : /*
16 : collection of CRCs.
17 : */
18 :
19 : #include <stdint.h>
20 : #include "crc.h"
21 :
22 : #include <AP_HAL/AP_HAL_Boards.h>
23 :
24 : /**
25 : * crc4 method from datasheet for 16 bytes (8 short values)
26 : *
27 : * @param [in] data
28 : * @return crc4
29 : */
30 86 : uint16_t crc_crc4(uint16_t *data)
31 : {
32 86 : uint16_t n_rem = 0;
33 : uint8_t n_bit;
34 :
35 1462 : for (uint8_t cnt = 0; cnt < 16; cnt++) {
36 : /* uneven bytes */
37 1376 : if (cnt & 1) {
38 688 : n_rem ^= (uint8_t)((data[cnt >> 1]) & 0x00FF);
39 : } else {
40 688 : n_rem ^= (uint8_t)(data[cnt >> 1] >> 8);
41 : }
42 :
43 12384 : for (n_bit = 8; n_bit > 0; n_bit--) {
44 11008 : if (n_rem & 0x8000) {
45 4813 : n_rem = (n_rem << 1) ^ 0x3000;
46 : } else {
47 6195 : n_rem = (n_rem << 1);
48 : }
49 : }
50 : }
51 :
52 86 : return (n_rem >> 12) & 0xF;
53 : }
54 :
55 : static const uint8_t crc8_table[] = {
56 : 0x00, 0x07, 0x0e, 0x09, 0x1c, 0x1b, 0x12, 0x15, 0x38, 0x3f, 0x36, 0x31,
57 : 0x24, 0x23, 0x2a, 0x2d, 0x70, 0x77, 0x7e, 0x79, 0x6c, 0x6b, 0x62, 0x65,
58 : 0x48, 0x4f, 0x46, 0x41, 0x54, 0x53, 0x5a, 0x5d, 0xe0, 0xe7, 0xee, 0xe9,
59 : 0xfc, 0xfb, 0xf2, 0xf5, 0xd8, 0xdf, 0xd6, 0xd1, 0xc4, 0xc3, 0xca, 0xcd,
60 : 0x90, 0x97, 0x9e, 0x99, 0x8c, 0x8b, 0x82, 0x85, 0xa8, 0xaf, 0xa6, 0xa1,
61 : 0xb4, 0xb3, 0xba, 0xbd, 0xc7, 0xc0, 0xc9, 0xce, 0xdb, 0xdc, 0xd5, 0xd2,
62 : 0xff, 0xf8, 0xf1, 0xf6, 0xe3, 0xe4, 0xed, 0xea, 0xb7, 0xb0, 0xb9, 0xbe,
63 : 0xab, 0xac, 0xa5, 0xa2, 0x8f, 0x88, 0x81, 0x86, 0x93, 0x94, 0x9d, 0x9a,
64 : 0x27, 0x20, 0x29, 0x2e, 0x3b, 0x3c, 0x35, 0x32, 0x1f, 0x18, 0x11, 0x16,
65 : 0x03, 0x04, 0x0d, 0x0a, 0x57, 0x50, 0x59, 0x5e, 0x4b, 0x4c, 0x45, 0x42,
66 : 0x6f, 0x68, 0x61, 0x66, 0x73, 0x74, 0x7d, 0x7a, 0x89, 0x8e, 0x87, 0x80,
67 : 0x95, 0x92, 0x9b, 0x9c, 0xb1, 0xb6, 0xbf, 0xb8, 0xad, 0xaa, 0xa3, 0xa4,
68 : 0xf9, 0xfe, 0xf7, 0xf0, 0xe5, 0xe2, 0xeb, 0xec, 0xc1, 0xc6, 0xcf, 0xc8,
69 : 0xdd, 0xda, 0xd3, 0xd4, 0x69, 0x6e, 0x67, 0x60, 0x75, 0x72, 0x7b, 0x7c,
70 : 0x51, 0x56, 0x5f, 0x58, 0x4d, 0x4a, 0x43, 0x44, 0x19, 0x1e, 0x17, 0x10,
71 : 0x05, 0x02, 0x0b, 0x0c, 0x21, 0x26, 0x2f, 0x28, 0x3d, 0x3a, 0x33, 0x34,
72 : 0x4e, 0x49, 0x40, 0x47, 0x52, 0x55, 0x5c, 0x5b, 0x76, 0x71, 0x78, 0x7f,
73 : 0x6a, 0x6d, 0x64, 0x63, 0x3e, 0x39, 0x30, 0x37, 0x22, 0x25, 0x2c, 0x2b,
74 : 0x06, 0x01, 0x08, 0x0f, 0x1a, 0x1d, 0x14, 0x13, 0xae, 0xa9, 0xa0, 0xa7,
75 : 0xb2, 0xb5, 0xbc, 0xbb, 0x96, 0x91, 0x98, 0x9f, 0x8a, 0x8d, 0x84, 0x83,
76 : 0xde, 0xd9, 0xd0, 0xd7, 0xc2, 0xc5, 0xcc, 0xcb, 0xe6, 0xe1, 0xe8, 0xef,
77 : 0xfa, 0xfd, 0xf4, 0xf3
78 : };
79 :
80 : /*
81 : crc8 from trone driver by Luis Rodrigues
82 : */
83 16058 : uint8_t crc_crc8(const uint8_t *p, uint8_t len)
84 : {
85 16058 : uint16_t crc = 0x0;
86 :
87 59084 : while (len--) {
88 43026 : const uint16_t i = (crc ^ *p++) & 0xFF;
89 43026 : crc = (crc8_table[i] ^ (crc << 8)) & 0xFF;
90 : }
91 :
92 16058 : return crc & 0xFF;
93 : }
94 :
95 : // CRC8 that does not use a lookup table: for generic polynomials
96 46070 : uint8_t crc8_generic(const uint8_t *buf, const uint16_t buf_len, const uint8_t polynomial, uint8_t initial_value)
97 : {
98 46070 : uint8_t crc = initial_value;
99 2016130 : for (uint16_t i = 0; i < buf_len; i++) {
100 1970060 : crc = crc8_dvb(buf[i], crc, polynomial);
101 : }
102 46070 : return crc;
103 : }
104 :
105 : // crc8 from betaflight
106 9120 : uint8_t crc8_dvb_s2(uint8_t crc, uint8_t a)
107 : {
108 9120 : return crc8_dvb(crc, a, 0xD5);
109 : }
110 :
111 : // crc8 from betaflight
112 10397915 : uint8_t crc8_dvb(uint8_t crc, uint8_t a, uint8_t seed)
113 : {
114 10397915 : crc ^= a;
115 93581235 : for (uint8_t i = 0; i < 8; ++i) {
116 83183320 : if (crc & 0x80) {
117 39095573 : crc = (crc << 1) ^ seed;
118 : } else {
119 44087747 : crc = crc << 1;
120 : }
121 : }
122 10397915 : return crc;
123 : }
124 :
125 : // crc8 from betaflight
126 84 : uint8_t crc8_dvb_s2_update(uint8_t crc, const void *data, uint32_t length)
127 : {
128 84 : const uint8_t *p = (const uint8_t *)data;
129 84 : const uint8_t *pend = p + length;
130 :
131 4872 : for (; p != pend; p++) {
132 4788 : crc = crc8_dvb_s2(crc, *p);
133 : }
134 84 : return crc;
135 : }
136 :
137 : // copied from AP_FETtecOneWire.cpp
138 757468 : uint8_t crc8_dvb_update(uint8_t crc, const uint8_t* buf, const uint16_t buf_len)
139 : {
140 9176203 : for (uint16_t i = 0; i < buf_len; i++) {
141 8418735 : crc = crc8_dvb(buf[i], crc, 0x7);
142 : }
143 757468 : return crc;
144 : }
145 :
146 : /*
147 : CRC8-Maxim implementation based on FastCRC library
148 : see https://github.com/FrankBoesing/FastCRC
149 : */
150 : static const uint8_t crc8_table_maxim[] = {
151 : 0x00, 0x5e, 0xbc, 0xe2, 0x61, 0x3f, 0xdd, 0x83,
152 : 0xc2, 0x9c, 0x7e, 0x20, 0xa3, 0xfd, 0x1f, 0x41,
153 : 0x9d, 0xc3, 0x21, 0x7f, 0xfc, 0xa2, 0x40, 0x1e,
154 : 0x5f, 0x01, 0xe3, 0xbd, 0x3e, 0x60, 0x82, 0xdc,
155 : 0x23, 0x7d, 0x9f, 0xc1, 0x42, 0x1c, 0xfe, 0xa0,
156 : 0xe1, 0xbf, 0x5d, 0x03, 0x80, 0xde, 0x3c, 0x62,
157 : 0xbe, 0xe0, 0x02, 0x5c, 0xdf, 0x81, 0x63, 0x3d,
158 : 0x7c, 0x22, 0xc0, 0x9e, 0x1d, 0x43, 0xa1, 0xff,
159 : 0x46, 0x18, 0xfa, 0xa4, 0x27, 0x79, 0x9b, 0xc5,
160 : 0x84, 0xda, 0x38, 0x66, 0xe5, 0xbb, 0x59, 0x07,
161 : 0xdb, 0x85, 0x67, 0x39, 0xba, 0xe4, 0x06, 0x58,
162 : 0x19, 0x47, 0xa5, 0xfb, 0x78, 0x26, 0xc4, 0x9a,
163 : 0x65, 0x3b, 0xd9, 0x87, 0x04, 0x5a, 0xb8, 0xe6,
164 : 0xa7, 0xf9, 0x1b, 0x45, 0xc6, 0x98, 0x7a, 0x24,
165 : 0xf8, 0xa6, 0x44, 0x1a, 0x99, 0xc7, 0x25, 0x7b,
166 : 0x3a, 0x64, 0x86, 0xd8, 0x5b, 0x05, 0xe7, 0xb9,
167 : 0x8c, 0xd2, 0x30, 0x6e, 0xed, 0xb3, 0x51, 0x0f,
168 : 0x4e, 0x10, 0xf2, 0xac, 0x2f, 0x71, 0x93, 0xcd,
169 : 0x11, 0x4f, 0xad, 0xf3, 0x70, 0x2e, 0xcc, 0x92,
170 : 0xd3, 0x8d, 0x6f, 0x31, 0xb2, 0xec, 0x0e, 0x50,
171 : 0xaf, 0xf1, 0x13, 0x4d, 0xce, 0x90, 0x72, 0x2c,
172 : 0x6d, 0x33, 0xd1, 0x8f, 0x0c, 0x52, 0xb0, 0xee,
173 : 0x32, 0x6c, 0x8e, 0xd0, 0x53, 0x0d, 0xef, 0xb1,
174 : 0xf0, 0xae, 0x4c, 0x12, 0x91, 0xcf, 0x2d, 0x73,
175 : 0xca, 0x94, 0x76, 0x28, 0xab, 0xf5, 0x17, 0x49,
176 : 0x08, 0x56, 0xb4, 0xea, 0x69, 0x37, 0xd5, 0x8b,
177 : 0x57, 0x09, 0xeb, 0xb5, 0x36, 0x68, 0x8a, 0xd4,
178 : 0x95, 0xcb, 0x29, 0x77, 0xf4, 0xaa, 0x48, 0x16,
179 : 0xe9, 0xb7, 0x55, 0x0b, 0x88, 0xd6, 0x34, 0x6a,
180 : 0x2b, 0x75, 0x97, 0xc9, 0x4a, 0x14, 0xf6, 0xa8,
181 : 0x74, 0x2a, 0xc8, 0x96, 0x15, 0x4b, 0xa9, 0xf7,
182 : 0xb6, 0xe8, 0x0a, 0x54, 0xd7, 0x89, 0x6b, 0x35
183 : };
184 :
185 0 : uint8_t crc8_maxim(const uint8_t *data, uint16_t length)
186 : {
187 0 : uint16_t crc = 0x0;
188 :
189 0 : while (length--) {
190 0 : crc = crc8_table_maxim[crc ^ *data];
191 0 : data++;
192 : }
193 :
194 0 : return crc;
195 : }
196 :
197 : // CRC8-SAE J1850 (X8+X4+X3+X2+1) left move table
198 : static const uint8_t crc8_table_sae[256] = {
199 : 0x00, 0x1D, 0x3A, 0x27, 0x74, 0x69, 0x4E, 0x53, 0xE8, 0xF5, 0xD2, 0xCF, 0x9C, 0x81, 0xA6, 0xBB,
200 : 0xCD, 0xD0, 0xF7, 0xEA, 0xB9, 0xA4, 0x83, 0x9E, 0x25, 0x38, 0x1F, 0x02, 0x51, 0x4C, 0x6B, 0x76,
201 : 0x87, 0x9A, 0xBD, 0xA0, 0xF3, 0xEE, 0xC9, 0xD4, 0x6F, 0x72, 0x55, 0x48, 0x1B, 0x06, 0x21, 0x3C,
202 : 0x4A, 0x57, 0x70, 0x6D, 0x3E, 0x23, 0x04, 0x19, 0xA2, 0xBF, 0x98, 0x85, 0xD6, 0xCB, 0xEC, 0xF1,
203 : 0x13, 0x0E, 0x29, 0x34, 0x67, 0x7A, 0x5D, 0x40, 0xFB, 0xE6, 0xC1, 0xDC, 0x8F, 0x92, 0xB5, 0xA8,
204 : 0xDE, 0xC3, 0xE4, 0xF9, 0xAA, 0xB7, 0x90, 0x8D, 0x36, 0x2B, 0x0C, 0x11, 0x42, 0x5F, 0x78, 0x65,
205 : 0x94, 0x89, 0xAE, 0xB3, 0xE0, 0xFD, 0xDA, 0xC7, 0x7C, 0x61, 0x46, 0x5B, 0x08, 0x15, 0x32, 0x2F,
206 : 0x59, 0x44, 0x63, 0x7E, 0x2D, 0x30, 0x17, 0x0A, 0xB1, 0xAC, 0x8B, 0x96, 0xC5, 0xD8, 0xFF, 0xE2,
207 : 0x26, 0x3B, 0x1C, 0x01, 0x52, 0x4F, 0x68, 0x75, 0xCE, 0xD3, 0xF4, 0xE9, 0xBA, 0xA7, 0x80, 0x9D,
208 : 0xEB, 0xF6, 0xD1, 0xCC, 0x9F, 0x82, 0xA5, 0xB8, 0x03, 0x1E, 0x39, 0x24, 0x77, 0x6A, 0x4D, 0x50,
209 : 0xA1, 0xBC, 0x9B, 0x86, 0xD5, 0xC8, 0xEF, 0xF2, 0x49, 0x54, 0x73, 0x6E, 0x3D, 0x20, 0x07, 0x1A,
210 : 0x6C, 0x71, 0x56, 0x4B, 0x18, 0x05, 0x22, 0x3F, 0x84, 0x99, 0xBE, 0xA3, 0xF0, 0xED, 0xCA, 0xD7,
211 : 0x35, 0x28, 0x0F, 0x12, 0x41, 0x5C, 0x7B, 0x66, 0xDD, 0xC0, 0xE7, 0xFA, 0xA9, 0xB4, 0x93, 0x8E,
212 : 0xF8, 0xE5, 0xC2, 0xDF, 0x8C, 0x91, 0xB6, 0xAB, 0x10, 0x0D, 0x2A, 0x37, 0x64, 0x79, 0x5E, 0x43,
213 : 0xB2, 0xAF, 0x88, 0x95, 0xC6, 0xDB, 0xFC, 0xE1, 0x5A, 0x47, 0x60, 0x7D, 0x2E, 0x33, 0x14, 0x09,
214 : 0x7F, 0x62, 0x45, 0x58, 0x0B, 0x16, 0x31, 0x2C, 0x97, 0x8A, 0xAD, 0xB0, 0xE3, 0xFE, 0xD9, 0xC4,
215 : };
216 :
217 0 : uint8_t crc8_sae(const uint8_t *data, uint16_t length)
218 : {
219 0 : uint8_t crc = 0xFF;
220 :
221 0 : while (length--) {
222 0 : crc = crc8_table_sae[crc ^ (*data & 0xFF)];
223 0 : data++;
224 : }
225 0 : crc ^= 0xFF;
226 :
227 0 : return crc;
228 : }
229 :
230 : // crc table for rangefinder rds02uf
231 : static const uint8_t crc8_table_rds02uf[256] = {
232 : 0x93,0x98,0xE4,0x46,0xEB,0xBA,0x04,0x4C,
233 : 0xFA,0x40,0xB8,0x96,0x0E,0xB2,0xB7,0xC0,
234 : 0x0C,0x32,0x9B,0x80,0xFF,0x30,0x7F,0x9D,
235 : 0xB3,0x81,0x58,0xE7,0xF1,0x19,0x7E,0xB6,
236 : 0xCD,0xF7,0xB4,0xCB,0xBC,0x5C,0xD6,0x09,
237 : 0x20,0x0A,0xE0,0x37,0x51,0x67,0x24,0x95,
238 : 0xE1,0x62,0xF8,0x5E,0x38,0x15,0x54,0x77,
239 : 0x63,0x57,0x6D,0xE9,0x89,0x76,0xBE,0x41,
240 : 0x5D,0xF9,0xB1,0x4D,0x6C,0x53,0x9C,0xA2,
241 : 0x23,0xC4,0x8E,0xC8,0x05,0x42,0x61,0x71,
242 : 0xC5,0x00,0x18,0x6F,0x5F,0xFB,0x7B,0x11,
243 : 0x65,0x2D,0x8C,0xED,0x14,0xAB,0x88,0xD5,
244 : 0xD9,0xC2,0x36,0x34,0x7C,0x5B,0x3C,0xF6,
245 : 0x48,0x0B,0xEE,0x02,0x83,0x79,0x17,0xE6,
246 : 0xA8,0x78,0xF5,0xD3,0x4E,0x50,0x52,0x91,
247 : 0xD8,0xC6,0x22,0xEC,0x3B,0xE5,0x3F,0x86,
248 : 0x06,0xCF,0x2B,0x2F,0x3D,0x59,0x1C,0x87,
249 : 0xEF,0x4F,0x10,0xD2,0x7D,0xDA,0x72,0xA0,
250 : 0x9F,0xDE,0x6B,0x75,0x56,0xBD,0xC7,0xC1,
251 : 0x70,0x1D,0x25,0x92,0xA5,0x31,0xE2,0xD7,
252 : 0xD0,0x9A,0xAF,0xA9,0xC9,0x97,0x08,0x33,
253 : 0x5A,0x99,0xC3,0x16,0x84,0x82,0x8A,0xF3,
254 : 0x4A,0xCE,0xDB,0x29,0x0F,0xAE,0x6E,0xE3,
255 : 0x8B,0x07,0x3A,0x74,0x47,0xB0,0xBB,0xB5,
256 : 0x7A,0xAA,0x2C,0xD4,0x03,0x3E,0x1A,0xA7,
257 : 0x27,0x64,0x06,0xBF,0x55,0x73,0x1E,0xFE,
258 : 0x49,0x01,0x39,0x28,0xF4,0x26,0xDF,0xDD,
259 : 0x44,0x0D,0x21,0xF2,0x85,0xB9,0xEA,0x4B,
260 : 0xDC,0x6A,0xCA,0xAC,0x12,0xFC,0x2E,0x2A,
261 : 0xA3,0xF0,0x66,0xE8,0x60,0x45,0xA1,0x8D,
262 : 0x68,0x35,0xFD,0x8F,0x9E,0x1F,0x13,0xD1,
263 : 0xAD,0x69,0xCC,0xA4,0x94,0x90,0x1B,0x43,
264 : };
265 :
266 695 : uint8_t crc8_rds02uf(const uint8_t *data, uint16_t length)
267 : {
268 695 : uint8_t crc = 0;
269 11815 : while (length--) {
270 11120 : crc = crc8_table_rds02uf[crc^*(data++)];
271 : }
272 695 : return crc;
273 : }
274 :
275 224258 : uint8_t crc_xor_of_bytes(const uint8_t* data, uint16_t length)
276 : {
277 224258 : uint8_t checksum = 0;
278 14140426 : for (uint16_t i = 0; i < length; i++) {
279 13916168 : checksum ^= data[i];
280 : }
281 224258 : return checksum;
282 : }
283 :
284 : /*
285 : xmodem CRC thanks to avr-liberty
286 : https://github.com/dreamiurg/avr-liberty
287 : */
288 180424 : uint16_t crc_xmodem_update(uint16_t crc, uint8_t data)
289 : {
290 180424 : crc = crc ^ ((uint16_t)data << 8);
291 1623816 : for (uint16_t i=0; i<8; i++)
292 : {
293 1443392 : if(crc & 0x8000) {
294 691312 : crc = (crc << 1) ^ 0x1021;
295 : } else {
296 752080 : crc <<= 1;
297 : }
298 : }
299 :
300 180424 : return crc;
301 : }
302 :
303 0 : uint16_t crc_xmodem(const uint8_t *data, uint16_t len)
304 : {
305 0 : uint16_t crc = 0;
306 0 : for (uint16_t i=0; i<len; i++) {
307 0 : crc = crc_xmodem_update(crc, data[i]);
308 : }
309 0 : return crc;
310 : }
311 :
312 : /*
313 : crc32 from Gary S Brown
314 : */
315 : static const uint32_t crc32_tab[] = {
316 : 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f,
317 : 0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988,
318 : 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91, 0x1db71064, 0x6ab020f2,
319 : 0xf3b97148, 0x84be41de, 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7,
320 : 0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9,
321 : 0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172,
322 : 0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b, 0x35b5a8fa, 0x42b2986c,
323 : 0xdbbbc9d6, 0xacbcf940, 0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59,
324 : 0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423,
325 : 0xcfba9599, 0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924,
326 : 0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190, 0x01db7106,
327 : 0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f, 0x9fbfe4a5, 0xe8b8d433,
328 : 0x7807c9a2, 0x0f00f934, 0x9609a88e, 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d,
329 : 0x91646c97, 0xe6635c01, 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e,
330 : 0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950,
331 : 0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65,
332 : 0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2, 0x4adfa541, 0x3dd895d7,
333 : 0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0,
334 : 0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, 0x270241aa,
335 : 0xbe0b1010, 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f,
336 : 0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17, 0x2eb40d81,
337 : 0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6, 0x03b6e20c, 0x74b1d29a,
338 : 0xead54739, 0x9dd277af, 0x04db2615, 0x73dc1683, 0xe3630b12, 0x94643b84,
339 : 0x0d6d6a3e, 0x7a6a5aa8, 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1,
340 : 0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb,
341 : 0x196c3671, 0x6e6b06e7, 0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc,
342 : 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5, 0xd6d6a3e8, 0xa1d1937e,
343 : 0x38d8c2c4, 0x4fdff252, 0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b,
344 : 0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55,
345 : 0x316e8eef, 0x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236,
346 : 0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe, 0xb2bd0b28,
347 : 0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d,
348 : 0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x026d930a, 0x9c0906a9, 0xeb0e363f,
349 : 0x72076785, 0x05005713, 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38,
350 : 0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21, 0x86d3d2d4, 0xf1d4e242,
351 : 0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777,
352 : 0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c, 0x8f659eff, 0xf862ae69,
353 : 0x616bffd3, 0x166ccf45, 0xa00ae278, 0xd70dd2ee, 0x4e048354, 0x3903b3c2,
354 : 0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db, 0xaed16a4a, 0xd9d65adc,
355 : 0x40df0b66, 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9,
356 : 0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605, 0xcdd70693,
357 : 0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94,
358 : 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d
359 : };
360 :
361 :
362 88924 : uint32_t crc_crc32(uint32_t crc, const uint8_t *buf, uint32_t size)
363 : {
364 1814372 : for (uint32_t i=0; i<size; i++) {
365 1725448 : crc = crc32_tab[(crc ^ buf[i]) & 0xff] ^ (crc >> 8);
366 : }
367 :
368 88924 : return crc;
369 : }
370 :
371 : // smaller (and slower) crc32 for bootloader
372 14360 : uint32_t crc32_small(uint32_t crc, const uint8_t *buf, uint32_t size)
373 : {
374 72999345 : while (size--) {
375 72984985 : const uint8_t byte = *buf++;
376 72984985 : crc ^= byte;
377 656864865 : for (uint8_t i=0; i<8; i++) {
378 583879880 : const uint32_t mask = -(crc & 1);
379 583879880 : crc >>= 1;
380 583879880 : crc ^= (0xEDB88320 & mask);
381 : }
382 : }
383 14360 : return crc;
384 : }
385 :
386 : /*
387 : * Copyright (C) 2010 Swift Navigation Inc.
388 : * Contact: Fergus Noble <[email protected]>
389 : *
390 : * This source is subject to the license found in the file 'LICENSE' which must
391 : * be be distributed together with this source. All other rights reserved.
392 : *
393 : * THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
394 : * EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
395 : * WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
396 : */
397 : /* CRC16 implementation according to CCITT standards */
398 : static const uint16_t crc16tab[256] = {
399 : 0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50A5, 0x60C6, 0x70E7,
400 : 0x8108, 0x9129, 0xA14A, 0xB16B, 0xC18C, 0xD1AD, 0xE1CE, 0xF1EF,
401 : 0x1231, 0x0210, 0x3273, 0x2252, 0x52B5, 0x4294, 0x72F7, 0x62D6,
402 : 0x9339, 0x8318, 0xB37B, 0xA35A, 0xD3BD, 0xC39C, 0xF3FF, 0xE3DE,
403 : 0x2462, 0x3443, 0x0420, 0x1401, 0x64E6, 0x74C7, 0x44A4, 0x5485,
404 : 0xA56A, 0xB54B, 0x8528, 0x9509, 0xE5EE, 0xF5CF, 0xC5AC, 0xD58D,
405 : 0x3653, 0x2672, 0x1611, 0x0630, 0x76D7, 0x66F6, 0x5695, 0x46B4,
406 : 0xB75B, 0xA77A, 0x9719, 0x8738, 0xF7DF, 0xE7FE, 0xD79D, 0xC7BC,
407 : 0x48C4, 0x58E5, 0x6886, 0x78A7, 0x0840, 0x1861, 0x2802, 0x3823,
408 : 0xC9CC, 0xD9ED, 0xE98E, 0xF9AF, 0x8948, 0x9969, 0xA90A, 0xB92B,
409 : 0x5AF5, 0x4AD4, 0x7AB7, 0x6A96, 0x1A71, 0x0A50, 0x3A33, 0x2A12,
410 : 0xDBFD, 0xCBDC, 0xFBBF, 0xEB9E, 0x9B79, 0x8B58, 0xBB3B, 0xAB1A,
411 : 0x6CA6, 0x7C87, 0x4CE4, 0x5CC5, 0x2C22, 0x3C03, 0x0C60, 0x1C41,
412 : 0xEDAE, 0xFD8F, 0xCDEC, 0xDDCD, 0xAD2A, 0xBD0B, 0x8D68, 0x9D49,
413 : 0x7E97, 0x6EB6, 0x5ED5, 0x4EF4, 0x3E13, 0x2E32, 0x1E51, 0x0E70,
414 : 0xFF9F, 0xEFBE, 0xDFDD, 0xCFFC, 0xBF1B, 0xAF3A, 0x9F59, 0x8F78,
415 : 0x9188, 0x81A9, 0xB1CA, 0xA1EB, 0xD10C, 0xC12D, 0xF14E, 0xE16F,
416 : 0x1080, 0x00A1, 0x30C2, 0x20E3, 0x5004, 0x4025, 0x7046, 0x6067,
417 : 0x83B9, 0x9398, 0xA3FB, 0xB3DA, 0xC33D, 0xD31C, 0xE37F, 0xF35E,
418 : 0x02B1, 0x1290, 0x22F3, 0x32D2, 0x4235, 0x5214, 0x6277, 0x7256,
419 : 0xB5EA, 0xA5CB, 0x95A8, 0x8589, 0xF56E, 0xE54F, 0xD52C, 0xC50D,
420 : 0x34E2, 0x24C3, 0x14A0, 0x0481, 0x7466, 0x6447, 0x5424, 0x4405,
421 : 0xA7DB, 0xB7FA, 0x8799, 0x97B8, 0xE75F, 0xF77E, 0xC71D, 0xD73C,
422 : 0x26D3, 0x36F2, 0x0691, 0x16B0, 0x6657, 0x7676, 0x4615, 0x5634,
423 : 0xD94C, 0xC96D, 0xF90E, 0xE92F, 0x99C8, 0x89E9, 0xB98A, 0xA9AB,
424 : 0x5844, 0x4865, 0x7806, 0x6827, 0x18C0, 0x08E1, 0x3882, 0x28A3,
425 : 0xCB7D, 0xDB5C, 0xEB3F, 0xFB1E, 0x8BF9, 0x9BD8, 0xABBB, 0xBB9A,
426 : 0x4A75, 0x5A54, 0x6A37, 0x7A16, 0x0AF1, 0x1AD0, 0x2AB3, 0x3A92,
427 : 0xFD2E, 0xED0F, 0xDD6C, 0xCD4D, 0xBDAA, 0xAD8B, 0x9DE8, 0x8DC9,
428 : 0x7C26, 0x6C07, 0x5C64, 0x4C45, 0x3CA2, 0x2C83, 0x1CE0, 0x0CC1,
429 : 0xEF1F, 0xFF3E, 0xCF5D, 0xDF7C, 0xAF9B, 0xBFBA, 0x8FD9, 0x9FF8,
430 : 0x6E17, 0x7E36, 0x4E55, 0x5E74, 0x2E93, 0x3EB2, 0x0ED1, 0x1EF0
431 : };
432 :
433 2149109 : uint16_t crc16_ccitt(const uint8_t *buf, uint32_t len, uint16_t crc)
434 : {
435 26995729 : for (uint32_t i = 0; i < len; i++) {
436 24846620 : crc = (crc << 8) ^ crc16tab[((crc >> 8) ^ *buf++) & 0x00FF];
437 : }
438 2149109 : return crc;
439 : }
440 :
441 : // CRC16_CCITT algorithm using right shift
442 0 : uint16_t crc16_ccitt_r(const uint8_t *buf, uint32_t len, uint16_t crc, uint16_t out)
443 : {
444 0 : for (uint32_t i = 0; i < len; i++) {
445 0 : crc ^= *buf++; // XOR byte into least sig. byte of crc
446 0 : for (uint8_t j = 0; j < 8; j++) { // loop over each bit
447 0 : if ((crc & 0x0001) != 0) { // if the LSB is set
448 0 : crc >>= 1; // shift right and XOR 0x8408
449 0 : crc ^= 0x8408;
450 : } else {
451 0 : crc >>= 1; // just shift right
452 : }
453 : }
454 : }
455 :
456 : // output xor
457 0 : crc = crc ^ out;
458 0 : return crc;
459 : }
460 :
461 0 : uint16_t crc16_ccitt_GDL90(const uint8_t *buf, uint32_t len, uint16_t crc)
462 : {
463 0 : for (uint32_t i = 0; i < len; i++) {
464 0 : crc = crc16tab[crc >> 8] ^ (crc << 8) ^ (uint16_t) *buf++;
465 : }
466 0 : return crc;
467 : }
468 :
469 : /**
470 : * Calculate Modbus CRC16 for array of bytes
471 : *
472 : * @param [in] buf input buffer
473 : * @param [in] len size of buffer
474 : * @return CRC value
475 : */
476 2789 : uint16_t calc_crc_modbus(const uint8_t *buf, uint16_t len)
477 : {
478 2789 : uint16_t crc = 0xFFFF;
479 50875 : for (uint16_t pos = 0; pos < len; pos++) {
480 48086 : crc ^= (uint16_t) buf[pos]; // XOR byte into least sig. byte of crc
481 432774 : for (uint8_t i = 8; i != 0; i--) { // Loop over each bit
482 384688 : if ((crc & 0x0001) != 0) { // If the LSB is set
483 202332 : crc >>= 1; // Shift right and XOR 0xA001
484 202332 : crc ^= 0xA001;
485 : } else {
486 : // Else LSB is not set
487 182356 : crc >>= 1; // Just shift right
488 : }
489 : }
490 : }
491 2789 : return crc;
492 : }
493 :
494 : // fletcher 16 implementation
495 0 : uint16_t crc_fletcher16(const uint8_t *buffer, uint32_t len) {
496 0 : uint16_t c0 = 0;
497 0 : uint16_t c1 = 0;
498 0 : for (uint32_t i = 0; i < len; i++) {
499 0 : c0 = (c0 + buffer[i]) % 255;
500 0 : c1 = (c1 + c0) % 255;
501 : }
502 :
503 0 : return (c1 << 8) | c0;
504 : }
505 :
506 : // FNV-1a implementation
507 : #define FNV_1_PRIME_64 1099511628211UL
508 24 : void hash_fnv_1a(uint32_t len, const uint8_t* buf, uint64_t* hash)
509 : {
510 : uint32_t i;
511 395 : for (i=0; i<len; i++) {
512 371 : *hash ^= (uint64_t)buf[i];
513 371 : *hash *= FNV_1_PRIME_64;
514 : }
515 24 : }
516 :
517 : // calculate 24 bit crc. We take an approach that saves memory and flash at the cost of higher CPU load.
518 28 : uint32_t crc_crc24(const uint8_t *bytes, uint16_t len)
519 : {
520 : static constexpr uint32_t POLYCRC24 = 0x1864CFB;
521 28 : uint32_t crc = 0;
522 514 : while (len--) {
523 486 : uint8_t b = *bytes++;
524 486 : const uint8_t idx = (crc>>16) ^ b;
525 486 : uint32_t crct = idx<<16;
526 4374 : for (uint8_t j=0; j<8; j++) {
527 3888 : crct <<= 1;
528 3888 : if (crct & 0x1000000) {
529 2057 : crct ^= POLYCRC24;
530 : }
531 : }
532 486 : crc = ((crc<<8)&0xFFFFFF) ^ crct;
533 : }
534 28 : return crc;
535 : }
536 :
537 : // simple 8 bit checksum used by FPort
538 0 : uint8_t crc_sum8_with_carry(const uint8_t *p, uint8_t len)
539 : {
540 0 : uint16_t sum = 0;
541 0 : for (uint8_t i=0; i<len; i++) {
542 0 : sum += p[i];
543 0 : sum += sum >> 8;
544 0 : sum &= 0xFF;
545 : }
546 0 : sum = 0xff - ((sum & 0xff) + (sum >> 8));
547 0 : return sum;
548 : }
549 :
550 : // CRC-16 (IBM/ANSI)
551 : // Polynomial : x16 + x15 + x2 + 1 (polynomial representation : 0x8005)
552 : // Initial Value : 0
553 91350 : uint16_t crc_crc16_ibm(uint16_t crc_accum, uint8_t *data_blk_ptr, uint16_t data_blk_size)
554 : {
555 : uint16_t i;
556 : static const uint16_t crc_table[256] = {0x0000,
557 : 0x8005, 0x800F, 0x000A, 0x801B, 0x001E, 0x0014, 0x8011,
558 : 0x8033, 0x0036, 0x003C, 0x8039, 0x0028, 0x802D, 0x8027,
559 : 0x0022, 0x8063, 0x0066, 0x006C, 0x8069, 0x0078, 0x807D,
560 : 0x8077, 0x0072, 0x0050, 0x8055, 0x805F, 0x005A, 0x804B,
561 : 0x004E, 0x0044, 0x8041, 0x80C3, 0x00C6, 0x00CC, 0x80C9,
562 : 0x00D8, 0x80DD, 0x80D7, 0x00D2, 0x00F0, 0x80F5, 0x80FF,
563 : 0x00FA, 0x80EB, 0x00EE, 0x00E4, 0x80E1, 0x00A0, 0x80A5,
564 : 0x80AF, 0x00AA, 0x80BB, 0x00BE, 0x00B4, 0x80B1, 0x8093,
565 : 0x0096, 0x009C, 0x8099, 0x0088, 0x808D, 0x8087, 0x0082,
566 : 0x8183, 0x0186, 0x018C, 0x8189, 0x0198, 0x819D, 0x8197,
567 : 0x0192, 0x01B0, 0x81B5, 0x81BF, 0x01BA, 0x81AB, 0x01AE,
568 : 0x01A4, 0x81A1, 0x01E0, 0x81E5, 0x81EF, 0x01EA, 0x81FB,
569 : 0x01FE, 0x01F4, 0x81F1, 0x81D3, 0x01D6, 0x01DC, 0x81D9,
570 : 0x01C8, 0x81CD, 0x81C7, 0x01C2, 0x0140, 0x8145, 0x814F,
571 : 0x014A, 0x815B, 0x015E, 0x0154, 0x8151, 0x8173, 0x0176,
572 : 0x017C, 0x8179, 0x0168, 0x816D, 0x8167, 0x0162, 0x8123,
573 : 0x0126, 0x012C, 0x8129, 0x0138, 0x813D, 0x8137, 0x0132,
574 : 0x0110, 0x8115, 0x811F, 0x011A, 0x810B, 0x010E, 0x0104,
575 : 0x8101, 0x8303, 0x0306, 0x030C, 0x8309, 0x0318, 0x831D,
576 : 0x8317, 0x0312, 0x0330, 0x8335, 0x833F, 0x033A, 0x832B,
577 : 0x032E, 0x0324, 0x8321, 0x0360, 0x8365, 0x836F, 0x036A,
578 : 0x837B, 0x037E, 0x0374, 0x8371, 0x8353, 0x0356, 0x035C,
579 : 0x8359, 0x0348, 0x834D, 0x8347, 0x0342, 0x03C0, 0x83C5,
580 : 0x83CF, 0x03CA, 0x83DB, 0x03DE, 0x03D4, 0x83D1, 0x83F3,
581 : 0x03F6, 0x03FC, 0x83F9, 0x03E8, 0x83ED, 0x83E7, 0x03E2,
582 : 0x83A3, 0x03A6, 0x03AC, 0x83A9, 0x03B8, 0x83BD, 0x83B7,
583 : 0x03B2, 0x0390, 0x8395, 0x839F, 0x039A, 0x838B, 0x038E,
584 : 0x0384, 0x8381, 0x0280, 0x8285, 0x828F, 0x028A, 0x829B,
585 : 0x029E, 0x0294, 0x8291, 0x82B3, 0x02B6, 0x02BC, 0x82B9,
586 : 0x02A8, 0x82AD, 0x82A7, 0x02A2, 0x82E3, 0x02E6, 0x02EC,
587 : 0x82E9, 0x02F8, 0x82FD, 0x82F7, 0x02F2, 0x02D0, 0x82D5,
588 : 0x82DF, 0x02DA, 0x82CB, 0x02CE, 0x02C4, 0x82C1, 0x8243,
589 : 0x0246, 0x024C, 0x8249, 0x0258, 0x825D, 0x8257, 0x0252,
590 : 0x0270, 0x8275, 0x827F, 0x027A, 0x826B, 0x026E, 0x0264,
591 : 0x8261, 0x0220, 0x8225, 0x822F, 0x022A, 0x823B, 0x023E,
592 : 0x0234, 0x8231, 0x8213, 0x0216, 0x021C, 0x8219, 0x0208,
593 : 0x820D, 0x8207, 0x0202 };
594 :
595 456750 : for (uint16_t j = 0; j < data_blk_size; j++) {
596 365400 : i = ((uint16_t)(crc_accum >> 8) ^ *data_blk_ptr++) & 0xFF;
597 365400 : crc_accum = (crc_accum << 8) ^ crc_table[i];
598 : }
599 :
600 91350 : return crc_accum;
601 : }
602 :
603 : /*
604 : 64 bit crc matching px4 bootloader
605 : */
606 0 : uint64_t crc_crc64(const uint32_t *data, uint16_t num_words)
607 : {
608 0 : const uint64_t poly = 0x42F0E1EBA9EA3693ULL;
609 0 : uint64_t crc = ~(0ULL);
610 0 : while (num_words--) {
611 0 : uint32_t value = *data++;
612 0 : for (uint8_t j = 0; j < 4; j++) {
613 0 : uint8_t byte = ((uint8_t *)&value)[j];
614 0 : crc ^= (uint64_t)byte << 56u;
615 0 : for (uint8_t i = 0; i < 8; i++) {
616 0 : if (crc & (1ull << 63u)) {
617 0 : crc = (uint64_t)(crc << 1u) ^ poly;
618 : } else {
619 0 : crc = (uint64_t)(crc << 1u);
620 : }
621 : }
622 : }
623 : }
624 0 : crc ^= ~(0ULL);
625 :
626 0 : return crc;
627 : }
628 :
629 : // return the parity of byte - "1" if there is an odd number of bits
630 : // set, "0" if there is an even number of bits set note that
631 : // __builtin_parity causes hardfaults on Pixracer-periph - and is
632 : // slower on 1 byte than this:
633 14 : uint8_t parity(uint8_t byte)
634 : {
635 14 : uint8_t p = 0;
636 :
637 14 : p ^= byte & 0x1;
638 14 : byte >>= 1;
639 14 : p ^= byte & 0x1;
640 14 : byte >>= 1;
641 14 : p ^= byte & 0x1;
642 14 : byte >>= 1;
643 14 : p ^= byte & 0x1;
644 14 : byte >>= 1;
645 14 : p ^= byte & 0x1;
646 14 : byte >>= 1;
647 14 : p ^= byte & 0x1;
648 14 : byte >>= 1;
649 14 : p ^= byte & 0x1;
650 14 : byte >>= 1;
651 14 : p ^= byte & 0x1;
652 :
653 14 : return p;
654 : }
655 :
656 : // sums the bytes in the supplied buffer, returns that sum mod 0xFFFF
657 116318 : uint16_t crc_sum_of_bytes_16(const uint8_t *data, uint16_t count)
658 : {
659 116318 : uint16_t ret = 0;
660 15241850 : for (uint32_t i=0; i<count; i++) {
661 15125532 : ret += data[i];
662 : }
663 116318 : return ret;
664 : }
665 :
666 : // sums the bytes in the supplied buffer, returns that sum mod 256
667 : // (i.e. shoved into a uint8_t)
668 41409 : uint8_t crc_sum_of_bytes(const uint8_t *data, uint16_t count)
669 : {
670 41409 : return crc_sum_of_bytes_16(data, count) & 0xFF;
671 : }
|