Library for Programmable Resolution 1-Wire Digital ThermometerDS18B20

Dependents:   DS18B20_HGC

Committer:
kriedel
Date:
Thu Feb 03 12:10:20 2011 +0000
Revision:
0:21c0c97a5d7c

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
kriedel 0:21c0c97a5d7c 1 /*
kriedel 0:21c0c97a5d7c 2 * OneWireCRC. This is a port to mbed of Jim Studt's Adruino One Wire
kriedel 0:21c0c97a5d7c 3 * library. Please see additional copyrights below this one, including
kriedel 0:21c0c97a5d7c 4 * references to other copyrights.
kriedel 0:21c0c97a5d7c 5 *
kriedel 0:21c0c97a5d7c 6 * Copyright (C) <2009> Petras Saduikis <petras@petras.co.uk>
kriedel 0:21c0c97a5d7c 7 *
kriedel 0:21c0c97a5d7c 8 * This file is part of OneWireCRC.
kriedel 0:21c0c97a5d7c 9 *
kriedel 0:21c0c97a5d7c 10 * OneWireCRC is free software: you can redistribute it and/or modify
kriedel 0:21c0c97a5d7c 11 * it under the terms of the GNU General Public License as published by
kriedel 0:21c0c97a5d7c 12 * the Free Software Foundation, either version 3 of the License, or
kriedel 0:21c0c97a5d7c 13 * (at your option) any later version.
kriedel 0:21c0c97a5d7c 14 *
kriedel 0:21c0c97a5d7c 15 * OneWireCRC is distributed in the hope that it will be useful,
kriedel 0:21c0c97a5d7c 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
kriedel 0:21c0c97a5d7c 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
kriedel 0:21c0c97a5d7c 18 * GNU General Public License for more details.
kriedel 0:21c0c97a5d7c 19 *
kriedel 0:21c0c97a5d7c 20 * You should have received a copy of the GNU General Public License
kriedel 0:21c0c97a5d7c 21 * along with OneWireCRC. If not, see <http://www.gnu.org/licenses/>.
kriedel 0:21c0c97a5d7c 22 */
kriedel 0:21c0c97a5d7c 23 /*
kriedel 0:21c0c97a5d7c 24 Copyright (c) 2007, Jim Studt
kriedel 0:21c0c97a5d7c 25
kriedel 0:21c0c97a5d7c 26 Updated to work with arduino-0008 and to include skip() as of
kriedel 0:21c0c97a5d7c 27 2007/07/06. --RJL20
kriedel 0:21c0c97a5d7c 28
kriedel 0:21c0c97a5d7c 29 Modified to calculate the 8-bit CRC directly, avoiding the need for
kriedel 0:21c0c97a5d7c 30 the 256-byte lookup table to be loaded in RAM. Tested in arduino-0010
kriedel 0:21c0c97a5d7c 31 -- Tom Pollard, Jan 23, 2008
kriedel 0:21c0c97a5d7c 32
kriedel 0:21c0c97a5d7c 33 Permission is hereby granted, free of charge, to any person obtaining
kriedel 0:21c0c97a5d7c 34 a copy of this software and associated documentation files (the
kriedel 0:21c0c97a5d7c 35 "Software"), to deal in the Software without restriction, including
kriedel 0:21c0c97a5d7c 36 without limitation the rights to use, copy, modify, merge, publish,
kriedel 0:21c0c97a5d7c 37 distribute, sublicense, and/or sell copies of the Software, and to
kriedel 0:21c0c97a5d7c 38 permit persons to whom the Software is furnished to do so, subject to
kriedel 0:21c0c97a5d7c 39 the following conditions:
kriedel 0:21c0c97a5d7c 40
kriedel 0:21c0c97a5d7c 41 The above copyright notice and this permission notice shall be
kriedel 0:21c0c97a5d7c 42 included in all copies or substantial portions of the Software.
kriedel 0:21c0c97a5d7c 43
kriedel 0:21c0c97a5d7c 44 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
kriedel 0:21c0c97a5d7c 45 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
kriedel 0:21c0c97a5d7c 46 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
kriedel 0:21c0c97a5d7c 47 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
kriedel 0:21c0c97a5d7c 48 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
kriedel 0:21c0c97a5d7c 49 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
kriedel 0:21c0c97a5d7c 50 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
kriedel 0:21c0c97a5d7c 51
kriedel 0:21c0c97a5d7c 52 Much of the code was inspired by Derek Yerger's code, though I don't
kriedel 0:21c0c97a5d7c 53 think much of that remains. In any event that was..
kriedel 0:21c0c97a5d7c 54 (copyleft) 2006 by Derek Yerger - Free to distribute freely.
kriedel 0:21c0c97a5d7c 55
kriedel 0:21c0c97a5d7c 56 The CRC code was excerpted and inspired by the Dallas Semiconductor
kriedel 0:21c0c97a5d7c 57 sample code bearing this copyright.
kriedel 0:21c0c97a5d7c 58 //---------------------------------------------------------------------------
kriedel 0:21c0c97a5d7c 59 // Copyright (C) 2000 Dallas Semiconductor Corporation, All Rights Reserved.
kriedel 0:21c0c97a5d7c 60 //
kriedel 0:21c0c97a5d7c 61 // Permission is hereby granted, free of charge, to any person obtaining a
kriedel 0:21c0c97a5d7c 62 // copy of this software and associated documentation files (the "Software"),
kriedel 0:21c0c97a5d7c 63 // to deal in the Software without restriction, including without limitation
kriedel 0:21c0c97a5d7c 64 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
kriedel 0:21c0c97a5d7c 65 // and/or sell copies of the Software, and to permit persons to whom the
kriedel 0:21c0c97a5d7c 66 // Software is furnished to do so, subject to the following conditions:
kriedel 0:21c0c97a5d7c 67 //
kriedel 0:21c0c97a5d7c 68 // The above copyright notice and this permission notice shall be included
kriedel 0:21c0c97a5d7c 69 // in all copies or substantial portions of the Software.
kriedel 0:21c0c97a5d7c 70 //
kriedel 0:21c0c97a5d7c 71 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
kriedel 0:21c0c97a5d7c 72 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
kriedel 0:21c0c97a5d7c 73 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
kriedel 0:21c0c97a5d7c 74 // IN NO EVENT SHALL DALLAS SEMICONDUCTOR BE LIABLE FOR ANY CLAIM, DAMAGES
kriedel 0:21c0c97a5d7c 75 // OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
kriedel 0:21c0c97a5d7c 76 // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
kriedel 0:21c0c97a5d7c 77 // OTHER DEALINGS IN THE SOFTWARE.
kriedel 0:21c0c97a5d7c 78 //
kriedel 0:21c0c97a5d7c 79 // Except as contained in this notice, the name of Dallas Semiconductor
kriedel 0:21c0c97a5d7c 80 // shall not be used except as stated in the Dallas Semiconductor
kriedel 0:21c0c97a5d7c 81 // Branding Policy.
kriedel 0:21c0c97a5d7c 82 //--------------------------------------------------------------------------
kriedel 0:21c0c97a5d7c 83 */
kriedel 0:21c0c97a5d7c 84
kriedel 0:21c0c97a5d7c 85 #include "OneWireCRC.h"
kriedel 0:21c0c97a5d7c 86 #include "OneWireDefs.h"
kriedel 0:21c0c97a5d7c 87
kriedel 0:21c0c97a5d7c 88 // recommended data sheet timings in micro seconds
kriedel 0:21c0c97a5d7c 89 const int standardT[] = {6, 64, 60, 10, 9, 55, 0, 480, 70, 410};
kriedel 0:21c0c97a5d7c 90 const int overdriveT[] = {1.5, 7.5, 7.5, 2.5, 0.75, 7, 2.5, 70, 8.5, 40};
kriedel 0:21c0c97a5d7c 91
kriedel 0:21c0c97a5d7c 92 OneWireCRC::OneWireCRC(PinName oneWire, eSpeed speed) : oneWirePort(oneWire)
kriedel 0:21c0c97a5d7c 93 {
kriedel 0:21c0c97a5d7c 94 if (STANDARD == speed) timing = standardT;
kriedel 0:21c0c97a5d7c 95 else timing = overdriveT; // overdrive
kriedel 0:21c0c97a5d7c 96
kriedel 0:21c0c97a5d7c 97 resetSearch(); // reset address search state
kriedel 0:21c0c97a5d7c 98 }
kriedel 0:21c0c97a5d7c 99
kriedel 0:21c0c97a5d7c 100 // Generate a 1-wire reset, return 1 if no presence detect was found,
kriedel 0:21c0c97a5d7c 101 // return 0 otherwise.
kriedel 0:21c0c97a5d7c 102 // (NOTE: does not handle alarm presence from DS2404/DS1994)
kriedel 0:21c0c97a5d7c 103 int OneWireCRC::reset()
kriedel 0:21c0c97a5d7c 104 {
kriedel 0:21c0c97a5d7c 105
kriedel 0:21c0c97a5d7c 106 BYTE result = 0; // sample presence pulse result
kriedel 0:21c0c97a5d7c 107
kriedel 0:21c0c97a5d7c 108 wait_us(timing[6]);
kriedel 0:21c0c97a5d7c 109 oneWirePort.output();
kriedel 0:21c0c97a5d7c 110 oneWirePort = 0;
kriedel 0:21c0c97a5d7c 111 wait_us(timing[7]);
kriedel 0:21c0c97a5d7c 112 oneWirePort.input();
kriedel 0:21c0c97a5d7c 113 wait_us(timing[8]);
kriedel 0:21c0c97a5d7c 114 result = !(oneWirePort & 0x01);
kriedel 0:21c0c97a5d7c 115 wait_us(timing[9]);
kriedel 0:21c0c97a5d7c 116
kriedel 0:21c0c97a5d7c 117 return result;
kriedel 0:21c0c97a5d7c 118 }
kriedel 0:21c0c97a5d7c 119
kriedel 0:21c0c97a5d7c 120 //
kriedel 0:21c0c97a5d7c 121 // Write a bit. Port and bit is used to cut lookup time and provide
kriedel 0:21c0c97a5d7c 122 // more certain timing.
kriedel 0:21c0c97a5d7c 123 //
kriedel 0:21c0c97a5d7c 124 void OneWireCRC::writeBit(int bit)
kriedel 0:21c0c97a5d7c 125 {
kriedel 0:21c0c97a5d7c 126 bit = bit & 0x01;
kriedel 0:21c0c97a5d7c 127
kriedel 0:21c0c97a5d7c 128 if (bit)
kriedel 0:21c0c97a5d7c 129 {
kriedel 0:21c0c97a5d7c 130 // Write '1' bit
kriedel 0:21c0c97a5d7c 131 oneWirePort.output();
kriedel 0:21c0c97a5d7c 132 oneWirePort = 0;
kriedel 0:21c0c97a5d7c 133 wait_us(timing[0]);
kriedel 0:21c0c97a5d7c 134 oneWirePort.input();
kriedel 0:21c0c97a5d7c 135 wait_us(timing[1]);
kriedel 0:21c0c97a5d7c 136 }
kriedel 0:21c0c97a5d7c 137 else
kriedel 0:21c0c97a5d7c 138 {
kriedel 0:21c0c97a5d7c 139 // Write '0' bit
kriedel 0:21c0c97a5d7c 140 oneWirePort.output();
kriedel 0:21c0c97a5d7c 141 oneWirePort = 0;
kriedel 0:21c0c97a5d7c 142 wait_us(timing[2]);
kriedel 0:21c0c97a5d7c 143 oneWirePort.input();
kriedel 0:21c0c97a5d7c 144 wait_us(timing[3]);
kriedel 0:21c0c97a5d7c 145 }
kriedel 0:21c0c97a5d7c 146 }
kriedel 0:21c0c97a5d7c 147
kriedel 0:21c0c97a5d7c 148 //
kriedel 0:21c0c97a5d7c 149 // Read a bit. Port and bit is used to cut lookup time and provide
kriedel 0:21c0c97a5d7c 150 // more certain timing.
kriedel 0:21c0c97a5d7c 151 //
kriedel 0:21c0c97a5d7c 152 int OneWireCRC::readBit()
kriedel 0:21c0c97a5d7c 153 {
kriedel 0:21c0c97a5d7c 154 BYTE result;
kriedel 0:21c0c97a5d7c 155
kriedel 0:21c0c97a5d7c 156 oneWirePort.output();
kriedel 0:21c0c97a5d7c 157 oneWirePort = 0;
kriedel 0:21c0c97a5d7c 158 wait_us(timing[0]);
kriedel 0:21c0c97a5d7c 159 oneWirePort.input();
kriedel 0:21c0c97a5d7c 160 wait_us(timing[4]);
kriedel 0:21c0c97a5d7c 161 result = oneWirePort & 0x01;
kriedel 0:21c0c97a5d7c 162 wait_us(timing[5]);
kriedel 0:21c0c97a5d7c 163
kriedel 0:21c0c97a5d7c 164 return result;
kriedel 0:21c0c97a5d7c 165 }
kriedel 0:21c0c97a5d7c 166
kriedel 0:21c0c97a5d7c 167 //
kriedel 0:21c0c97a5d7c 168 // Write a byte. The writing code uses the active drivers to raise the
kriedel 0:21c0c97a5d7c 169 // pin high, if you need power after the write (e.g. DS18S20 in
kriedel 0:21c0c97a5d7c 170 // parasite power mode) then set 'power' to 1, otherwise the pin will
kriedel 0:21c0c97a5d7c 171 // go tri-state at the end of the write to avoid heating in a short or
kriedel 0:21c0c97a5d7c 172 // other mishap.
kriedel 0:21c0c97a5d7c 173 //
kriedel 0:21c0c97a5d7c 174 void OneWireCRC::writeByte(int data)
kriedel 0:21c0c97a5d7c 175 {
kriedel 0:21c0c97a5d7c 176 // Loop to write each bit in the byte, LS-bit first
kriedel 0:21c0c97a5d7c 177 for (int loop = 0; loop < 8; loop++)
kriedel 0:21c0c97a5d7c 178 {
kriedel 0:21c0c97a5d7c 179 writeBit(data & 0x01);
kriedel 0:21c0c97a5d7c 180
kriedel 0:21c0c97a5d7c 181 // shift the data byte for the next bit
kriedel 0:21c0c97a5d7c 182 data >>= 1;
kriedel 0:21c0c97a5d7c 183 }
kriedel 0:21c0c97a5d7c 184 }
kriedel 0:21c0c97a5d7c 185
kriedel 0:21c0c97a5d7c 186 //
kriedel 0:21c0c97a5d7c 187 // Read a byte
kriedel 0:21c0c97a5d7c 188 //
kriedel 0:21c0c97a5d7c 189 int OneWireCRC::readByte()
kriedel 0:21c0c97a5d7c 190 {
kriedel 0:21c0c97a5d7c 191 int result = 0;
kriedel 0:21c0c97a5d7c 192
kriedel 0:21c0c97a5d7c 193 for (int loop = 0; loop < 8; loop++)
kriedel 0:21c0c97a5d7c 194 {
kriedel 0:21c0c97a5d7c 195 // shift the result to get it ready for the next bit
kriedel 0:21c0c97a5d7c 196 result >>= 1;
kriedel 0:21c0c97a5d7c 197
kriedel 0:21c0c97a5d7c 198 // if result is one, then set MS bit
kriedel 0:21c0c97a5d7c 199 if (readBit()) result |= 0x80;
kriedel 0:21c0c97a5d7c 200 }
kriedel 0:21c0c97a5d7c 201
kriedel 0:21c0c97a5d7c 202 return result;
kriedel 0:21c0c97a5d7c 203 }
kriedel 0:21c0c97a5d7c 204
kriedel 0:21c0c97a5d7c 205 int OneWireCRC::touchByte(int data)
kriedel 0:21c0c97a5d7c 206 {
kriedel 0:21c0c97a5d7c 207 int result = 0;
kriedel 0:21c0c97a5d7c 208
kriedel 0:21c0c97a5d7c 209 for (int loop = 0; loop < 8; loop++)
kriedel 0:21c0c97a5d7c 210 {
kriedel 0:21c0c97a5d7c 211 // shift the result to get it ready for the next bit
kriedel 0:21c0c97a5d7c 212 result >>= 1;
kriedel 0:21c0c97a5d7c 213
kriedel 0:21c0c97a5d7c 214 // If sending a '1' then read a bit else write a '0'
kriedel 0:21c0c97a5d7c 215 if (data & 0x01)
kriedel 0:21c0c97a5d7c 216 {
kriedel 0:21c0c97a5d7c 217 if (readBit()) result |= 0x80;
kriedel 0:21c0c97a5d7c 218 }
kriedel 0:21c0c97a5d7c 219 else writeBit(0);
kriedel 0:21c0c97a5d7c 220
kriedel 0:21c0c97a5d7c 221 // shift the data byte for the next bit
kriedel 0:21c0c97a5d7c 222 data >>= 1;
kriedel 0:21c0c97a5d7c 223 }
kriedel 0:21c0c97a5d7c 224
kriedel 0:21c0c97a5d7c 225 return result;
kriedel 0:21c0c97a5d7c 226 }
kriedel 0:21c0c97a5d7c 227
kriedel 0:21c0c97a5d7c 228 void OneWireCRC::block(BYTE* data, int data_len)
kriedel 0:21c0c97a5d7c 229 {
kriedel 0:21c0c97a5d7c 230 for (int loop = 0; loop < data_len; loop++)
kriedel 0:21c0c97a5d7c 231 {
kriedel 0:21c0c97a5d7c 232 data[loop] = touchByte(data[loop]);
kriedel 0:21c0c97a5d7c 233 }
kriedel 0:21c0c97a5d7c 234 }
kriedel 0:21c0c97a5d7c 235
kriedel 0:21c0c97a5d7c 236 int OneWireCRC::overdriveSkip(BYTE* data, int data_len)
kriedel 0:21c0c97a5d7c 237 {
kriedel 0:21c0c97a5d7c 238 // set the speed to 'standard'
kriedel 0:21c0c97a5d7c 239 timing = standardT;
kriedel 0:21c0c97a5d7c 240
kriedel 0:21c0c97a5d7c 241 // reset all devices
kriedel 0:21c0c97a5d7c 242 if (reset()) return 0; // if no devices found
kriedel 0:21c0c97a5d7c 243
kriedel 0:21c0c97a5d7c 244 // overdrive skip command
kriedel 0:21c0c97a5d7c 245 writeByte(OVERDRIVE_SKIP);
kriedel 0:21c0c97a5d7c 246
kriedel 0:21c0c97a5d7c 247 // set the speed to 'overdrive'
kriedel 0:21c0c97a5d7c 248 timing = overdriveT;
kriedel 0:21c0c97a5d7c 249
kriedel 0:21c0c97a5d7c 250 // do a 1-Wire reset in 'overdrive' and return presence result
kriedel 0:21c0c97a5d7c 251 return reset();
kriedel 0:21c0c97a5d7c 252 }
kriedel 0:21c0c97a5d7c 253
kriedel 0:21c0c97a5d7c 254 //
kriedel 0:21c0c97a5d7c 255 // Do a ROM select
kriedel 0:21c0c97a5d7c 256 //
kriedel 0:21c0c97a5d7c 257 void OneWireCRC::matchROM(BYTE rom[8])
kriedel 0:21c0c97a5d7c 258 {
kriedel 0:21c0c97a5d7c 259 writeByte(MATCH_ROM); // Choose ROM
kriedel 0:21c0c97a5d7c 260
kriedel 0:21c0c97a5d7c 261 for(int i = 0; i < 8; i++) writeByte(rom[i]);
kriedel 0:21c0c97a5d7c 262 }
kriedel 0:21c0c97a5d7c 263
kriedel 0:21c0c97a5d7c 264 //
kriedel 0:21c0c97a5d7c 265 // Do a ROM skip
kriedel 0:21c0c97a5d7c 266 //
kriedel 0:21c0c97a5d7c 267 void OneWireCRC::skipROM()
kriedel 0:21c0c97a5d7c 268 {
kriedel 0:21c0c97a5d7c 269 writeByte(SKIP_ROM); // Skip ROM
kriedel 0:21c0c97a5d7c 270 }
kriedel 0:21c0c97a5d7c 271
kriedel 0:21c0c97a5d7c 272 //
kriedel 0:21c0c97a5d7c 273 // You need to use this function to start a search again from the beginning.
kriedel 0:21c0c97a5d7c 274 // You do not need to do it for the first search, though you could.
kriedel 0:21c0c97a5d7c 275 //
kriedel 0:21c0c97a5d7c 276 void OneWireCRC::resetSearch()
kriedel 0:21c0c97a5d7c 277 {
kriedel 0:21c0c97a5d7c 278 searchJunction = -1;
kriedel 0:21c0c97a5d7c 279 searchExhausted = false;
kriedel 0:21c0c97a5d7c 280 for (int i = 0; i < 8; i++)
kriedel 0:21c0c97a5d7c 281 {
kriedel 0:21c0c97a5d7c 282 address[i] = 0;
kriedel 0:21c0c97a5d7c 283 }
kriedel 0:21c0c97a5d7c 284 }
kriedel 0:21c0c97a5d7c 285
kriedel 0:21c0c97a5d7c 286 //
kriedel 0:21c0c97a5d7c 287 // Perform a search. If this function returns a '1' then it has
kriedel 0:21c0c97a5d7c 288 // enumerated the next device and you may retrieve the ROM from the
kriedel 0:21c0c97a5d7c 289 // OneWire::address variable. If there are no devices, no further
kriedel 0:21c0c97a5d7c 290 // devices, or something horrible happens in the middle of the
kriedel 0:21c0c97a5d7c 291 // enumeration then a 0 is returned. If a new device is found then
kriedel 0:21c0c97a5d7c 292 // its address is copied to newAddr. Use OneWire::reset_search() to
kriedel 0:21c0c97a5d7c 293 // start over.
kriedel 0:21c0c97a5d7c 294 //
kriedel 0:21c0c97a5d7c 295 BYTE OneWireCRC::search(BYTE* newAddr)
kriedel 0:21c0c97a5d7c 296 {
kriedel 0:21c0c97a5d7c 297 BYTE i;
kriedel 0:21c0c97a5d7c 298 int lastJunction = -1;
kriedel 0:21c0c97a5d7c 299 BYTE done = 1;
kriedel 0:21c0c97a5d7c 300
kriedel 0:21c0c97a5d7c 301 if (searchExhausted) return 0;
kriedel 0:21c0c97a5d7c 302
kriedel 0:21c0c97a5d7c 303 if (!reset()) return 0;
kriedel 0:21c0c97a5d7c 304
kriedel 0:21c0c97a5d7c 305 writeByte(SEARCH_ROM);
kriedel 0:21c0c97a5d7c 306
kriedel 0:21c0c97a5d7c 307 for(i = 0; i < 64; i++)
kriedel 0:21c0c97a5d7c 308 {
kriedel 0:21c0c97a5d7c 309 BYTE a = readBit( );
kriedel 0:21c0c97a5d7c 310 BYTE nota = readBit( );
kriedel 0:21c0c97a5d7c 311 BYTE ibyte = i/8;
kriedel 0:21c0c97a5d7c 312 BYTE ibit = 1 << (i & 7);
kriedel 0:21c0c97a5d7c 313
kriedel 0:21c0c97a5d7c 314 // I don't think this should happen, this means nothing responded, but maybe if
kriedel 0:21c0c97a5d7c 315 // something vanishes during the search it will come up.
kriedel 0:21c0c97a5d7c 316 if (a && nota) return 0;
kriedel 0:21c0c97a5d7c 317
kriedel 0:21c0c97a5d7c 318 if (!a && !nota)
kriedel 0:21c0c97a5d7c 319 {
kriedel 0:21c0c97a5d7c 320 if (i == searchJunction)
kriedel 0:21c0c97a5d7c 321 {
kriedel 0:21c0c97a5d7c 322 // this is our time to decide differently, we went zero last time, go one.
kriedel 0:21c0c97a5d7c 323 a = 1;
kriedel 0:21c0c97a5d7c 324 searchJunction = lastJunction;
kriedel 0:21c0c97a5d7c 325 }
kriedel 0:21c0c97a5d7c 326 else if (i < searchJunction)
kriedel 0:21c0c97a5d7c 327 {
kriedel 0:21c0c97a5d7c 328 // take whatever we took last time, look in address
kriedel 0:21c0c97a5d7c 329 if (address[ibyte] & ibit) a = 1;
kriedel 0:21c0c97a5d7c 330 else
kriedel 0:21c0c97a5d7c 331 {
kriedel 0:21c0c97a5d7c 332 // Only 0s count as pending junctions, we've already exhasuted the 0 side of 1s
kriedel 0:21c0c97a5d7c 333 a = 0;
kriedel 0:21c0c97a5d7c 334 done = 0;
kriedel 0:21c0c97a5d7c 335 lastJunction = i;
kriedel 0:21c0c97a5d7c 336 }
kriedel 0:21c0c97a5d7c 337 }
kriedel 0:21c0c97a5d7c 338 else
kriedel 0:21c0c97a5d7c 339 {
kriedel 0:21c0c97a5d7c 340 // we are blazing new tree, take the 0
kriedel 0:21c0c97a5d7c 341 a = 0;
kriedel 0:21c0c97a5d7c 342 searchJunction = i;
kriedel 0:21c0c97a5d7c 343 done = 0;
kriedel 0:21c0c97a5d7c 344 }
kriedel 0:21c0c97a5d7c 345 lastJunction = i;
kriedel 0:21c0c97a5d7c 346 }
kriedel 0:21c0c97a5d7c 347
kriedel 0:21c0c97a5d7c 348 if (a) address[ibyte] |= ibit;
kriedel 0:21c0c97a5d7c 349 else address[ibyte] &= ~ibit;
kriedel 0:21c0c97a5d7c 350
kriedel 0:21c0c97a5d7c 351 writeBit(a);
kriedel 0:21c0c97a5d7c 352 }
kriedel 0:21c0c97a5d7c 353
kriedel 0:21c0c97a5d7c 354 if (done) searchExhausted = true;
kriedel 0:21c0c97a5d7c 355
kriedel 0:21c0c97a5d7c 356 for (i = 0; i < 8; i++) newAddr[i] = address[i];
kriedel 0:21c0c97a5d7c 357
kriedel 0:21c0c97a5d7c 358 return 1;
kriedel 0:21c0c97a5d7c 359 }
kriedel 0:21c0c97a5d7c 360
kriedel 0:21c0c97a5d7c 361 // The 1-Wire CRC scheme is described in Maxim Application Note 27:
kriedel 0:21c0c97a5d7c 362 // "Understanding and Using Cyclic Redundancy Checks with Maxim iButton Products"
kriedel 0:21c0c97a5d7c 363 //
kriedel 0:21c0c97a5d7c 364
kriedel 0:21c0c97a5d7c 365 #if ONEWIRE_CRC8_TABLE
kriedel 0:21c0c97a5d7c 366 // This table comes from Dallas sample code where it is freely reusable,
kriedel 0:21c0c97a5d7c 367 // though Copyright (C) 2000 Dallas Semiconductor Corporation
kriedel 0:21c0c97a5d7c 368 static BYTE dscrc_table[] =
kriedel 0:21c0c97a5d7c 369 {
kriedel 0:21c0c97a5d7c 370 0, 94,188,226, 97, 63,221,131,194,156,126, 32,163,253, 31, 65,
kriedel 0:21c0c97a5d7c 371 157,195, 33,127,252,162, 64, 30, 95, 1,227,189, 62, 96,130,220,
kriedel 0:21c0c97a5d7c 372 35,125,159,193, 66, 28,254,160,225,191, 93, 3,128,222, 60, 98,
kriedel 0:21c0c97a5d7c 373 190,224, 2, 92,223,129, 99, 61,124, 34,192,158, 29, 67,161,255,
kriedel 0:21c0c97a5d7c 374 70, 24,250,164, 39,121,155,197,132,218, 56,102,229,187, 89, 7,
kriedel 0:21c0c97a5d7c 375 219,133,103, 57,186,228, 6, 88, 25, 71,165,251,120, 38,196,154,
kriedel 0:21c0c97a5d7c 376 101, 59,217,135, 4, 90,184,230,167,249, 27, 69,198,152,122, 36,
kriedel 0:21c0c97a5d7c 377 248,166, 68, 26,153,199, 37,123, 58,100,134,216, 91, 5,231,185,
kriedel 0:21c0c97a5d7c 378 140,210, 48,110,237,179, 81, 15, 78, 16,242,172, 47,113,147,205,
kriedel 0:21c0c97a5d7c 379 17, 79,173,243,112, 46,204,146,211,141,111, 49,178,236, 14, 80,
kriedel 0:21c0c97a5d7c 380 175,241, 19, 77,206,144,114, 44,109, 51,209,143, 12, 82,176,238,
kriedel 0:21c0c97a5d7c 381 50,108,142,208, 83, 13,239,177,240,174, 76, 18,145,207, 45,115,
kriedel 0:21c0c97a5d7c 382 202,148,118, 40,171,245, 23, 73, 8, 86,180,234,105, 55,213,139,
kriedel 0:21c0c97a5d7c 383 87, 9,235,181, 54,104,138,212,149,203, 41,119,244,170, 72, 22,
kriedel 0:21c0c97a5d7c 384 233,183, 85, 11,136,214, 52,106, 43,117,151,201, 74, 20,246,168,
kriedel 0:21c0c97a5d7c 385 116, 42,200,150, 21, 75,169,247,182,232, 10, 84,215,137,107, 53};
kriedel 0:21c0c97a5d7c 386
kriedel 0:21c0c97a5d7c 387 //
kriedel 0:21c0c97a5d7c 388 // Compute a Dallas Semiconductor 8 bit CRC. These show up in the ROM
kriedel 0:21c0c97a5d7c 389 // and the registers. (note: this might better be done without the
kriedel 0:21c0c97a5d7c 390 // table, it would probably be smaller and certainly fast enough
kriedel 0:21c0c97a5d7c 391 // compared to all those delayMicrosecond() calls. But I got
kriedel 0:21c0c97a5d7c 392 // confused, so I use this table from the examples.)
kriedel 0:21c0c97a5d7c 393 //
kriedel 0:21c0c97a5d7c 394 BYTE OneWireCRC::crc8(BYTE* addr, BYTE len)
kriedel 0:21c0c97a5d7c 395 {
kriedel 0:21c0c97a5d7c 396 BYTE i;
kriedel 0:21c0c97a5d7c 397 BYTE crc = 0;
kriedel 0:21c0c97a5d7c 398
kriedel 0:21c0c97a5d7c 399 for (i = 0; i < len; i++)
kriedel 0:21c0c97a5d7c 400 {
kriedel 0:21c0c97a5d7c 401 crc = dscrc_table[crc ^ addr[i] ];
kriedel 0:21c0c97a5d7c 402 }
kriedel 0:21c0c97a5d7c 403
kriedel 0:21c0c97a5d7c 404 return crc;
kriedel 0:21c0c97a5d7c 405 }
kriedel 0:21c0c97a5d7c 406 #else
kriedel 0:21c0c97a5d7c 407 //
kriedel 0:21c0c97a5d7c 408 // Compute a Dallas Semiconductor 8 bit CRC directly.
kriedel 0:21c0c97a5d7c 409 //
kriedel 0:21c0c97a5d7c 410 BYTE OneWireCRC::crc8(BYTE* addr, BYTE len)
kriedel 0:21c0c97a5d7c 411 {
kriedel 0:21c0c97a5d7c 412 BYTE i, j;
kriedel 0:21c0c97a5d7c 413 BYTE crc = 0;
kriedel 0:21c0c97a5d7c 414
kriedel 0:21c0c97a5d7c 415 for (i = 0; i < len; i++)
kriedel 0:21c0c97a5d7c 416 {
kriedel 0:21c0c97a5d7c 417 BYTE inbyte = addr[i];
kriedel 0:21c0c97a5d7c 418 for (j = 0; j < 8; j++)
kriedel 0:21c0c97a5d7c 419 {
kriedel 0:21c0c97a5d7c 420 BYTE mix = (crc ^ inbyte) & 0x01;
kriedel 0:21c0c97a5d7c 421 crc >>= 1;
kriedel 0:21c0c97a5d7c 422 if (mix) crc ^= 0x8C;
kriedel 0:21c0c97a5d7c 423 inbyte >>= 1;
kriedel 0:21c0c97a5d7c 424 }
kriedel 0:21c0c97a5d7c 425 }
kriedel 0:21c0c97a5d7c 426
kriedel 0:21c0c97a5d7c 427 return crc;
kriedel 0:21c0c97a5d7c 428 }
kriedel 0:21c0c97a5d7c 429 #endif
kriedel 0:21c0c97a5d7c 430
kriedel 0:21c0c97a5d7c 431 static short oddparity[16] = { 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0 };
kriedel 0:21c0c97a5d7c 432
kriedel 0:21c0c97a5d7c 433 //
kriedel 0:21c0c97a5d7c 434 // Compute a Dallas Semiconductor 16 bit CRC. I have never seen one of
kriedel 0:21c0c97a5d7c 435 // these, but here it is.
kriedel 0:21c0c97a5d7c 436 //
kriedel 0:21c0c97a5d7c 437 unsigned short OneWireCRC::crc16(unsigned short* data, unsigned short len)
kriedel 0:21c0c97a5d7c 438 {
kriedel 0:21c0c97a5d7c 439 unsigned short i;
kriedel 0:21c0c97a5d7c 440 unsigned short crc = 0;
kriedel 0:21c0c97a5d7c 441
kriedel 0:21c0c97a5d7c 442 for ( i = 0; i < len; i++)
kriedel 0:21c0c97a5d7c 443 {
kriedel 0:21c0c97a5d7c 444 unsigned short cdata = data[len];
kriedel 0:21c0c97a5d7c 445
kriedel 0:21c0c97a5d7c 446 cdata = (cdata ^ (crc & 0xff)) & 0xff;
kriedel 0:21c0c97a5d7c 447 crc >>= 8;
kriedel 0:21c0c97a5d7c 448
kriedel 0:21c0c97a5d7c 449 if (oddparity[cdata & 0xf] ^ oddparity[cdata >> 4]) crc ^= 0xc001;
kriedel 0:21c0c97a5d7c 450
kriedel 0:21c0c97a5d7c 451 cdata <<= 6;
kriedel 0:21c0c97a5d7c 452 crc ^= cdata;
kriedel 0:21c0c97a5d7c 453 cdata <<= 1;
kriedel 0:21c0c97a5d7c 454 crc ^= cdata;
kriedel 0:21c0c97a5d7c 455 }
kriedel 0:21c0c97a5d7c 456
kriedel 0:21c0c97a5d7c 457 return crc;
kriedel 0:21c0c97a5d7c 458 }
kriedel 0:21c0c97a5d7c 459