Implementation of 1-Wire with added Alarm Search Functionality

Dependents:   Max32630_One_Wire_Interface

Committer:
j3
Date:
Tue Mar 08 18:00:40 2016 -0800
Revision:
13:d1bdb03703de
Parent:
7:78a8857b3810
Child:
17:b646b1e3970b
first commit from offline development, cross your fingers!

Who changed what in which revision?

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