Use printf for LOG_DEBUG in sitl.
[inav.git] / src / main / common / crc.c
blobd11d37ae9ed4e1095c173f64dbfcafe91f651d7f
1 /*
2 * This file is part of Cleanflight.
4 * Cleanflight is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * Cleanflight is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with Cleanflight. If not, see <http://www.gnu.org/licenses/>.
18 #include <stdint.h>
20 #include "crc.h"
21 #include "streambuf.h"
24 uint16_t crc16_ccitt(uint16_t crc, unsigned char a)
26 crc ^= (uint16_t)a << 8;
27 for (int ii = 0; ii < 8; ++ii) {
28 if (crc & 0x8000) {
29 crc = (crc << 1) ^ 0x1021;
30 } else {
31 crc = crc << 1;
34 return crc;
37 uint16_t crc16_ccitt_update(uint16_t crc, const void *data, uint32_t length)
39 const uint8_t *p = (const uint8_t *)data;
40 const uint8_t *pend = p + length;
42 for (; p != pend; p++) {
43 crc = crc16_ccitt(crc, *p);
45 return crc;
48 void crc16_ccitt_sbuf_append(sbuf_t *dst, uint8_t *start)
50 uint16_t crc = 0;
51 const uint8_t * const end = sbufPtr(dst);
52 for (const uint8_t *ptr = start; ptr < end; ++ptr) {
53 crc = crc16_ccitt(crc, *ptr);
55 sbufWriteU16(dst, crc);
58 uint8_t crc8_dvb_s2(uint8_t crc, unsigned char a)
60 crc ^= a;
61 for (int ii = 0; ii < 8; ++ii) {
62 if (crc & 0x80) {
63 crc = (crc << 1) ^ 0xD5;
64 } else {
65 crc = crc << 1;
68 return crc;
71 uint8_t crc8_dvb_s2_update(uint8_t crc, const void *data, uint32_t length)
73 const uint8_t *p = (const uint8_t *)data;
74 const uint8_t *pend = p + length;
76 for (; p != pend; p++) {
77 crc = crc8_dvb_s2(crc, *p);
79 return crc;
82 void crc8_dvb_s2_sbuf_append(sbuf_t *dst, uint8_t *start)
84 uint8_t crc = 0;
85 const uint8_t * const end = dst->ptr;
86 for (const uint8_t *ptr = start; ptr < end; ++ptr) {
87 crc = crc8_dvb_s2(crc, *ptr);
89 sbufWriteU8(dst, crc);
92 uint8_t crc8_xor_update(uint8_t crc, const void *data, uint32_t length)
94 const uint8_t *p = (const uint8_t *)data;
95 const uint8_t *pend = p + length;
97 for (; p != pend; p++) {
98 crc ^= *p;
100 return crc;
103 void crc8_xor_sbuf_append(sbuf_t *dst, uint8_t *start)
105 uint8_t crc = 0;
106 const uint8_t *end = dst->ptr;
107 for (uint8_t *ptr = start; ptr < end; ++ptr) {
108 crc ^= *ptr;
110 sbufWriteU8(dst, crc);
113 uint8_t crc8(uint8_t crc, uint8_t a)
115 uint8_t crc_u = a;
116 crc_u ^= crc;
118 for (int i=0; i<8; i++) {
119 crc_u = ( crc_u & 0x80 ) ? 0x7 ^ ( crc_u << 1 ) : ( crc_u << 1 );
122 return crc_u;
125 uint8_t crc8_update(uint8_t crc, const void *data, uint32_t length)
127 const uint8_t *p = (const uint8_t *)data;
128 const uint8_t *pend = p + length;
130 for (; p != pend; p++) {
131 crc = crc8(crc, *p);
133 return crc;
136 uint8_t crc8_sum_update(uint8_t crc, const void *data, uint32_t length)
138 const uint8_t *p = (const uint8_t *)data;
139 const uint8_t *pend = p + length;
141 for (; p != pend; p++) {
142 crc += *p;
144 return crc;