zzz
OneWire.cpp@11:bc8ed7280966, 2019-01-27 (annotated)
- Committer:
- hudakz
- Date:
- Sun Jan 27 18:01:19 2019 +0000
- Revision:
- 11:bc8ed7280966
- Parent:
- 10:c89b9ad6097c
- Child:
- 12:27a1b359b95c
Modified.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hudakz | 0:acf75feb0947 | 1 | /* |
hudakz | 0:acf75feb0947 | 2 | Copyright (c) 2007, Jim Studt (original old version - many contributors since) |
hudakz | 0:acf75feb0947 | 3 | |
hudakz | 0:acf75feb0947 | 4 | The latest version of this library may be found at: |
hudakz | 9:4af0015b0f47 | 5 | http://www.pjrc.com/teensy/td_libs_Onehtml |
hudakz | 0:acf75feb0947 | 6 | |
hudakz | 0:acf75feb0947 | 7 | OneWire has been maintained by Paul Stoffregen (paul@pjrc.com) since |
hudakz | 0:acf75feb0947 | 8 | January 2010. At the time, it was in need of many bug fixes, but had |
hudakz | 0:acf75feb0947 | 9 | been abandoned the original author (Jim Studt). None of the known |
hudakz | 9:4af0015b0f47 | 10 | contributors were interested in maintaining One Paul typically |
hudakz | 0:acf75feb0947 | 11 | works on OneWire every 6 to 12 months. Patches usually wait that |
hudakz | 0:acf75feb0947 | 12 | long. If anyone is interested in more actively maintaining OneWire, |
hudakz | 0:acf75feb0947 | 13 | please contact Paul. |
hudakz | 0:acf75feb0947 | 14 | |
hudakz | 0:acf75feb0947 | 15 | Version 2.2: |
hudakz | 0:acf75feb0947 | 16 | Teensy 3.0 compatibility, Paul Stoffregen, paul@pjrc.com |
hudakz | 0:acf75feb0947 | 17 | Arduino Due compatibility, http://arduino.cc/forum/index.php?topic=141030 |
hudakz | 0:acf75feb0947 | 18 | Fix DS18B20 example negative temperature |
hudakz | 0:acf75feb0947 | 19 | Fix DS18B20 example's low res modes, Ken Butcher |
hudakz | 0:acf75feb0947 | 20 | Improve reset timing, Mark Tillotson |
hudakz | 0:acf75feb0947 | 21 | Add const qualifiers, Bertrik Sikken |
hudakz | 0:acf75feb0947 | 22 | Add initial value input to crc16, Bertrik Sikken |
hudakz | 0:acf75feb0947 | 23 | Add target_search() function, Scott Roberts |
hudakz | 0:acf75feb0947 | 24 | |
hudakz | 0:acf75feb0947 | 25 | Version 2.1: |
hudakz | 0:acf75feb0947 | 26 | Arduino 1.0 compatibility, Paul Stoffregen |
hudakz | 0:acf75feb0947 | 27 | Improve temperature example, Paul Stoffregen |
hudakz | 0:acf75feb0947 | 28 | DS250x_PROM example, Guillermo Lovato |
hudakz | 0:acf75feb0947 | 29 | PIC32 (chipKit) compatibility, Jason Dangel, dangel.jason AT gmail.com |
hudakz | 0:acf75feb0947 | 30 | Improvements from Glenn Trewitt: |
hudakz | 0:acf75feb0947 | 31 | - crc16() now works |
hudakz | 0:acf75feb0947 | 32 | - check_crc16() does all of calculation/checking work. |
hudakz | 0:acf75feb0947 | 33 | - Added read_bytes() and write_bytes(), to reduce tedious loops. |
hudakz | 0:acf75feb0947 | 34 | - Added ds2408 example. |
hudakz | 0:acf75feb0947 | 35 | Delete very old, out-of-date readme file (info is here) |
hudakz | 0:acf75feb0947 | 36 | |
hudakz | 0:acf75feb0947 | 37 | Version 2.0: Modifications by Paul Stoffregen, January 2010: |
hudakz | 9:4af0015b0f47 | 38 | http://www.pjrc.com/teensy/td_libs_Onehtml |
hudakz | 0:acf75feb0947 | 39 | Search fix from Robin James |
hudakz | 0:acf75feb0947 | 40 | http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1238032295/27#27 |
hudakz | 0:acf75feb0947 | 41 | Use direct optimized I/O in all cases |
hudakz | 0:acf75feb0947 | 42 | Disable interrupts during timing critical sections |
hudakz | 0:acf75feb0947 | 43 | (this solves many random communication errors) |
hudakz | 0:acf75feb0947 | 44 | Disable interrupts during read-modify-write I/O |
hudakz | 0:acf75feb0947 | 45 | Reduce RAM consumption by eliminating unnecessary |
hudakz | 0:acf75feb0947 | 46 | variables and trimming many to 8 bits |
hudakz | 0:acf75feb0947 | 47 | Optimize both crc8 - table version moved to flash |
hudakz | 0:acf75feb0947 | 48 | |
hudakz | 0:acf75feb0947 | 49 | Modified to work with larger numbers of devices - avoids loop. |
hudakz | 0:acf75feb0947 | 50 | Tested in Arduino 11 alpha with 12 sensors. |
hudakz | 0:acf75feb0947 | 51 | 26 Sept 2008 -- Robin James |
hudakz | 0:acf75feb0947 | 52 | http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1238032295/27#27 |
hudakz | 0:acf75feb0947 | 53 | |
hudakz | 0:acf75feb0947 | 54 | Updated to work with arduino-0008 and to include skip() as of |
hudakz | 0:acf75feb0947 | 55 | 2007/07/06. --RJL20 |
hudakz | 0:acf75feb0947 | 56 | |
hudakz | 0:acf75feb0947 | 57 | Modified to calculate the 8-bit CRC directly, avoiding the need for |
hudakz | 0:acf75feb0947 | 58 | the 256-byte lookup table to be loaded in RAM. Tested in arduino-0010 |
hudakz | 0:acf75feb0947 | 59 | -- Tom Pollard, Jan 23, 2008 |
hudakz | 0:acf75feb0947 | 60 | |
hudakz | 0:acf75feb0947 | 61 | Jim Studt's original library was modified by Josh Larios. |
hudakz | 0:acf75feb0947 | 62 | |
hudakz | 0:acf75feb0947 | 63 | Tom Pollard, pollard@alum.mit.edu, contributed around May 20, 2008 |
hudakz | 0:acf75feb0947 | 64 | |
hudakz | 0:acf75feb0947 | 65 | Permission is hereby granted, free of charge, to any person obtaining |
hudakz | 0:acf75feb0947 | 66 | a copy of this software and associated documentation files (the |
hudakz | 0:acf75feb0947 | 67 | "Software"), to deal in the Software without restriction, including |
hudakz | 0:acf75feb0947 | 68 | without limitation the rights to use, copy, modify, merge, publish, |
hudakz | 0:acf75feb0947 | 69 | distribute, sublicense, and/or sell copies of the Software, and to |
hudakz | 0:acf75feb0947 | 70 | permit persons to whom the Software is furnished to do so, subject to |
hudakz | 0:acf75feb0947 | 71 | the following conditions: |
hudakz | 0:acf75feb0947 | 72 | |
hudakz | 0:acf75feb0947 | 73 | The above copyright notice and this permission notice shall be |
hudakz | 0:acf75feb0947 | 74 | included in all copies or substantial portions of the Software. |
hudakz | 0:acf75feb0947 | 75 | |
hudakz | 0:acf75feb0947 | 76 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
hudakz | 0:acf75feb0947 | 77 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
hudakz | 0:acf75feb0947 | 78 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
hudakz | 0:acf75feb0947 | 79 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
hudakz | 0:acf75feb0947 | 80 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
hudakz | 0:acf75feb0947 | 81 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
hudakz | 0:acf75feb0947 | 82 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
hudakz | 0:acf75feb0947 | 83 | |
hudakz | 0:acf75feb0947 | 84 | Much of the code was inspired by Derek Yerger's code, though I don't |
hudakz | 0:acf75feb0947 | 85 | think much of that remains. In any event that was.. |
hudakz | 0:acf75feb0947 | 86 | (copyleft) 2006 by Derek Yerger - Free to distribute freely. |
hudakz | 0:acf75feb0947 | 87 | |
hudakz | 0:acf75feb0947 | 88 | The CRC code was excerpted and inspired by the Dallas Semiconductor |
hudakz | 0:acf75feb0947 | 89 | sample code bearing this copyright. |
hudakz | 0:acf75feb0947 | 90 | //--------------------------------------------------------------------------- |
hudakz | 0:acf75feb0947 | 91 | // Copyright (C) 2000 Dallas Semiconductor Corporation, All Rights Reserved. |
hudakz | 0:acf75feb0947 | 92 | // |
hudakz | 0:acf75feb0947 | 93 | // Permission is hereby granted, free of charge, to any person obtaining a |
hudakz | 0:acf75feb0947 | 94 | // copy of this software and associated documentation files (the "Software"), |
hudakz | 0:acf75feb0947 | 95 | // to deal in the Software without restriction, including without limitation |
hudakz | 0:acf75feb0947 | 96 | // the rights to use, copy, modify, merge, publish, distribute, sublicense, |
hudakz | 0:acf75feb0947 | 97 | // and/or sell copies of the Software, and to permit persons to whom the |
hudakz | 0:acf75feb0947 | 98 | // Software is furnished to do so, subject to the following conditions: |
hudakz | 0:acf75feb0947 | 99 | // |
hudakz | 0:acf75feb0947 | 100 | // The above copyright notice and this permission notice shall be included |
hudakz | 0:acf75feb0947 | 101 | // in all copies or substantial portions of the Software. |
hudakz | 0:acf75feb0947 | 102 | // |
hudakz | 0:acf75feb0947 | 103 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
hudakz | 0:acf75feb0947 | 104 | // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
hudakz | 0:acf75feb0947 | 105 | // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
hudakz | 0:acf75feb0947 | 106 | // IN NO EVENT SHALL DALLAS SEMICONDUCTOR BE LIABLE FOR ANY CLAIM, DAMAGES |
hudakz | 0:acf75feb0947 | 107 | // OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
hudakz | 0:acf75feb0947 | 108 | // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
hudakz | 0:acf75feb0947 | 109 | // OTHER DEALINGS IN THE SOFTWARE. |
hudakz | 0:acf75feb0947 | 110 | // |
hudakz | 0:acf75feb0947 | 111 | // Except as contained in this notice, the name of Dallas Semiconductor |
hudakz | 0:acf75feb0947 | 112 | // shall not be used except as stated in the Dallas Semiconductor |
hudakz | 0:acf75feb0947 | 113 | // Branding Policy. |
hudakz | 0:acf75feb0947 | 114 | //-------------------------------------------------------------------------- |
hudakz | 0:acf75feb0947 | 115 | */ |
hudakz | 0:acf75feb0947 | 116 | #include "OneWire.h" |
hudakz | 0:acf75feb0947 | 117 | |
hudakz | 8:87fbdaba5535 | 118 | /** |
hudakz | 10:c89b9ad6097c | 119 | * @brief Constructs a OneWire object. |
hudakz | 10:c89b9ad6097c | 120 | * @note GPIO is configured as output and an internal pull up resistor is connected. |
hudakz | 10:c89b9ad6097c | 121 | * But because for STM chips it takes very long time to change from output |
hudakz | 10:c89b9ad6097c | 122 | * to input an open drain mode is used rather and the GPIO remains output forever. |
hudakz | 8:87fbdaba5535 | 123 | * @param |
hudakz | 8:87fbdaba5535 | 124 | * @retval |
hudakz | 8:87fbdaba5535 | 125 | */ |
hudakz | 8:87fbdaba5535 | 126 | OneWire::OneWire(PinName pin) : |
hudakz | 9:4af0015b0f47 | 127 | DigitalInOut(pin) |
hudakz | 7:acf3f0ee66d2 | 128 | { |
hudakz | 11:bc8ed7280966 | 129 | MODE(); // set mode either PullUp or OpenDrain for STM |
hudakz | 0:acf75feb0947 | 130 | #if ONEWIRE_SEARCH |
hudakz | 0:acf75feb0947 | 131 | reset_search(); |
hudakz | 0:acf75feb0947 | 132 | #endif |
hudakz | 0:acf75feb0947 | 133 | } |
hudakz | 0:acf75feb0947 | 134 | |
hudakz | 10:c89b9ad6097c | 135 | /** |
hudakz | 10:c89b9ad6097c | 136 | * @brief Performs the onewire reset function. |
hudakz | 10:c89b9ad6097c | 137 | * @note We will wait up to 250uS for the bus to come high, |
hudakz | 10:c89b9ad6097c | 138 | * if it doesn't then it is broken or shorted and we return a 0; |
hudakz | 10:c89b9ad6097c | 139 | * @param |
hudakz | 10:c89b9ad6097c | 140 | * @retval 1 if a device asserted a presence pulse, 0 otherwise. |
hudakz | 10:c89b9ad6097c | 141 | */ |
hudakz | 0:acf75feb0947 | 142 | uint8_t OneWire::reset(void) |
hudakz | 0:acf75feb0947 | 143 | { |
hudakz | 8:87fbdaba5535 | 144 | uint8_t r; |
hudakz | 8:87fbdaba5535 | 145 | uint8_t retries = 125; |
hudakz | 0:acf75feb0947 | 146 | |
hudakz | 9:4af0015b0f47 | 147 | INPUT(); |
hudakz | 0:acf75feb0947 | 148 | // wait until the wire is high... just in case |
hudakz | 0:acf75feb0947 | 149 | do { |
hudakz | 9:4af0015b0f47 | 150 | if (--retries == 0) |
hudakz | 8:87fbdaba5535 | 151 | return 0; |
hudakz | 0:acf75feb0947 | 152 | wait_us(2); |
hudakz | 11:bc8ed7280966 | 153 | } while (READ() == 0); |
hudakz | 0:acf75feb0947 | 154 | |
hudakz | 9:4af0015b0f47 | 155 | OUTPUT(); |
hudakz | 9:4af0015b0f47 | 156 | WRITE(0); |
hudakz | 0:acf75feb0947 | 157 | wait_us(480); |
hudakz | 9:4af0015b0f47 | 158 | INPUT(); |
hudakz | 9:4af0015b0f47 | 159 | wait_us(65); |
hudakz | 9:4af0015b0f47 | 160 | r = !READ(); |
hudakz | 9:4af0015b0f47 | 161 | wait_us(420); |
hudakz | 0:acf75feb0947 | 162 | return r; |
hudakz | 0:acf75feb0947 | 163 | } |
hudakz | 0:acf75feb0947 | 164 | |
hudakz | 10:c89b9ad6097c | 165 | /** |
hudakz | 10:c89b9ad6097c | 166 | * @brief Writes a bit. |
hudakz | 10:c89b9ad6097c | 167 | * @note GPIO registers are used for STM chips to cut time. |
hudakz | 10:c89b9ad6097c | 168 | * @param |
hudakz | 10:c89b9ad6097c | 169 | * @retval |
hudakz | 10:c89b9ad6097c | 170 | */ |
hudakz | 0:acf75feb0947 | 171 | void OneWire::write_bit(uint8_t v) |
hudakz | 0:acf75feb0947 | 172 | { |
hudakz | 9:4af0015b0f47 | 173 | OUTPUT(); |
hudakz | 0:acf75feb0947 | 174 | if (v & 1) { |
hudakz | 9:4af0015b0f47 | 175 | WRITE(0); // drive output low |
hudakz | 9:4af0015b0f47 | 176 | wait_us(1); |
hudakz | 9:4af0015b0f47 | 177 | WRITE(1); // drive output high |
hudakz | 9:4af0015b0f47 | 178 | wait_us(60); |
hudakz | 8:87fbdaba5535 | 179 | } |
hudakz | 8:87fbdaba5535 | 180 | else { |
hudakz | 9:4af0015b0f47 | 181 | WRITE(0); // drive output low |
hudakz | 9:4af0015b0f47 | 182 | wait_us(60); |
hudakz | 9:4af0015b0f47 | 183 | WRITE(1); // drive output high |
hudakz | 9:4af0015b0f47 | 184 | wait_us(1); |
hudakz | 0:acf75feb0947 | 185 | } |
hudakz | 0:acf75feb0947 | 186 | } |
hudakz | 0:acf75feb0947 | 187 | |
hudakz | 10:c89b9ad6097c | 188 | /** |
hudakz | 10:c89b9ad6097c | 189 | * @brief Reads a bit. |
hudakz | 10:c89b9ad6097c | 190 | * @note GPIO registers are used for STM chips to cut time. |
hudakz | 10:c89b9ad6097c | 191 | * @param |
hudakz | 10:c89b9ad6097c | 192 | * @retval |
hudakz | 10:c89b9ad6097c | 193 | */ |
hudakz | 0:acf75feb0947 | 194 | uint8_t OneWire::read_bit(void) |
hudakz | 0:acf75feb0947 | 195 | { |
hudakz | 0:acf75feb0947 | 196 | uint8_t r; |
hudakz | 7:acf3f0ee66d2 | 197 | int t; |
hudakz | 0:acf75feb0947 | 198 | |
hudakz | 9:4af0015b0f47 | 199 | OUTPUT(); |
hudakz | 10:c89b9ad6097c | 200 | timer.start(); |
hudakz | 9:4af0015b0f47 | 201 | WRITE(0); |
hudakz | 9:4af0015b0f47 | 202 | INPUT(); |
hudakz | 7:acf3f0ee66d2 | 203 | t = timer.read_us(); |
hudakz | 8:87fbdaba5535 | 204 | if (t < 7) |
hudakz | 8:87fbdaba5535 | 205 | wait_us(7 - t); |
hudakz | 9:4af0015b0f47 | 206 | r = READ(); |
hudakz | 6:d6e084297fb9 | 207 | timer.stop(); |
hudakz | 6:d6e084297fb9 | 208 | timer.reset(); |
hudakz | 9:4af0015b0f47 | 209 | wait_us(55); |
hudakz | 0:acf75feb0947 | 210 | return r; |
hudakz | 0:acf75feb0947 | 211 | } |
hudakz | 0:acf75feb0947 | 212 | |
hudakz | 10:c89b9ad6097c | 213 | /** |
hudakz | 10:c89b9ad6097c | 214 | * @brief Writes a byte. |
hudakz | 10:c89b9ad6097c | 215 | * @note The writing code uses the active drivers to raise the |
hudakz | 10:c89b9ad6097c | 216 | pin high, if you need power after the write (e.g. DS18S20 in |
hudakz | 10:c89b9ad6097c | 217 | parasite power mode) then set 'power' to 1, otherwise the pin will |
hudakz | 10:c89b9ad6097c | 218 | go tri-state at the end of the write to avoid heating in a short or |
hudakz | 10:c89b9ad6097c | 219 | other mishap. |
hudakz | 10:c89b9ad6097c | 220 | * @param |
hudakz | 10:c89b9ad6097c | 221 | * @retval |
hudakz | 10:c89b9ad6097c | 222 | */ |
hudakz | 9:4af0015b0f47 | 223 | void OneWire::write_byte(uint8_t v, uint8_t power /* = 0 */ ) |
hudakz | 8:87fbdaba5535 | 224 | { |
hudakz | 0:acf75feb0947 | 225 | uint8_t bitMask; |
hudakz | 0:acf75feb0947 | 226 | |
hudakz | 9:4af0015b0f47 | 227 | for (bitMask = 0x01; bitMask; bitMask <<= 1) |
hudakz | 9:4af0015b0f47 | 228 | write_bit((bitMask & v) ? 1 : 0); |
hudakz | 8:87fbdaba5535 | 229 | if (!power) |
hudakz | 9:4af0015b0f47 | 230 | INPUT(); |
hudakz | 0:acf75feb0947 | 231 | } |
hudakz | 0:acf75feb0947 | 232 | |
hudakz | 8:87fbdaba5535 | 233 | /** |
hudakz | 10:c89b9ad6097c | 234 | * @brief Writes bytes. |
hudakz | 8:87fbdaba5535 | 235 | * @note |
hudakz | 8:87fbdaba5535 | 236 | * @param |
hudakz | 8:87fbdaba5535 | 237 | * @retval |
hudakz | 8:87fbdaba5535 | 238 | */ |
hudakz | 8:87fbdaba5535 | 239 | void OneWire::write_bytes(const uint8_t* buf, uint16_t count, bool power /* = 0 */ ) |
hudakz | 8:87fbdaba5535 | 240 | { |
hudakz | 8:87fbdaba5535 | 241 | for (uint16_t i = 0; i < count; i++) |
hudakz | 9:4af0015b0f47 | 242 | write_byte(buf[i]); |
hudakz | 8:87fbdaba5535 | 243 | if (!power) |
hudakz | 9:4af0015b0f47 | 244 | INPUT(); |
hudakz | 0:acf75feb0947 | 245 | } |
hudakz | 0:acf75feb0947 | 246 | |
hudakz | 10:c89b9ad6097c | 247 | /** |
hudakz | 10:c89b9ad6097c | 248 | * @brief Reads a byte. |
hudakz | 10:c89b9ad6097c | 249 | * @note |
hudakz | 10:c89b9ad6097c | 250 | * @param |
hudakz | 10:c89b9ad6097c | 251 | * @retval |
hudakz | 10:c89b9ad6097c | 252 | */ |
hudakz | 9:4af0015b0f47 | 253 | uint8_t OneWire::read_byte() |
hudakz | 8:87fbdaba5535 | 254 | { |
hudakz | 0:acf75feb0947 | 255 | uint8_t bitMask; |
hudakz | 0:acf75feb0947 | 256 | uint8_t r = 0; |
hudakz | 0:acf75feb0947 | 257 | |
hudakz | 0:acf75feb0947 | 258 | for (bitMask = 0x01; bitMask; bitMask <<= 1) { |
hudakz | 9:4af0015b0f47 | 259 | if (read_bit()) |
hudakz | 8:87fbdaba5535 | 260 | r |= bitMask; |
hudakz | 0:acf75feb0947 | 261 | } |
hudakz | 8:87fbdaba5535 | 262 | |
hudakz | 0:acf75feb0947 | 263 | return r; |
hudakz | 0:acf75feb0947 | 264 | } |
hudakz | 0:acf75feb0947 | 265 | |
hudakz | 8:87fbdaba5535 | 266 | /** |
hudakz | 10:c89b9ad6097c | 267 | * @brief Reads bytes. |
hudakz | 8:87fbdaba5535 | 268 | * @note |
hudakz | 8:87fbdaba5535 | 269 | * @param |
hudakz | 8:87fbdaba5535 | 270 | * @retval |
hudakz | 8:87fbdaba5535 | 271 | */ |
hudakz | 8:87fbdaba5535 | 272 | void OneWire::read_bytes(uint8_t* buf, uint16_t count) |
hudakz | 8:87fbdaba5535 | 273 | { |
hudakz | 8:87fbdaba5535 | 274 | for (uint16_t i = 0; i < count; i++) |
hudakz | 9:4af0015b0f47 | 275 | buf[i] = read_byte(); |
hudakz | 0:acf75feb0947 | 276 | } |
hudakz | 0:acf75feb0947 | 277 | |
hudakz | 10:c89b9ad6097c | 278 | /** |
hudakz | 10:c89b9ad6097c | 279 | * @brief Selects ROM. |
hudakz | 10:c89b9ad6097c | 280 | * @note |
hudakz | 10:c89b9ad6097c | 281 | * @param |
hudakz | 10:c89b9ad6097c | 282 | * @retval |
hudakz | 10:c89b9ad6097c | 283 | */ |
hudakz | 0:acf75feb0947 | 284 | void OneWire::select(const uint8_t rom[8]) |
hudakz | 0:acf75feb0947 | 285 | { |
hudakz | 0:acf75feb0947 | 286 | uint8_t i; |
hudakz | 0:acf75feb0947 | 287 | |
hudakz | 10:c89b9ad6097c | 288 | write_byte(0x55); // Choose ROM |
hudakz | 8:87fbdaba5535 | 289 | for (i = 0; i < 8; i++) |
hudakz | 9:4af0015b0f47 | 290 | write_byte(rom[i]); |
hudakz | 0:acf75feb0947 | 291 | } |
hudakz | 0:acf75feb0947 | 292 | |
hudakz | 10:c89b9ad6097c | 293 | /** |
hudakz | 10:c89b9ad6097c | 294 | * @brief Skips ROM select. |
hudakz | 10:c89b9ad6097c | 295 | * @note |
hudakz | 10:c89b9ad6097c | 296 | * @param |
hudakz | 10:c89b9ad6097c | 297 | * @retval |
hudakz | 10:c89b9ad6097c | 298 | */ |
hudakz | 0:acf75feb0947 | 299 | void OneWire::skip() |
hudakz | 0:acf75feb0947 | 300 | { |
hudakz | 10:c89b9ad6097c | 301 | write_byte(0xCC); // Skip ROM |
hudakz | 0:acf75feb0947 | 302 | } |
hudakz | 0:acf75feb0947 | 303 | |
hudakz | 8:87fbdaba5535 | 304 | /** |
hudakz | 10:c89b9ad6097c | 305 | * @brief Unpowers the chip. |
hudakz | 8:87fbdaba5535 | 306 | * @note |
hudakz | 8:87fbdaba5535 | 307 | * @param |
hudakz | 8:87fbdaba5535 | 308 | * @retval |
hudakz | 8:87fbdaba5535 | 309 | */ |
hudakz | 0:acf75feb0947 | 310 | void OneWire::depower() |
hudakz | 0:acf75feb0947 | 311 | { |
hudakz | 9:4af0015b0f47 | 312 | INPUT(); |
hudakz | 0:acf75feb0947 | 313 | } |
hudakz | 0:acf75feb0947 | 314 | |
hudakz | 0:acf75feb0947 | 315 | #if ONEWIRE_SEARCH |
hudakz | 8:87fbdaba5535 | 316 | // |
hudakz | 0:acf75feb0947 | 317 | |
hudakz | 10:c89b9ad6097c | 318 | /** |
hudakz | 10:c89b9ad6097c | 319 | * @brief Resets the search state. |
hudakz | 10:c89b9ad6097c | 320 | * @note We need to use this function to start a search again from the beginning. |
hudakz | 10:c89b9ad6097c | 321 | * We do not need to do it for the first search, though we could. |
hudakz | 10:c89b9ad6097c | 322 | * @param |
hudakz | 10:c89b9ad6097c | 323 | * @retval |
hudakz | 10:c89b9ad6097c | 324 | */ |
hudakz | 0:acf75feb0947 | 325 | void OneWire::reset_search() |
hudakz | 0:acf75feb0947 | 326 | { |
hudakz | 8:87fbdaba5535 | 327 | // reset the search state |
hudakz | 8:87fbdaba5535 | 328 | LastDiscrepancy = 0; |
hudakz | 8:87fbdaba5535 | 329 | LastDeviceFlag = false; |
hudakz | 8:87fbdaba5535 | 330 | LastFamilyDiscrepancy = 0; |
hudakz | 8:87fbdaba5535 | 331 | for (int i = 7;; i--) { |
hudakz | 8:87fbdaba5535 | 332 | ROM_NO[i] = 0; |
hudakz | 8:87fbdaba5535 | 333 | if (i == 0) |
hudakz | 8:87fbdaba5535 | 334 | break; |
hudakz | 8:87fbdaba5535 | 335 | } |
hudakz | 0:acf75feb0947 | 336 | } |
hudakz | 0:acf75feb0947 | 337 | |
hudakz | 10:c89b9ad6097c | 338 | /** |
hudakz | 10:c89b9ad6097c | 339 | * @brief Sets the search state to find SearchFamily type devices. |
hudakz | 10:c89b9ad6097c | 340 | * @note |
hudakz | 10:c89b9ad6097c | 341 | * @param |
hudakz | 10:c89b9ad6097c | 342 | * @retval |
hudakz | 10:c89b9ad6097c | 343 | */ |
hudakz | 0:acf75feb0947 | 344 | void OneWire::target_search(uint8_t family_code) |
hudakz | 0:acf75feb0947 | 345 | { |
hudakz | 8:87fbdaba5535 | 346 | // set the search state to find SearchFamily type devices |
hudakz | 8:87fbdaba5535 | 347 | ROM_NO[0] = family_code; |
hudakz | 8:87fbdaba5535 | 348 | for (uint8_t i = 1; i < 8; i++) |
hudakz | 8:87fbdaba5535 | 349 | ROM_NO[i] = 0; |
hudakz | 8:87fbdaba5535 | 350 | LastDiscrepancy = 64; |
hudakz | 8:87fbdaba5535 | 351 | LastFamilyDiscrepancy = 0; |
hudakz | 8:87fbdaba5535 | 352 | LastDeviceFlag = false; |
hudakz | 0:acf75feb0947 | 353 | } |
hudakz | 0:acf75feb0947 | 354 | |
hudakz | 10:c89b9ad6097c | 355 | /** |
hudakz | 10:c89b9ad6097c | 356 | * @brief Performs a search. |
hudakz | 10:c89b9ad6097c | 357 | * @note Perform a search. If this function returns a '1' then it has |
hudakz | 10:c89b9ad6097c | 358 | enumerated the next device and you may retrieve the ROM from the |
hudakz | 10:c89b9ad6097c | 359 | OneWire::address variable. If there are no devices, no further |
hudakz | 10:c89b9ad6097c | 360 | devices, or something horrible happens in the middle of the |
hudakz | 10:c89b9ad6097c | 361 | enumeration then a 0 is returned. If a new device is found then |
hudakz | 10:c89b9ad6097c | 362 | its address is copied to newAddr. Use OneWire::reset_search() to |
hudakz | 10:c89b9ad6097c | 363 | start over. |
hudakz | 10:c89b9ad6097c | 364 | |
hudakz | 10:c89b9ad6097c | 365 | --- Replaced by the one from the Dallas Semiconductor web site --- |
hudakz | 10:c89b9ad6097c | 366 | ------------------------------------------------------------------------- |
hudakz | 10:c89b9ad6097c | 367 | Perform the 1-Wire Search Algorithm on the 1-Wire bus using the existing |
hudakz | 10:c89b9ad6097c | 368 | search state. |
hudakz | 10:c89b9ad6097c | 369 | * @param |
hudakz | 10:c89b9ad6097c | 370 | * @retval true : device found, ROM number in ROM_NO buffer |
hudakz | 10:c89b9ad6097c | 371 | * false : device not found, end of search |
hudakz | 10:c89b9ad6097c | 372 | */ |
hudakz | 8:87fbdaba5535 | 373 | uint8_t OneWire::search(uint8_t* newAddr) |
hudakz | 0:acf75feb0947 | 374 | { |
hudakz | 8:87fbdaba5535 | 375 | uint8_t id_bit_number; |
hudakz | 8:87fbdaba5535 | 376 | uint8_t last_zero, rom_byte_number, search_result; |
hudakz | 8:87fbdaba5535 | 377 | uint8_t id_bit, cmp_id_bit; |
hudakz | 0:acf75feb0947 | 378 | |
hudakz | 8:87fbdaba5535 | 379 | unsigned char rom_byte_mask, search_direction; |
hudakz | 0:acf75feb0947 | 380 | |
hudakz | 8:87fbdaba5535 | 381 | // initialize for search |
hudakz | 8:87fbdaba5535 | 382 | id_bit_number = 1; |
hudakz | 8:87fbdaba5535 | 383 | last_zero = 0; |
hudakz | 8:87fbdaba5535 | 384 | rom_byte_number = 0; |
hudakz | 8:87fbdaba5535 | 385 | rom_byte_mask = 1; |
hudakz | 8:87fbdaba5535 | 386 | search_result = 0; |
hudakz | 0:acf75feb0947 | 387 | |
hudakz | 8:87fbdaba5535 | 388 | // if the last call was not the last one |
hudakz | 8:87fbdaba5535 | 389 | if (!LastDeviceFlag) { |
hudakz | 8:87fbdaba5535 | 390 | // 1-Wire reset |
hudakz | 8:87fbdaba5535 | 391 | if (!reset()) { |
hudakz | 8:87fbdaba5535 | 392 | // reset the search |
hudakz | 8:87fbdaba5535 | 393 | LastDiscrepancy = 0; |
hudakz | 8:87fbdaba5535 | 394 | LastDeviceFlag = false; |
hudakz | 8:87fbdaba5535 | 395 | LastFamilyDiscrepancy = 0; |
hudakz | 8:87fbdaba5535 | 396 | return false; |
hudakz | 8:87fbdaba5535 | 397 | } |
hudakz | 0:acf75feb0947 | 398 | |
hudakz | 8:87fbdaba5535 | 399 | // issue the search command |
hudakz | 9:4af0015b0f47 | 400 | write_byte(0xF0); |
hudakz | 0:acf75feb0947 | 401 | |
hudakz | 8:87fbdaba5535 | 402 | // loop to do the search |
hudakz | 8:87fbdaba5535 | 403 | do { |
hudakz | 8:87fbdaba5535 | 404 | // read a bit and its complement |
hudakz | 8:87fbdaba5535 | 405 | id_bit = read_bit(); |
hudakz | 8:87fbdaba5535 | 406 | cmp_id_bit = read_bit(); |
hudakz | 0:acf75feb0947 | 407 | |
hudakz | 8:87fbdaba5535 | 408 | // check for no devices on 1-wire |
hudakz | 8:87fbdaba5535 | 409 | if ((id_bit == 1) && (cmp_id_bit == 1)) |
hudakz | 8:87fbdaba5535 | 410 | break; |
hudakz | 8:87fbdaba5535 | 411 | else { |
hudakz | 8:87fbdaba5535 | 412 | // all devices coupled have 0 or 1 |
hudakz | 8:87fbdaba5535 | 413 | if (id_bit != cmp_id_bit) |
hudakz | 8:87fbdaba5535 | 414 | search_direction = id_bit; // bit write value for search |
hudakz | 8:87fbdaba5535 | 415 | else { |
hudakz | 8:87fbdaba5535 | 416 | // if this discrepancy if before the Last Discrepancy |
hudakz | 8:87fbdaba5535 | 417 | // on a previous next then pick the same as last time |
hudakz | 8:87fbdaba5535 | 418 | if (id_bit_number < LastDiscrepancy) |
hudakz | 8:87fbdaba5535 | 419 | search_direction = ((ROM_NO[rom_byte_number] & rom_byte_mask) > 0); |
hudakz | 8:87fbdaba5535 | 420 | else |
hudakz | 8:87fbdaba5535 | 421 | // if equal to last pick 1, if not then pick 0 |
hudakz | 8:87fbdaba5535 | 422 | search_direction = (id_bit_number == LastDiscrepancy); |
hudakz | 0:acf75feb0947 | 423 | |
hudakz | 8:87fbdaba5535 | 424 | // if 0 was picked then record its position in LastZero |
hudakz | 8:87fbdaba5535 | 425 | if (search_direction == 0) { |
hudakz | 8:87fbdaba5535 | 426 | last_zero = id_bit_number; |
hudakz | 0:acf75feb0947 | 427 | |
hudakz | 8:87fbdaba5535 | 428 | // check for Last discrepancy in family |
hudakz | 8:87fbdaba5535 | 429 | if (last_zero < 9) |
hudakz | 8:87fbdaba5535 | 430 | LastFamilyDiscrepancy = last_zero; |
hudakz | 8:87fbdaba5535 | 431 | } |
hudakz | 8:87fbdaba5535 | 432 | } |
hudakz | 0:acf75feb0947 | 433 | |
hudakz | 8:87fbdaba5535 | 434 | // set or clear the bit in the ROM byte rom_byte_number |
hudakz | 8:87fbdaba5535 | 435 | // with mask rom_byte_mask |
hudakz | 8:87fbdaba5535 | 436 | if (search_direction == 1) |
hudakz | 8:87fbdaba5535 | 437 | ROM_NO[rom_byte_number] |= rom_byte_mask; |
hudakz | 8:87fbdaba5535 | 438 | else |
hudakz | 8:87fbdaba5535 | 439 | ROM_NO[rom_byte_number] &= ~rom_byte_mask; |
hudakz | 0:acf75feb0947 | 440 | |
hudakz | 8:87fbdaba5535 | 441 | // serial number search direction write bit |
hudakz | 8:87fbdaba5535 | 442 | write_bit(search_direction); |
hudakz | 0:acf75feb0947 | 443 | |
hudakz | 8:87fbdaba5535 | 444 | // increment the byte counter id_bit_number |
hudakz | 8:87fbdaba5535 | 445 | // and shift the mask rom_byte_mask |
hudakz | 8:87fbdaba5535 | 446 | id_bit_number++; |
hudakz | 8:87fbdaba5535 | 447 | rom_byte_mask <<= 1; |
hudakz | 0:acf75feb0947 | 448 | |
hudakz | 8:87fbdaba5535 | 449 | // if the mask is 0 then go to new SerialNum byte rom_byte_number and reset mask |
hudakz | 8:87fbdaba5535 | 450 | if (rom_byte_mask == 0) { |
hudakz | 8:87fbdaba5535 | 451 | rom_byte_number++; |
hudakz | 8:87fbdaba5535 | 452 | rom_byte_mask = 1; |
hudakz | 8:87fbdaba5535 | 453 | } |
hudakz | 0:acf75feb0947 | 454 | } |
hudakz | 8:87fbdaba5535 | 455 | } while (rom_byte_number < 8); |
hudakz | 8:87fbdaba5535 | 456 | // loop until through all ROM bytes 0-7 |
hudakz | 8:87fbdaba5535 | 457 | // if the search was successful then |
hudakz | 8:87fbdaba5535 | 458 | if (!(id_bit_number < 65)) { |
hudakz | 8:87fbdaba5535 | 459 | // search successful so set LastDiscrepancy,LastDeviceFlag,search_result |
hudakz | 8:87fbdaba5535 | 460 | LastDiscrepancy = last_zero; |
hudakz | 0:acf75feb0947 | 461 | |
hudakz | 8:87fbdaba5535 | 462 | // check for last device |
hudakz | 8:87fbdaba5535 | 463 | if (LastDiscrepancy == 0) |
hudakz | 8:87fbdaba5535 | 464 | LastDeviceFlag = true; |
hudakz | 0:acf75feb0947 | 465 | |
hudakz | 8:87fbdaba5535 | 466 | search_result = true; |
hudakz | 8:87fbdaba5535 | 467 | } |
hudakz | 8:87fbdaba5535 | 468 | } |
hudakz | 0:acf75feb0947 | 469 | |
hudakz | 8:87fbdaba5535 | 470 | // if no device found then reset counters so next 'search' will be like a first |
hudakz | 8:87fbdaba5535 | 471 | if (!search_result || !ROM_NO[0]) { |
hudakz | 8:87fbdaba5535 | 472 | LastDiscrepancy = 0; |
hudakz | 8:87fbdaba5535 | 473 | LastDeviceFlag = false; |
hudakz | 8:87fbdaba5535 | 474 | LastFamilyDiscrepancy = 0; |
hudakz | 8:87fbdaba5535 | 475 | search_result = false; |
hudakz | 8:87fbdaba5535 | 476 | } |
hudakz | 0:acf75feb0947 | 477 | |
hudakz | 8:87fbdaba5535 | 478 | for (int i = 0; i < 8; i++) |
hudakz | 8:87fbdaba5535 | 479 | newAddr[i] = ROM_NO[i]; |
hudakz | 8:87fbdaba5535 | 480 | return search_result; |
hudakz | 8:87fbdaba5535 | 481 | } |
hudakz | 0:acf75feb0947 | 482 | #endif |
hudakz | 10:c89b9ad6097c | 483 | // |
hudakz | 0:acf75feb0947 | 484 | #if ONEWIRE_CRC |
hudakz | 0:acf75feb0947 | 485 | // |
hudakz | 10:c89b9ad6097c | 486 | /** |
hudakz | 10:c89b9ad6097c | 487 | * @brief Computes a Dallas Semiconductor 8 bit CRC directly. |
hudakz | 10:c89b9ad6097c | 488 | * @note The 1-Wire CRC scheme is described in Maxim Application Note 27: |
hudakz | 10:c89b9ad6097c | 489 | "Understanding and Using Cyclic Redundancy Checks with Maxim iButton Products" |
hudakz | 10:c89b9ad6097c | 490 | * @param |
hudakz | 10:c89b9ad6097c | 491 | * @retval |
hudakz | 10:c89b9ad6097c | 492 | */ |
hudakz | 8:87fbdaba5535 | 493 | uint8_t OneWire::crc8(const uint8_t* addr, uint8_t len) |
hudakz | 0:acf75feb0947 | 494 | { |
hudakz | 0:acf75feb0947 | 495 | uint8_t crc = 0; |
hudakz | 8:87fbdaba5535 | 496 | |
hudakz | 0:acf75feb0947 | 497 | while (len--) { |
hudakz | 0:acf75feb0947 | 498 | uint8_t inbyte = *addr++; |
hudakz | 0:acf75feb0947 | 499 | for (uint8_t i = 8; i; i--) { |
hudakz | 0:acf75feb0947 | 500 | uint8_t mix = (crc ^ inbyte) & 0x01; |
hudakz | 0:acf75feb0947 | 501 | crc >>= 1; |
hudakz | 8:87fbdaba5535 | 502 | if (mix) |
hudakz | 8:87fbdaba5535 | 503 | crc ^= 0x8C; |
hudakz | 0:acf75feb0947 | 504 | inbyte >>= 1; |
hudakz | 0:acf75feb0947 | 505 | } |
hudakz | 0:acf75feb0947 | 506 | } |
hudakz | 8:87fbdaba5535 | 507 | |
hudakz | 0:acf75feb0947 | 508 | return crc; |
hudakz | 0:acf75feb0947 | 509 | } |
hudakz | 0:acf75feb0947 | 510 | #endif |