annotate service/arduinoNode/arduino-libraries/OneWire/OneWire.cpp @ 165:af4e9d9f0bd8

some external arduino libs, minus examples and docs Ignore-this: 444126f11a1755109b3b29cbeaa6b9bd
author drewp@bigasterisk.com
date Sat, 11 Apr 2015 01:43:14 -0700
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
165
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
1 /*
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
2 Copyright (c) 2007, Jim Studt (original old version - many contributors since)
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
3
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
4 The latest version of this library may be found at:
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
5 http://www.pjrc.com/teensy/td_libs_OneWire.html
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
6
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
7 OneWire has been maintained by Paul Stoffregen (paul@pjrc.com) since
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
8 January 2010. At the time, it was in need of many bug fixes, but had
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
9 been abandoned the original author (Jim Studt). None of the known
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
10 contributors were interested in maintaining OneWire. Paul typically
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
11 works on OneWire every 6 to 12 months. Patches usually wait that
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
12 long. If anyone is interested in more actively maintaining OneWire,
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
13 please contact Paul.
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
14
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
15 Version 2.2:
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
16 Teensy 3.0 compatibility, Paul Stoffregen, paul@pjrc.com
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
17 Arduino Due compatibility, http://arduino.cc/forum/index.php?topic=141030
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
18 Fix DS18B20 example negative temperature
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
19 Fix DS18B20 example's low res modes, Ken Butcher
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
20 Improve reset timing, Mark Tillotson
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
21 Add const qualifiers, Bertrik Sikken
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
22 Add initial value input to crc16, Bertrik Sikken
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
23 Add target_search() function, Scott Roberts
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
24
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
25 Version 2.1:
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
26 Arduino 1.0 compatibility, Paul Stoffregen
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
27 Improve temperature example, Paul Stoffregen
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
28 DS250x_PROM example, Guillermo Lovato
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
29 PIC32 (chipKit) compatibility, Jason Dangel, dangel.jason AT gmail.com
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
30 Improvements from Glenn Trewitt:
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
31 - crc16() now works
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
32 - check_crc16() does all of calculation/checking work.
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
33 - Added read_bytes() and write_bytes(), to reduce tedious loops.
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
34 - Added ds2408 example.
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
35 Delete very old, out-of-date readme file (info is here)
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
36
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
37 Version 2.0: Modifications by Paul Stoffregen, January 2010:
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
38 http://www.pjrc.com/teensy/td_libs_OneWire.html
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
39 Search fix from Robin James
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
40 http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1238032295/27#27
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
41 Use direct optimized I/O in all cases
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
42 Disable interrupts during timing critical sections
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
43 (this solves many random communication errors)
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
44 Disable interrupts during read-modify-write I/O
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
45 Reduce RAM consumption by eliminating unnecessary
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
46 variables and trimming many to 8 bits
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
47 Optimize both crc8 - table version moved to flash
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
48
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
49 Modified to work with larger numbers of devices - avoids loop.
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
50 Tested in Arduino 11 alpha with 12 sensors.
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
51 26 Sept 2008 -- Robin James
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
52 http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1238032295/27#27
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
53
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
54 Updated to work with arduino-0008 and to include skip() as of
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
55 2007/07/06. --RJL20
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
56
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
57 Modified to calculate the 8-bit CRC directly, avoiding the need for
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
58 the 256-byte lookup table to be loaded in RAM. Tested in arduino-0010
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
59 -- Tom Pollard, Jan 23, 2008
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
60
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
61 Jim Studt's original library was modified by Josh Larios.
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
62
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
63 Tom Pollard, pollard@alum.mit.edu, contributed around May 20, 2008
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
64
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
65 Permission is hereby granted, free of charge, to any person obtaining
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
66 a copy of this software and associated documentation files (the
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
67 "Software"), to deal in the Software without restriction, including
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
68 without limitation the rights to use, copy, modify, merge, publish,
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
69 distribute, sublicense, and/or sell copies of the Software, and to
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
70 permit persons to whom the Software is furnished to do so, subject to
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
71 the following conditions:
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
72
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
73 The above copyright notice and this permission notice shall be
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
74 included in all copies or substantial portions of the Software.
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
75
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
76 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
77 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
78 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
79 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
80 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
81 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
82 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
83
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
84 Much of the code was inspired by Derek Yerger's code, though I don't
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
85 think much of that remains. In any event that was..
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
86 (copyleft) 2006 by Derek Yerger - Free to distribute freely.
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
87
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
88 The CRC code was excerpted and inspired by the Dallas Semiconductor
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
89 sample code bearing this copyright.
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
90 //---------------------------------------------------------------------------
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
91 // Copyright (C) 2000 Dallas Semiconductor Corporation, All Rights Reserved.
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
92 //
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
93 // Permission is hereby granted, free of charge, to any person obtaining a
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
94 // copy of this software and associated documentation files (the "Software"),
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
95 // to deal in the Software without restriction, including without limitation
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
96 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
97 // and/or sell copies of the Software, and to permit persons to whom the
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
98 // Software is furnished to do so, subject to the following conditions:
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
99 //
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
100 // The above copyright notice and this permission notice shall be included
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
101 // in all copies or substantial portions of the Software.
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
102 //
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
103 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
104 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
105 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
106 // IN NO EVENT SHALL DALLAS SEMICONDUCTOR BE LIABLE FOR ANY CLAIM, DAMAGES
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
107 // OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
108 // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
109 // OTHER DEALINGS IN THE SOFTWARE.
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
110 //
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
111 // Except as contained in this notice, the name of Dallas Semiconductor
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
112 // shall not be used except as stated in the Dallas Semiconductor
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
113 // Branding Policy.
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
114 //--------------------------------------------------------------------------
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
115 */
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
116
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
117 #include "OneWire.h"
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
118
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
119
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
120 OneWire::OneWire(uint8_t pin)
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
121 {
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
122 pinMode(pin, INPUT);
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
123 bitmask = PIN_TO_BITMASK(pin);
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
124 baseReg = PIN_TO_BASEREG(pin);
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
125 #if ONEWIRE_SEARCH
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
126 reset_search();
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
127 #endif
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
128 }
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
129
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
130
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
131 // Perform the onewire reset function. We will wait up to 250uS for
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
132 // the bus to come high, if it doesn't then it is broken or shorted
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
133 // and we return a 0;
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
134 //
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
135 // Returns 1 if a device asserted a presence pulse, 0 otherwise.
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
136 //
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
137 uint8_t OneWire::reset(void)
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
138 {
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
139 IO_REG_TYPE mask = bitmask;
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
140 volatile IO_REG_TYPE *reg IO_REG_ASM = baseReg;
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
141 uint8_t r;
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
142 uint8_t retries = 125;
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
143
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
144 noInterrupts();
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
145 DIRECT_MODE_INPUT(reg, mask);
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
146 interrupts();
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
147 // wait until the wire is high... just in case
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
148 do {
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
149 if (--retries == 0) return 0;
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
150 delayMicroseconds(2);
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
151 } while ( !DIRECT_READ(reg, mask));
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
152
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
153 noInterrupts();
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
154 DIRECT_WRITE_LOW(reg, mask);
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
155 DIRECT_MODE_OUTPUT(reg, mask); // drive output low
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
156 interrupts();
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
157 delayMicroseconds(480);
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
158 noInterrupts();
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
159 DIRECT_MODE_INPUT(reg, mask); // allow it to float
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
160 delayMicroseconds(70);
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
161 r = !DIRECT_READ(reg, mask);
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
162 interrupts();
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
163 delayMicroseconds(410);
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
164 return r;
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
165 }
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
166
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
167 //
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
168 // Write a bit. Port and bit is used to cut lookup time and provide
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
169 // more certain timing.
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
170 //
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
171 void OneWire::write_bit(uint8_t v)
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
172 {
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
173 IO_REG_TYPE mask=bitmask;
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
174 volatile IO_REG_TYPE *reg IO_REG_ASM = baseReg;
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
175
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
176 if (v & 1) {
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
177 noInterrupts();
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
178 DIRECT_WRITE_LOW(reg, mask);
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
179 DIRECT_MODE_OUTPUT(reg, mask); // drive output low
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
180 delayMicroseconds(10);
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
181 DIRECT_WRITE_HIGH(reg, mask); // drive output high
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
182 interrupts();
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
183 delayMicroseconds(55);
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
184 } else {
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
185 noInterrupts();
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
186 DIRECT_WRITE_LOW(reg, mask);
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
187 DIRECT_MODE_OUTPUT(reg, mask); // drive output low
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
188 delayMicroseconds(65);
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
189 DIRECT_WRITE_HIGH(reg, mask); // drive output high
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
190 interrupts();
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
191 delayMicroseconds(5);
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
192 }
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
193 }
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
194
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
195 //
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
196 // Read a bit. Port and bit is used to cut lookup time and provide
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
197 // more certain timing.
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
198 //
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
199 uint8_t OneWire::read_bit(void)
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
200 {
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
201 IO_REG_TYPE mask=bitmask;
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
202 volatile IO_REG_TYPE *reg IO_REG_ASM = baseReg;
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
203 uint8_t r;
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
204
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
205 noInterrupts();
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
206 DIRECT_MODE_OUTPUT(reg, mask);
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
207 DIRECT_WRITE_LOW(reg, mask);
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
208 delayMicroseconds(3);
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
209 DIRECT_MODE_INPUT(reg, mask); // let pin float, pull up will raise
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
210 delayMicroseconds(10);
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
211 r = DIRECT_READ(reg, mask);
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
212 interrupts();
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
213 delayMicroseconds(53);
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
214 return r;
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
215 }
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
216
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
217 //
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
218 // Write a byte. The writing code uses the active drivers to raise the
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
219 // pin high, if you need power after the write (e.g. DS18S20 in
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
220 // parasite power mode) then set 'power' to 1, otherwise the pin will
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
221 // go tri-state at the end of the write to avoid heating in a short or
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
222 // other mishap.
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
223 //
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
224 void OneWire::write(uint8_t v, uint8_t power /* = 0 */) {
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
225 uint8_t bitMask;
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
226
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
227 for (bitMask = 0x01; bitMask; bitMask <<= 1) {
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
228 OneWire::write_bit( (bitMask & v)?1:0);
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
229 }
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
230 if ( !power) {
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
231 noInterrupts();
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
232 DIRECT_MODE_INPUT(baseReg, bitmask);
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
233 DIRECT_WRITE_LOW(baseReg, bitmask);
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
234 interrupts();
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
235 }
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
236 }
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
237
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
238 void OneWire::write_bytes(const uint8_t *buf, uint16_t count, bool power /* = 0 */) {
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
239 for (uint16_t i = 0 ; i < count ; i++)
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
240 write(buf[i]);
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
241 if (!power) {
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
242 noInterrupts();
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
243 DIRECT_MODE_INPUT(baseReg, bitmask);
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
244 DIRECT_WRITE_LOW(baseReg, bitmask);
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
245 interrupts();
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
246 }
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
247 }
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
248
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
249 //
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
250 // Read a byte
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
251 //
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
252 uint8_t OneWire::read() {
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
253 uint8_t bitMask;
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
254 uint8_t r = 0;
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
255
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
256 for (bitMask = 0x01; bitMask; bitMask <<= 1) {
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
257 if ( OneWire::read_bit()) r |= bitMask;
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
258 }
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
259 return r;
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
260 }
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
261
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
262 void OneWire::read_bytes(uint8_t *buf, uint16_t count) {
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
263 for (uint16_t i = 0 ; i < count ; i++)
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
264 buf[i] = read();
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
265 }
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
266
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
267 //
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
268 // Do a ROM select
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
269 //
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
270 void OneWire::select(const uint8_t rom[8])
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
271 {
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
272 uint8_t i;
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
273
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
274 write(0x55); // Choose ROM
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
275
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
276 for (i = 0; i < 8; i++) write(rom[i]);
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
277 }
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
278
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
279 //
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
280 // Do a ROM skip
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
281 //
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
282 void OneWire::skip()
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
283 {
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
284 write(0xCC); // Skip ROM
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
285 }
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
286
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
287 void OneWire::depower()
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
288 {
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
289 noInterrupts();
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
290 DIRECT_MODE_INPUT(baseReg, bitmask);
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
291 interrupts();
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
292 }
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
293
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
294 #if ONEWIRE_SEARCH
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
295
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
296 //
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
297 // You need to use this function to start a search again from the beginning.
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
298 // You do not need to do it for the first search, though you could.
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
299 //
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
300 void OneWire::reset_search()
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
301 {
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
302 // reset the search state
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
303 LastDiscrepancy = 0;
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
304 LastDeviceFlag = FALSE;
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
305 LastFamilyDiscrepancy = 0;
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
306 for(int i = 7; ; i--) {
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
307 ROM_NO[i] = 0;
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
308 if ( i == 0) break;
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
309 }
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
310 }
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
311
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
312 // Setup the search to find the device type 'family_code' on the next call
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
313 // to search(*newAddr) if it is present.
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
314 //
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
315 void OneWire::target_search(uint8_t family_code)
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
316 {
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
317 // set the search state to find SearchFamily type devices
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
318 ROM_NO[0] = family_code;
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
319 for (uint8_t i = 1; i < 8; i++)
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
320 ROM_NO[i] = 0;
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
321 LastDiscrepancy = 64;
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
322 LastFamilyDiscrepancy = 0;
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
323 LastDeviceFlag = FALSE;
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
324 }
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
325
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
326 //
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
327 // Perform a search. If this function returns a '1' then it has
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
328 // enumerated the next device and you may retrieve the ROM from the
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
329 // OneWire::address variable. If there are no devices, no further
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
330 // devices, or something horrible happens in the middle of the
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
331 // enumeration then a 0 is returned. If a new device is found then
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
332 // its address is copied to newAddr. Use OneWire::reset_search() to
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
333 // start over.
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
334 //
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
335 // --- Replaced by the one from the Dallas Semiconductor web site ---
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
336 //--------------------------------------------------------------------------
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
337 // Perform the 1-Wire Search Algorithm on the 1-Wire bus using the existing
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
338 // search state.
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
339 // Return TRUE : device found, ROM number in ROM_NO buffer
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
340 // FALSE : device not found, end of search
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
341 //
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
342 uint8_t OneWire::search(uint8_t *newAddr)
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
343 {
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
344 uint8_t id_bit_number;
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
345 uint8_t last_zero, rom_byte_number, search_result;
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
346 uint8_t id_bit, cmp_id_bit;
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
347
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
348 unsigned char rom_byte_mask, search_direction;
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
349
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
350 // initialize for search
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
351 id_bit_number = 1;
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
352 last_zero = 0;
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
353 rom_byte_number = 0;
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
354 rom_byte_mask = 1;
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
355 search_result = 0;
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
356
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
357 // if the last call was not the last one
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
358 if (!LastDeviceFlag)
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
359 {
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
360 // 1-Wire reset
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
361 if (!reset())
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
362 {
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
363 // reset the search
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
364 LastDiscrepancy = 0;
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
365 LastDeviceFlag = FALSE;
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
366 LastFamilyDiscrepancy = 0;
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
367 return FALSE;
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
368 }
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
369
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
370 // issue the search command
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
371 write(0xF0);
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
372
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
373 // loop to do the search
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
374 do
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
375 {
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
376 // read a bit and its complement
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
377 id_bit = read_bit();
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
378 cmp_id_bit = read_bit();
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
379
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
380 // check for no devices on 1-wire
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
381 if ((id_bit == 1) && (cmp_id_bit == 1))
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
382 break;
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
383 else
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
384 {
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
385 // all devices coupled have 0 or 1
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
386 if (id_bit != cmp_id_bit)
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
387 search_direction = id_bit; // bit write value for search
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
388 else
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
389 {
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
390 // if this discrepancy if before the Last Discrepancy
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
391 // on a previous next then pick the same as last time
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
392 if (id_bit_number < LastDiscrepancy)
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
393 search_direction = ((ROM_NO[rom_byte_number] & rom_byte_mask) > 0);
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
394 else
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
395 // if equal to last pick 1, if not then pick 0
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
396 search_direction = (id_bit_number == LastDiscrepancy);
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
397
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
398 // if 0 was picked then record its position in LastZero
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
399 if (search_direction == 0)
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
400 {
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
401 last_zero = id_bit_number;
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
402
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
403 // check for Last discrepancy in family
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
404 if (last_zero < 9)
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
405 LastFamilyDiscrepancy = last_zero;
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
406 }
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
407 }
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
408
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
409 // set or clear the bit in the ROM byte rom_byte_number
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
410 // with mask rom_byte_mask
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
411 if (search_direction == 1)
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
412 ROM_NO[rom_byte_number] |= rom_byte_mask;
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
413 else
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
414 ROM_NO[rom_byte_number] &= ~rom_byte_mask;
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
415
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
416 // serial number search direction write bit
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
417 write_bit(search_direction);
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
418
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
419 // increment the byte counter id_bit_number
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
420 // and shift the mask rom_byte_mask
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
421 id_bit_number++;
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
422 rom_byte_mask <<= 1;
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
423
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
424 // if the mask is 0 then go to new SerialNum byte rom_byte_number and reset mask
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
425 if (rom_byte_mask == 0)
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
426 {
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
427 rom_byte_number++;
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
428 rom_byte_mask = 1;
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
429 }
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
430 }
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
431 }
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
432 while(rom_byte_number < 8); // loop until through all ROM bytes 0-7
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
433
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
434 // if the search was successful then
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
435 if (!(id_bit_number < 65))
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
436 {
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
437 // search successful so set LastDiscrepancy,LastDeviceFlag,search_result
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
438 LastDiscrepancy = last_zero;
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
439
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
440 // check for last device
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
441 if (LastDiscrepancy == 0)
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
442 LastDeviceFlag = TRUE;
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
443
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
444 search_result = TRUE;
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
445 }
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
446 }
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
447
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
448 // if no device found then reset counters so next 'search' will be like a first
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
449 if (!search_result || !ROM_NO[0])
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
450 {
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
451 LastDiscrepancy = 0;
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
452 LastDeviceFlag = FALSE;
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
453 LastFamilyDiscrepancy = 0;
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
454 search_result = FALSE;
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
455 }
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
456 for (int i = 0; i < 8; i++) newAddr[i] = ROM_NO[i];
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
457 return search_result;
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
458 }
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
459
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
460 #endif
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
461
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
462 #if ONEWIRE_CRC
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
463 // The 1-Wire CRC scheme is described in Maxim Application Note 27:
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
464 // "Understanding and Using Cyclic Redundancy Checks with Maxim iButton Products"
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
465 //
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
466
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
467 #if ONEWIRE_CRC8_TABLE
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
468 // This table comes from Dallas sample code where it is freely reusable,
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
469 // though Copyright (C) 2000 Dallas Semiconductor Corporation
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
470 static const uint8_t PROGMEM dscrc_table[] = {
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
471 0, 94,188,226, 97, 63,221,131,194,156,126, 32,163,253, 31, 65,
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
472 157,195, 33,127,252,162, 64, 30, 95, 1,227,189, 62, 96,130,220,
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
473 35,125,159,193, 66, 28,254,160,225,191, 93, 3,128,222, 60, 98,
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
474 190,224, 2, 92,223,129, 99, 61,124, 34,192,158, 29, 67,161,255,
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
475 70, 24,250,164, 39,121,155,197,132,218, 56,102,229,187, 89, 7,
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
476 219,133,103, 57,186,228, 6, 88, 25, 71,165,251,120, 38,196,154,
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
477 101, 59,217,135, 4, 90,184,230,167,249, 27, 69,198,152,122, 36,
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
478 248,166, 68, 26,153,199, 37,123, 58,100,134,216, 91, 5,231,185,
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
479 140,210, 48,110,237,179, 81, 15, 78, 16,242,172, 47,113,147,205,
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
480 17, 79,173,243,112, 46,204,146,211,141,111, 49,178,236, 14, 80,
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
481 175,241, 19, 77,206,144,114, 44,109, 51,209,143, 12, 82,176,238,
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
482 50,108,142,208, 83, 13,239,177,240,174, 76, 18,145,207, 45,115,
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
483 202,148,118, 40,171,245, 23, 73, 8, 86,180,234,105, 55,213,139,
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
484 87, 9,235,181, 54,104,138,212,149,203, 41,119,244,170, 72, 22,
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
485 233,183, 85, 11,136,214, 52,106, 43,117,151,201, 74, 20,246,168,
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
486 116, 42,200,150, 21, 75,169,247,182,232, 10, 84,215,137,107, 53};
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
487
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
488 //
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
489 // Compute a Dallas Semiconductor 8 bit CRC. These show up in the ROM
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
490 // and the registers. (note: this might better be done without to
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
491 // table, it would probably be smaller and certainly fast enough
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
492 // compared to all those delayMicrosecond() calls. But I got
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
493 // confused, so I use this table from the examples.)
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
494 //
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
495 uint8_t OneWire::crc8(const uint8_t *addr, uint8_t len)
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
496 {
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
497 uint8_t crc = 0;
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
498
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
499 while (len--) {
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
500 crc = pgm_read_byte(dscrc_table + (crc ^ *addr++));
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
501 }
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
502 return crc;
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
503 }
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
504 #else
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
505 //
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
506 // Compute a Dallas Semiconductor 8 bit CRC directly.
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
507 // this is much slower, but much smaller, than the lookup table.
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
508 //
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
509 uint8_t OneWire::crc8(const uint8_t *addr, uint8_t len)
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
510 {
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
511 uint8_t crc = 0;
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
512
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
513 while (len--) {
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
514 uint8_t inbyte = *addr++;
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
515 for (uint8_t i = 8; i; i--) {
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
516 uint8_t mix = (crc ^ inbyte) & 0x01;
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
517 crc >>= 1;
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
518 if (mix) crc ^= 0x8C;
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
519 inbyte >>= 1;
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
520 }
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
521 }
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
522 return crc;
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
523 }
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
524 #endif
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
525
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
526 #if ONEWIRE_CRC16
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
527 bool OneWire::check_crc16(const uint8_t* input, uint16_t len, const uint8_t* inverted_crc, uint16_t crc)
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
528 {
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
529 crc = ~crc16(input, len, crc);
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
530 return (crc & 0xFF) == inverted_crc[0] && (crc >> 8) == inverted_crc[1];
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
531 }
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
532
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
533 uint16_t OneWire::crc16(const uint8_t* input, uint16_t len, uint16_t crc)
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
534 {
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
535 static const uint8_t oddparity[16] =
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
536 { 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0 };
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
537
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
538 for (uint16_t i = 0 ; i < len ; i++) {
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
539 // Even though we're just copying a byte from the input,
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
540 // we'll be doing 16-bit computation with it.
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
541 uint16_t cdata = input[i];
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
542 cdata = (cdata ^ crc) & 0xff;
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
543 crc >>= 8;
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
544
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
545 if (oddparity[cdata & 0x0F] ^ oddparity[cdata >> 4])
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
546 crc ^= 0xC001;
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
547
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
548 cdata <<= 6;
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
549 crc ^= cdata;
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
550 cdata <<= 1;
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
551 crc ^= cdata;
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
552 }
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
553 return crc;
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
554 }
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
555 #endif
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
556
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
557 #endif