Implementation of 1-Wire with added Alarm Search Functionality
Dependents: Max32630_One_Wire_Interface
OneWire_Masters/DS248x/ds248x.cpp@21:00c94aeb533e, 2016-03-21 (annotated)
- Committer:
- IanBenzMaxim
- Date:
- Mon Mar 21 14:12:28 2016 -0500
- Revision:
- 21:00c94aeb533e
- Parent:
- 17:b646b1e3970b
- Child:
- 23:e8e403d61359
Added class for DS2465. Added a ReadBytePower operation to OneWireMaster since this is required by the authenticators including the DS28E15. Tweaked member data of OneWireMaster. Added path to header file includes to reduce compiler setup required.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
j3 | 1:91e52f8ab8bf | 1 | /******************************************************************//** |
j3 | 1:91e52f8ab8bf | 2 | * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved. |
j3 | 1:91e52f8ab8bf | 3 | * |
j3 | 1:91e52f8ab8bf | 4 | * Permission is hereby granted, free of charge, to any person obtaining a |
j3 | 1:91e52f8ab8bf | 5 | * copy of this software and associated documentation files (the "Software"), |
j3 | 1:91e52f8ab8bf | 6 | * to deal in the Software without restriction, including without limitation |
j3 | 1:91e52f8ab8bf | 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
j3 | 1:91e52f8ab8bf | 8 | * and/or sell copies of the Software, and to permit persons to whom the |
j3 | 1:91e52f8ab8bf | 9 | * Software is furnished to do so, subject to the following conditions: |
j3 | 1:91e52f8ab8bf | 10 | * |
j3 | 1:91e52f8ab8bf | 11 | * The above copyright notice and this permission notice shall be included |
j3 | 1:91e52f8ab8bf | 12 | * in all copies or substantial portions of the Software. |
j3 | 1:91e52f8ab8bf | 13 | * |
j3 | 1:91e52f8ab8bf | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
j3 | 1:91e52f8ab8bf | 15 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
j3 | 1:91e52f8ab8bf | 16 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
j3 | 1:91e52f8ab8bf | 17 | * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES |
j3 | 1:91e52f8ab8bf | 18 | * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
j3 | 1:91e52f8ab8bf | 19 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
j3 | 1:91e52f8ab8bf | 20 | * OTHER DEALINGS IN THE SOFTWARE. |
j3 | 1:91e52f8ab8bf | 21 | * |
j3 | 1:91e52f8ab8bf | 22 | * Except as contained in this notice, the name of Maxim Integrated |
j3 | 1:91e52f8ab8bf | 23 | * Products, Inc. shall not be used except as stated in the Maxim Integrated |
j3 | 1:91e52f8ab8bf | 24 | * Products, Inc. Branding Policy. |
j3 | 1:91e52f8ab8bf | 25 | * |
j3 | 1:91e52f8ab8bf | 26 | * The mere transfer of this software does not imply any licenses |
j3 | 1:91e52f8ab8bf | 27 | * of trade secrets, proprietary technology, copyrights, patents, |
j3 | 1:91e52f8ab8bf | 28 | * trademarks, maskwork rights, or any other form of intellectual |
j3 | 1:91e52f8ab8bf | 29 | * property whatsoever. Maxim Integrated Products, Inc. retains all |
j3 | 1:91e52f8ab8bf | 30 | * ownership rights. |
j3 | 1:91e52f8ab8bf | 31 | **********************************************************************/ |
j3 | 1:91e52f8ab8bf | 32 | |
j3 | 1:91e52f8ab8bf | 33 | |
j3 | 1:91e52f8ab8bf | 34 | #include "ds248x.h" |
j3 | 1:91e52f8ab8bf | 35 | |
j3 | 1:91e52f8ab8bf | 36 | |
j3 | 1:91e52f8ab8bf | 37 | //********************************************************************* |
j3 | 5:ce108eeb878d | 38 | Ds248x::Ds248x(I2C &i2c_bus, DS248X_I2C_ADRS adrs) |
j3 | 6:1faafa0b3cd7 | 39 | :_p_i2c_bus(&i2c_bus), _i2c_owner(false) |
j3 | 1:91e52f8ab8bf | 40 | { |
j3 | 6:1faafa0b3cd7 | 41 | set_i2c_adrs(adrs); |
j3 | 1:91e52f8ab8bf | 42 | } |
j3 | 1:91e52f8ab8bf | 43 | |
j3 | 1:91e52f8ab8bf | 44 | |
j3 | 1:91e52f8ab8bf | 45 | //********************************************************************* |
j3 | 5:ce108eeb878d | 46 | Ds248x::Ds248x(PinName sda, PinName scl, DS248X_I2C_ADRS adrs) |
j3 | 6:1faafa0b3cd7 | 47 | :_p_i2c_bus(new I2C(sda, scl)), _i2c_owner(true) |
j3 | 1:91e52f8ab8bf | 48 | { |
j3 | 6:1faafa0b3cd7 | 49 | set_i2c_adrs(adrs); |
j3 | 1:91e52f8ab8bf | 50 | } |
j3 | 1:91e52f8ab8bf | 51 | |
j3 | 1:91e52f8ab8bf | 52 | |
j3 | 1:91e52f8ab8bf | 53 | //********************************************************************* |
j3 | 1:91e52f8ab8bf | 54 | Ds248x::~Ds248x() |
j3 | 1:91e52f8ab8bf | 55 | { |
j3 | 5:ce108eeb878d | 56 | if(_i2c_owner) |
j3 | 1:91e52f8ab8bf | 57 | { |
j3 | 1:91e52f8ab8bf | 58 | delete _p_i2c_bus; |
j3 | 1:91e52f8ab8bf | 59 | } |
j3 | 1:91e52f8ab8bf | 60 | } |
j3 | 1:91e52f8ab8bf | 61 | |
j3 | 1:91e52f8ab8bf | 62 | |
j3 | 1:91e52f8ab8bf | 63 | //********************************************************************* |
j3 | 17:b646b1e3970b | 64 | OneWireInterface::CmdResult Ds248x::OWInitMaster(void) |
j3 | 14:7b2886a50321 | 65 | { |
j3 | 14:7b2886a50321 | 66 | return(detect()); |
j3 | 14:7b2886a50321 | 67 | } |
j3 | 14:7b2886a50321 | 68 | |
j3 | 14:7b2886a50321 | 69 | |
j3 | 14:7b2886a50321 | 70 | //********************************************************************* |
j3 | 17:b646b1e3970b | 71 | OneWireInterface::CmdResult Ds248x::detect(void) |
j3 | 2:02d228c25fd4 | 72 | { |
j3 | 17:b646b1e3970b | 73 | OneWireInterface::CmdResult result; |
j3 | 2:02d228c25fd4 | 74 | |
j3 | 2:02d228c25fd4 | 75 | // reset the ds2484 ON selected address |
j3 | 17:b646b1e3970b | 76 | result = reset(); |
j3 | 17:b646b1e3970b | 77 | if(result == OneWireInterface::Success) |
j3 | 2:02d228c25fd4 | 78 | { |
j3 | 2:02d228c25fd4 | 79 | // default configuration |
j3 | 17:b646b1e3970b | 80 | _c1WS = 0; |
j3 | 17:b646b1e3970b | 81 | _cSPU = 0; |
j3 | 17:b646b1e3970b | 82 | _cPDN = 0; |
j3 | 17:b646b1e3970b | 83 | _cAPU = 0; |
j3 | 2:02d228c25fd4 | 84 | |
j3 | 17:b646b1e3970b | 85 | result = write_config(_c1WS | _cSPU | _cPDN | _cAPU); |
j3 | 2:02d228c25fd4 | 86 | } |
j3 | 2:02d228c25fd4 | 87 | |
j3 | 17:b646b1e3970b | 88 | return result; |
j3 | 2:02d228c25fd4 | 89 | } |
j3 | 2:02d228c25fd4 | 90 | |
j3 | 2:02d228c25fd4 | 91 | |
j3 | 2:02d228c25fd4 | 92 | //********************************************************************* |
j3 | 17:b646b1e3970b | 93 | OneWireInterface::CmdResult Ds248x::reset(void) |
j3 | 1:91e52f8ab8bf | 94 | { |
j3 | 17:b646b1e3970b | 95 | OneWireInterface::CmdResult result; |
j3 | 17:b646b1e3970b | 96 | |
j3 | 2:02d228c25fd4 | 97 | char status; |
j3 | 2:02d228c25fd4 | 98 | char packet[] = {CMD_DRST}; |
j3 | 2:02d228c25fd4 | 99 | |
j3 | 2:02d228c25fd4 | 100 | // Device Reset |
j3 | 2:02d228c25fd4 | 101 | // S AD,0 [A] DRST [A] Sr AD,1 [A] [SS] A\ P |
j3 | 2:02d228c25fd4 | 102 | // [] indicates from slave |
j3 | 2:02d228c25fd4 | 103 | // SS status byte to read to verify state |
j3 | 2:02d228c25fd4 | 104 | |
j3 | 17:b646b1e3970b | 105 | if(_p_i2c_bus->write(_w_adrs, packet, 1) != I2C_WRITE_OK) |
j3 | 17:b646b1e3970b | 106 | { |
j3 | 17:b646b1e3970b | 107 | result = OneWireInterface::CommunicationWriteError; |
j3 | 17:b646b1e3970b | 108 | } |
j3 | 17:b646b1e3970b | 109 | else |
j3 | 17:b646b1e3970b | 110 | { |
j3 | 17:b646b1e3970b | 111 | if(_p_i2c_bus->read(_r_adrs, &status, 1) != I2C_READ_OK) |
j3 | 17:b646b1e3970b | 112 | { |
j3 | 17:b646b1e3970b | 113 | result = OneWireInterface::CommunicationReadError; |
j3 | 17:b646b1e3970b | 114 | } |
j3 | 17:b646b1e3970b | 115 | else |
j3 | 17:b646b1e3970b | 116 | { |
j3 | 17:b646b1e3970b | 117 | if((status & 0xF7) == 0x10) |
j3 | 17:b646b1e3970b | 118 | { |
j3 | 17:b646b1e3970b | 119 | result = OneWireInterface::Success; |
j3 | 17:b646b1e3970b | 120 | } |
j3 | 17:b646b1e3970b | 121 | else |
j3 | 17:b646b1e3970b | 122 | { |
j3 | 17:b646b1e3970b | 123 | result = OneWireInterface::OperationFailure; |
j3 | 17:b646b1e3970b | 124 | } |
j3 | 17:b646b1e3970b | 125 | } |
j3 | 17:b646b1e3970b | 126 | } |
j3 | 2:02d228c25fd4 | 127 | |
j3 | 17:b646b1e3970b | 128 | return result; |
j3 | 2:02d228c25fd4 | 129 | } |
j3 | 2:02d228c25fd4 | 130 | |
j3 | 2:02d228c25fd4 | 131 | |
j3 | 2:02d228c25fd4 | 132 | //********************************************************************* |
j3 | 17:b646b1e3970b | 133 | OneWireInterface::CmdResult Ds248x::write_config(uint8_t config) |
j3 | 2:02d228c25fd4 | 134 | { |
j3 | 17:b646b1e3970b | 135 | OneWireInterface::CmdResult result; |
j3 | 17:b646b1e3970b | 136 | |
j3 | 2:02d228c25fd4 | 137 | char read_config; |
j3 | 2:02d228c25fd4 | 138 | char packet [] = {CMD_WCFG, (config | (~config << 4))}; |
j3 | 2:02d228c25fd4 | 139 | |
j3 | 17:b646b1e3970b | 140 | if(_p_i2c_bus->write(_w_adrs, packet, 2) != I2C_WRITE_OK) |
j3 | 2:02d228c25fd4 | 141 | { |
j3 | 17:b646b1e3970b | 142 | result = OneWireInterface::CommunicationWriteError; |
j3 | 2:02d228c25fd4 | 143 | } |
j3 | 2:02d228c25fd4 | 144 | else |
j3 | 2:02d228c25fd4 | 145 | { |
j3 | 17:b646b1e3970b | 146 | if(_p_i2c_bus->read(_r_adrs, &read_config, 1) != I2C_READ_OK) |
j3 | 17:b646b1e3970b | 147 | { |
j3 | 17:b646b1e3970b | 148 | result = OneWireInterface::CommunicationReadError; |
j3 | 17:b646b1e3970b | 149 | } |
j3 | 17:b646b1e3970b | 150 | else |
j3 | 17:b646b1e3970b | 151 | { |
j3 | 17:b646b1e3970b | 152 | // check for failure due to incorrect read back |
j3 | 17:b646b1e3970b | 153 | if (config != read_config) |
j3 | 17:b646b1e3970b | 154 | { |
j3 | 17:b646b1e3970b | 155 | reset(); |
j3 | 17:b646b1e3970b | 156 | result = OneWireInterface::OperationFailure; |
j3 | 17:b646b1e3970b | 157 | } |
j3 | 17:b646b1e3970b | 158 | else |
j3 | 17:b646b1e3970b | 159 | { |
j3 | 17:b646b1e3970b | 160 | result = OneWireInterface::Success; |
j3 | 17:b646b1e3970b | 161 | } |
j3 | 17:b646b1e3970b | 162 | } |
j3 | 2:02d228c25fd4 | 163 | } |
j3 | 2:02d228c25fd4 | 164 | |
j3 | 17:b646b1e3970b | 165 | return result; |
j3 | 1:91e52f8ab8bf | 166 | } |
j3 | 1:91e52f8ab8bf | 167 | |
j3 | 1:91e52f8ab8bf | 168 | |
j3 | 1:91e52f8ab8bf | 169 | //********************************************************************* |
j3 | 17:b646b1e3970b | 170 | OneWireInterface::CmdResult Ds248x::channel_select(uint8_t channel) |
j3 | 1:91e52f8ab8bf | 171 | { |
j3 | 17:b646b1e3970b | 172 | OneWireInterface::CmdResult result; |
j3 | 2:02d228c25fd4 | 173 | |
j3 | 2:02d228c25fd4 | 174 | char ch, ch_read, check; |
j3 | 2:02d228c25fd4 | 175 | char packet [2]; |
j3 | 2:02d228c25fd4 | 176 | |
j3 | 2:02d228c25fd4 | 177 | packet[0] = CMD_CHSL; |
j3 | 2:02d228c25fd4 | 178 | |
j3 | 2:02d228c25fd4 | 179 | // Channel Select (Case A) |
j3 | 2:02d228c25fd4 | 180 | // S AD,0 [A] CHSL [A] CC [A] Sr AD,1 [A] [RR] A\ P |
j3 | 2:02d228c25fd4 | 181 | // [] indicates from slave |
j3 | 2:02d228c25fd4 | 182 | // CC channel value |
j3 | 2:02d228c25fd4 | 183 | // RR channel read back |
j3 | 2:02d228c25fd4 | 184 | |
j3 | 2:02d228c25fd4 | 185 | switch (channel) |
j3 | 2:02d228c25fd4 | 186 | { |
j3 | 2:02d228c25fd4 | 187 | default: case 0: ch = 0xF0; ch_read = 0xB8; break; |
j3 | 2:02d228c25fd4 | 188 | case 1: ch = 0xE1; ch_read = 0xB1; break; |
j3 | 2:02d228c25fd4 | 189 | case 2: ch = 0xD2; ch_read = 0xAA; break; |
j3 | 2:02d228c25fd4 | 190 | case 3: ch = 0xC3; ch_read = 0xA3; break; |
j3 | 2:02d228c25fd4 | 191 | case 4: ch = 0xB4; ch_read = 0x9C; break; |
j3 | 2:02d228c25fd4 | 192 | case 5: ch = 0xA5; ch_read = 0x95; break; |
j3 | 2:02d228c25fd4 | 193 | case 6: ch = 0x96; ch_read = 0x8E; break; |
j3 | 2:02d228c25fd4 | 194 | case 7: ch = 0x87; ch_read = 0x87; break; |
j3 | 2:02d228c25fd4 | 195 | }; |
j3 | 2:02d228c25fd4 | 196 | |
j3 | 2:02d228c25fd4 | 197 | packet[1] = ch; |
j3 | 2:02d228c25fd4 | 198 | |
j3 | 17:b646b1e3970b | 199 | if(_p_i2c_bus->write(_w_adrs, packet, 2) != I2C_WRITE_OK) |
j3 | 17:b646b1e3970b | 200 | { |
j3 | 17:b646b1e3970b | 201 | result = OneWireInterface::CommunicationWriteError; |
j3 | 17:b646b1e3970b | 202 | } |
j3 | 17:b646b1e3970b | 203 | else |
j3 | 17:b646b1e3970b | 204 | { |
j3 | 17:b646b1e3970b | 205 | if(_p_i2c_bus->read(_r_adrs, &check, 1) != I2C_READ_OK) |
j3 | 17:b646b1e3970b | 206 | { |
j3 | 17:b646b1e3970b | 207 | result = OneWireInterface::CommunicationReadError; |
j3 | 17:b646b1e3970b | 208 | } |
j3 | 17:b646b1e3970b | 209 | else |
j3 | 17:b646b1e3970b | 210 | { |
j3 | 17:b646b1e3970b | 211 | // check for failure due to incorrect read back of channel |
j3 | 17:b646b1e3970b | 212 | if (check == ch_read) |
j3 | 17:b646b1e3970b | 213 | { |
j3 | 17:b646b1e3970b | 214 | result = OneWireInterface::Success; |
j3 | 17:b646b1e3970b | 215 | } |
j3 | 17:b646b1e3970b | 216 | else |
j3 | 17:b646b1e3970b | 217 | { |
j3 | 17:b646b1e3970b | 218 | result = OneWireInterface::OperationFailure; |
j3 | 17:b646b1e3970b | 219 | } |
j3 | 17:b646b1e3970b | 220 | } |
j3 | 17:b646b1e3970b | 221 | } |
j3 | 2:02d228c25fd4 | 222 | |
j3 | 17:b646b1e3970b | 223 | return result; |
j3 | 2:02d228c25fd4 | 224 | } |
j3 | 2:02d228c25fd4 | 225 | |
j3 | 2:02d228c25fd4 | 226 | |
j3 | 2:02d228c25fd4 | 227 | //********************************************************************* |
j3 | 17:b646b1e3970b | 228 | OneWireInterface::CmdResult Ds248x::adjust_timing(uint8_t param, uint8_t val) |
j3 | 2:02d228c25fd4 | 229 | { |
j3 | 17:b646b1e3970b | 230 | OneWireInterface::CmdResult result; |
j3 | 17:b646b1e3970b | 231 | |
j3 | 2:02d228c25fd4 | 232 | char read_port_config; |
j3 | 2:02d228c25fd4 | 233 | char control_byte; |
j3 | 2:02d228c25fd4 | 234 | |
j3 | 2:02d228c25fd4 | 235 | control_byte = (((param & 0x0F) << 4) | (val & 0x0F)); |
j3 | 2:02d228c25fd4 | 236 | |
j3 | 2:02d228c25fd4 | 237 | char packet [] = {CMD_A1WP, control_byte}; |
j3 | 2:02d228c25fd4 | 238 | |
j3 | 17:b646b1e3970b | 239 | if(_p_i2c_bus->write(_w_adrs, packet, 2) != I2C_WRITE_OK) |
j3 | 17:b646b1e3970b | 240 | { |
j3 | 17:b646b1e3970b | 241 | result = OneWireInterface::CommunicationWriteError; |
j3 | 17:b646b1e3970b | 242 | } |
j3 | 17:b646b1e3970b | 243 | else |
j3 | 2:02d228c25fd4 | 244 | { |
j3 | 17:b646b1e3970b | 245 | if(_p_i2c_bus->read(_r_adrs, &read_port_config, 1) != I2C_READ_OK) |
j3 | 17:b646b1e3970b | 246 | { |
j3 | 17:b646b1e3970b | 247 | result = OneWireInterface::CommunicationReadError; |
j3 | 17:b646b1e3970b | 248 | } |
j3 | 17:b646b1e3970b | 249 | else |
j3 | 17:b646b1e3970b | 250 | { |
j3 | 17:b646b1e3970b | 251 | // check for failure due to incorrect read back |
j3 | 17:b646b1e3970b | 252 | if ((control_byte & 0x0F) != read_port_config) |
j3 | 17:b646b1e3970b | 253 | { |
j3 | 17:b646b1e3970b | 254 | result = OneWireInterface::OperationFailure; |
j3 | 17:b646b1e3970b | 255 | reset(); |
j3 | 17:b646b1e3970b | 256 | } |
j3 | 17:b646b1e3970b | 257 | else |
j3 | 17:b646b1e3970b | 258 | { |
j3 | 17:b646b1e3970b | 259 | result = OneWireInterface::Success; |
j3 | 17:b646b1e3970b | 260 | } |
j3 | 17:b646b1e3970b | 261 | } |
j3 | 2:02d228c25fd4 | 262 | } |
j3 | 17:b646b1e3970b | 263 | |
j3 | 17:b646b1e3970b | 264 | return result; |
j3 | 1:91e52f8ab8bf | 265 | } |
j3 | 1:91e52f8ab8bf | 266 | |
j3 | 1:91e52f8ab8bf | 267 | |
j3 | 1:91e52f8ab8bf | 268 | //********************************************************************* |
j3 | 17:b646b1e3970b | 269 | OneWireInterface::CmdResult Ds248x::search_triplet(uint8_t search_direction, uint8_t & status) |
j3 | 1:91e52f8ab8bf | 270 | { |
j3 | 17:b646b1e3970b | 271 | OneWireInterface::CmdResult result; |
j3 | 17:b646b1e3970b | 272 | |
j3 | 2:02d228c25fd4 | 273 | uint8_t poll_count = 0; |
j3 | 2:02d228c25fd4 | 274 | char packet [] = {CMD_1WT, search_direction ? 0x80 : 0x00}; |
j3 | 17:b646b1e3970b | 275 | char read_data; |
j3 | 2:02d228c25fd4 | 276 | |
j3 | 2:02d228c25fd4 | 277 | // 1-Wire Triplet (Case B) |
j3 | 2:02d228c25fd4 | 278 | // S AD,0 [A] 1WT [A] SS [A] Sr AD,1 [A] [Status] A [Status] A\ P |
j3 | 2:02d228c25fd4 | 279 | // \--------/ |
j3 | 2:02d228c25fd4 | 280 | // Repeat until 1WB bit has changed to 0 |
j3 | 2:02d228c25fd4 | 281 | // [] indicates from slave |
j3 | 2:02d228c25fd4 | 282 | // SS indicates byte containing search direction bit value in msbit |
j3 | 2:02d228c25fd4 | 283 | |
j3 | 17:b646b1e3970b | 284 | if(_p_i2c_bus->write(_w_adrs, packet, 2) != I2C_WRITE_OK) |
j3 | 2:02d228c25fd4 | 285 | { |
j3 | 17:b646b1e3970b | 286 | result = OneWireInterface::CommunicationWriteError; |
j3 | 2:02d228c25fd4 | 287 | } |
j3 | 2:02d228c25fd4 | 288 | else |
j3 | 2:02d228c25fd4 | 289 | { |
j3 | 17:b646b1e3970b | 290 | // loop checking 1WB bit for completion of 1-Wire operation |
j3 | 17:b646b1e3970b | 291 | // abort if poll limit reached |
j3 | 17:b646b1e3970b | 292 | |
j3 | 17:b646b1e3970b | 293 | //dummy write for loop |
j3 | 17:b646b1e3970b | 294 | result = OneWireInterface::Success; //so far |
j3 | 17:b646b1e3970b | 295 | do |
j3 | 17:b646b1e3970b | 296 | { |
j3 | 17:b646b1e3970b | 297 | if(_p_i2c_bus->read(_r_adrs, &read_data, 1) != I2C_READ_OK) |
j3 | 17:b646b1e3970b | 298 | { |
j3 | 17:b646b1e3970b | 299 | result = OneWireInterface::CommunicationReadError; |
j3 | 17:b646b1e3970b | 300 | } |
j3 | 17:b646b1e3970b | 301 | } |
j3 | 17:b646b1e3970b | 302 | while ((read_data & STATUS_1WB) && (poll_count++ < POLL_LIMIT) && (result != OneWireInterface::CommunicationReadError)); |
j3 | 17:b646b1e3970b | 303 | |
j3 | 17:b646b1e3970b | 304 | if((result == OneWireInterface::CommunicationReadError) || (poll_count >= POLL_LIMIT)) |
j3 | 17:b646b1e3970b | 305 | { |
j3 | 17:b646b1e3970b | 306 | // check for failure due to poll limit reached |
j3 | 17:b646b1e3970b | 307 | if(poll_count >= POLL_LIMIT) |
j3 | 17:b646b1e3970b | 308 | { |
j3 | 17:b646b1e3970b | 309 | // handle error |
j3 | 17:b646b1e3970b | 310 | // ... |
j3 | 17:b646b1e3970b | 311 | reset(); |
j3 | 17:b646b1e3970b | 312 | result = OneWireInterface::TimeoutError; |
j3 | 17:b646b1e3970b | 313 | } |
j3 | 17:b646b1e3970b | 314 | } |
j3 | 17:b646b1e3970b | 315 | else |
j3 | 17:b646b1e3970b | 316 | { |
j3 | 17:b646b1e3970b | 317 | status = read_data; |
j3 | 17:b646b1e3970b | 318 | result = OneWireInterface::Success; |
j3 | 17:b646b1e3970b | 319 | } |
j3 | 2:02d228c25fd4 | 320 | } |
j3 | 17:b646b1e3970b | 321 | |
j3 | 17:b646b1e3970b | 322 | return result; |
j3 | 1:91e52f8ab8bf | 323 | } |
j3 | 1:91e52f8ab8bf | 324 | |
j3 | 1:91e52f8ab8bf | 325 | |
j3 | 1:91e52f8ab8bf | 326 | //********************************************************************* |
j3 | 17:b646b1e3970b | 327 | OneWireInterface::CmdResult Ds248x::OWReset(void) |
j3 | 2:02d228c25fd4 | 328 | { |
j3 | 17:b646b1e3970b | 329 | OneWireInterface::CmdResult result; |
j3 | 2:02d228c25fd4 | 330 | |
j3 | 2:02d228c25fd4 | 331 | uint8_t poll_count = 0; |
j3 | 2:02d228c25fd4 | 332 | char status; |
j3 | 2:02d228c25fd4 | 333 | char packet [] = {CMD_1WRS}; |
j3 | 1:91e52f8ab8bf | 334 | |
j3 | 2:02d228c25fd4 | 335 | // 1-Wire reset (Case B) |
j3 | 2:02d228c25fd4 | 336 | // S AD,0 [A] 1WRS [A] Sr AD,1 [A] [Status] A [Status] A\ P |
j3 | 2:02d228c25fd4 | 337 | // \--------/ |
j3 | 2:02d228c25fd4 | 338 | // Repeat until 1WB bit has changed to 0 |
j3 | 2:02d228c25fd4 | 339 | // [] indicates from slave |
j3 | 2:02d228c25fd4 | 340 | |
j3 | 17:b646b1e3970b | 341 | if(_p_i2c_bus->write(_w_adrs, packet, 1) != I2C_WRITE_OK) |
j3 | 2:02d228c25fd4 | 342 | { |
j3 | 17:b646b1e3970b | 343 | result = OneWireInterface::CommunicationWriteError; |
j3 | 2:02d228c25fd4 | 344 | } |
j3 | 2:02d228c25fd4 | 345 | else |
j3 | 2:02d228c25fd4 | 346 | { |
j3 | 17:b646b1e3970b | 347 | // loop checking 1WB bit for completion of 1-Wire operation |
j3 | 17:b646b1e3970b | 348 | // abort if poll limit reached |
j3 | 17:b646b1e3970b | 349 | //dummy write for loop |
j3 | 17:b646b1e3970b | 350 | result = OneWireInterface::Success; //so far |
j3 | 17:b646b1e3970b | 351 | do |
j3 | 2:02d228c25fd4 | 352 | { |
j3 | 17:b646b1e3970b | 353 | if(_p_i2c_bus->read(_r_adrs, &status, 1) != I2C_READ_OK) |
j3 | 17:b646b1e3970b | 354 | { |
j3 | 17:b646b1e3970b | 355 | result = OneWireInterface::CommunicationReadError; |
j3 | 17:b646b1e3970b | 356 | } |
j3 | 17:b646b1e3970b | 357 | } |
j3 | 17:b646b1e3970b | 358 | while ((status & STATUS_1WB) && (poll_count++ < POLL_LIMIT) && (result != OneWireInterface::CommunicationReadError)); |
j3 | 17:b646b1e3970b | 359 | |
j3 | 17:b646b1e3970b | 360 | if((result == OneWireInterface::CommunicationReadError) || (poll_count >= POLL_LIMIT)) |
j3 | 17:b646b1e3970b | 361 | { |
j3 | 17:b646b1e3970b | 362 | // check for failure due to poll limit reached |
j3 | 17:b646b1e3970b | 363 | if(poll_count >= POLL_LIMIT) |
j3 | 17:b646b1e3970b | 364 | { |
j3 | 17:b646b1e3970b | 365 | // handle error |
j3 | 17:b646b1e3970b | 366 | // ... |
j3 | 17:b646b1e3970b | 367 | reset(); |
j3 | 17:b646b1e3970b | 368 | result = OneWireInterface::TimeoutError; |
j3 | 17:b646b1e3970b | 369 | } |
j3 | 2:02d228c25fd4 | 370 | } |
j3 | 2:02d228c25fd4 | 371 | else |
j3 | 2:02d228c25fd4 | 372 | { |
j3 | 17:b646b1e3970b | 373 | // check for short condition |
j3 | 17:b646b1e3970b | 374 | if (status & STATUS_SD) |
j3 | 17:b646b1e3970b | 375 | { |
j3 | 17:b646b1e3970b | 376 | _short_detected = true; |
j3 | 17:b646b1e3970b | 377 | } |
j3 | 17:b646b1e3970b | 378 | else |
j3 | 17:b646b1e3970b | 379 | { |
j3 | 17:b646b1e3970b | 380 | _short_detected = false; |
j3 | 17:b646b1e3970b | 381 | } |
j3 | 17:b646b1e3970b | 382 | |
j3 | 17:b646b1e3970b | 383 | // check for presence detect |
j3 | 17:b646b1e3970b | 384 | if (status & STATUS_PPD) |
j3 | 17:b646b1e3970b | 385 | { |
j3 | 17:b646b1e3970b | 386 | result = OneWireInterface::Success; |
j3 | 17:b646b1e3970b | 387 | } |
j3 | 17:b646b1e3970b | 388 | else |
j3 | 17:b646b1e3970b | 389 | { |
j3 | 17:b646b1e3970b | 390 | result = OneWireInterface::OperationFailure; |
j3 | 17:b646b1e3970b | 391 | } |
j3 | 2:02d228c25fd4 | 392 | } |
j3 | 2:02d228c25fd4 | 393 | } |
j3 | 17:b646b1e3970b | 394 | |
j3 | 17:b646b1e3970b | 395 | return result; |
j3 | 1:91e52f8ab8bf | 396 | } |
j3 | 1:91e52f8ab8bf | 397 | |
j3 | 1:91e52f8ab8bf | 398 | |
j3 | 1:91e52f8ab8bf | 399 | //********************************************************************* |
j3 | 17:b646b1e3970b | 400 | OneWireInterface::CmdResult Ds248x::OWTouchBit(uint8_t & sendrecvbit) |
j3 | 1:91e52f8ab8bf | 401 | { |
j3 | 17:b646b1e3970b | 402 | OneWireInterface::CmdResult result; |
j3 | 17:b646b1e3970b | 403 | |
j3 | 2:02d228c25fd4 | 404 | uint8_t poll_count = 0; |
j3 | 2:02d228c25fd4 | 405 | char status; |
j3 | 17:b646b1e3970b | 406 | char packet[] = {CMD_1WSB, sendrecvbit ? 0x80 : 0x00}; |
j3 | 2:02d228c25fd4 | 407 | |
j3 | 2:02d228c25fd4 | 408 | // 1-Wire bit (Case B) |
j3 | 2:02d228c25fd4 | 409 | // S AD,0 [A] 1WSB [A] BB [A] Sr AD,1 [A] [Status] A [Status] A\ P |
j3 | 2:02d228c25fd4 | 410 | // \--------/ |
j3 | 2:02d228c25fd4 | 411 | // Repeat until 1WB bit has changed to 0 |
j3 | 2:02d228c25fd4 | 412 | // [] indicates from slave |
j3 | 2:02d228c25fd4 | 413 | // BB indicates byte containing bit value in msbit |
j3 | 2:02d228c25fd4 | 414 | |
j3 | 17:b646b1e3970b | 415 | if(_p_i2c_bus->write(_w_adrs, packet, 2) != I2C_WRITE_OK) |
j3 | 2:02d228c25fd4 | 416 | { |
j3 | 17:b646b1e3970b | 417 | result = OneWireInterface::CommunicationWriteError; |
j3 | 2:02d228c25fd4 | 418 | } |
j3 | 2:02d228c25fd4 | 419 | else |
j3 | 2:02d228c25fd4 | 420 | { |
j3 | 17:b646b1e3970b | 421 | // loop checking 1WB bit for completion of 1-Wire operation |
j3 | 17:b646b1e3970b | 422 | // abort if poll limit reached |
j3 | 17:b646b1e3970b | 423 | //dummy write for loop |
j3 | 17:b646b1e3970b | 424 | result = OneWireInterface::Success; //so far |
j3 | 17:b646b1e3970b | 425 | do |
j3 | 2:02d228c25fd4 | 426 | { |
j3 | 17:b646b1e3970b | 427 | if(_p_i2c_bus->read(_r_adrs, &status, 1) != I2C_READ_OK) |
j3 | 17:b646b1e3970b | 428 | { |
j3 | 17:b646b1e3970b | 429 | result = OneWireInterface::CommunicationReadError; |
j3 | 17:b646b1e3970b | 430 | } |
j3 | 17:b646b1e3970b | 431 | } |
j3 | 17:b646b1e3970b | 432 | while ((status & STATUS_1WB) && (poll_count++ < POLL_LIMIT) && (result != OneWireInterface::CommunicationReadError)); |
j3 | 17:b646b1e3970b | 433 | |
j3 | 17:b646b1e3970b | 434 | if((result == OneWireInterface::CommunicationReadError) || (poll_count >= POLL_LIMIT)) |
j3 | 17:b646b1e3970b | 435 | { |
j3 | 17:b646b1e3970b | 436 | // check for failure due to poll limit reached |
j3 | 17:b646b1e3970b | 437 | if (poll_count >= POLL_LIMIT) |
j3 | 17:b646b1e3970b | 438 | { |
j3 | 17:b646b1e3970b | 439 | // handle error |
j3 | 17:b646b1e3970b | 440 | // ... |
j3 | 17:b646b1e3970b | 441 | reset(); |
j3 | 17:b646b1e3970b | 442 | result = OneWireInterface::TimeoutError; |
j3 | 17:b646b1e3970b | 443 | } |
j3 | 2:02d228c25fd4 | 444 | } |
j3 | 2:02d228c25fd4 | 445 | else |
j3 | 2:02d228c25fd4 | 446 | { |
j3 | 17:b646b1e3970b | 447 | // return bit state through out param |
j3 | 17:b646b1e3970b | 448 | if (status & STATUS_SBR) |
j3 | 17:b646b1e3970b | 449 | { |
j3 | 17:b646b1e3970b | 450 | sendrecvbit = 1; |
j3 | 17:b646b1e3970b | 451 | |
j3 | 17:b646b1e3970b | 452 | } |
j3 | 17:b646b1e3970b | 453 | else |
j3 | 17:b646b1e3970b | 454 | { |
j3 | 17:b646b1e3970b | 455 | sendrecvbit = 0; |
j3 | 17:b646b1e3970b | 456 | } |
j3 | 17:b646b1e3970b | 457 | result = OneWireInterface::Success; |
j3 | 2:02d228c25fd4 | 458 | } |
j3 | 2:02d228c25fd4 | 459 | } |
j3 | 17:b646b1e3970b | 460 | |
j3 | 17:b646b1e3970b | 461 | return result; |
j3 | 1:91e52f8ab8bf | 462 | } |
j3 | 1:91e52f8ab8bf | 463 | |
j3 | 1:91e52f8ab8bf | 464 | |
j3 | 1:91e52f8ab8bf | 465 | //********************************************************************* |
j3 | 17:b646b1e3970b | 466 | OneWireInterface::CmdResult Ds248x::OWWriteByte(uint8_t sendbyte) |
j3 | 1:91e52f8ab8bf | 467 | { |
j3 | 17:b646b1e3970b | 468 | OneWireInterface::CmdResult result; |
j3 | 1:91e52f8ab8bf | 469 | |
j3 | 2:02d228c25fd4 | 470 | uint8_t poll_count = 0; |
j3 | 2:02d228c25fd4 | 471 | char status; |
j3 | 2:02d228c25fd4 | 472 | char packet [] = {CMD_1WWB, sendbyte}; |
j3 | 2:02d228c25fd4 | 473 | |
j3 | 2:02d228c25fd4 | 474 | // 1-Wire Write Byte (Case B) |
j3 | 2:02d228c25fd4 | 475 | // S AD,0 [A] 1WWB [A] DD [A] Sr AD,1 [A] [Status] A [Status] A\ P |
j3 | 2:02d228c25fd4 | 476 | // \--------/ |
j3 | 2:02d228c25fd4 | 477 | // Repeat until 1WB bit has changed to 0 |
j3 | 2:02d228c25fd4 | 478 | // [] indicates from slave |
j3 | 2:02d228c25fd4 | 479 | // DD data to write |
j3 | 2:02d228c25fd4 | 480 | |
j3 | 17:b646b1e3970b | 481 | if(_p_i2c_bus->write(_w_adrs, packet, 2) != I2C_WRITE_OK) |
j3 | 2:02d228c25fd4 | 482 | { |
j3 | 17:b646b1e3970b | 483 | result = OneWireInterface::CommunicationWriteError; |
j3 | 2:02d228c25fd4 | 484 | } |
j3 | 2:02d228c25fd4 | 485 | else |
j3 | 2:02d228c25fd4 | 486 | { |
j3 | 17:b646b1e3970b | 487 | // loop checking 1WB bit for completion of 1-Wire operation |
j3 | 17:b646b1e3970b | 488 | // abort if poll limit reached |
j3 | 17:b646b1e3970b | 489 | //dummy write for loop |
j3 | 17:b646b1e3970b | 490 | result = OneWireInterface::Success; //so far |
j3 | 17:b646b1e3970b | 491 | do |
j3 | 17:b646b1e3970b | 492 | { |
j3 | 17:b646b1e3970b | 493 | if(_p_i2c_bus->read(_r_adrs, &status, 1) != I2C_READ_OK) |
j3 | 17:b646b1e3970b | 494 | { |
j3 | 17:b646b1e3970b | 495 | result = OneWireInterface::CommunicationReadError; |
j3 | 17:b646b1e3970b | 496 | } |
j3 | 17:b646b1e3970b | 497 | } |
j3 | 17:b646b1e3970b | 498 | while ((status & STATUS_1WB) && (poll_count++ < POLL_LIMIT) && (result != OneWireInterface::CommunicationReadError)); |
j3 | 17:b646b1e3970b | 499 | |
j3 | 17:b646b1e3970b | 500 | if((result == OneWireInterface::CommunicationReadError) || (poll_count >= POLL_LIMIT)) |
j3 | 17:b646b1e3970b | 501 | { |
j3 | 17:b646b1e3970b | 502 | // check for failure due to poll limit reached |
j3 | 17:b646b1e3970b | 503 | if (poll_count >= POLL_LIMIT) |
j3 | 17:b646b1e3970b | 504 | { |
j3 | 17:b646b1e3970b | 505 | // handle error |
j3 | 17:b646b1e3970b | 506 | // ... |
j3 | 17:b646b1e3970b | 507 | reset(); |
j3 | 17:b646b1e3970b | 508 | result = OneWireInterface::TimeoutError; |
j3 | 17:b646b1e3970b | 509 | } |
j3 | 17:b646b1e3970b | 510 | } |
j3 | 17:b646b1e3970b | 511 | else |
j3 | 17:b646b1e3970b | 512 | { |
j3 | 17:b646b1e3970b | 513 | result = OneWireInterface::Success; |
j3 | 17:b646b1e3970b | 514 | } |
j3 | 2:02d228c25fd4 | 515 | } |
j3 | 2:02d228c25fd4 | 516 | |
j3 | 17:b646b1e3970b | 517 | return result; |
j3 | 1:91e52f8ab8bf | 518 | } |
j3 | 1:91e52f8ab8bf | 519 | |
j3 | 1:91e52f8ab8bf | 520 | |
j3 | 1:91e52f8ab8bf | 521 | //********************************************************************* |
j3 | 17:b646b1e3970b | 522 | OneWireInterface::CmdResult Ds248x::OWReadByte(uint8_t & recvbyte) |
j3 | 1:91e52f8ab8bf | 523 | { |
j3 | 17:b646b1e3970b | 524 | OneWireInterface::CmdResult result; |
j3 | 2:02d228c25fd4 | 525 | |
j3 | 2:02d228c25fd4 | 526 | uint8_t poll_count = 0; |
j3 | 2:02d228c25fd4 | 527 | char data, status; |
j3 | 2:02d228c25fd4 | 528 | char packet[2] = {CMD_1WRB, 0}; |
j3 | 2:02d228c25fd4 | 529 | |
j3 | 2:02d228c25fd4 | 530 | // 1-Wire Read Bytes (Case C) |
j3 | 2:02d228c25fd4 | 531 | // S AD,0 [A] 1WRB [A] Sr AD,1 [A] [Status] A [Status] A\ |
j3 | 2:02d228c25fd4 | 532 | // \--------/ |
j3 | 2:02d228c25fd4 | 533 | // Repeat until 1WB bit has changed to 0 |
j3 | 2:02d228c25fd4 | 534 | // Sr AD,0 [A] SRP [A] E1 [A] Sr AD,1 [A] DD A\ P |
j3 | 2:02d228c25fd4 | 535 | // |
j3 | 2:02d228c25fd4 | 536 | // [] indicates from slave |
j3 | 2:02d228c25fd4 | 537 | // DD data read |
j3 | 2:02d228c25fd4 | 538 | |
j3 | 17:b646b1e3970b | 539 | if(_p_i2c_bus->write(_w_adrs, packet, 1) != I2C_WRITE_OK) |
j3 | 2:02d228c25fd4 | 540 | { |
j3 | 17:b646b1e3970b | 541 | result = OneWireInterface::CommunicationWriteError; |
j3 | 2:02d228c25fd4 | 542 | } |
j3 | 2:02d228c25fd4 | 543 | else |
j3 | 2:02d228c25fd4 | 544 | { |
j3 | 17:b646b1e3970b | 545 | // loop checking 1WB bit for completion of 1-Wire operation |
j3 | 17:b646b1e3970b | 546 | // abort if poll limit reached |
j3 | 17:b646b1e3970b | 547 | //dummy write for loop |
j3 | 17:b646b1e3970b | 548 | result = OneWireInterface::Success; //so far |
j3 | 17:b646b1e3970b | 549 | do |
j3 | 17:b646b1e3970b | 550 | { |
j3 | 17:b646b1e3970b | 551 | if(_p_i2c_bus->read(_r_adrs, &status, 1) != I2C_READ_OK) |
j3 | 17:b646b1e3970b | 552 | { |
j3 | 17:b646b1e3970b | 553 | result = OneWireInterface::CommunicationReadError; |
j3 | 17:b646b1e3970b | 554 | } |
j3 | 17:b646b1e3970b | 555 | } |
j3 | 17:b646b1e3970b | 556 | while ((status & STATUS_1WB) && (poll_count++ < POLL_LIMIT) && (result != OneWireInterface::CommunicationReadError)); |
j3 | 17:b646b1e3970b | 557 | |
j3 | 17:b646b1e3970b | 558 | if((result == OneWireInterface::CommunicationReadError) || (poll_count >= POLL_LIMIT)) |
j3 | 17:b646b1e3970b | 559 | { |
j3 | 17:b646b1e3970b | 560 | // check for failure due to poll limit reached |
j3 | 17:b646b1e3970b | 561 | if (poll_count >= POLL_LIMIT) |
j3 | 17:b646b1e3970b | 562 | { |
j3 | 17:b646b1e3970b | 563 | // handle error |
j3 | 17:b646b1e3970b | 564 | // ... |
j3 | 17:b646b1e3970b | 565 | reset(); |
j3 | 17:b646b1e3970b | 566 | result = OneWireInterface::TimeoutError; |
j3 | 17:b646b1e3970b | 567 | } |
j3 | 17:b646b1e3970b | 568 | } |
j3 | 17:b646b1e3970b | 569 | else |
j3 | 17:b646b1e3970b | 570 | { |
j3 | 17:b646b1e3970b | 571 | packet[0] = CMD_SRP; |
j3 | 17:b646b1e3970b | 572 | packet[1] = 0xE1; |
j3 | 17:b646b1e3970b | 573 | |
j3 | 17:b646b1e3970b | 574 | if(_p_i2c_bus->write(_w_adrs, packet, 2) != I2C_WRITE_OK) |
j3 | 17:b646b1e3970b | 575 | { |
j3 | 17:b646b1e3970b | 576 | result = OneWireInterface::CommunicationWriteError; |
j3 | 17:b646b1e3970b | 577 | } |
j3 | 17:b646b1e3970b | 578 | else |
j3 | 17:b646b1e3970b | 579 | { |
j3 | 17:b646b1e3970b | 580 | if(_p_i2c_bus->read(_r_adrs, &data, 1) != I2C_READ_OK) |
j3 | 17:b646b1e3970b | 581 | { |
j3 | 17:b646b1e3970b | 582 | result = OneWireInterface::CommunicationReadError; |
j3 | 17:b646b1e3970b | 583 | } |
j3 | 17:b646b1e3970b | 584 | else |
j3 | 17:b646b1e3970b | 585 | { |
j3 | 17:b646b1e3970b | 586 | recvbyte = data; |
j3 | 17:b646b1e3970b | 587 | result = OneWireInterface::Success; |
j3 | 17:b646b1e3970b | 588 | } |
j3 | 17:b646b1e3970b | 589 | } |
j3 | 17:b646b1e3970b | 590 | } |
j3 | 2:02d228c25fd4 | 591 | } |
j3 | 2:02d228c25fd4 | 592 | |
j3 | 17:b646b1e3970b | 593 | return result; |
j3 | 17:b646b1e3970b | 594 | } |
j3 | 17:b646b1e3970b | 595 | |
j3 | 17:b646b1e3970b | 596 | |
j3 | 17:b646b1e3970b | 597 | //********************************************************************* |
j3 | 17:b646b1e3970b | 598 | OneWireInterface::CmdResult Ds248x::OWWriteBlock(const uint8_t *tran_buf, uint8_t tran_len) |
j3 | 17:b646b1e3970b | 599 | { |
j3 | 17:b646b1e3970b | 600 | OneWireInterface::CmdResult result; |
j3 | 17:b646b1e3970b | 601 | |
j3 | 17:b646b1e3970b | 602 | for(uint8_t idx = 0; idx < tran_len; idx++) |
j3 | 17:b646b1e3970b | 603 | { |
j3 | 17:b646b1e3970b | 604 | result = OWWriteByte(tran_buf[idx]); |
j3 | 17:b646b1e3970b | 605 | if(result != OneWireInterface::Success) |
j3 | 17:b646b1e3970b | 606 | { |
j3 | 17:b646b1e3970b | 607 | break; |
j3 | 17:b646b1e3970b | 608 | } |
j3 | 17:b646b1e3970b | 609 | } |
j3 | 17:b646b1e3970b | 610 | |
j3 | 17:b646b1e3970b | 611 | return result; |
j3 | 17:b646b1e3970b | 612 | } |
j3 | 17:b646b1e3970b | 613 | |
j3 | 17:b646b1e3970b | 614 | |
j3 | 17:b646b1e3970b | 615 | //********************************************************************* |
j3 | 17:b646b1e3970b | 616 | OneWireInterface::CmdResult Ds248x::OWReadBlock(uint8_t *rx_buf, uint8_t rx_len) |
j3 | 17:b646b1e3970b | 617 | { |
j3 | 17:b646b1e3970b | 618 | OneWireInterface::CmdResult result; |
j3 | 17:b646b1e3970b | 619 | |
j3 | 17:b646b1e3970b | 620 | for(uint8_t idx = 0; idx < rx_len; idx++) |
j3 | 17:b646b1e3970b | 621 | { |
j3 | 17:b646b1e3970b | 622 | //OwReadByte() uses pass by reference |
j3 | 17:b646b1e3970b | 623 | result = OWReadByte(rx_buf[idx]); |
j3 | 17:b646b1e3970b | 624 | if(result != OneWireInterface::Success) |
j3 | 17:b646b1e3970b | 625 | { |
j3 | 17:b646b1e3970b | 626 | break; |
j3 | 17:b646b1e3970b | 627 | } |
j3 | 17:b646b1e3970b | 628 | } |
j3 | 17:b646b1e3970b | 629 | |
j3 | 17:b646b1e3970b | 630 | return result; |
j3 | 1:91e52f8ab8bf | 631 | } |
j3 | 1:91e52f8ab8bf | 632 | |
j3 | 1:91e52f8ab8bf | 633 | |
j3 | 1:91e52f8ab8bf | 634 | //********************************************************************* |
j3 | 17:b646b1e3970b | 635 | OneWireInterface::CmdResult Ds248x::OWSearch(RomId & romId) |
j3 | 1:91e52f8ab8bf | 636 | { |
j3 | 17:b646b1e3970b | 637 | int id_bit_number; |
j3 | 17:b646b1e3970b | 638 | int last_zero, rom_byte_number; |
j3 | 17:b646b1e3970b | 639 | int id_bit, cmp_id_bit; |
j3 | 17:b646b1e3970b | 640 | unsigned char rom_byte_mask, status; |
j3 | 17:b646b1e3970b | 641 | bool search_result; |
j3 | 17:b646b1e3970b | 642 | unsigned char crc8 = 0; |
j3 | 17:b646b1e3970b | 643 | SEARCH_DIRECTION search_direction; |
j3 | 17:b646b1e3970b | 644 | |
j3 | 17:b646b1e3970b | 645 | // initialize for search |
j3 | 17:b646b1e3970b | 646 | id_bit_number = 1; |
j3 | 17:b646b1e3970b | 647 | last_zero = 0; |
j3 | 17:b646b1e3970b | 648 | rom_byte_number = 0; |
j3 | 17:b646b1e3970b | 649 | rom_byte_mask = 1; |
j3 | 17:b646b1e3970b | 650 | search_result = false; |
j3 | 2:02d228c25fd4 | 651 | |
j3 | 17:b646b1e3970b | 652 | // if the last call was not the last one |
j3 | 17:b646b1e3970b | 653 | if (!_last_device_flag) |
j3 | 17:b646b1e3970b | 654 | { |
j3 | 17:b646b1e3970b | 655 | // 1-Wire reset |
j3 | 17:b646b1e3970b | 656 | OneWireInterface::CmdResult result = OWReset(); |
j3 | 17:b646b1e3970b | 657 | if (result != OneWireInterface::Success) |
j3 | 17:b646b1e3970b | 658 | { |
j3 | 17:b646b1e3970b | 659 | // reset the search |
j3 | 17:b646b1e3970b | 660 | _last_discrepancy = 0; |
j3 | 17:b646b1e3970b | 661 | _last_device_flag = false; |
j3 | 17:b646b1e3970b | 662 | _last_family_discrepancy = 0; |
j3 | 17:b646b1e3970b | 663 | return result; |
j3 | 17:b646b1e3970b | 664 | } |
j3 | 17:b646b1e3970b | 665 | |
j3 | 17:b646b1e3970b | 666 | // issue the search command |
j3 | 17:b646b1e3970b | 667 | OWWriteByte(0xF0); |
j3 | 2:02d228c25fd4 | 668 | |
j3 | 17:b646b1e3970b | 669 | // loop to do the search |
j3 | 17:b646b1e3970b | 670 | do |
j3 | 17:b646b1e3970b | 671 | { |
j3 | 17:b646b1e3970b | 672 | // if this discrepancy if before the Last Discrepancy |
j3 | 17:b646b1e3970b | 673 | // on a previous next then pick the same as last time |
j3 | 17:b646b1e3970b | 674 | if (id_bit_number < _last_discrepancy) |
j3 | 17:b646b1e3970b | 675 | { |
j3 | 17:b646b1e3970b | 676 | if ((romId[rom_byte_number] & rom_byte_mask) > 0) |
j3 | 17:b646b1e3970b | 677 | search_direction = DIRECTION_WRITE_ONE; |
j3 | 17:b646b1e3970b | 678 | else |
j3 | 17:b646b1e3970b | 679 | search_direction = DIRECTION_WRITE_ZERO; |
j3 | 17:b646b1e3970b | 680 | } |
j3 | 17:b646b1e3970b | 681 | else |
j3 | 17:b646b1e3970b | 682 | { |
j3 | 17:b646b1e3970b | 683 | // if equal to last pick 1, if not then pick 0 |
j3 | 17:b646b1e3970b | 684 | if (id_bit_number == _last_discrepancy) |
j3 | 17:b646b1e3970b | 685 | search_direction = DIRECTION_WRITE_ONE; |
j3 | 17:b646b1e3970b | 686 | else |
j3 | 17:b646b1e3970b | 687 | search_direction = DIRECTION_WRITE_ZERO; |
j3 | 17:b646b1e3970b | 688 | } |
j3 | 2:02d228c25fd4 | 689 | |
j3 | 17:b646b1e3970b | 690 | // Peform a triple operation on the DS2465 which will perform 2 read bits and 1 write bit |
j3 | 17:b646b1e3970b | 691 | search_triplet(search_direction, status); |
j3 | 17:b646b1e3970b | 692 | |
j3 | 17:b646b1e3970b | 693 | // check bit results in status byte |
j3 | 17:b646b1e3970b | 694 | id_bit = ((status & STATUS_SBR) == STATUS_SBR); |
j3 | 17:b646b1e3970b | 695 | cmp_id_bit = ((status & STATUS_TSB) == STATUS_TSB); |
j3 | 17:b646b1e3970b | 696 | search_direction = ((status & STATUS_DIR) == STATUS_DIR) ? DIRECTION_WRITE_ONE : DIRECTION_WRITE_ZERO; |
j3 | 17:b646b1e3970b | 697 | |
j3 | 17:b646b1e3970b | 698 | // check for no devices on 1-wire |
j3 | 17:b646b1e3970b | 699 | if ((id_bit) && (cmp_id_bit)) |
j3 | 17:b646b1e3970b | 700 | break; |
j3 | 17:b646b1e3970b | 701 | else |
j3 | 17:b646b1e3970b | 702 | { |
j3 | 17:b646b1e3970b | 703 | if ((!id_bit) && (!cmp_id_bit) && (search_direction == DIRECTION_WRITE_ZERO)) |
j3 | 2:02d228c25fd4 | 704 | { |
j3 | 17:b646b1e3970b | 705 | last_zero = id_bit_number; |
j3 | 17:b646b1e3970b | 706 | |
j3 | 17:b646b1e3970b | 707 | // check for Last discrepancy in family |
j3 | 17:b646b1e3970b | 708 | if (last_zero < 9) |
j3 | 17:b646b1e3970b | 709 | _last_family_discrepancy = last_zero; |
j3 | 2:02d228c25fd4 | 710 | } |
j3 | 2:02d228c25fd4 | 711 | |
j3 | 17:b646b1e3970b | 712 | // set or clear the bit in the ROM byte rom_byte_number |
j3 | 17:b646b1e3970b | 713 | // with mask rom_byte_mask |
j3 | 17:b646b1e3970b | 714 | if (search_direction == DIRECTION_WRITE_ONE) |
j3 | 17:b646b1e3970b | 715 | romId[rom_byte_number] |= rom_byte_mask; |
j3 | 17:b646b1e3970b | 716 | else |
j3 | 17:b646b1e3970b | 717 | romId[rom_byte_number] &= (unsigned char)~rom_byte_mask; |
j3 | 2:02d228c25fd4 | 718 | |
j3 | 17:b646b1e3970b | 719 | // increment the byte counter id_bit_number |
j3 | 17:b646b1e3970b | 720 | // and shift the mask rom_byte_mask |
j3 | 17:b646b1e3970b | 721 | id_bit_number++; |
j3 | 17:b646b1e3970b | 722 | rom_byte_mask <<= 1; |
j3 | 2:02d228c25fd4 | 723 | |
j3 | 17:b646b1e3970b | 724 | // if the mask is 0 then go to new SerialNum byte rom_byte_number and reset mask |
j3 | 17:b646b1e3970b | 725 | if (rom_byte_mask == 0) |
j3 | 17:b646b1e3970b | 726 | { |
j3 | 17:b646b1e3970b | 727 | crc8 = romId.calculateCRC8(crc8, romId[rom_byte_number]); // accumulate the CRC |
j3 | 17:b646b1e3970b | 728 | rom_byte_number++; |
j3 | 17:b646b1e3970b | 729 | rom_byte_mask = 1; |
j3 | 17:b646b1e3970b | 730 | } |
j3 | 17:b646b1e3970b | 731 | } |
j3 | 17:b646b1e3970b | 732 | } |
j3 | 17:b646b1e3970b | 733 | while(rom_byte_number < RomId::byteLen); // loop until through all ROM bytes 0-7 |
j3 | 2:02d228c25fd4 | 734 | |
j3 | 17:b646b1e3970b | 735 | // if the search was successful then |
j3 | 17:b646b1e3970b | 736 | if (!((id_bit_number <= (RomId::byteLen * 8)) || (crc8 != 0))) |
j3 | 17:b646b1e3970b | 737 | { |
j3 | 17:b646b1e3970b | 738 | // search successful so set m_last_discrepancy,m_last_device_flag,search_result |
j3 | 17:b646b1e3970b | 739 | _last_discrepancy = last_zero; |
j3 | 2:02d228c25fd4 | 740 | |
j3 | 17:b646b1e3970b | 741 | // check for last device |
j3 | 17:b646b1e3970b | 742 | if (_last_discrepancy == 0) |
j3 | 17:b646b1e3970b | 743 | _last_device_flag = true; |
j3 | 2:02d228c25fd4 | 744 | |
j3 | 17:b646b1e3970b | 745 | search_result = true; |
j3 | 17:b646b1e3970b | 746 | } |
j3 | 17:b646b1e3970b | 747 | } |
j3 | 2:02d228c25fd4 | 748 | |
j3 | 17:b646b1e3970b | 749 | // if no device found then reset counters so next 'search' will be like a first |
j3 | 17:b646b1e3970b | 750 | if (!search_result || (romId.familyCode() == 0)) |
j3 | 17:b646b1e3970b | 751 | { |
j3 | 17:b646b1e3970b | 752 | _last_discrepancy = 0; |
j3 | 17:b646b1e3970b | 753 | _last_device_flag = false; |
j3 | 17:b646b1e3970b | 754 | _last_family_discrepancy = 0; |
j3 | 17:b646b1e3970b | 755 | search_result = false; |
j3 | 17:b646b1e3970b | 756 | } |
j3 | 2:02d228c25fd4 | 757 | |
j3 | 17:b646b1e3970b | 758 | return search_result ? OneWireInterface::Success : OneWireInterface::OperationFailure; |
j3 | 1:91e52f8ab8bf | 759 | } |
j3 | 1:91e52f8ab8bf | 760 | |
j3 | 1:91e52f8ab8bf | 761 | |
j3 | 1:91e52f8ab8bf | 762 | //********************************************************************* |
j3 | 17:b646b1e3970b | 763 | OneWireInterface::CmdResult Ds248x::OWSpeed(OW_SPEED new_speed) |
j3 | 1:91e52f8ab8bf | 764 | { |
j3 | 2:02d228c25fd4 | 765 | // set the speed |
j3 | 5:ce108eeb878d | 766 | if (new_speed == SPEED_OVERDRIVE) |
j3 | 2:02d228c25fd4 | 767 | { |
j3 | 2:02d228c25fd4 | 768 | _c1WS = CONFIG_1WS; |
j3 | 2:02d228c25fd4 | 769 | } |
j3 | 2:02d228c25fd4 | 770 | else |
j3 | 2:02d228c25fd4 | 771 | { |
j3 | 17:b646b1e3970b | 772 | _c1WS = 0; |
j3 | 2:02d228c25fd4 | 773 | } |
j3 | 2:02d228c25fd4 | 774 | |
j3 | 17:b646b1e3970b | 775 | // write the new config, and return result of op |
j3 | 17:b646b1e3970b | 776 | return write_config(_c1WS | _cSPU | _cPDN | _cAPU); |
j3 | 1:91e52f8ab8bf | 777 | } |
j3 | 1:91e52f8ab8bf | 778 | |
j3 | 1:91e52f8ab8bf | 779 | |
j3 | 1:91e52f8ab8bf | 780 | //********************************************************************* |
j3 | 17:b646b1e3970b | 781 | OneWireInterface::CmdResult Ds248x::OWLevel(OW_LEVEL new_level) |
j3 | 1:91e52f8ab8bf | 782 | { |
j3 | 17:b646b1e3970b | 783 | OneWireInterface::CmdResult result; |
j3 | 2:02d228c25fd4 | 784 | |
j3 | 2:02d228c25fd4 | 785 | // function only will turn back to non-strong pull-up |
j3 | 5:ce108eeb878d | 786 | if (new_level != LEVEL_NORMAL) |
j3 | 2:02d228c25fd4 | 787 | { |
j3 | 17:b646b1e3970b | 788 | result = OneWireInterface::OperationFailure; |
j3 | 2:02d228c25fd4 | 789 | } |
j3 | 2:02d228c25fd4 | 790 | else |
j3 | 2:02d228c25fd4 | 791 | { |
j3 | 2:02d228c25fd4 | 792 | // clear the strong pull-up bit in the global config state |
j3 | 17:b646b1e3970b | 793 | _cSPU = 0; |
j3 | 17:b646b1e3970b | 794 | |
j3 | 17:b646b1e3970b | 795 | result = write_config(_c1WS | _cSPU | _cPDN | _cAPU); |
j3 | 2:02d228c25fd4 | 796 | } |
j3 | 2:02d228c25fd4 | 797 | |
j3 | 17:b646b1e3970b | 798 | return result; |
j3 | 1:91e52f8ab8bf | 799 | } |
j3 | 1:91e52f8ab8bf | 800 | |
j3 | 1:91e52f8ab8bf | 801 | |
j3 | 1:91e52f8ab8bf | 802 | //********************************************************************* |
j3 | 17:b646b1e3970b | 803 | OneWireInterface::CmdResult Ds248x::OWWriteBytePower(uint8_t sendbyte) |
j3 | 1:91e52f8ab8bf | 804 | { |
j3 | 17:b646b1e3970b | 805 | OneWireInterface::CmdResult result; |
j3 | 2:02d228c25fd4 | 806 | |
j3 | 2:02d228c25fd4 | 807 | // set strong pull-up enable |
j3 | 2:02d228c25fd4 | 808 | _cSPU = CONFIG_SPU; |
j3 | 2:02d228c25fd4 | 809 | |
j3 | 2:02d228c25fd4 | 810 | // write the new config |
j3 | 17:b646b1e3970b | 811 | result = write_config(_c1WS | _cSPU | _cPDN | _cAPU); |
j3 | 17:b646b1e3970b | 812 | if (result == OneWireInterface::Success) |
j3 | 2:02d228c25fd4 | 813 | { |
j3 | 2:02d228c25fd4 | 814 | // perform write byte |
j3 | 17:b646b1e3970b | 815 | result = OWWriteByte(sendbyte); |
j3 | 2:02d228c25fd4 | 816 | } |
j3 | 2:02d228c25fd4 | 817 | |
j3 | 17:b646b1e3970b | 818 | return result; |
j3 | 1:91e52f8ab8bf | 819 | } |
j3 | 1:91e52f8ab8bf | 820 | |
j3 | 1:91e52f8ab8bf | 821 | |
j3 | 1:91e52f8ab8bf | 822 | //********************************************************************* |
j3 | 17:b646b1e3970b | 823 | OneWireInterface::CmdResult Ds248x::OWReadBitPower(uint8_t applyPowerResponse) |
j3 | 1:91e52f8ab8bf | 824 | { |
j3 | 17:b646b1e3970b | 825 | OneWireInterface::CmdResult result; |
j3 | 2:02d228c25fd4 | 826 | uint8_t rdbit; |
j3 | 2:02d228c25fd4 | 827 | |
j3 | 2:02d228c25fd4 | 828 | // set strong pull-up enable |
j3 | 2:02d228c25fd4 | 829 | _cSPU = CONFIG_SPU; |
j3 | 2:02d228c25fd4 | 830 | |
j3 | 2:02d228c25fd4 | 831 | // write the new config |
j3 | 17:b646b1e3970b | 832 | result = write_config(_c1WS | _cSPU | _cPDN | _cAPU); |
j3 | 17:b646b1e3970b | 833 | if (result == OneWireInterface::Success) |
j3 | 2:02d228c25fd4 | 834 | { |
j3 | 2:02d228c25fd4 | 835 | // perform read bit |
j3 | 17:b646b1e3970b | 836 | result = OWReadBit(rdbit); |
j3 | 17:b646b1e3970b | 837 | if(result == OneWireInterface::Success) |
j3 | 2:02d228c25fd4 | 838 | { |
j3 | 17:b646b1e3970b | 839 | // check if response was correct, if not then turn off strong pull-up |
j3 | 17:b646b1e3970b | 840 | if (rdbit != applyPowerResponse) |
j3 | 17:b646b1e3970b | 841 | { |
j3 | 17:b646b1e3970b | 842 | OWLevel(LEVEL_NORMAL); |
j3 | 17:b646b1e3970b | 843 | result = OneWireInterface::OperationFailure; |
j3 | 17:b646b1e3970b | 844 | } |
j3 | 2:02d228c25fd4 | 845 | } |
j3 | 2:02d228c25fd4 | 846 | } |
j3 | 2:02d228c25fd4 | 847 | |
j3 | 17:b646b1e3970b | 848 | return result; |
j3 | 1:91e52f8ab8bf | 849 | } |
j3 | 1:91e52f8ab8bf | 850 | |
j3 | 1:91e52f8ab8bf | 851 | |
IanBenzMaxim | 21:00c94aeb533e | 852 | OneWireInterface::CmdResult Ds248x::OWReadBytePower(uint8_t & recvbyte) |
IanBenzMaxim | 21:00c94aeb533e | 853 | { |
IanBenzMaxim | 21:00c94aeb533e | 854 | OneWireInterface::CmdResult result = Success; |
IanBenzMaxim | 21:00c94aeb533e | 855 | uint8_t recvbit; |
IanBenzMaxim | 21:00c94aeb533e | 856 | |
IanBenzMaxim | 21:00c94aeb533e | 857 | recvbyte = 0; |
IanBenzMaxim | 21:00c94aeb533e | 858 | for (unsigned int i = 1; i <= 8; i++) |
IanBenzMaxim | 21:00c94aeb533e | 859 | { |
IanBenzMaxim | 21:00c94aeb533e | 860 | // Set strong pull-up on last bit |
IanBenzMaxim | 21:00c94aeb533e | 861 | if (i == 8) |
IanBenzMaxim | 21:00c94aeb533e | 862 | { |
IanBenzMaxim | 21:00c94aeb533e | 863 | // set strong pull-up enable |
IanBenzMaxim | 21:00c94aeb533e | 864 | _cSPU = CONFIG_SPU; |
IanBenzMaxim | 21:00c94aeb533e | 865 | |
IanBenzMaxim | 21:00c94aeb533e | 866 | // write the new config |
IanBenzMaxim | 21:00c94aeb533e | 867 | result = write_config(_c1WS | _cSPU | _cPDN | _cAPU); |
IanBenzMaxim | 21:00c94aeb533e | 868 | if (result != Success) |
IanBenzMaxim | 21:00c94aeb533e | 869 | break; |
IanBenzMaxim | 21:00c94aeb533e | 870 | } |
IanBenzMaxim | 21:00c94aeb533e | 871 | result = OWReadBit(recvbit); |
IanBenzMaxim | 21:00c94aeb533e | 872 | if (result != Success) |
IanBenzMaxim | 21:00c94aeb533e | 873 | break; |
IanBenzMaxim | 21:00c94aeb533e | 874 | recvbyte = (recvbyte << 1) | recvbit; |
IanBenzMaxim | 21:00c94aeb533e | 875 | } |
IanBenzMaxim | 21:00c94aeb533e | 876 | |
IanBenzMaxim | 21:00c94aeb533e | 877 | return result; |
IanBenzMaxim | 21:00c94aeb533e | 878 | } |
IanBenzMaxim | 21:00c94aeb533e | 879 | |
IanBenzMaxim | 21:00c94aeb533e | 880 | |
j3 | 1:91e52f8ab8bf | 881 | //********************************************************************* |
j3 | 6:1faafa0b3cd7 | 882 | void Ds248x::set_i2c_adrs(DS248X_I2C_ADRS adrs) |
j3 | 6:1faafa0b3cd7 | 883 | { |
j3 | 6:1faafa0b3cd7 | 884 | _w_adrs = (adrs << 1); |
j3 | 6:1faafa0b3cd7 | 885 | _r_adrs = (_w_adrs | 1); |
j3 | 6:1faafa0b3cd7 | 886 | } |