First Publish

Dependencies:   BridgeDriver2 FrontPanelButtons MAX31855 MCP23017 SDFileSystem TextLCD mbed

Committer:
mehatfie
Date:
Mon Nov 10 22:59:49 2014 +0000
Revision:
1:9954bf6d7d25
Parent:
0:20e78c9d2ea9
First Commit

Who changed what in which revision?

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