Dependents:   1W-EEPROM

Committer:
Wimpie
Date:
Tue Jan 17 08:30:45 2012 +0000
Revision:
2:193926923cb0
Parent:
1:7218c076189b
readbyte readword

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Wimpie 1:7218c076189b 1 /*
Wimpie 1:7218c076189b 2 * OneWireCRC. This is a port to mbed of Jim Studt's Adruino One Wire
Wimpie 1:7218c076189b 3 * library. Please see additional copyrights below this one, including
Wimpie 1:7218c076189b 4 * references to other copyrights.
Wimpie 1:7218c076189b 5 *
Wimpie 1:7218c076189b 6 * Copyright (C) <2011> Version 1.2 Wim De Roeve
Wimpie 1:7218c076189b 7 * <2009> Petras Saduikis <petras@petras.co.uk>
Wimpie 1:7218c076189b 8 *
Wimpie 1:7218c076189b 9 * This file is part of OneWireCRC.
Wimpie 1:7218c076189b 10 *
Wimpie 1:7218c076189b 11 * OneWireCRC is free software: you can redistribute it and/or modify
Wimpie 1:7218c076189b 12 * it under the terms of the GNU General Public License as published by
Wimpie 1:7218c076189b 13 * the Free Software Foundation, either version 3 of the License, or
Wimpie 1:7218c076189b 14 * (at your option) any later version.
Wimpie 1:7218c076189b 15 *
Wimpie 1:7218c076189b 16 * OneWireCRC is distributed in the hope that it will be useful,
Wimpie 1:7218c076189b 17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Wimpie 1:7218c076189b 18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Wimpie 1:7218c076189b 19 * GNU General Public License for more details.
Wimpie 1:7218c076189b 20 *
Wimpie 1:7218c076189b 21 * You should have received a copy of the GNU General Public License
Wimpie 1:7218c076189b 22 * along with OneWireCRC. If not, see <http://www.gnu.org/licenses/>.
Wimpie 1:7218c076189b 23 */
Wimpie 1:7218c076189b 24 /*
Wimpie 1:7218c076189b 25 Copyright (c) 2007, Jim Studt
Wimpie 1:7218c076189b 26
Wimpie 1:7218c076189b 27 Version 2.0: Modifications by Paul Stoffregen, January 2010:
Wimpie 1:7218c076189b 28 http://www.pjrc.com/teensy/td_libs_OneWire.html
Wimpie 1:7218c076189b 29 Search fix from Robin James
Wimpie 1:7218c076189b 30 http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1238032295/27#27
Wimpie 1:7218c076189b 31 Use direct optimized I/O in all cases
Wimpie 1:7218c076189b 32 Disable interrupts during timing critical sections
Wimpie 1:7218c076189b 33 (this solves many random communication errors)
Wimpie 1:7218c076189b 34 Disable interrupts during read-modify-write I/O
Wimpie 1:7218c076189b 35 Reduce RAM consumption by eliminating unnecessary
Wimpie 1:7218c076189b 36 variables and trimming many to 8 bits
Wimpie 1:7218c076189b 37 Optimize both crc8 - table version moved to flash
Wimpie 1:7218c076189b 38
Wimpie 1:7218c076189b 39 Modified to work with larger numbers of devices - avoids loop.
Wimpie 1:7218c076189b 40 Tested in Arduino 11 alpha with 12 sensors.
Wimpie 1:7218c076189b 41 26 Sept 2008 -- Robin James
Wimpie 1:7218c076189b 42 http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1238032295/27#27
Wimpie 1:7218c076189b 43
Wimpie 1:7218c076189b 44
Wimpie 1:7218c076189b 45 Updated to work with arduino-0008 and to include skip() as of
Wimpie 1:7218c076189b 46 2007/07/06. --RJL20
Wimpie 1:7218c076189b 47
Wimpie 1:7218c076189b 48 Modified to calculate the 8-bit CRC directly, avoiding the need for
Wimpie 1:7218c076189b 49 the 256-byte lookup table to be loaded in RAM. Tested in arduino-0010
Wimpie 1:7218c076189b 50 -- Tom Pollard, Jan 23, 2008
Wimpie 1:7218c076189b 51
Wimpie 1:7218c076189b 52 Permission is hereby granted, free of charge, to any person obtaining
Wimpie 1:7218c076189b 53 a copy of this software and associated documentation files (the
Wimpie 1:7218c076189b 54 "Software"), to deal in the Software without restriction, including
Wimpie 1:7218c076189b 55 without limitation the rights to use, copy, modify, merge, publish,
Wimpie 1:7218c076189b 56 distribute, sublicense, and/or sell copies of the Software, and to
Wimpie 1:7218c076189b 57 permit persons to whom the Software is furnished to do so, subject to
Wimpie 1:7218c076189b 58 the following conditions:
Wimpie 1:7218c076189b 59
Wimpie 1:7218c076189b 60 The above copyright notice and this permission notice shall be
Wimpie 1:7218c076189b 61 included in all copies or substantial portions of the Software.
Wimpie 1:7218c076189b 62
Wimpie 1:7218c076189b 63 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
Wimpie 1:7218c076189b 64 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
Wimpie 1:7218c076189b 65 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
Wimpie 1:7218c076189b 66 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
Wimpie 1:7218c076189b 67 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
Wimpie 1:7218c076189b 68 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
Wimpie 1:7218c076189b 69 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Wimpie 1:7218c076189b 70
Wimpie 1:7218c076189b 71 Much of the code was inspired by Derek Yerger's code, though I don't
Wimpie 1:7218c076189b 72 think much of that remains. In any event that was..
Wimpie 1:7218c076189b 73 (copyleft) 2006 by Derek Yerger - Free to distribute freely.
Wimpie 1:7218c076189b 74
Wimpie 1:7218c076189b 75 The CRC code was excerpted and inspired by the Dallas Semiconductor
Wimpie 1:7218c076189b 76 sample code bearing this copyright.
Wimpie 1:7218c076189b 77 */
Wimpie 1:7218c076189b 78 //---------------------------------------------------------------------------
Wimpie 1:7218c076189b 79 // Copyright (C) 2000 Dallas Semiconductor Corporation, All Rights Reserved.
Wimpie 1:7218c076189b 80 //
Wimpie 1:7218c076189b 81 // Permission is hereby granted, free of charge, to any person obtaining a
Wimpie 1:7218c076189b 82 // copy of this software and associated documentation files (the "Software"),
Wimpie 1:7218c076189b 83 // to deal in the Software without restriction, including without limitation
Wimpie 1:7218c076189b 84 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
Wimpie 1:7218c076189b 85 // and/or sell copies of the Software, and to permit persons to whom the
Wimpie 1:7218c076189b 86 // Software is furnished to do so, subject to the following conditions:
Wimpie 1:7218c076189b 87 //
Wimpie 1:7218c076189b 88 // The above copyright notice and this permission notice shall be included
Wimpie 1:7218c076189b 89 // in all copies or substantial portions of the Software.
Wimpie 1:7218c076189b 90 //
Wimpie 1:7218c076189b 91 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
Wimpie 1:7218c076189b 92 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
Wimpie 1:7218c076189b 93 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
Wimpie 1:7218c076189b 94 // IN NO EVENT SHALL DALLAS SEMICONDUCTOR BE LIABLE FOR ANY CLAIM, DAMAGES
Wimpie 1:7218c076189b 95 // OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
Wimpie 1:7218c076189b 96 // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
Wimpie 1:7218c076189b 97 // OTHER DEALINGS IN THE SOFTWARE.
Wimpie 1:7218c076189b 98 //
Wimpie 1:7218c076189b 99 // Except as contained in this notice, the name of Dallas Semiconductor
Wimpie 1:7218c076189b 100 // shall not be used except as stated in the Dallas Semiconductor
Wimpie 1:7218c076189b 101 // Branding Policy.
Wimpie 1:7218c076189b 102 //--------------------------------------------------------------------------
Wimpie 1:7218c076189b 103
Wimpie 1:7218c076189b 104 #ifndef SNATCH59_ONEWIRECRC_H
Wimpie 1:7218c076189b 105 #define SNATCH59_ONEWIRECRC_H
Wimpie 1:7218c076189b 106
Wimpie 1:7218c076189b 107 #include <mbed.h>
Wimpie 1:7218c076189b 108
Wimpie 1:7218c076189b 109 // Select the table-lookup method of computing the 8-bit CRC by setting this to 1
Wimpie 1:7218c076189b 110 #ifndef ONEWIRE_CRC8_TABLE
Wimpie 1:7218c076189b 111 #define ONEWIRE_CRC8_TABLE 1
Wimpie 1:7218c076189b 112 #endif
Wimpie 1:7218c076189b 113
Wimpie 1:7218c076189b 114 typedef unsigned char BYTE; // used to be uint8_t : something a byte wide, whatever ....
Wimpie 1:7218c076189b 115
Wimpie 1:7218c076189b 116 enum eSpeed {OVERDRIVE, STANDARD};
Wimpie 1:7218c076189b 117
Wimpie 1:7218c076189b 118 class OneWireCRC {
Wimpie 1:7218c076189b 119 public:
Wimpie 1:7218c076189b 120 OneWireCRC(PinName oneWire, eSpeed);
Wimpie 1:7218c076189b 121
Wimpie 1:7218c076189b 122 // reset, read, write functions
Wimpie 1:7218c076189b 123 int reset();
Wimpie 1:7218c076189b 124 void writeByte(int data);
Wimpie 1:7218c076189b 125 int readByte();
Wimpie 1:7218c076189b 126 int touchByte(int data);
Wimpie 1:7218c076189b 127 void block(BYTE* data, int data_len);
Wimpie 1:7218c076189b 128 int overdriveSkip(BYTE* data, int data_len);
Wimpie 1:7218c076189b 129
Wimpie 1:7218c076189b 130 // address functions
Wimpie 1:7218c076189b 131 void matchROM(BYTE rom[8]);
Wimpie 1:7218c076189b 132 void skipROM();
Wimpie 1:7218c076189b 133
Wimpie 1:7218c076189b 134 // address search functions
Wimpie 1:7218c076189b 135 void resetSearch();
Wimpie 1:7218c076189b 136 BYTE search(BYTE* newAddr);
Wimpie 1:7218c076189b 137
Wimpie 1:7218c076189b 138 // CRC check functions
Wimpie 1:7218c076189b 139 static BYTE crc8(BYTE* addr, BYTE len);
Wimpie 1:7218c076189b 140 static unsigned short crc16(unsigned short* data, unsigned short len);
Wimpie 1:7218c076189b 141
Wimpie 1:7218c076189b 142 BYTE ROMCode[8];
Wimpie 1:7218c076189b 143 private:
Wimpie 1:7218c076189b 144 const int* timing;
Wimpie 1:7218c076189b 145
Wimpie 1:7218c076189b 146 /*int searchJunction; // so we can set to it -1 somewhere
Wimpie 1:7218c076189b 147 bool searchExhausted;
Wimpie 1:7218c076189b 148 BYTE _address[8];*/
Wimpie 1:7218c076189b 149
Wimpie 1:7218c076189b 150 BYTE ROM_NO[8];
Wimpie 1:7218c076189b 151 uint8_t LastDiscrepancy;
Wimpie 1:7218c076189b 152 uint8_t LastFamilyDiscrepancy;
Wimpie 1:7218c076189b 153 uint8_t LastDeviceFlag;
Wimpie 1:7218c076189b 154
Wimpie 1:7218c076189b 155
Wimpie 1:7218c076189b 156 DigitalInOut oneWirePort;
Wimpie 1:7218c076189b 157
Wimpie 1:7218c076189b 158 // read/write bit functions
Wimpie 1:7218c076189b 159 void writeBit(int bit);
Wimpie 1:7218c076189b 160 int readBit();
Wimpie 1:7218c076189b 161 };
Wimpie 1:7218c076189b 162
Wimpie 1:7218c076189b 163 #endif