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
DS18B20Multiple/OneWireCRC.h@0:14942e263231, 2014-08-15 (annotated)
- 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?
User | Revision | Line number | New 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 | //--------------------------------------------------------------------------- |
flatcat | 0:14942e263231 | 60 | // Copyright (C) 2000 Dallas Semiconductor Corporation, All Rights Reserved. |
flatcat | 0:14942e263231 | 61 | // |
flatcat | 0:14942e263231 | 62 | // Permission is hereby granted, free of charge, to any person obtaining a |
flatcat | 0:14942e263231 | 63 | // copy of this software and associated documentation files (the "Software"), |
flatcat | 0:14942e263231 | 64 | // to deal in the Software without restriction, including without limitation |
flatcat | 0:14942e263231 | 65 | // the rights to use, copy, modify, merge, publish, distribute, sublicense, |
flatcat | 0:14942e263231 | 66 | // and/or sell copies of the Software, and to permit persons to whom the |
flatcat | 0:14942e263231 | 67 | // Software is furnished to do so, subject to the following conditions: |
flatcat | 0:14942e263231 | 68 | // |
flatcat | 0:14942e263231 | 69 | // The above copyright notice and this permission notice shall be included |
flatcat | 0:14942e263231 | 70 | // in all copies or substantial portions of the Software. |
flatcat | 0:14942e263231 | 71 | // |
flatcat | 0:14942e263231 | 72 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
flatcat | 0:14942e263231 | 73 | // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
flatcat | 0:14942e263231 | 74 | // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
flatcat | 0:14942e263231 | 75 | // IN NO EVENT SHALL DALLAS SEMICONDUCTOR BE LIABLE FOR ANY CLAIM, DAMAGES |
flatcat | 0:14942e263231 | 76 | // OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
flatcat | 0:14942e263231 | 77 | // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
flatcat | 0:14942e263231 | 78 | // OTHER DEALINGS IN THE SOFTWARE. |
flatcat | 0:14942e263231 | 79 | // |
flatcat | 0:14942e263231 | 80 | // Except as contained in this notice, the name of Dallas Semiconductor |
flatcat | 0:14942e263231 | 81 | // shall not be used except as stated in the Dallas Semiconductor |
flatcat | 0:14942e263231 | 82 | // Branding Policy. |
flatcat | 0:14942e263231 | 83 | //-------------------------------------------------------------------------- |
flatcat | 0:14942e263231 | 84 | |
flatcat | 0:14942e263231 | 85 | #ifndef SNATCH59_ONEWIRECRC_H |
flatcat | 0:14942e263231 | 86 | #define SNATCH59_ONEWIRECRC_H |
flatcat | 0:14942e263231 | 87 | |
flatcat | 0:14942e263231 | 88 | #include <mbed.h> |
flatcat | 0:14942e263231 | 89 | |
flatcat | 0:14942e263231 | 90 | // Select the table-lookup method of computing the 8-bit CRC by setting this to 1 |
flatcat | 0:14942e263231 | 91 | #ifndef ONEWIRE_CRC8_TABLE |
flatcat | 0:14942e263231 | 92 | #define ONEWIRE_CRC8_TABLE 1 |
flatcat | 0:14942e263231 | 93 | #endif |
flatcat | 0:14942e263231 | 94 | |
flatcat | 0:14942e263231 | 95 | typedef unsigned char BYTE; // used to be uint8_t : something a byte wide, whatever .... |
flatcat | 0:14942e263231 | 96 | |
flatcat | 0:14942e263231 | 97 | enum eSpeed {OVERDRIVE, STANDARD}; |
flatcat | 0:14942e263231 | 98 | |
flatcat | 0:14942e263231 | 99 | class OneWireCRC |
flatcat | 0:14942e263231 | 100 | { |
flatcat | 0:14942e263231 | 101 | public: |
flatcat | 0:14942e263231 | 102 | OneWireCRC(PinName oneWire, eSpeed); |
flatcat | 0:14942e263231 | 103 | |
flatcat | 0:14942e263231 | 104 | // reset, read, write functions |
flatcat | 0:14942e263231 | 105 | int reset(); |
flatcat | 0:14942e263231 | 106 | void writeByte(int data); |
flatcat | 0:14942e263231 | 107 | int readByte(); |
flatcat | 0:14942e263231 | 108 | int touchByte(int data); |
flatcat | 0:14942e263231 | 109 | void block(BYTE* data, int data_len); |
flatcat | 0:14942e263231 | 110 | int overdriveSkip(BYTE* data, int data_len); |
flatcat | 0:14942e263231 | 111 | |
flatcat | 0:14942e263231 | 112 | // address functions |
flatcat | 0:14942e263231 | 113 | void matchROM(BYTE rom[8]); |
flatcat | 0:14942e263231 | 114 | void skipROM(); |
flatcat | 0:14942e263231 | 115 | |
flatcat | 0:14942e263231 | 116 | // address search functions |
flatcat | 0:14942e263231 | 117 | void resetSearch(); |
flatcat | 0:14942e263231 | 118 | BYTE search(BYTE* newAddr); |
flatcat | 0:14942e263231 | 119 | |
flatcat | 0:14942e263231 | 120 | // CRC check functions |
flatcat | 0:14942e263231 | 121 | static BYTE crc8(BYTE* addr, BYTE len); |
flatcat | 0:14942e263231 | 122 | static unsigned short crc16(unsigned short* data, unsigned short len); |
flatcat | 0:14942e263231 | 123 | |
flatcat | 0:14942e263231 | 124 | private: |
flatcat | 0:14942e263231 | 125 | const int* timing; |
flatcat | 0:14942e263231 | 126 | |
flatcat | 0:14942e263231 | 127 | BYTE address[8]; |
flatcat | 0:14942e263231 | 128 | int searchJunction; // so we can set to it -1 somewhere |
flatcat | 0:14942e263231 | 129 | bool searchExhausted; |
flatcat | 0:14942e263231 | 130 | |
flatcat | 0:14942e263231 | 131 | DigitalInOut oneWirePort; |
flatcat | 0:14942e263231 | 132 | |
flatcat | 0:14942e263231 | 133 | // read/write bit functions |
flatcat | 0:14942e263231 | 134 | void writeBit(int bit); |
flatcat | 0:14942e263231 | 135 | int readBit(); |
flatcat | 0:14942e263231 | 136 | }; |
flatcat | 0:14942e263231 | 137 | |
flatcat | 0:14942e263231 | 138 | #endif |