forked from Traumflug/Teacup_Firmware
-
Notifications
You must be signed in to change notification settings - Fork 0
/
crc.c
38 lines (33 loc) · 705 Bytes
/
crc.c
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
#include "crc.h"
/** \file
\brief crc16 routine
*/
#ifndef SIMULATOR
#include <util/crc16.h>
#else
// Equivalent to avr-libc's _crc16_update.
uint16_t _crc16_update(uint16_t crc, uint8_t a) {
int i;
crc ^= a;
for (i = 0; i < 8; ++i) {
if (crc & 1)
crc = (crc >> 1) ^ 0xA001;
else
crc = (crc >> 1);
}
return crc;
}
#endif
/** block-at-once CRC16 calculator
\param *data data to find crc16 for
\param len length of data
\return uint16 crc16 of passed data
uses avr-libc's optimised crc16 routine
*/
uint16_t crc_block(void *data, uint16_t len) {
uint16_t crc = 0xfeed;
for (; len; data++, len--) {
crc = _crc16_update(crc, *((uint8_t *) data));
}
return crc;
}