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