Implementation of 1-Wire with added Alarm Search Functionality

Dependents:   Max32630_One_Wire_Interface

Committer:
j3
Date:
Mon Mar 21 23:18:45 2016 +0000
Revision:
23:e8e403d61359
Parent:
22:686273e55cdc
Child:
27:d5aaefa252f1
Merged OneWireInterface into OneWireMaster for clarity

Who changed what in which revision?

UserRevisionLine numberNew contents of line
j3 4:ca27db159b10 1 /******************************************************************//**
j3 4:ca27db159b10 2 * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved.
j3 4:ca27db159b10 3 *
j3 4:ca27db159b10 4 * Permission is hereby granted, free of charge, to any person obtaining a
j3 4:ca27db159b10 5 * copy of this software and associated documentation files (the "Software"),
j3 4:ca27db159b10 6 * to deal in the Software without restriction, including without limitation
j3 4:ca27db159b10 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
j3 4:ca27db159b10 8 * and/or sell copies of the Software, and to permit persons to whom the
j3 4:ca27db159b10 9 * Software is furnished to do so, subject to the following conditions:
j3 4:ca27db159b10 10 *
j3 4:ca27db159b10 11 * The above copyright notice and this permission notice shall be included
j3 4:ca27db159b10 12 * in all copies or substantial portions of the Software.
j3 4:ca27db159b10 13 *
j3 4:ca27db159b10 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
j3 4:ca27db159b10 15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
j3 4:ca27db159b10 16 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
j3 4:ca27db159b10 17 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
j3 4:ca27db159b10 18 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
j3 4:ca27db159b10 19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
j3 4:ca27db159b10 20 * OTHER DEALINGS IN THE SOFTWARE.
j3 4:ca27db159b10 21 *
j3 4:ca27db159b10 22 * Except as contained in this notice, the name of Maxim Integrated
j3 4:ca27db159b10 23 * Products, Inc. shall not be used except as stated in the Maxim Integrated
j3 4:ca27db159b10 24 * Products, Inc. Branding Policy.
j3 4:ca27db159b10 25 *
j3 4:ca27db159b10 26 * The mere transfer of this software does not imply any licenses
j3 4:ca27db159b10 27 * of trade secrets, proprietary technology, copyrights, patents,
j3 4:ca27db159b10 28 * trademarks, maskwork rights, or any other form of intellectual
j3 4:ca27db159b10 29 * property whatsoever. Maxim Integrated Products, Inc. retains all
j3 4:ca27db159b10 30 * ownership rights.
j3 4:ca27db159b10 31 **********************************************************************/
j3 4:ca27db159b10 32
j3 4:ca27db159b10 33
j3 7:78a8857b3810 34 #include "ds28e17.h"
j3 4:ca27db159b10 35
j3 7:78a8857b3810 36 const uint16_t Ds28e17::_oddparity[] = { 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0 };
j3 4:ca27db159b10 37
j3 4:ca27db159b10 38
j3 4:ca27db159b10 39 //*********************************************************************
j3 22:686273e55cdc 40 Ds28e17::Ds28e17(OneWireMaster &owm)
j3 17:b646b1e3970b 41 :_owm(owm)
j3 4:ca27db159b10 42 {
j3 7:78a8857b3810 43
j3 4:ca27db159b10 44 }
j3 4:ca27db159b10 45
j3 4:ca27db159b10 46
j3 4:ca27db159b10 47 //*********************************************************************
j3 17:b646b1e3970b 48 void Ds28e17::set_rom_id(const RomId &romId)
j3 17:b646b1e3970b 49 {
j3 17:b646b1e3970b 50 _romId = romId;
j3 17:b646b1e3970b 51 }
j3 17:b646b1e3970b 52
j3 17:b646b1e3970b 53
j3 17:b646b1e3970b 54 //*********************************************************************
j3 17:b646b1e3970b 55 RomId Ds28e17::get_rom_id(void)
j3 17:b646b1e3970b 56 {
j3 17:b646b1e3970b 57 return _romId;
j3 17:b646b1e3970b 58 }
j3 17:b646b1e3970b 59
j3 17:b646b1e3970b 60
j3 17:b646b1e3970b 61 //*********************************************************************
j3 17:b646b1e3970b 62 Ds28e17::CmdResult Ds28e17::I2C_WriteDataWithStop(uint8_t I2C_addr, uint8_t length,
j3 7:78a8857b3810 63 uint8_t *data, uint8_t &status,
j3 7:78a8857b3810 64 uint8_t &wr_status)
j3 4:ca27db159b10 65 {
j3 17:b646b1e3970b 66 Ds28e17::CmdResult bridge_result = Ds28e17::OperationFailure;
j3 4:ca27db159b10 67
j3 7:78a8857b3810 68 size_t send_cnt = 0;
j3 7:78a8857b3810 69 uint8_t send_block[0xff];
j3 7:78a8857b3810 70
j3 23:e8e403d61359 71 OneWireMaster::CmdResult ow_result = _owm.OWMatchROM(_romId);
j3 17:b646b1e3970b 72
j3 23:e8e403d61359 73 if(ow_result == OneWireMaster::Success)
j3 7:78a8857b3810 74 {
j3 7:78a8857b3810 75 //seed the crc
j3 7:78a8857b3810 76 _crc16 = 0;
j3 7:78a8857b3810 77
j3 7:78a8857b3810 78 // Form the 1-Wire Packet
j3 7:78a8857b3810 79
j3 7:78a8857b3810 80 // I2C Write Data with Stop command
j3 7:78a8857b3810 81 send_block[send_cnt] = CMD_I2C_WRITE_W_STOP;
j3 7:78a8857b3810 82 docrc16(send_block[send_cnt++]);
j3 7:78a8857b3810 83
j3 7:78a8857b3810 84 // I2C Address
j3 7:78a8857b3810 85 send_block[send_cnt] = I2C_addr;
j3 7:78a8857b3810 86 docrc16(send_block[send_cnt++]);
j3 7:78a8857b3810 87
j3 7:78a8857b3810 88 // Length field
j3 7:78a8857b3810 89 send_block[send_cnt] = length;
j3 7:78a8857b3810 90 docrc16(send_block[send_cnt++]);
j3 7:78a8857b3810 91
j3 7:78a8857b3810 92 // Form the write data
j3 17:b646b1e3970b 93 for (size_t idx = 0; idx < length; idx++)
j3 7:78a8857b3810 94 {
j3 7:78a8857b3810 95 send_block[send_cnt] = data[idx];
j3 7:78a8857b3810 96 docrc16(send_block[send_cnt++]);
j3 7:78a8857b3810 97 }
j3 7:78a8857b3810 98
j3 7:78a8857b3810 99 // Form the CRC16
j3 7:78a8857b3810 100 _crc16 = _crc16^0xFFFF;
j3 7:78a8857b3810 101 send_block[send_cnt++] = ((uint8_t)(_crc16 & 0xFF));
j3 7:78a8857b3810 102 send_block[send_cnt++] = ((uint8_t)((_crc16 >> 8) & 0xFF));
j3 7:78a8857b3810 103
j3 7:78a8857b3810 104 // Send Packet
j3 17:b646b1e3970b 105 bridge_result = send_packet(send_block, send_cnt, status, wr_status);
j3 7:78a8857b3810 106 }
j3 4:ca27db159b10 107
j3 17:b646b1e3970b 108 return bridge_result;
j3 4:ca27db159b10 109 }
j3 4:ca27db159b10 110
j3 4:ca27db159b10 111
j3 4:ca27db159b10 112 //*********************************************************************
j3 17:b646b1e3970b 113 Ds28e17::CmdResult Ds28e17::I2C_WriteDataNoStop(uint8_t I2C_addr, uint8_t length,
j3 7:78a8857b3810 114 uint8_t *data, uint8_t &status,
j3 7:78a8857b3810 115 uint8_t &wr_status)
j3 4:ca27db159b10 116 {
j3 17:b646b1e3970b 117 Ds28e17::CmdResult bridge_result = Ds28e17::OperationFailure;
j3 4:ca27db159b10 118
j3 7:78a8857b3810 119 size_t send_cnt = 0;
j3 7:78a8857b3810 120 uint8_t send_block[0xff];
j3 7:78a8857b3810 121
j3 23:e8e403d61359 122 OneWireMaster::CmdResult ow_result = _owm.OWMatchROM(_romId);
j3 17:b646b1e3970b 123
j3 23:e8e403d61359 124 if(ow_result == OneWireMaster::Success)
j3 7:78a8857b3810 125 {
j3 7:78a8857b3810 126 // seed the crc
j3 7:78a8857b3810 127 _crc16 = 0;
j3 7:78a8857b3810 128
j3 7:78a8857b3810 129 // I2C Write Data with Stop command
j3 7:78a8857b3810 130 send_block[send_cnt] = CMD_I2C_WRITE_NO_STOP;
j3 7:78a8857b3810 131 docrc16(send_block[send_cnt++]);
j3 7:78a8857b3810 132
j3 7:78a8857b3810 133 // I2C Address
j3 7:78a8857b3810 134 send_block[send_cnt] = I2C_addr;
j3 7:78a8857b3810 135 docrc16(send_block[send_cnt++]);
j3 7:78a8857b3810 136
j3 7:78a8857b3810 137 // Length field
j3 7:78a8857b3810 138 send_block[send_cnt] = length;
j3 7:78a8857b3810 139 docrc16(send_block[send_cnt++]);
j3 7:78a8857b3810 140
j3 7:78a8857b3810 141 // Form the write data
j3 17:b646b1e3970b 142 for(size_t idx = 0; idx < length; idx++)
j3 7:78a8857b3810 143 {
j3 7:78a8857b3810 144 send_block[send_cnt] = data[idx];
j3 7:78a8857b3810 145 docrc16(send_block[send_cnt++]);
j3 7:78a8857b3810 146 }
j3 7:78a8857b3810 147
j3 7:78a8857b3810 148 // Form the CRC16
j3 7:78a8857b3810 149 _crc16 = _crc16^0xFFFF;
j3 7:78a8857b3810 150 send_block[send_cnt++] = ((uint8_t)(_crc16 & 0xFF));
j3 7:78a8857b3810 151 send_block[send_cnt++] = ((uint8_t)((_crc16 >> 8) & 0xFF));
j3 7:78a8857b3810 152
j3 7:78a8857b3810 153 // Send Packet
j3 17:b646b1e3970b 154 bridge_result = send_packet(send_block, send_cnt, status, wr_status);
j3 7:78a8857b3810 155 }
j3 7:78a8857b3810 156
j3 17:b646b1e3970b 157 return bridge_result;
j3 4:ca27db159b10 158 }
j3 4:ca27db159b10 159
j3 4:ca27db159b10 160
j3 4:ca27db159b10 161 //*********************************************************************
j3 17:b646b1e3970b 162 Ds28e17::CmdResult Ds28e17::I2C_WriteDataOnly(uint8_t length, uint8_t *data,
j3 7:78a8857b3810 163 uint8_t &status, uint8_t &wr_status)
j3 4:ca27db159b10 164 {
j3 17:b646b1e3970b 165 Ds28e17::CmdResult bridge_result = Ds28e17::OperationFailure;
j3 4:ca27db159b10 166
j3 7:78a8857b3810 167 size_t send_cnt = 0;
j3 7:78a8857b3810 168 uint8_t send_block[0xff];
j3 7:78a8857b3810 169
j3 23:e8e403d61359 170 OneWireMaster::CmdResult ow_result = _owm.OWMatchROM(_romId);
j3 17:b646b1e3970b 171
j3 23:e8e403d61359 172 if(ow_result == OneWireMaster::Success)
j3 7:78a8857b3810 173 {
j3 7:78a8857b3810 174 // seed the crc
j3 7:78a8857b3810 175 _crc16 = 0;
j3 7:78a8857b3810 176
j3 7:78a8857b3810 177 // Form the 1-Wire Packet
j3 7:78a8857b3810 178
j3 7:78a8857b3810 179 // I2C Write Data with Stop command
j3 7:78a8857b3810 180 send_block[send_cnt] = CMD_I2C_WRITE_ONLY;
j3 7:78a8857b3810 181 docrc16(send_block[send_cnt++]);
j3 7:78a8857b3810 182
j3 7:78a8857b3810 183 // Length field
j3 7:78a8857b3810 184 send_block[send_cnt] = length;
j3 7:78a8857b3810 185 docrc16(send_block[send_cnt++]);
j3 7:78a8857b3810 186
j3 7:78a8857b3810 187 // Form the write data
j3 17:b646b1e3970b 188 for (size_t idx = 0; idx < length; idx++)
j3 7:78a8857b3810 189 {
j3 7:78a8857b3810 190 send_block[send_cnt] = data[idx];
j3 7:78a8857b3810 191 docrc16(send_block[send_cnt++]);
j3 7:78a8857b3810 192 }
j3 7:78a8857b3810 193
j3 7:78a8857b3810 194 // Form the CRC16\
j3 7:78a8857b3810 195 _crc16 = _crc16^0xFFFF;
j3 7:78a8857b3810 196 send_block[send_cnt++] = (_crc16 & 0xFF);
j3 7:78a8857b3810 197 send_block[send_cnt++] = ((_crc16 >> 8) & 0xFF);
j3 7:78a8857b3810 198
j3 7:78a8857b3810 199 // Send Packet
j3 17:b646b1e3970b 200 bridge_result = send_packet(send_block, send_cnt, status, wr_status);
j3 7:78a8857b3810 201 }
j3 7:78a8857b3810 202
j3 17:b646b1e3970b 203 return bridge_result;
j3 4:ca27db159b10 204 }
j3 4:ca27db159b10 205
j3 4:ca27db159b10 206
j3 4:ca27db159b10 207 //*********************************************************************
j3 17:b646b1e3970b 208 Ds28e17::CmdResult Ds28e17::I2C_WriteDataOnlyWithStop(uint8_t length, uint8_t *data,
j3 7:78a8857b3810 209 uint8_t &status, uint8_t &wr_status)
j3 4:ca27db159b10 210 {
j3 17:b646b1e3970b 211 Ds28e17::CmdResult bridge_result = Ds28e17::OperationFailure;
j3 4:ca27db159b10 212
j3 7:78a8857b3810 213 size_t send_cnt = 0;
j3 7:78a8857b3810 214 uint8_t send_block[0xff];
j3 7:78a8857b3810 215
j3 23:e8e403d61359 216 OneWireMaster::CmdResult ow_result = _owm.OWMatchROM(_romId);
j3 17:b646b1e3970b 217
j3 23:e8e403d61359 218 if(ow_result == OneWireMaster::Success)
j3 7:78a8857b3810 219 {
j3 7:78a8857b3810 220 //seed the crc
j3 7:78a8857b3810 221 _crc16 = 0;
j3 7:78a8857b3810 222
j3 7:78a8857b3810 223 // Form the 1-Wire Packet
j3 7:78a8857b3810 224
j3 7:78a8857b3810 225 // I2C Write Data with Stop command
j3 7:78a8857b3810 226 send_block[send_cnt] = CMD_I2C_WRITE_ONLY_W_STOP;
j3 7:78a8857b3810 227 docrc16(send_block[send_cnt++]);
j3 7:78a8857b3810 228
j3 7:78a8857b3810 229 // Length field
j3 7:78a8857b3810 230 send_block[send_cnt] = length;
j3 7:78a8857b3810 231 docrc16(send_block[send_cnt++]);
j3 7:78a8857b3810 232
j3 7:78a8857b3810 233 // Form the write data
j3 17:b646b1e3970b 234 for (size_t idx = 0; idx < length; idx++)
j3 7:78a8857b3810 235 {
j3 7:78a8857b3810 236 send_block[send_cnt] = data[idx];
j3 7:78a8857b3810 237 docrc16(send_block[send_cnt++]);
j3 7:78a8857b3810 238 }
j3 7:78a8857b3810 239
j3 7:78a8857b3810 240 // Form the CRC16
j3 7:78a8857b3810 241 _crc16 = _crc16^0xFFFF;
j3 7:78a8857b3810 242 send_block[send_cnt++] = (_crc16 & 0xFF);
j3 7:78a8857b3810 243 send_block[send_cnt++] = ((_crc16 >> 8) & 0xFF);
j3 7:78a8857b3810 244
j3 7:78a8857b3810 245 // Send Packet
j3 17:b646b1e3970b 246 bridge_result = send_packet(send_block, send_cnt, status, wr_status);
j3 17:b646b1e3970b 247 }
j3 7:78a8857b3810 248
j3 17:b646b1e3970b 249 return bridge_result;
j3 4:ca27db159b10 250 }
j3 4:ca27db159b10 251
j3 4:ca27db159b10 252
j3 4:ca27db159b10 253 //*********************************************************************
j3 17:b646b1e3970b 254 Ds28e17::CmdResult Ds28e17::I2C_WriteReadDataWithStop(uint8_t I2C_addr, uint8_t length,
j3 4:ca27db159b10 255 uint8_t *data, uint8_t nu_bytes_read,
j3 7:78a8857b3810 256 uint8_t &status, uint8_t &wr_status,
j3 4:ca27db159b10 257 uint8_t *read_data)
j3 4:ca27db159b10 258 {
j3 17:b646b1e3970b 259 Ds28e17::CmdResult bridge_result = Ds28e17::OperationFailure;
j3 4:ca27db159b10 260
j3 7:78a8857b3810 261 size_t send_cnt = 0;
j3 7:78a8857b3810 262 size_t idx = 0;
j3 7:78a8857b3810 263 uint8_t send_block[0xff];
j3 7:78a8857b3810 264
j3 23:e8e403d61359 265 OneWireMaster::CmdResult ow_result = _owm.OWMatchROM(_romId);
j3 17:b646b1e3970b 266
j3 23:e8e403d61359 267 if(ow_result == OneWireMaster::Success)
j3 7:78a8857b3810 268 {
j3 7:78a8857b3810 269 //seed the crc
j3 7:78a8857b3810 270 _crc16 = 0;
j3 7:78a8857b3810 271
j3 7:78a8857b3810 272 // Form the 1-Wire Packet
j3 7:78a8857b3810 273
j3 7:78a8857b3810 274 // I2C Write Data with Stop command
j3 7:78a8857b3810 275 send_block[send_cnt] = CMD_I2C_WRITE_READ_W_STOP;
j3 7:78a8857b3810 276 docrc16(send_block[send_cnt++]);
j3 7:78a8857b3810 277
j3 7:78a8857b3810 278 // I2C Address
j3 7:78a8857b3810 279 send_block[send_cnt] = I2C_addr;
j3 7:78a8857b3810 280 docrc16(send_block[send_cnt++]);
j3 7:78a8857b3810 281
j3 7:78a8857b3810 282 // Length field
j3 7:78a8857b3810 283 send_block[send_cnt] = length;
j3 7:78a8857b3810 284 docrc16(send_block[send_cnt++]);
j3 7:78a8857b3810 285
j3 7:78a8857b3810 286 // Form the write data
j3 7:78a8857b3810 287 for (idx = 0; idx < length; idx++)
j3 7:78a8857b3810 288 {
j3 7:78a8857b3810 289 send_block[send_cnt] = data[idx];
j3 7:78a8857b3810 290 docrc16(send_block[send_cnt++]);
j3 7:78a8857b3810 291 }
j3 7:78a8857b3810 292
j3 7:78a8857b3810 293 // # of bytes to Read field
j3 7:78a8857b3810 294 send_block[send_cnt] = nu_bytes_read;
j3 7:78a8857b3810 295 docrc16(send_block[send_cnt++]);
j3 7:78a8857b3810 296
j3 7:78a8857b3810 297 // Form the CRC16
j3 7:78a8857b3810 298 _crc16 = _crc16^0xFFFF;
j3 7:78a8857b3810 299 send_block[send_cnt++] = (_crc16 & 0xFF);
j3 7:78a8857b3810 300 send_block[send_cnt++] = ((_crc16 >> 8) & 0xFF);
j3 7:78a8857b3810 301
j3 7:78a8857b3810 302 // Send Packet
j3 17:b646b1e3970b 303 bridge_result = send_packet(send_block, send_cnt, status, wr_status);
j3 17:b646b1e3970b 304 if(bridge_result == Ds28e17::Success)
j3 7:78a8857b3810 305 {
j3 17:b646b1e3970b 306 ow_result = _owm.OWReadBlock(read_data, nu_bytes_read);
j3 23:e8e403d61359 307 if(ow_result == OneWireMaster::Success)
j3 7:78a8857b3810 308 {
j3 17:b646b1e3970b 309 bridge_result = Ds28e17::Success;
j3 7:78a8857b3810 310 }
j3 17:b646b1e3970b 311 else
j3 17:b646b1e3970b 312 {
j3 17:b646b1e3970b 313 bridge_result = Ds28e17::CommsReadBlockError;
j3 17:b646b1e3970b 314 }
j3 7:78a8857b3810 315 }
j3 7:78a8857b3810 316 }
j3 7:78a8857b3810 317
j3 17:b646b1e3970b 318 return bridge_result;
j3 4:ca27db159b10 319 }
j3 4:ca27db159b10 320
j3 4:ca27db159b10 321
j3 4:ca27db159b10 322 //*********************************************************************
j3 17:b646b1e3970b 323 Ds28e17::CmdResult Ds28e17::I2C_ReadDataWithStop(uint8_t I2C_addr, uint8_t nu_bytes_read,
j3 7:78a8857b3810 324 uint8_t &status, uint8_t *read_data)
j3 4:ca27db159b10 325 {
j3 17:b646b1e3970b 326 Ds28e17::CmdResult bridge_result = Ds28e17::OperationFailure;
j3 4:ca27db159b10 327
j3 7:78a8857b3810 328 size_t send_cnt = 0;
j3 7:78a8857b3810 329 uint8_t send_block[0xff];
j3 7:78a8857b3810 330
j3 23:e8e403d61359 331 OneWireMaster::CmdResult ow_result = _owm.OWMatchROM(_romId);
j3 17:b646b1e3970b 332
j3 23:e8e403d61359 333 if(ow_result == OneWireMaster::Success)
j3 7:78a8857b3810 334 {
j3 7:78a8857b3810 335 //seed the crc for transmit bytes
j3 7:78a8857b3810 336 _crc16 = 0;
j3 7:78a8857b3810 337
j3 7:78a8857b3810 338 // Form the 1-Wire Packet to send
j3 7:78a8857b3810 339
j3 7:78a8857b3810 340 // I2C Write Data with Stop command
j3 7:78a8857b3810 341 send_block[send_cnt] = CMD_I2C_READ_W_STOP;
j3 7:78a8857b3810 342 docrc16(send_block[send_cnt++]);
j3 7:78a8857b3810 343
j3 7:78a8857b3810 344 // I2C Address
j3 7:78a8857b3810 345 send_block[send_cnt] = I2C_addr;
j3 7:78a8857b3810 346 docrc16(send_block[send_cnt++]);
j3 7:78a8857b3810 347
j3 7:78a8857b3810 348 // # of bytes to Read field
j3 7:78a8857b3810 349 send_block[send_cnt] = nu_bytes_read;
j3 7:78a8857b3810 350 docrc16(send_block[send_cnt++]);
j3 7:78a8857b3810 351
j3 7:78a8857b3810 352 // Form the CRC16
j3 7:78a8857b3810 353 _crc16 = _crc16^0xFFFF;
j3 7:78a8857b3810 354 send_block[send_cnt++] = (_crc16 & 0xFF);
j3 7:78a8857b3810 355 send_block[send_cnt++] = ((_crc16 >> 8) & 0xFF);
j3 7:78a8857b3810 356
j3 7:78a8857b3810 357 // Send Packet
j3 17:b646b1e3970b 358 bridge_result = send_packet(send_block, send_cnt, status);
j3 17:b646b1e3970b 359 if(bridge_result == Ds28e17::Success)
j3 7:78a8857b3810 360 {
j3 17:b646b1e3970b 361 ow_result = _owm.OWReadBlock(read_data, nu_bytes_read);
j3 23:e8e403d61359 362 if(ow_result == OneWireMaster::Success)
j3 7:78a8857b3810 363 {
j3 17:b646b1e3970b 364 bridge_result = Ds28e17::Success;
j3 7:78a8857b3810 365 }
j3 17:b646b1e3970b 366 else
j3 17:b646b1e3970b 367 {
j3 17:b646b1e3970b 368 bridge_result = Ds28e17::CommsReadBlockError;
j3 17:b646b1e3970b 369 }
j3 7:78a8857b3810 370 }
j3 7:78a8857b3810 371 }
j3 7:78a8857b3810 372
j3 17:b646b1e3970b 373 return bridge_result;
j3 4:ca27db159b10 374 }
j3 4:ca27db159b10 375
j3 4:ca27db159b10 376
j3 4:ca27db159b10 377 //*********************************************************************
j3 17:b646b1e3970b 378 Ds28e17::CmdResult Ds28e17::WriteConfigReg(uint8_t data)
j3 7:78a8857b3810 379 {
j3 17:b646b1e3970b 380 Ds28e17::CmdResult bridge_result = Ds28e17::OperationFailure;
j3 7:78a8857b3810 381
j3 23:e8e403d61359 382 OneWireMaster::CmdResult ow_result = _owm.OWMatchROM(_romId);
j3 17:b646b1e3970b 383
j3 23:e8e403d61359 384 if(ow_result == OneWireMaster::Success)
j3 7:78a8857b3810 385 {
j3 7:78a8857b3810 386 // Send CMD and Data
j3 17:b646b1e3970b 387 uint8_t send_block[] = {CMD_WRITE_CONFIG_REG, data};
j3 17:b646b1e3970b 388
j3 17:b646b1e3970b 389 ow_result = _owm.OWWriteBlock(send_block, 2);
j3 23:e8e403d61359 390 if(ow_result == OneWireMaster::Success)
j3 17:b646b1e3970b 391 {
j3 17:b646b1e3970b 392 _i2c_speed = data & 0x03; // Save off _i2c_speed setting to be used by other functions
j3 17:b646b1e3970b 393 bridge_result = Ds28e17::Success;
j3 17:b646b1e3970b 394 }
j3 17:b646b1e3970b 395 else
j3 17:b646b1e3970b 396 {
j3 17:b646b1e3970b 397 bridge_result = Ds28e17::CommsWriteBlockError;
j3 17:b646b1e3970b 398 }
j3 7:78a8857b3810 399 }
j3 7:78a8857b3810 400
j3 17:b646b1e3970b 401 return bridge_result;
j3 7:78a8857b3810 402 }
j3 7:78a8857b3810 403
j3 7:78a8857b3810 404
j3 7:78a8857b3810 405 //*********************************************************************
j3 17:b646b1e3970b 406 Ds28e17::CmdResult Ds28e17::ReadConfigReg(uint8_t & config)
j3 7:78a8857b3810 407 {
j3 17:b646b1e3970b 408 Ds28e17::CmdResult bridge_result = Ds28e17::OperationFailure;
j3 7:78a8857b3810 409
j3 23:e8e403d61359 410 OneWireMaster::CmdResult ow_result = _owm.OWMatchROM(_romId);
j3 17:b646b1e3970b 411
j3 23:e8e403d61359 412 if(ow_result == OneWireMaster::Success)
j3 7:78a8857b3810 413 {
j3 7:78a8857b3810 414 // Send CMD and receive Data
j3 17:b646b1e3970b 415 ow_result = _owm.OWWriteByte(CMD_READ_CONFIG_REG);
j3 23:e8e403d61359 416 if(ow_result == OneWireMaster::Success)
j3 17:b646b1e3970b 417 {
j3 17:b646b1e3970b 418 ow_result = _owm.OWReadByte(config);
j3 23:e8e403d61359 419 if(ow_result == OneWireMaster::Success)
j3 17:b646b1e3970b 420 {
j3 17:b646b1e3970b 421 bridge_result = Ds28e17::Success;
j3 17:b646b1e3970b 422 }
j3 17:b646b1e3970b 423 else
j3 17:b646b1e3970b 424 {
j3 17:b646b1e3970b 425 bridge_result = Ds28e17::CommsReadByteError;
j3 17:b646b1e3970b 426 }
j3 17:b646b1e3970b 427 }
j3 17:b646b1e3970b 428 else
j3 17:b646b1e3970b 429 {
j3 17:b646b1e3970b 430 bridge_result = Ds28e17::CommsWriteByteError;
j3 17:b646b1e3970b 431 }
j3 7:78a8857b3810 432 }
j3 7:78a8857b3810 433
j3 17:b646b1e3970b 434 return bridge_result;
j3 7:78a8857b3810 435 }
j3 7:78a8857b3810 436
j3 7:78a8857b3810 437
j3 7:78a8857b3810 438 //*********************************************************************
j3 17:b646b1e3970b 439 Ds28e17::CmdResult Ds28e17::DisableOWMode()
j3 7:78a8857b3810 440 {
j3 17:b646b1e3970b 441 Ds28e17::CmdResult bridge_result = Ds28e17::OperationFailure;
j3 7:78a8857b3810 442
j3 23:e8e403d61359 443 OneWireMaster::CmdResult ow_result = _owm.OWMatchROM(_romId);
j3 17:b646b1e3970b 444
j3 23:e8e403d61359 445 if(ow_result == OneWireMaster::Success)
j3 7:78a8857b3810 446 {
j3 7:78a8857b3810 447 // Send CMD
j3 17:b646b1e3970b 448 ow_result = _owm.OWWriteByte(CMD_DISABLE_OW_MODE);
j3 23:e8e403d61359 449 if(ow_result == OneWireMaster::Success)
j3 17:b646b1e3970b 450 {
j3 17:b646b1e3970b 451 bridge_result = Ds28e17::Success;
j3 17:b646b1e3970b 452 }
j3 17:b646b1e3970b 453 else
j3 17:b646b1e3970b 454 {
j3 17:b646b1e3970b 455 bridge_result = Ds28e17::CommsWriteByteError;
j3 17:b646b1e3970b 456 }
j3 7:78a8857b3810 457 }
j3 7:78a8857b3810 458
j3 17:b646b1e3970b 459 return bridge_result;
j3 7:78a8857b3810 460 }
j3 7:78a8857b3810 461
j3 7:78a8857b3810 462
j3 7:78a8857b3810 463 //*********************************************************************
j3 17:b646b1e3970b 464 Ds28e17::CmdResult Ds28e17::EnableSleepMode()
j3 7:78a8857b3810 465 {
j3 17:b646b1e3970b 466 Ds28e17::CmdResult bridge_result = Ds28e17::OperationFailure;
j3 7:78a8857b3810 467
j3 23:e8e403d61359 468 OneWireMaster::CmdResult ow_result = _owm.OWMatchROM(_romId);
j3 17:b646b1e3970b 469
j3 23:e8e403d61359 470 if(ow_result == OneWireMaster::Success)
j3 7:78a8857b3810 471 {
j3 7:78a8857b3810 472 // Send CMD
j3 17:b646b1e3970b 473 ow_result = _owm.OWWriteByte(CMD_ENABLE_SLEEP_MODE);
j3 23:e8e403d61359 474 if(ow_result == OneWireMaster::Success)
j3 17:b646b1e3970b 475 {
j3 17:b646b1e3970b 476 bridge_result = Ds28e17::Success;
j3 17:b646b1e3970b 477 }
j3 17:b646b1e3970b 478 else
j3 17:b646b1e3970b 479 {
j3 17:b646b1e3970b 480 bridge_result = Ds28e17::CommsWriteByteError;
j3 17:b646b1e3970b 481 }
j3 7:78a8857b3810 482 }
j3 7:78a8857b3810 483
j3 17:b646b1e3970b 484 return bridge_result;
j3 7:78a8857b3810 485 }
j3 7:78a8857b3810 486
j3 7:78a8857b3810 487
j3 7:78a8857b3810 488 //*********************************************************************
j3 17:b646b1e3970b 489 Ds28e17::CmdResult Ds28e17::ReadDeviceRevision(uint8_t & rev)
j3 7:78a8857b3810 490 {
j3 17:b646b1e3970b 491 Ds28e17::CmdResult bridge_result = Ds28e17::OperationFailure;
j3 7:78a8857b3810 492
j3 23:e8e403d61359 493 OneWireMaster::CmdResult ow_result = _owm.OWMatchROM(_romId);
j3 17:b646b1e3970b 494
j3 23:e8e403d61359 495 if(ow_result == OneWireMaster::Success)
j3 7:78a8857b3810 496 {
j3 7:78a8857b3810 497 // Send CMD and receive Data
j3 17:b646b1e3970b 498 ow_result = _owm.OWWriteByte(CMD_READ_DEVICE_REV);
j3 23:e8e403d61359 499 if(ow_result == OneWireMaster::Success)
j3 17:b646b1e3970b 500 {
j3 17:b646b1e3970b 501 ow_result = _owm.OWReadByte(rev);
j3 23:e8e403d61359 502 if(ow_result == OneWireMaster::Success)
j3 17:b646b1e3970b 503 {
j3 17:b646b1e3970b 504 bridge_result = Ds28e17::Success;
j3 17:b646b1e3970b 505 }
j3 17:b646b1e3970b 506 else
j3 17:b646b1e3970b 507 {
j3 17:b646b1e3970b 508 bridge_result = Ds28e17::CommsReadByteError;
j3 17:b646b1e3970b 509 }
j3 17:b646b1e3970b 510 }
j3 17:b646b1e3970b 511 else
j3 17:b646b1e3970b 512 {
j3 17:b646b1e3970b 513 bridge_result = Ds28e17::CommsWriteByteError;
j3 17:b646b1e3970b 514 }
j3 7:78a8857b3810 515 }
j3 7:78a8857b3810 516
j3 17:b646b1e3970b 517 return bridge_result;
j3 7:78a8857b3810 518 }
j3 7:78a8857b3810 519
j3 7:78a8857b3810 520
j3 7:78a8857b3810 521 //*********************************************************************
j3 7:78a8857b3810 522 uint16_t Ds28e17::docrc16(uint16_t data)
j3 7:78a8857b3810 523 {
j3 7:78a8857b3810 524 data = ((data ^ (_crc16 & 0xff)) & 0xff);
j3 7:78a8857b3810 525 _crc16 >>= 8;
j3 7:78a8857b3810 526
j3 7:78a8857b3810 527 if (Ds28e17::_oddparity[data & 0xf] ^ Ds28e17::_oddparity[data >> 4])
j3 7:78a8857b3810 528 {
j3 7:78a8857b3810 529 _crc16 ^= 0xc001;
j3 7:78a8857b3810 530 }
j3 7:78a8857b3810 531
j3 7:78a8857b3810 532 data <<= 6;
j3 7:78a8857b3810 533 _crc16 ^= data;
j3 7:78a8857b3810 534 data <<= 1;
j3 7:78a8857b3810 535 _crc16 ^= data;
j3 7:78a8857b3810 536
j3 7:78a8857b3810 537 return _crc16;
j3 7:78a8857b3810 538 }
j3 7:78a8857b3810 539
j3 7:78a8857b3810 540
j3 17:b646b1e3970b 541 //*********************************************************************
j3 17:b646b1e3970b 542 Ds28e17::CmdResult Ds28e17::send_packet(const uint8_t * data, uint8_t data_length,
j3 17:b646b1e3970b 543 uint8_t & status, uint8_t & wr_status)
j3 17:b646b1e3970b 544 {
j3 17:b646b1e3970b 545 Ds28e17::CmdResult bridge_result = Ds28e17::CommsWriteBlockError;
j3 17:b646b1e3970b 546 uint32_t poll_count = 0;
j3 17:b646b1e3970b 547
j3 23:e8e403d61359 548 OneWireMaster::CmdResult ow_result = _owm.OWWriteBlock(data, data_length);
j3 17:b646b1e3970b 549
j3 23:e8e403d61359 550 if(ow_result == OneWireMaster::Success)
j3 17:b646b1e3970b 551 {
j3 17:b646b1e3970b 552 // Poll for Zero 1-Wire bit and return if an error occurs
j3 17:b646b1e3970b 553 uint8_t recvbit = 0x01;
j3 17:b646b1e3970b 554 do
j3 17:b646b1e3970b 555 {
j3 17:b646b1e3970b 556 ow_result = _owm.OWReadBit(recvbit);
j3 17:b646b1e3970b 557 }
j3 23:e8e403d61359 558 while(recvbit && (poll_count++ < POLL_LIMIT) && (ow_result == OneWireMaster::Success));
j3 17:b646b1e3970b 559
j3 23:e8e403d61359 560 if(ow_result == OneWireMaster::Success)
j3 17:b646b1e3970b 561 {
j3 17:b646b1e3970b 562 if(poll_count < POLL_LIMIT)
j3 17:b646b1e3970b 563 {
j3 17:b646b1e3970b 564 //Read Status and write status
j3 17:b646b1e3970b 565 uint8_t read_block[2];
j3 17:b646b1e3970b 566
j3 17:b646b1e3970b 567 ow_result = _owm.OWReadBlock(read_block, 2);
j3 17:b646b1e3970b 568
j3 23:e8e403d61359 569 if(ow_result == OneWireMaster::Success)
j3 17:b646b1e3970b 570 {
j3 17:b646b1e3970b 571 status = read_block[0];
j3 17:b646b1e3970b 572 wr_status = read_block[1];
j3 17:b646b1e3970b 573 bridge_result = Ds28e17::Success;
j3 17:b646b1e3970b 574 }
j3 17:b646b1e3970b 575 else
j3 17:b646b1e3970b 576 {
j3 17:b646b1e3970b 577 bridge_result = Ds28e17::CommsReadBlockError;
j3 17:b646b1e3970b 578 }
j3 17:b646b1e3970b 579 }
j3 17:b646b1e3970b 580 else
j3 17:b646b1e3970b 581 {
j3 17:b646b1e3970b 582 bridge_result = Ds28e17::TimeoutError;
j3 17:b646b1e3970b 583 }
j3 17:b646b1e3970b 584 }
j3 17:b646b1e3970b 585 else
j3 17:b646b1e3970b 586 {
j3 17:b646b1e3970b 587 bridge_result = Ds28e17::CommsReadBitError;
j3 17:b646b1e3970b 588 }
j3 17:b646b1e3970b 589 }
j3 17:b646b1e3970b 590
j3 17:b646b1e3970b 591 return bridge_result;
j3 17:b646b1e3970b 592 }
j3 17:b646b1e3970b 593
j3 17:b646b1e3970b 594
j3 17:b646b1e3970b 595 //*********************************************************************
j3 17:b646b1e3970b 596 Ds28e17::CmdResult Ds28e17::send_packet(const uint8_t * data, uint8_t data_length,
j3 17:b646b1e3970b 597 uint8_t & status)
j3 17:b646b1e3970b 598 {
j3 17:b646b1e3970b 599 Ds28e17::CmdResult bridge_result = Ds28e17::CommsWriteBlockError;
j3 17:b646b1e3970b 600 uint32_t poll_count = 0;
j3 17:b646b1e3970b 601
j3 23:e8e403d61359 602 OneWireMaster::CmdResult ow_result = _owm.OWWriteBlock(data, data_length);
j3 17:b646b1e3970b 603
j3 23:e8e403d61359 604 if(ow_result == OneWireMaster::Success)
j3 17:b646b1e3970b 605 {
j3 17:b646b1e3970b 606 // Poll for Zero 1-Wire bit and return if an error occurs
j3 17:b646b1e3970b 607 uint8_t recvbit = 0x01;
j3 17:b646b1e3970b 608 do
j3 17:b646b1e3970b 609 {
j3 17:b646b1e3970b 610 ow_result = _owm.OWReadBit(recvbit);
j3 17:b646b1e3970b 611 }
j3 23:e8e403d61359 612 while(recvbit && (poll_count++ < POLL_LIMIT) && (ow_result == OneWireMaster::Success));
j3 17:b646b1e3970b 613
j3 23:e8e403d61359 614 if(ow_result == OneWireMaster::Success)
j3 17:b646b1e3970b 615 {
j3 17:b646b1e3970b 616 if(poll_count < POLL_LIMIT)
j3 17:b646b1e3970b 617 {
j3 17:b646b1e3970b 618 //Read Status
j3 17:b646b1e3970b 619 ow_result = _owm.OWReadByte(status);
j3 23:e8e403d61359 620 if(ow_result == OneWireMaster::Success)
j3 17:b646b1e3970b 621 {
j3 17:b646b1e3970b 622 bridge_result = Ds28e17::Success;
j3 17:b646b1e3970b 623 }
j3 17:b646b1e3970b 624 else
j3 17:b646b1e3970b 625 {
j3 17:b646b1e3970b 626 bridge_result = Ds28e17::CommsReadByteError;
j3 17:b646b1e3970b 627 }
j3 17:b646b1e3970b 628 }
j3 17:b646b1e3970b 629 else
j3 17:b646b1e3970b 630 {
j3 17:b646b1e3970b 631 bridge_result = Ds28e17::TimeoutError;
j3 17:b646b1e3970b 632 }
j3 17:b646b1e3970b 633 }
j3 17:b646b1e3970b 634 else
j3 17:b646b1e3970b 635 {
j3 17:b646b1e3970b 636 bridge_result = Ds28e17::CommsReadBitError;
j3 17:b646b1e3970b 637 }
j3 17:b646b1e3970b 638 }
j3 17:b646b1e3970b 639
j3 17:b646b1e3970b 640 return bridge_result;
j3 17:b646b1e3970b 641 }
j3 17:b646b1e3970b 642