Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of OneWire by
OneWire.h@6:28e60d774095, 2017-09-09 (annotated)
- Committer:
- mooreaa
- Date:
- Sat Sep 09 09:10:49 2017 +0000
- Revision:
- 6:28e60d774095
- Parent:
- 0:acf75feb0947
initial not working
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hudakz | 0:acf75feb0947 | 1 | #ifndef OneWire_h |
hudakz | 0:acf75feb0947 | 2 | #define OneWire_h |
hudakz | 0:acf75feb0947 | 3 | |
hudakz | 0:acf75feb0947 | 4 | #include <inttypes.h> |
hudakz | 0:acf75feb0947 | 5 | #include <mbed.h> |
hudakz | 0:acf75feb0947 | 6 | |
hudakz | 0:acf75feb0947 | 7 | // You can exclude certain features from OneWire. In theory, this |
hudakz | 0:acf75feb0947 | 8 | // might save some space. In practice, the compiler automatically |
hudakz | 0:acf75feb0947 | 9 | // removes unused code (technically, the linker, using -fdata-sections |
hudakz | 0:acf75feb0947 | 10 | // and -ffunction-sections when compiling, and Wl,--gc-sections |
hudakz | 0:acf75feb0947 | 11 | // when linking), so most of these will not result in any code size |
hudakz | 0:acf75feb0947 | 12 | // reduction. Well, unless you try to use the missing features |
hudakz | 0:acf75feb0947 | 13 | // and redesign your program to not need them! ONEWIRE_CRC8_TABLE |
hudakz | 0:acf75feb0947 | 14 | // is the exception, because it selects a fast but large algorithm |
hudakz | 0:acf75feb0947 | 15 | // or a small but slow algorithm. |
hudakz | 0:acf75feb0947 | 16 | |
hudakz | 0:acf75feb0947 | 17 | // you can exclude onewire_search by defining that to 0 |
hudakz | 0:acf75feb0947 | 18 | #ifndef ONEWIRE_SEARCH |
hudakz | 0:acf75feb0947 | 19 | #define ONEWIRE_SEARCH 1 |
hudakz | 0:acf75feb0947 | 20 | #endif |
hudakz | 0:acf75feb0947 | 21 | |
hudakz | 0:acf75feb0947 | 22 | // You can exclude CRC checks altogether by defining this to 0 |
hudakz | 0:acf75feb0947 | 23 | #ifndef ONEWIRE_CRC |
hudakz | 0:acf75feb0947 | 24 | #define ONEWIRE_CRC 1 |
hudakz | 0:acf75feb0947 | 25 | #endif |
hudakz | 0:acf75feb0947 | 26 | |
hudakz | 0:acf75feb0947 | 27 | class OneWire |
hudakz | 0:acf75feb0947 | 28 | { |
hudakz | 0:acf75feb0947 | 29 | private: |
hudakz | 0:acf75feb0947 | 30 | DigitalInOut wire; |
hudakz | 0:acf75feb0947 | 31 | |
hudakz | 0:acf75feb0947 | 32 | #if ONEWIRE_SEARCH |
hudakz | 0:acf75feb0947 | 33 | // global search state |
hudakz | 0:acf75feb0947 | 34 | unsigned char ROM_NO[8]; |
hudakz | 0:acf75feb0947 | 35 | uint8_t LastDiscrepancy; |
hudakz | 0:acf75feb0947 | 36 | uint8_t LastFamilyDiscrepancy; |
hudakz | 0:acf75feb0947 | 37 | uint8_t LastDeviceFlag; |
hudakz | 0:acf75feb0947 | 38 | #endif |
hudakz | 0:acf75feb0947 | 39 | |
hudakz | 0:acf75feb0947 | 40 | public: |
mooreaa | 6:28e60d774095 | 41 | |
mooreaa | 6:28e60d774095 | 42 | enum ONEWIRE_RESULT |
mooreaa | 6:28e60d774095 | 43 | { |
mooreaa | 6:28e60d774095 | 44 | ONEWIRE_OK = 0, |
mooreaa | 6:28e60d774095 | 45 | ONEWIRE_NO_RESPONSE, |
mooreaa | 6:28e60d774095 | 46 | ONEWIRE_ERROR, |
mooreaa | 6:28e60d774095 | 47 | ONEWIRE_ERROR_SHORT_CIRCUIT |
mooreaa | 6:28e60d774095 | 48 | }; |
mooreaa | 6:28e60d774095 | 49 | |
hudakz | 0:acf75feb0947 | 50 | OneWire(PinName pin); |
mooreaa | 6:28e60d774095 | 51 | |
mooreaa | 6:28e60d774095 | 52 | |
mooreaa | 6:28e60d774095 | 53 | |
mooreaa | 6:28e60d774095 | 54 | // Perform a 1-Wire reset cycle. Returns ONEWIRE_OK if a device responds |
mooreaa | 6:28e60d774095 | 55 | // with a presence pulse. Returns ONEWIRE_NO_RESPONCE if there is no device or the |
mooreaa | 6:28e60d774095 | 56 | // bus is shorted or otherwise held low for more than 250uS |
mooreaa | 6:28e60d774095 | 57 | |
mooreaa | 6:28e60d774095 | 58 | uint8_t init_sequence(); |
hudakz | 0:acf75feb0947 | 59 | |
mooreaa | 6:28e60d774095 | 60 | uint8_t reset( bool force = true ); |
mooreaa | 6:28e60d774095 | 61 | |
mooreaa | 6:28e60d774095 | 62 | |
mooreaa | 6:28e60d774095 | 63 | |
hudakz | 0:acf75feb0947 | 64 | |
hudakz | 0:acf75feb0947 | 65 | // Issue a 1-Wire rom select command, you do the reset first. |
hudakz | 0:acf75feb0947 | 66 | void select(const uint8_t rom[8]); |
hudakz | 0:acf75feb0947 | 67 | |
hudakz | 0:acf75feb0947 | 68 | // Issue a 1-Wire rom skip command, to address all on bus. |
hudakz | 0:acf75feb0947 | 69 | void skip(void); |
hudakz | 0:acf75feb0947 | 70 | |
hudakz | 0:acf75feb0947 | 71 | // Write a byte. If 'power' is one then the wire is held high at |
hudakz | 0:acf75feb0947 | 72 | // the end for parasitically powered devices. You are responsible |
hudakz | 0:acf75feb0947 | 73 | // for eventually depowering it by calling depower() or doing |
hudakz | 0:acf75feb0947 | 74 | // another read or write. |
hudakz | 0:acf75feb0947 | 75 | void write(uint8_t v, uint8_t power = 0); |
hudakz | 0:acf75feb0947 | 76 | |
hudakz | 0:acf75feb0947 | 77 | void write_bytes(const uint8_t *buf, uint16_t count, bool power = 0); |
hudakz | 0:acf75feb0947 | 78 | |
hudakz | 0:acf75feb0947 | 79 | // Read a byte. |
hudakz | 0:acf75feb0947 | 80 | uint8_t read(void); |
hudakz | 0:acf75feb0947 | 81 | |
hudakz | 0:acf75feb0947 | 82 | void read_bytes(uint8_t *buf, uint16_t count); |
hudakz | 0:acf75feb0947 | 83 | |
hudakz | 0:acf75feb0947 | 84 | // Write a bit. The bus is always left powered at the end, see |
hudakz | 0:acf75feb0947 | 85 | // note in write() about that. |
hudakz | 0:acf75feb0947 | 86 | void write_bit(uint8_t v); |
hudakz | 0:acf75feb0947 | 87 | |
hudakz | 0:acf75feb0947 | 88 | // Read a bit. |
hudakz | 0:acf75feb0947 | 89 | uint8_t read_bit(void); |
hudakz | 0:acf75feb0947 | 90 | |
hudakz | 0:acf75feb0947 | 91 | // Stop forcing power onto the bus. You only need to do this if |
hudakz | 0:acf75feb0947 | 92 | // you used the 'power' flag to write() or used a write_bit() call |
hudakz | 0:acf75feb0947 | 93 | // and aren't about to do another read or write. You would rather |
hudakz | 0:acf75feb0947 | 94 | // not leave this powered if you don't have to, just in case |
hudakz | 0:acf75feb0947 | 95 | // someone shorts your bus. |
hudakz | 0:acf75feb0947 | 96 | void depower(void); |
hudakz | 0:acf75feb0947 | 97 | |
hudakz | 0:acf75feb0947 | 98 | #if ONEWIRE_SEARCH |
hudakz | 0:acf75feb0947 | 99 | // Clear the search state so that if will start from the beginning again. |
hudakz | 0:acf75feb0947 | 100 | void reset_search(); |
hudakz | 0:acf75feb0947 | 101 | |
hudakz | 0:acf75feb0947 | 102 | // Setup the search to find the device type 'family_code' on the next call |
hudakz | 0:acf75feb0947 | 103 | // to search(*newAddr) if it is present. |
hudakz | 0:acf75feb0947 | 104 | void target_search(uint8_t family_code); |
hudakz | 0:acf75feb0947 | 105 | |
hudakz | 0:acf75feb0947 | 106 | // Look for the next device. Returns 1 if a new address has been |
hudakz | 0:acf75feb0947 | 107 | // returned. A zero might mean that the bus is shorted, there are |
hudakz | 0:acf75feb0947 | 108 | // no devices, or you have already retrieved all of them. It |
hudakz | 0:acf75feb0947 | 109 | // might be a good idea to check the CRC to make sure you didn't |
hudakz | 0:acf75feb0947 | 110 | // get garbage. The order is deterministic. You will always get |
hudakz | 0:acf75feb0947 | 111 | // the same devices in the same order. |
hudakz | 0:acf75feb0947 | 112 | uint8_t search(uint8_t *newAddr); |
hudakz | 0:acf75feb0947 | 113 | #endif |
hudakz | 0:acf75feb0947 | 114 | |
hudakz | 0:acf75feb0947 | 115 | #if ONEWIRE_CRC |
hudakz | 0:acf75feb0947 | 116 | // Compute a Dallas Semiconductor 8 bit CRC, these are used in the |
hudakz | 0:acf75feb0947 | 117 | // ROM and scratchpad registers. |
hudakz | 0:acf75feb0947 | 118 | static uint8_t crc8(const uint8_t *addr, uint8_t len); |
hudakz | 0:acf75feb0947 | 119 | |
hudakz | 0:acf75feb0947 | 120 | #if ONEWIRE_CRC16 |
hudakz | 0:acf75feb0947 | 121 | // Compute the 1-Wire CRC16 and compare it against the received CRC. |
hudakz | 0:acf75feb0947 | 122 | // Example usage (reading a DS2408): |
hudakz | 0:acf75feb0947 | 123 | // // Put everything in a buffer so we can compute the CRC easily. |
hudakz | 0:acf75feb0947 | 124 | // uint8_t buf[13]; |
hudakz | 0:acf75feb0947 | 125 | // buf[0] = 0xF0; // Read PIO Registers |
hudakz | 0:acf75feb0947 | 126 | // buf[1] = 0x88; // LSB address |
hudakz | 0:acf75feb0947 | 127 | // buf[2] = 0x00; // MSB address |
hudakz | 0:acf75feb0947 | 128 | // WriteBytes(net, buf, 3); // Write 3 cmd bytes |
hudakz | 0:acf75feb0947 | 129 | // ReadBytes(net, buf+3, 10); // Read 6 data bytes, 2 0xFF, 2 CRC16 |
hudakz | 0:acf75feb0947 | 130 | // if (!CheckCRC16(buf, 11, &buf[11])) { |
hudakz | 0:acf75feb0947 | 131 | // // Handle error. |
hudakz | 0:acf75feb0947 | 132 | // } |
hudakz | 0:acf75feb0947 | 133 | // |
hudakz | 0:acf75feb0947 | 134 | // @param input - Array of bytes to checksum. |
hudakz | 0:acf75feb0947 | 135 | // @param len - How many bytes to use. |
hudakz | 0:acf75feb0947 | 136 | // @param inverted_crc - The two CRC16 bytes in the received data. |
hudakz | 0:acf75feb0947 | 137 | // This should just point into the received data, |
hudakz | 0:acf75feb0947 | 138 | // *not* at a 16-bit integer. |
hudakz | 0:acf75feb0947 | 139 | // @param crc - The crc starting value (optional) |
hudakz | 0:acf75feb0947 | 140 | // @return True, iff the CRC matches. |
hudakz | 0:acf75feb0947 | 141 | static bool check_crc16(const uint8_t* input, uint16_t len, const uint8_t* inverted_crc, uint16_t crc = 0); |
hudakz | 0:acf75feb0947 | 142 | |
hudakz | 0:acf75feb0947 | 143 | // Compute a Dallas Semiconductor 16 bit CRC. This is required to check |
hudakz | 0:acf75feb0947 | 144 | // the integrity of data received from many 1-Wire devices. Note that the |
hudakz | 0:acf75feb0947 | 145 | // CRC computed here is *not* what you'll get from the 1-Wire network, |
hudakz | 0:acf75feb0947 | 146 | // for two reasons: |
hudakz | 0:acf75feb0947 | 147 | // 1) The CRC is transmitted bitwise inverted. |
hudakz | 0:acf75feb0947 | 148 | // 2) Depending on the endian-ness of your processor, the binary |
hudakz | 0:acf75feb0947 | 149 | // representation of the two-byte return value may have a different |
hudakz | 0:acf75feb0947 | 150 | // byte order than the two bytes you get from 1-Wire. |
hudakz | 0:acf75feb0947 | 151 | // @param input - Array of bytes to checksum. |
hudakz | 0:acf75feb0947 | 152 | // @param len - How many bytes to use. |
hudakz | 0:acf75feb0947 | 153 | // @param crc - The crc starting value (optional) |
hudakz | 0:acf75feb0947 | 154 | // @return The CRC16, as defined by Dallas Semiconductor. |
hudakz | 0:acf75feb0947 | 155 | static uint16_t crc16(const uint8_t* input, uint16_t len, uint16_t crc = 0); |
hudakz | 0:acf75feb0947 | 156 | #endif |
hudakz | 0:acf75feb0947 | 157 | #endif |
hudakz | 0:acf75feb0947 | 158 | }; |
hudakz | 0:acf75feb0947 | 159 | |
hudakz | 0:acf75feb0947 | 160 | #endif |