-
Notifications
You must be signed in to change notification settings - Fork 0
/
crc.c
137 lines (125 loc) · 3.39 KB
/
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
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*! \file crc.c
\brief crc functions
*/
#include "crc.h"
#include "pic.h"
unsigned int CRC_XModem(far rom char *Data,int length,unsigned int crc_seed);
unsigned int CRC_iButton(far rom char *Data,int length,unsigned int crc_seed);
unsigned int CRC_CCITT(far rom char *Data,int length,unsigned int crc_seed);
unsigned int CRC_16(far rom char *Data,int length,unsigned int crc_seed);
///////////////////////////////////////////////////////////////
/// Computes CRC 16 on ROM data
/// Data = ROM Pointer to data
/// length = total bytes of data
/// crc = Seed value for the CRC
/// returns the CRC
///////////////////////////////////////////////////////////////
unsigned int CRC_16(far rom char *Data,int length,unsigned int crc_seed)
{
int s,t;
unsigned int crc = crc_seed;
for(t=0;t<length;t++)
{
crc ^= Data[t];
for (s=0;s<8;s++)
{
if (crc & 1)
crc=(crc >> 1)^0xA001;
else
crc=crc >> 1;
}
}
return crc;
}
///////////////////////////////////////////////////////////////
/// Computes CCITT CRC on ROM data
/// Data = ROM Pointer to data
/// length = total bytes of data
/// crc = Seed value for the CRC
/// returns the CRC
///////////////////////////////////////////////////////////////
unsigned int CRC_CCITT(far rom char *Data,int length,unsigned int crc_seed)
{
int t,s;
char c,d;
unsigned int crc = crc_seed;
for(t=0;t<length;t++)
{
c=Data[t];
crc = (unsigned char)(crc >> 8) | (crc << 8);
crc ^= c;
crc ^= (unsigned char)(crc & 0xff) >> 4;
crc ^= (crc<<8) << 4;
crc ^= ((crc & 0xff) << 4) << 1;
}
return crc;
}
///////////////////////////////////////////////////////////////
/// Computes CCITT CRC one character at a time
/// c = Next character
/// crc = pointer to unsigned int containing crc
///////////////////////////////////////////////////////////////
void CRC_CCITT_CHAR(char c,unsigned int *crc)
{
*crc = (unsigned char)(*crc >> 8) | (*crc << 8);
*crc ^= c;
*crc ^= (unsigned char)(*crc & 0xff) >> 4;
*crc ^= (*crc<<8) << 4;
*crc ^= ((*crc & 0xff) << 4) << 1;
}
///////////////////////////////////////////////////////////////
/// Computes iButton CRC ROM data
/// Data = ROM Pointer to data
/// length = total bytes of data
/// crc = Seed value for the CRC
/// returns the CRC
///////////////////////////////////////////////////////////////
unsigned int CRC_iButton(far rom char *Data,int length,unsigned int crc_seed)
{
int t,s;
unsigned int crc = crc_seed;
for(t=0;t<length;t++)
{
crc = crc ^ Data[t];
for(s=0;s<8;s++)
{
if(crc & 0x01)
{
crc = (crc_seed >> 1);
crc ^= 0x8c;
} else {
crc>>=1;
}
}
}
return crc;
}
///////////////////////////////////////////////////////////////
/// Computes XModem CRC for ROM data
/// Data = ROM Pointer to data
/// length = total bytes of data
/// crc = Seed value for the CRC
/// returns the CRC
///////////////////////////////////////////////////////////////
unsigned int CRC_XModem(far rom char *Data,int length,unsigned int crc_seed)
{
int s,t;
unsigned int d;
unsigned int crc = crc_seed;
for(t=0;t<length;t++)
{
d = Data[t];
crc = crc ^ (d << 8);
for(s=0;s<8;s++)
{
if(crc & 0x8000)
{
crc <<=1;
crc ^= 0x1021;
} else {
crc <<=1;
}
}
}
return crc;
}