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/>.
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
) {
29 crc
= (crc
<< 1) ^ 0x1021;
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
);
48 void crc16_ccitt_sbuf_append(sbuf_t
*dst
, uint8_t *start
)
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
)
61 for (int ii
= 0; ii
< 8; ++ii
) {
63 crc
= (crc
<< 1) ^ 0xD5;
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
);
82 void crc8_dvb_s2_sbuf_append(sbuf_t
*dst
, uint8_t *start
)
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
++) {
103 void crc8_xor_sbuf_append(sbuf_t
*dst
, uint8_t *start
)
106 const uint8_t *end
= dst
->ptr
;
107 for (uint8_t *ptr
= start
; ptr
< end
; ++ptr
) {
110 sbufWriteU8(dst
, crc
);
113 uint8_t crc8(uint8_t crc
, uint8_t a
)
118 for (int i
=0; i
<8; i
++) {
119 crc_u
= ( crc_u
& 0x80 ) ? 0x7 ^ ( crc_u
<< 1 ) : ( crc_u
<< 1 );
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
++) {
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
++) {