forked from synapticon/siitool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
crc8.c
81 lines (63 loc) · 1.72 KB
/
crc8.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include "crc8.h"
/* Polynom: x^8 + x^2 + x + 1 = 100000111 = 0x107 */
#define POLYNOM 0x07
#define MSB 0x80 /* this is byte specific */
uint8_t crc8byte(uint8_t *crc, uint8_t msg)
{
uint8_t rem = *crc ^ msg;
for (uint8_t b=0; b<8; b++) {
if (rem&MSB)
rem = (rem<<1) ^ (uint8_t)POLYNOM;
else
rem = (rem<<1);
}
*crc = rem;
return rem;
}
uint8_t crc8(const uint8_t *msg, size_t blen)
{
uint8_t rem = 0xff;
uint8_t rpoly = POLYNOM;
int b;
for (size_t i=0; i<blen; i++) {
rem ^= msg[i];
for (b=0; b<8; b++) {
if (rem&MSB)
rem = (rem<<1) ^ rpoly;
else
rem = (rem<<1);
}
}
return rem;
}
#if 0 /* test */
#include <stdio.h>
int main(int argc, char *argv[])
{
uint8_t crc = 0xff;
uint8_t input[] = { 0x08, 0x0e, 0x02, 0x88, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x2f, 0x00 };
crc = crc8(input, sizeof(input)/sizeof(*input));
printf("polynom: %.2x\n", POLYNOM);
printf("Message (size %d): 0x", sizeof(input)/sizeof(*input));
for (int i=0; i<sizeof(input)/sizeof(*input); i++)
printf("%.2x", input[i]);
printf("\n");
printf("CRC: %x\n\n", crc);
uint8_t input1[] = { 0x08, 0x0e, 0x02, 0x88, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
crc = crc8(input1, sizeof(input1)/sizeof(*input1));
printf("polynom: %.2x\n", POLYNOM);
printf("Message (size %d): 0x", sizeof(input1)/sizeof(*input1));
for (int i=0; i<sizeof(input1)/sizeof(*input1); i++)
printf("%.2x", input1[i]);
printf("\n");
printf("CRC: %x\n\n", crc);
printf("Now testing crc8byte()\n");
uint8_t crcb = 0xff;
for (int i=0; i<(sizeof(input)/sizeof(*input)); i++) {
printf("%.2x", input[i]);
crcb = crc8byte(&crcb, input[i]);
}
printf("CRC = 0x%.2x\n", crc);
return 0;
}
#endif