This is the device firmware for the controlBoard in the DIY 3D Printable Raspberry Pi Raman Spectrometer. For more information please visit: http://hackaday.io/project/1279

Dependencies:   mbed

Committer:
flatcat
Date:
Fri Aug 15 10:38:50 2014 +0000
Revision:
0:14942e263231
http://hackaday.io/project/1279

Who changed what in which revision?

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