Library for Programmable Resolution 1-Wire Digital ThermometerDS18B20
OneWireCRC.h@0:21c0c97a5d7c, 2011-02-03 (annotated)
- Committer:
- kriedel
- Date:
- Thu Feb 03 12:10:20 2011 +0000
- Revision:
- 0:21c0c97a5d7c
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kriedel | 0:21c0c97a5d7c | 1 | /* |
kriedel | 0:21c0c97a5d7c | 2 | * OneWireCRC. This is a port to mbed of Jim Studt's Adruino One Wire |
kriedel | 0:21c0c97a5d7c | 3 | * library. Please see additional copyrights below this one, including |
kriedel | 0:21c0c97a5d7c | 4 | * references to other copyrights. |
kriedel | 0:21c0c97a5d7c | 5 | * |
kriedel | 0:21c0c97a5d7c | 6 | * Copyright (C) <2009> Petras Saduikis <petras@petras.co.uk> |
kriedel | 0:21c0c97a5d7c | 7 | * |
kriedel | 0:21c0c97a5d7c | 8 | * This file is part of OneWireCRC. |
kriedel | 0:21c0c97a5d7c | 9 | * |
kriedel | 0:21c0c97a5d7c | 10 | * OneWireCRC is free software: you can redistribute it and/or modify |
kriedel | 0:21c0c97a5d7c | 11 | * it under the terms of the GNU General Public License as published by |
kriedel | 0:21c0c97a5d7c | 12 | * the Free Software Foundation, either version 3 of the License, or |
kriedel | 0:21c0c97a5d7c | 13 | * (at your option) any later version. |
kriedel | 0:21c0c97a5d7c | 14 | * |
kriedel | 0:21c0c97a5d7c | 15 | * OneWireCRC is distributed in the hope that it will be useful, |
kriedel | 0:21c0c97a5d7c | 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
kriedel | 0:21c0c97a5d7c | 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
kriedel | 0:21c0c97a5d7c | 18 | * GNU General Public License for more details. |
kriedel | 0:21c0c97a5d7c | 19 | * |
kriedel | 0:21c0c97a5d7c | 20 | * You should have received a copy of the GNU General Public License |
kriedel | 0:21c0c97a5d7c | 21 | * along with OneWireCRC. If not, see <http://www.gnu.org/licenses/>. |
kriedel | 0:21c0c97a5d7c | 22 | */ |
kriedel | 0:21c0c97a5d7c | 23 | /* |
kriedel | 0:21c0c97a5d7c | 24 | Copyright (c) 2007, Jim Studt |
kriedel | 0:21c0c97a5d7c | 25 | |
kriedel | 0:21c0c97a5d7c | 26 | Updated to work with arduino-0008 and to include skip() as of |
kriedel | 0:21c0c97a5d7c | 27 | 2007/07/06. --RJL20 |
kriedel | 0:21c0c97a5d7c | 28 | |
kriedel | 0:21c0c97a5d7c | 29 | Modified to calculate the 8-bit CRC directly, avoiding the need for |
kriedel | 0:21c0c97a5d7c | 30 | the 256-byte lookup table to be loaded in RAM. Tested in arduino-0010 |
kriedel | 0:21c0c97a5d7c | 31 | -- Tom Pollard, Jan 23, 2008 |
kriedel | 0:21c0c97a5d7c | 32 | |
kriedel | 0:21c0c97a5d7c | 33 | Permission is hereby granted, free of charge, to any person obtaining |
kriedel | 0:21c0c97a5d7c | 34 | a copy of this software and associated documentation files (the |
kriedel | 0:21c0c97a5d7c | 35 | "Software"), to deal in the Software without restriction, including |
kriedel | 0:21c0c97a5d7c | 36 | without limitation the rights to use, copy, modify, merge, publish, |
kriedel | 0:21c0c97a5d7c | 37 | distribute, sublicense, and/or sell copies of the Software, and to |
kriedel | 0:21c0c97a5d7c | 38 | permit persons to whom the Software is furnished to do so, subject to |
kriedel | 0:21c0c97a5d7c | 39 | the following conditions: |
kriedel | 0:21c0c97a5d7c | 40 | |
kriedel | 0:21c0c97a5d7c | 41 | The above copyright notice and this permission notice shall be |
kriedel | 0:21c0c97a5d7c | 42 | included in all copies or substantial portions of the Software. |
kriedel | 0:21c0c97a5d7c | 43 | |
kriedel | 0:21c0c97a5d7c | 44 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
kriedel | 0:21c0c97a5d7c | 45 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
kriedel | 0:21c0c97a5d7c | 46 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
kriedel | 0:21c0c97a5d7c | 47 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
kriedel | 0:21c0c97a5d7c | 48 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
kriedel | 0:21c0c97a5d7c | 49 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
kriedel | 0:21c0c97a5d7c | 50 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
kriedel | 0:21c0c97a5d7c | 51 | |
kriedel | 0:21c0c97a5d7c | 52 | Much of the code was inspired by Derek Yerger's code, though I don't |
kriedel | 0:21c0c97a5d7c | 53 | think much of that remains. In any event that was.. |
kriedel | 0:21c0c97a5d7c | 54 | (copyleft) 2006 by Derek Yerger - Free to distribute freely. |
kriedel | 0:21c0c97a5d7c | 55 | |
kriedel | 0:21c0c97a5d7c | 56 | The CRC code was excerpted and inspired by the Dallas Semiconductor |
kriedel | 0:21c0c97a5d7c | 57 | sample code bearing this copyright. |
kriedel | 0:21c0c97a5d7c | 58 | */ |
kriedel | 0:21c0c97a5d7c | 59 | //--------------------------------------------------------------------------- |
kriedel | 0:21c0c97a5d7c | 60 | // Copyright (C) 2000 Dallas Semiconductor Corporation, All Rights Reserved. |
kriedel | 0:21c0c97a5d7c | 61 | // |
kriedel | 0:21c0c97a5d7c | 62 | // Permission is hereby granted, free of charge, to any person obtaining a |
kriedel | 0:21c0c97a5d7c | 63 | // copy of this software and associated documentation files (the "Software"), |
kriedel | 0:21c0c97a5d7c | 64 | // to deal in the Software without restriction, including without limitation |
kriedel | 0:21c0c97a5d7c | 65 | // the rights to use, copy, modify, merge, publish, distribute, sublicense, |
kriedel | 0:21c0c97a5d7c | 66 | // and/or sell copies of the Software, and to permit persons to whom the |
kriedel | 0:21c0c97a5d7c | 67 | // Software is furnished to do so, subject to the following conditions: |
kriedel | 0:21c0c97a5d7c | 68 | // |
kriedel | 0:21c0c97a5d7c | 69 | // The above copyright notice and this permission notice shall be included |
kriedel | 0:21c0c97a5d7c | 70 | // in all copies or substantial portions of the Software. |
kriedel | 0:21c0c97a5d7c | 71 | // |
kriedel | 0:21c0c97a5d7c | 72 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
kriedel | 0:21c0c97a5d7c | 73 | // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
kriedel | 0:21c0c97a5d7c | 74 | // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
kriedel | 0:21c0c97a5d7c | 75 | // IN NO EVENT SHALL DALLAS SEMICONDUCTOR BE LIABLE FOR ANY CLAIM, DAMAGES |
kriedel | 0:21c0c97a5d7c | 76 | // OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
kriedel | 0:21c0c97a5d7c | 77 | // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
kriedel | 0:21c0c97a5d7c | 78 | // OTHER DEALINGS IN THE SOFTWARE. |
kriedel | 0:21c0c97a5d7c | 79 | // |
kriedel | 0:21c0c97a5d7c | 80 | // Except as contained in this notice, the name of Dallas Semiconductor |
kriedel | 0:21c0c97a5d7c | 81 | // shall not be used except as stated in the Dallas Semiconductor |
kriedel | 0:21c0c97a5d7c | 82 | // Branding Policy. |
kriedel | 0:21c0c97a5d7c | 83 | //-------------------------------------------------------------------------- |
kriedel | 0:21c0c97a5d7c | 84 | |
kriedel | 0:21c0c97a5d7c | 85 | #ifndef SNATCH59_ONEWIRECRC_H |
kriedel | 0:21c0c97a5d7c | 86 | #define SNATCH59_ONEWIRECRC_H |
kriedel | 0:21c0c97a5d7c | 87 | |
kriedel | 0:21c0c97a5d7c | 88 | #include <mbed.h> |
kriedel | 0:21c0c97a5d7c | 89 | |
kriedel | 0:21c0c97a5d7c | 90 | // Select the table-lookup method of computing the 8-bit CRC by setting this to 1 |
kriedel | 0:21c0c97a5d7c | 91 | #ifndef ONEWIRE_CRC8_TABLE |
kriedel | 0:21c0c97a5d7c | 92 | #define ONEWIRE_CRC8_TABLE 1 |
kriedel | 0:21c0c97a5d7c | 93 | #endif |
kriedel | 0:21c0c97a5d7c | 94 | |
kriedel | 0:21c0c97a5d7c | 95 | typedef unsigned char BYTE; // used to be uint8_t : something a byte wide, whatever .... |
kriedel | 0:21c0c97a5d7c | 96 | |
kriedel | 0:21c0c97a5d7c | 97 | enum eSpeed {OVERDRIVE, STANDARD}; |
kriedel | 0:21c0c97a5d7c | 98 | |
kriedel | 0:21c0c97a5d7c | 99 | class OneWireCRC |
kriedel | 0:21c0c97a5d7c | 100 | { |
kriedel | 0:21c0c97a5d7c | 101 | public: |
kriedel | 0:21c0c97a5d7c | 102 | OneWireCRC(PinName oneWire, eSpeed); |
kriedel | 0:21c0c97a5d7c | 103 | |
kriedel | 0:21c0c97a5d7c | 104 | // reset, read, write functions |
kriedel | 0:21c0c97a5d7c | 105 | int reset(); |
kriedel | 0:21c0c97a5d7c | 106 | void writeByte(int data); |
kriedel | 0:21c0c97a5d7c | 107 | int readByte(); |
kriedel | 0:21c0c97a5d7c | 108 | int touchByte(int data); |
kriedel | 0:21c0c97a5d7c | 109 | void block(BYTE* data, int data_len); |
kriedel | 0:21c0c97a5d7c | 110 | int overdriveSkip(BYTE* data, int data_len); |
kriedel | 0:21c0c97a5d7c | 111 | |
kriedel | 0:21c0c97a5d7c | 112 | // address functions |
kriedel | 0:21c0c97a5d7c | 113 | void matchROM(BYTE rom[8]); |
kriedel | 0:21c0c97a5d7c | 114 | void skipROM(); |
kriedel | 0:21c0c97a5d7c | 115 | |
kriedel | 0:21c0c97a5d7c | 116 | // address search functions |
kriedel | 0:21c0c97a5d7c | 117 | void resetSearch(); |
kriedel | 0:21c0c97a5d7c | 118 | BYTE search(BYTE* newAddr); |
kriedel | 0:21c0c97a5d7c | 119 | |
kriedel | 0:21c0c97a5d7c | 120 | // CRC check functions |
kriedel | 0:21c0c97a5d7c | 121 | static BYTE crc8(BYTE* addr, BYTE len); |
kriedel | 0:21c0c97a5d7c | 122 | static unsigned short crc16(unsigned short* data, unsigned short len); |
kriedel | 0:21c0c97a5d7c | 123 | |
kriedel | 0:21c0c97a5d7c | 124 | private: |
kriedel | 0:21c0c97a5d7c | 125 | const int* timing; |
kriedel | 0:21c0c97a5d7c | 126 | |
kriedel | 0:21c0c97a5d7c | 127 | BYTE address[8]; |
kriedel | 0:21c0c97a5d7c | 128 | int searchJunction; // so we can set to it -1 somewhere |
kriedel | 0:21c0c97a5d7c | 129 | bool searchExhausted; |
kriedel | 0:21c0c97a5d7c | 130 | |
kriedel | 0:21c0c97a5d7c | 131 | DigitalInOut oneWirePort; |
kriedel | 0:21c0c97a5d7c | 132 | |
kriedel | 0:21c0c97a5d7c | 133 | // read/write bit functions |
kriedel | 0:21c0c97a5d7c | 134 | void writeBit(int bit); |
kriedel | 0:21c0c97a5d7c | 135 | int readBit(); |
kriedel | 0:21c0c97a5d7c | 136 | }; |
kriedel | 0:21c0c97a5d7c | 137 | |
kriedel | 0:21c0c97a5d7c | 138 | #endif |