A copy of very incomplete program for forum

Dependencies:   mbed SDFileSystem

Committer:
roselea
Date:
Sat Mar 17 14:34:23 2012 +0000
Revision:
1:d1c29c7b7ab3
Parent:
0:bfcb5b67b1d6

        

Who changed what in which revision?

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