-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrc16.c
35 lines (35 loc) · 889 Bytes
/
crc16.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
#include "crc16.h"
/* http://www.nongnu.org/avr-libc/user-manual/group__util__crc.html
* uint8_t serno[] = { 0x02, 0x1c, 0xb8, 0x01, 0, 0, 0, 0xa2 };
* int
* checkcrc(void)
* {
* uint8_t crc = 0, i;
* for (i = 0; i < sizeof serno / sizeof serno[0]; i++)
* crc = _crc_ibutton_update(crc, serno[i]);
* return crc; // must be 0
*
*/
// C implemtation for computer
#ifndef AVR
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
uint16_t compute_crc(char *data, int len){
uint16_t crc = 0xffff;
int i;
for(i=0; i<len; i++){
crc=_crc16_update(crc,data[i]);
}
return crc;
}