Custom

Dependencies:   mbed

Fork of OneWireCRC by Petras Saduikis

Committer:
fmanzano_dtk
Date:
Tue Oct 04 16:53:47 2016 +0000
Revision:
1:6af898f6c5e8
Parent:
0:01a6a40578c9
Owned;

Who changed what in which revision?

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