MCPcopy Create free account
hub / github.com/RobTillaart/Arduino / crc16

Function crc16

libraries/CRC/CRC.cpp:138–165  ·  view source on GitHub ↗

CRC POLYNOME = x15 + 1 = 1000 0000 0000 0001 = 0x8001

Source from the content-addressed store, hash-verified

136
137// CRC POLYNOME = x15 + 1 = 1000 0000 0000 0001 = 0x8001
138uint16_t crc16(const uint8_t *array, uint16_t length, const uint16_t polynome,
139 const uint16_t startmask, const uint16_t endmask,
140 const bool reverseIn, const bool reverseOut)
141{
142 uint16_t crc = startmask;
143 while (length--)
144 {
145 if ((length & 0xFF) == 0) yield(); // RTOS
146 uint8_t data = *array++;
147 if (reverseIn) data = reverse8(data);
148 crc ^= ((uint16_t)data) << 8;
149 for (uint8_t i = 8; i; i--)
150 {
151 if (crc & (1 << 15))
152 {
153 crc <<= 1;
154 crc ^= polynome;
155 }
156 else
157 {
158 crc <<= 1;
159 }
160 }
161 }
162 if (reverseOut) crc = reverse16(crc);
163 crc ^= endmask;
164 return crc;
165}
166
167
168// CRC-CCITT POLYNOME = x13 + X5 + 1 = 0001 0000 0010 0001 = 0x1021

Callers 2

crc16_CCITTFunction · 0.85
unittestFunction · 0.85

Calls 2

reverse8Function · 0.85
reverse16Function · 0.85

Tested by 1

unittestFunction · 0.68