-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathOWIcrc.c
134 lines (124 loc) · 3.51 KB
/
OWIcrc.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
// This file has been prepared for Doxygen automatic documentation generation.
/*! \file ********************************************************************
*
* Atmel Corporation
*
* \li File: OWIcrc.c
* \li Compiler: IAR EWAAVR 3.20a
* \li Support mail: [email protected]
*
* \li Supported devices: All AVRs.
*
* \li Application Note: AVR318 - Dallas 1-Wire(R) master.
*
*
* \li Description: CRC algorithms typically used in a 1-Wire(R)
* environment.
*
* $Revision: 1.1.1.1 $
* $Date: 2010-01-07 16:39:25 $
****************************************************************************/
#include "OWIcrc.h"
#include "OWIdefs.h"
/*! \brief Compute the CRC8 value of a data set.
*
* This function will compute the CRC8 or DOW-CRC of inData using seed
* as inital value for the CRC.
*
* \param inData One byte of data to compute CRC from.
*
* \param seed The starting value of the CRC.
*
* \return The CRC8 of inData with seed as initial value.
*
* \note Setting seed to 0 computes the crc8 of the inData.
*
* \note Constantly passing the return value of this function
* As the seed argument computes the CRC8 value of a
* longer string of data.
*/
unsigned char OWI_ComputeCRC8(unsigned char inData, unsigned char seed)
{
unsigned char bitsLeft;
unsigned char temp;
for (bitsLeft = 8; bitsLeft > 0; bitsLeft--)
{
temp = ((seed ^ inData) & 0x01);
if (temp == 0)
{
seed >>= 1;
}
else
{
seed ^= 0x18;
seed >>= 1;
seed |= 0x80;
}
inData >>= 1;
}
return seed;
}
/*! \brief Compute the CRC16 value of a data set.
*
* This function will compute the CRC16 of inData using seed
* as inital value for the CRC.
*
* \param inData One byte of data to compute CRC from.
*
* \param seed The starting value of the CRC.
*
* \return The CRC16 of inData with seed as initial value.
*
* \note Setting seed to 0 computes the crc16 of the inData.
*
* \note Constantly passing the return value of this function
* As the seed argument computes the CRC16 value of a
* longer string of data.
*/
unsigned int OWI_ComputeCRC16(unsigned char inData, unsigned int seed)
{
unsigned char bitsLeft;
unsigned char temp;
for (bitsLeft = 8; bitsLeft > 0; bitsLeft--)
{
temp = ((seed ^ inData) & 0x01);
if (temp == 0)
{
seed >>= 1;
}
else
{
seed ^= 0x4002;
seed >>= 1;
seed |= 0x8000;
}
inData >>= 1;
}
return seed;
}
/*! \brief Calculate and check the CRC of a 64 bit ROM identifier.
*
* This function computes the CRC8 value of the first 56 bits of a
* 64 bit identifier. It then checks the calculated value against the
* CRC value stored in ROM.
*
* \param romvalue A pointer to an array holding a 64 bit identifier.
*
* \retval OWI_CRC_OK The CRC's matched.
* \retval OWI_CRC_ERROR There was a discrepancy between the calculated and the stored CRC.
*/
unsigned char OWI_CheckRomCRC(unsigned char * romValue)
{
unsigned char i;
unsigned char crc8 = 0;
for (i = 0; i < 7; i++)
{
crc8 = OWI_ComputeCRC8(*romValue, crc8);
romValue++;
}
if (crc8 == (*romValue))
{
return OWI_CRC_OK;
}
return OWI_CRC_ERROR;
}