提交 | 用户 | age
|
6a6261
|
1 |
#include "crc.h"
|
Q |
2 |
|
|
3 |
|
|
4 |
uint16_t ComputeCrc( uint16_t crc, uint8_t dataByte, uint16_t polynomial )
|
|
5 |
{
|
|
6 |
uint8_t i;
|
|
7 |
|
|
8 |
for( i = 0; i < 8; i++ )
|
|
9 |
{
|
|
10 |
if( ( ( ( crc & 0x8000 ) >> 8 ) ^ ( dataByte & 0x80 ) ) != 0 )
|
|
11 |
{
|
|
12 |
crc <<= 1; // shift left once
|
|
13 |
crc ^= polynomial; // XOR with polynomial
|
|
14 |
}
|
|
15 |
else
|
|
16 |
{
|
|
17 |
crc <<= 1; // shift left once
|
|
18 |
}
|
|
19 |
dataByte <<= 1; // Next data bit
|
|
20 |
}
|
|
21 |
return crc;
|
|
22 |
}
|
|
23 |
|
|
24 |
|
|
25 |
uint16_t RadioComputeCRC( uint8_t *buffer, uint8_t length, uint8_t crcType )
|
|
26 |
{
|
|
27 |
uint8_t i = 0;
|
|
28 |
uint16_t crc = 0;
|
|
29 |
uint16_t polynomial = 0;
|
|
30 |
|
|
31 |
polynomial = ( crcType == CRC_TYPE_IBM ) ? POLYNOMIAL_IBM : POLYNOMIAL_CCITT;
|
|
32 |
crc = ( crcType == CRC_TYPE_IBM ) ? CRC_IBM_SEED : CRC_CCITT_SEED;
|
|
33 |
for( i = 0; i < length; i++ )
|
|
34 |
{
|
|
35 |
crc = ComputeCrc( crc, buffer[i], polynomial );
|
|
36 |
}
|
|
37 |
if( crcType == CRC_TYPE_IBM )
|
|
38 |
{
|
|
39 |
return crc;
|
|
40 |
}
|
|
41 |
else
|
|
42 |
{
|
|
43 |
return( ( uint16_t ) ( ~crc ));
|
|
44 |
}
|
|
45 |
}
|
|
46 |
|
|
47 |
|