annotate crc8.c @ 178:77c2a9caca3d

a few fixes
author Matt Johnston <matt@ucc.asn.au>
date Thu, 30 Jan 2014 22:55:27 +0800
parents 7da9a3f23592
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
9
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1 /* please read copyright-notice at EOF */
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3 #include <stdint.h>
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
4
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
5 #define CRC8INIT 0x00
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
6 #define CRC8POLY 0x18 //0X18 = X^8+X^5+X^4+X^0
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
7
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
8 uint8_t crc8( uint8_t *data, uint16_t number_of_bytes_in_data )
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
9 {
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
10 uint8_t crc;
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
11 uint16_t loop_count;
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
12 uint8_t bit_counter;
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
13 uint8_t b;
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
14 uint8_t feedback_bit;
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
15
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
16 crc = CRC8INIT;
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
17
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
18 for (loop_count = 0; loop_count != number_of_bytes_in_data; loop_count++)
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
19 {
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
20 b = data[loop_count];
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
21
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
22 bit_counter = 8;
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
23 do {
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
24 feedback_bit = (crc ^ b) & 0x01;
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
25
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
26 if ( feedback_bit == 0x01 ) {
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
27 crc = crc ^ CRC8POLY;
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
28 }
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
29 crc = (crc >> 1) & 0x7F;
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
30 if ( feedback_bit == 0x01 ) {
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
31 crc = crc | 0x80;
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
32 }
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
33
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
34 b = b >> 1;
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
35 bit_counter--;
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
36
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
37 } while (bit_counter > 0);
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
38 }
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
39
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
40 return crc;
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
41 }
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
42
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
43 /*
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
44 This code is from Colin O'Flynn - Copyright (c) 2002
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
45 only minor changes by M.Thomas 9/2004
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
46
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
47 Permission is hereby granted, free of charge, to any person obtaining a copy of
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
48 this software and associated documentation files (the "Software"), to deal in
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
49 the Software without restriction, including without limitation the rights to
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
50 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
51 the Software, and to permit persons to whom the Software is furnished to do so,
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
52 subject to the following conditions:
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
53
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
54 The above copyright notice and this permission notice shall be included in all
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
55 copies or substantial portions of the Software.
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
56
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
57 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
58 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
59 FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
60 COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
61 IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
62 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
63 */