crc.c
1.36 KB
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
#include "osint.h"
#include "controller.h"
#define CRC5GEN 21 /* CRC5 generator = x^5 + x^4 + x^2 + 1 (-x^5) */
#define ADDR_LEN 11 /* address length in bits */
#define CRC8GEN 133 /* CRC8 generator = x^8 + x^7 + x^2 + 1 (-x^8) */
#define DATA_LEN 32 /* data length in bytes */
u8 __osContAddressCrc(u16 addr)
/* calculate CRC5 by long division algorithm */
/* bufp: pointer to address buffer */
/* return: 5-bit crc value */
{
u32 temp = 0;
u32 i = 0x400;
do {
temp <<= 1;
if ((u32)addr & i) {
if (temp & 0x20) {
temp ^= 0x14;
} else {
temp++;
}
} else {
if (temp & 0x20) temp ^= 0x15;
}
i >>= 1;
} while (i != 0) ;
i = 5;
do {
temp <<= 1;
if (temp & 0x20) temp ^= 0x15;
} while (--i != 0);
return( (u8)(0x1f & temp));
}
u8 __osContDataCrc(u8 *data)
/* calculate CRC8 for the data buffer of length DATA_LEN bytes */
/* using table-driven algorithm */
/* bufp: pointer to data buffer */
/* return: 8-bit crc value */
{
u32 temp=0;
u32 i, j;
for( i = 32 ; i != 0 ; i-- ) {
for( j = 0x80 ; j != 0 ; j >>= 1) {
temp <<= 1;
if ((u32)(*data) & j) {
if (temp & 0x100) {
temp ^= 0x84;
} else{
temp++;
}
} else {
if (temp & 0x100) temp ^= 0x85;
}
}
data++;
}
do {
temp <<= 1;
if (temp & 0x100) temp ^= 0x85;
} while ( ++i < 8 ) ;
return( (u8)temp );
}