Implementation of 1-Wire with added Alarm Search Functionality

Dependents:   Max32630_One_Wire_Interface

Committer:
IanBenzMaxim
Date:
Thu May 12 14:38:16 2016 -0500
Revision:
73:2cecc1372acc
Parent:
OneWire_Masters/OneWireMaster.cpp@71:562f5c702094
Child:
74:23be10c32fa3
Added namespaces. Renamed files and directories for consistency. Use <stdint.h> instead of <cstdint> since it is not supported by C++98.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
j3 5:ce108eeb878d 1 /******************************************************************//**
j3 5:ce108eeb878d 2 * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved.
j3 5:ce108eeb878d 3 *
j3 5:ce108eeb878d 4 * Permission is hereby granted, free of charge, to any person obtaining a
j3 5:ce108eeb878d 5 * copy of this software and associated documentation files (the "Software"),
j3 5:ce108eeb878d 6 * to deal in the Software without restriction, including without limitation
j3 5:ce108eeb878d 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
j3 5:ce108eeb878d 8 * and/or sell copies of the Software, and to permit persons to whom the
j3 5:ce108eeb878d 9 * Software is furnished to do so, subject to the following conditions:
j3 5:ce108eeb878d 10 *
j3 5:ce108eeb878d 11 * The above copyright notice and this permission notice shall be included
j3 5:ce108eeb878d 12 * in all copies or substantial portions of the Software.
j3 5:ce108eeb878d 13 *
j3 5:ce108eeb878d 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
j3 5:ce108eeb878d 15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
j3 5:ce108eeb878d 16 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
j3 5:ce108eeb878d 17 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
j3 5:ce108eeb878d 18 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
j3 5:ce108eeb878d 19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
j3 5:ce108eeb878d 20 * OTHER DEALINGS IN THE SOFTWARE.
j3 5:ce108eeb878d 21 *
j3 5:ce108eeb878d 22 * Except as contained in this notice, the name of Maxim Integrated
j3 5:ce108eeb878d 23 * Products, Inc. shall not be used except as stated in the Maxim Integrated
j3 5:ce108eeb878d 24 * Products, Inc. Branding Policy.
j3 5:ce108eeb878d 25 *
j3 5:ce108eeb878d 26 * The mere transfer of this software does not imply any licenses
j3 5:ce108eeb878d 27 * of trade secrets, proprietary technology, copyrights, patents,
j3 5:ce108eeb878d 28 * trademarks, maskwork rights, or any other form of intellectual
j3 5:ce108eeb878d 29 * property whatsoever. Maxim Integrated Products, Inc. retains all
j3 5:ce108eeb878d 30 * ownership rights.
j3 5:ce108eeb878d 31 **********************************************************************/
j3 5:ce108eeb878d 32
j3 15:f6cb0d906fb6 33 #include "OneWireMaster.h"
IanBenzMaxim 73:2cecc1372acc 34 #include "RomId.h"
IanBenzMaxim 27:d5aaefa252f1 35
IanBenzMaxim 73:2cecc1372acc 36 using OneWire::Masters::OneWireMaster;
j3 15:f6cb0d906fb6 37
IanBenzMaxim 43:23017dcd2ec3 38 enum OW_ROM_CMD
IanBenzMaxim 43:23017dcd2ec3 39 {
IanBenzMaxim 43:23017dcd2ec3 40 READ_ROM = 0x33,
IanBenzMaxim 43:23017dcd2ec3 41 MATCH_ROM = 0x55,
IanBenzMaxim 43:23017dcd2ec3 42 SEARCH_ROM = 0xF0,
IanBenzMaxim 43:23017dcd2ec3 43 SKIP_ROM = 0xCC,
IanBenzMaxim 43:23017dcd2ec3 44 RESUME = 0xA5,
IanBenzMaxim 43:23017dcd2ec3 45 OVERDRIVE_SKIP_ROM = 0x3C,
IanBenzMaxim 43:23017dcd2ec3 46 OVERDRIVE_MATCH_ROM = 0x69
IanBenzMaxim 43:23017dcd2ec3 47 };
j3 15:f6cb0d906fb6 48
j3 15:f6cb0d906fb6 49
IanBenzMaxim 71:562f5c702094 50 //*********************************************************************
IanBenzMaxim 71:562f5c702094 51 OneWireMaster::CmdResult OneWireMaster::OWWriteBlock(const uint8_t *tran_buf, uint8_t tran_len)
IanBenzMaxim 71:562f5c702094 52 {
IanBenzMaxim 71:562f5c702094 53 CmdResult result;
IanBenzMaxim 71:562f5c702094 54
IanBenzMaxim 71:562f5c702094 55 for(uint8_t idx = 0; idx < tran_len; idx++)
IanBenzMaxim 71:562f5c702094 56 {
IanBenzMaxim 71:562f5c702094 57 result = OWWriteByte(tran_buf[idx]);
IanBenzMaxim 71:562f5c702094 58 if(result != Success)
IanBenzMaxim 71:562f5c702094 59 {
IanBenzMaxim 71:562f5c702094 60 break;
IanBenzMaxim 71:562f5c702094 61 }
IanBenzMaxim 71:562f5c702094 62 }
IanBenzMaxim 71:562f5c702094 63
IanBenzMaxim 71:562f5c702094 64 return result;
IanBenzMaxim 71:562f5c702094 65 }
IanBenzMaxim 71:562f5c702094 66
IanBenzMaxim 71:562f5c702094 67
IanBenzMaxim 71:562f5c702094 68 //*********************************************************************
IanBenzMaxim 71:562f5c702094 69 OneWireMaster::CmdResult OneWireMaster::OWReadBlock(uint8_t *rx_buf, uint8_t rx_len)
IanBenzMaxim 71:562f5c702094 70 {
IanBenzMaxim 71:562f5c702094 71 CmdResult result;
IanBenzMaxim 71:562f5c702094 72
IanBenzMaxim 71:562f5c702094 73 for(uint8_t idx = 0; idx < rx_len; idx++)
IanBenzMaxim 71:562f5c702094 74 {
IanBenzMaxim 71:562f5c702094 75 result = OWReadByte(rx_buf[idx]);
IanBenzMaxim 71:562f5c702094 76 if(result != Success)
IanBenzMaxim 71:562f5c702094 77 {
IanBenzMaxim 71:562f5c702094 78 break;
IanBenzMaxim 71:562f5c702094 79 }
IanBenzMaxim 71:562f5c702094 80 }
IanBenzMaxim 71:562f5c702094 81
IanBenzMaxim 71:562f5c702094 82 return result;
IanBenzMaxim 71:562f5c702094 83 }
IanBenzMaxim 71:562f5c702094 84
IanBenzMaxim 71:562f5c702094 85
IanBenzMaxim 32:bce180b544ed 86 OneWireMaster::CmdResult OneWireMaster::OWFirst(SearchState & searchState)
j3 15:f6cb0d906fb6 87 {
j3 17:b646b1e3970b 88 // reset the search state
IanBenzMaxim 32:bce180b544ed 89 searchState.reset();
j3 17:b646b1e3970b 90
IanBenzMaxim 32:bce180b544ed 91 return OWSearch(searchState);
j3 15:f6cb0d906fb6 92 }
j3 15:f6cb0d906fb6 93
j3 15:f6cb0d906fb6 94
j3 15:f6cb0d906fb6 95 //*********************************************************************
IanBenzMaxim 32:bce180b544ed 96 OneWireMaster::CmdResult OneWireMaster::OWNext(SearchState & searchState)
j3 15:f6cb0d906fb6 97 {
j3 17:b646b1e3970b 98 // leave the search state alone
IanBenzMaxim 32:bce180b544ed 99 return OWSearch(searchState);
j3 15:f6cb0d906fb6 100 }
j3 15:f6cb0d906fb6 101
j3 15:f6cb0d906fb6 102
j3 15:f6cb0d906fb6 103 //*********************************************************************
j3 23:e8e403d61359 104 OneWireMaster::CmdResult OneWireMaster::OWVerify(const RomId & romId)
j3 15:f6cb0d906fb6 105 {
j3 23:e8e403d61359 106 OneWireMaster::CmdResult result;
IanBenzMaxim 32:bce180b544ed 107 SearchState searchState;
j3 42:698635a0d073 108
j3 42:698635a0d073 109 searchState.romId = romId;
j3 15:f6cb0d906fb6 110
j3 15:f6cb0d906fb6 111 // set search to find the same device
IanBenzMaxim 32:bce180b544ed 112 searchState.last_discrepancy = 64;
IanBenzMaxim 32:bce180b544ed 113 searchState.last_device_flag = false;
j3 15:f6cb0d906fb6 114
IanBenzMaxim 32:bce180b544ed 115 result = OWSearch(searchState);
j3 23:e8e403d61359 116 if (result == OneWireMaster::Success)
j3 15:f6cb0d906fb6 117 {
j3 15:f6cb0d906fb6 118 // check if same device found
IanBenzMaxim 32:bce180b544ed 119 if (romId != searchState.romId)
j3 15:f6cb0d906fb6 120 {
j3 23:e8e403d61359 121 result = OneWireMaster::OperationFailure;
j3 15:f6cb0d906fb6 122 }
j3 15:f6cb0d906fb6 123 }
j3 15:f6cb0d906fb6 124
j3 17:b646b1e3970b 125 return result;
j3 15:f6cb0d906fb6 126 }
j3 15:f6cb0d906fb6 127
j3 15:f6cb0d906fb6 128
j3 15:f6cb0d906fb6 129 //*********************************************************************
IanBenzMaxim 32:bce180b544ed 130 void OneWireMaster::OWTargetSetup(SearchState & searchState)
j3 15:f6cb0d906fb6 131 {
j3 15:f6cb0d906fb6 132 // set the search state to find SearchFamily type devices
IanBenzMaxim 73:2cecc1372acc 133 uint8_t familyCode = searchState.romId.familyCode();
IanBenzMaxim 32:bce180b544ed 134 searchState.reset();
IanBenzMaxim 32:bce180b544ed 135 searchState.romId.setFamilyCode(familyCode);
IanBenzMaxim 32:bce180b544ed 136 searchState.last_discrepancy = 64;
j3 15:f6cb0d906fb6 137 }
j3 5:ce108eeb878d 138
j3 5:ce108eeb878d 139
j3 5:ce108eeb878d 140 //*********************************************************************
IanBenzMaxim 32:bce180b544ed 141 void OneWireMaster::OWFamilySkipSetup(SearchState & searchState)
j3 15:f6cb0d906fb6 142 {
j3 15:f6cb0d906fb6 143 // set the Last discrepancy to last family discrepancy
IanBenzMaxim 32:bce180b544ed 144 searchState.last_discrepancy = searchState.last_family_discrepancy;
j3 17:b646b1e3970b 145
j3 15:f6cb0d906fb6 146 // clear the last family discrpepancy
IanBenzMaxim 32:bce180b544ed 147 searchState.last_family_discrepancy = 0;
j3 17:b646b1e3970b 148
j3 15:f6cb0d906fb6 149 // check for end of list
IanBenzMaxim 32:bce180b544ed 150 if (searchState.last_discrepancy == 0)
j3 15:f6cb0d906fb6 151 {
IanBenzMaxim 32:bce180b544ed 152 searchState.last_device_flag = true;
j3 15:f6cb0d906fb6 153 }
j3 15:f6cb0d906fb6 154 }
j3 15:f6cb0d906fb6 155
j3 15:f6cb0d906fb6 156
j3 15:f6cb0d906fb6 157 //*********************************************************************
j3 23:e8e403d61359 158 OneWireMaster::CmdResult OneWireMaster::OWReadROM(RomId & romId)
j3 15:f6cb0d906fb6 159 {
j3 23:e8e403d61359 160 OneWireMaster::CmdResult result;
j3 17:b646b1e3970b 161 uint8_t buf[2 + RomId::byteLen];
j3 17:b646b1e3970b 162
j3 17:b646b1e3970b 163 result = OWReset();
j3 23:e8e403d61359 164 if (result == OneWireMaster::Success)
j3 15:f6cb0d906fb6 165 {
IanBenzMaxim 24:8942d8478d68 166 result = OWWriteByte(READ_ROM);
j3 17:b646b1e3970b 167 }
j3 17:b646b1e3970b 168
j3 17:b646b1e3970b 169 // read the ROM
j3 23:e8e403d61359 170 if (result == OneWireMaster::Success)
j3 17:b646b1e3970b 171 {
j3 17:b646b1e3970b 172 result = OWReadBlock(buf, RomId::byteLen);
j3 15:f6cb0d906fb6 173 }
j3 15:f6cb0d906fb6 174
j3 17:b646b1e3970b 175 // verify CRC8
IanBenzMaxim 24:8942d8478d68 176 if ((result == OneWireMaster::Success) && (RomId::calculateCRC8(buf, RomId::byteLen) == 0))
j3 17:b646b1e3970b 177 {
IanBenzMaxim 73:2cecc1372acc 178 romId = RomId(reinterpret_cast<uint8_t (&)[RomId::byteLen]>(buf[0]));
j3 17:b646b1e3970b 179 }
j3 17:b646b1e3970b 180
j3 17:b646b1e3970b 181 return result;
j3 15:f6cb0d906fb6 182 }
j3 15:f6cb0d906fb6 183
j3 15:f6cb0d906fb6 184
j3 15:f6cb0d906fb6 185 //*********************************************************************
j3 23:e8e403d61359 186 OneWireMaster::CmdResult OneWireMaster::OWSkipROM(void)
j3 15:f6cb0d906fb6 187 {
j3 23:e8e403d61359 188 OneWireMaster::CmdResult result;
j3 15:f6cb0d906fb6 189
j3 17:b646b1e3970b 190 result = OWReset();
j3 23:e8e403d61359 191 if (result == OneWireMaster::Success)
j3 15:f6cb0d906fb6 192 {
IanBenzMaxim 24:8942d8478d68 193 result = OWWriteByte(SKIP_ROM);
j3 15:f6cb0d906fb6 194 }
j3 15:f6cb0d906fb6 195
j3 17:b646b1e3970b 196 return result;
j3 15:f6cb0d906fb6 197 }
j3 15:f6cb0d906fb6 198
j3 15:f6cb0d906fb6 199
j3 15:f6cb0d906fb6 200 //*********************************************************************
j3 23:e8e403d61359 201 OneWireMaster::CmdResult OneWireMaster::OWMatchROM(const RomId & romId)
j3 15:f6cb0d906fb6 202 {
j3 23:e8e403d61359 203 OneWireMaster::CmdResult result;
j3 15:f6cb0d906fb6 204
j3 17:b646b1e3970b 205 uint8_t buf[1 + RomId::byteLen];
j3 17:b646b1e3970b 206
j3 17:b646b1e3970b 207 // use MatchROM
j3 17:b646b1e3970b 208 result = OWReset();
j3 23:e8e403d61359 209 if (result == OneWireMaster::Success)
j3 15:f6cb0d906fb6 210 {
IanBenzMaxim 24:8942d8478d68 211 buf[0] = MATCH_ROM;
j3 17:b646b1e3970b 212 std::memcpy(&buf[1], romId, RomId::byteLen);
j3 17:b646b1e3970b 213 // send command and rom
j3 17:b646b1e3970b 214 result = OWWriteBlock(buf, 1 + RomId::byteLen);
j3 15:f6cb0d906fb6 215 }
j3 17:b646b1e3970b 216
j3 17:b646b1e3970b 217 return result;
j3 15:f6cb0d906fb6 218 }
j3 15:f6cb0d906fb6 219
j3 15:f6cb0d906fb6 220
j3 15:f6cb0d906fb6 221 //*********************************************************************
j3 23:e8e403d61359 222 OneWireMaster::CmdResult OneWireMaster::OWOverdriveSkipROM(void)
j3 15:f6cb0d906fb6 223 {
IanBenzMaxim 32:bce180b544ed 224 OneWireMaster::CmdResult result = OWSetSpeed(SPEED_STANDARD);
j3 15:f6cb0d906fb6 225
j3 23:e8e403d61359 226 if (result == OneWireMaster::Success)
j3 15:f6cb0d906fb6 227 {
j3 17:b646b1e3970b 228 result = OWReset();
j3 15:f6cb0d906fb6 229 }
j3 15:f6cb0d906fb6 230
j3 23:e8e403d61359 231 if (result == OneWireMaster::Success)
j3 17:b646b1e3970b 232 {
IanBenzMaxim 24:8942d8478d68 233 result = OWWriteByte(OVERDRIVE_SKIP_ROM);
j3 17:b646b1e3970b 234 }
j3 17:b646b1e3970b 235
j3 23:e8e403d61359 236 if (result == OneWireMaster::Success)
j3 17:b646b1e3970b 237 {
IanBenzMaxim 32:bce180b544ed 238 result = OWSetSpeed(SPEED_OVERDRIVE);
j3 17:b646b1e3970b 239 }
j3 17:b646b1e3970b 240
j3 17:b646b1e3970b 241 return result;
j3 15:f6cb0d906fb6 242 }
j3 15:f6cb0d906fb6 243
j3 15:f6cb0d906fb6 244
j3 15:f6cb0d906fb6 245 //*********************************************************************
j3 23:e8e403d61359 246 OneWireMaster::CmdResult OneWireMaster::OWOverdriveMatchROM(const RomId & romId)
j3 15:f6cb0d906fb6 247 {
j3 23:e8e403d61359 248 OneWireMaster::CmdResult result;
j3 15:f6cb0d906fb6 249
j3 17:b646b1e3970b 250 // use overdrive MatchROM
IanBenzMaxim 32:bce180b544ed 251 OWSetSpeed(SPEED_STANDARD);
j3 17:b646b1e3970b 252
j3 17:b646b1e3970b 253 result = OWReset();
j3 23:e8e403d61359 254 if (result == OneWireMaster::Success)
j3 15:f6cb0d906fb6 255 {
IanBenzMaxim 24:8942d8478d68 256 result = OWWriteByte(OVERDRIVE_MATCH_ROM);
j3 23:e8e403d61359 257 if (result == OneWireMaster::Success)
j3 15:f6cb0d906fb6 258 {
IanBenzMaxim 32:bce180b544ed 259 OWSetSpeed(SPEED_OVERDRIVE);
j3 17:b646b1e3970b 260 // send ROM
j3 17:b646b1e3970b 261 result = OWWriteBlock(romId, RomId::byteLen);
j3 15:f6cb0d906fb6 262 }
j3 15:f6cb0d906fb6 263 }
j3 17:b646b1e3970b 264 return result;
j3 15:f6cb0d906fb6 265 }
j3 15:f6cb0d906fb6 266
j3 15:f6cb0d906fb6 267
j3 15:f6cb0d906fb6 268 //*********************************************************************
j3 23:e8e403d61359 269 OneWireMaster::CmdResult OneWireMaster::OWResume(void)
j3 15:f6cb0d906fb6 270 {
j3 23:e8e403d61359 271 OneWireMaster::CmdResult result;
j3 15:f6cb0d906fb6 272
j3 17:b646b1e3970b 273 result = OWReset();
j3 23:e8e403d61359 274 if (result == OneWireMaster::Success)
j3 15:f6cb0d906fb6 275 {
IanBenzMaxim 24:8942d8478d68 276 result = OWWriteByte(RESUME);
j3 15:f6cb0d906fb6 277 }
j3 15:f6cb0d906fb6 278
j3 17:b646b1e3970b 279 return result;
j3 15:f6cb0d906fb6 280 }
j3 15:f6cb0d906fb6 281
j3 15:f6cb0d906fb6 282
IanBenzMaxim 32:bce180b544ed 283 OneWireMaster::CmdResult OneWireMaster::OWSearch(SearchState & searchState)
IanBenzMaxim 32:bce180b544ed 284 {
IanBenzMaxim 73:2cecc1372acc 285 uint8_t id_bit_number;
IanBenzMaxim 73:2cecc1372acc 286 uint8_t last_zero, rom_byte_number;
IanBenzMaxim 73:2cecc1372acc 287 uint8_t id_bit, cmp_id_bit;
IanBenzMaxim 73:2cecc1372acc 288 uint8_t rom_byte_mask;
IanBenzMaxim 32:bce180b544ed 289 bool search_result;
IanBenzMaxim 73:2cecc1372acc 290 uint8_t crc8 = 0;
IanBenzMaxim 32:bce180b544ed 291 SearchDirection search_direction;
IanBenzMaxim 32:bce180b544ed 292
IanBenzMaxim 32:bce180b544ed 293 // initialize for search
IanBenzMaxim 32:bce180b544ed 294 id_bit_number = 1;
IanBenzMaxim 32:bce180b544ed 295 last_zero = 0;
IanBenzMaxim 32:bce180b544ed 296 rom_byte_number = 0;
IanBenzMaxim 32:bce180b544ed 297 rom_byte_mask = 1;
IanBenzMaxim 32:bce180b544ed 298 search_result = false;
IanBenzMaxim 32:bce180b544ed 299
IanBenzMaxim 32:bce180b544ed 300 // if the last call was not the last one
IanBenzMaxim 32:bce180b544ed 301 if (!searchState.last_device_flag)
IanBenzMaxim 32:bce180b544ed 302 {
IanBenzMaxim 32:bce180b544ed 303 // 1-Wire reset
IanBenzMaxim 32:bce180b544ed 304 OneWireMaster::CmdResult result = OWReset();
IanBenzMaxim 32:bce180b544ed 305 if (result != OneWireMaster::Success)
IanBenzMaxim 32:bce180b544ed 306 {
IanBenzMaxim 32:bce180b544ed 307 // reset the search
IanBenzMaxim 32:bce180b544ed 308 searchState.reset();
IanBenzMaxim 32:bce180b544ed 309 return result;
IanBenzMaxim 32:bce180b544ed 310 }
IanBenzMaxim 32:bce180b544ed 311
IanBenzMaxim 32:bce180b544ed 312 // issue the search command
IanBenzMaxim 32:bce180b544ed 313 OneWireMaster::OWWriteByte(SEARCH_ROM);
IanBenzMaxim 32:bce180b544ed 314
IanBenzMaxim 32:bce180b544ed 315 // loop to do the search
IanBenzMaxim 32:bce180b544ed 316 do
IanBenzMaxim 32:bce180b544ed 317 {
IanBenzMaxim 32:bce180b544ed 318 // if this discrepancy if before the Last Discrepancy
IanBenzMaxim 32:bce180b544ed 319 // on a previous next then pick the same as last time
IanBenzMaxim 32:bce180b544ed 320 if (id_bit_number < searchState.last_discrepancy)
IanBenzMaxim 32:bce180b544ed 321 {
IanBenzMaxim 32:bce180b544ed 322 if ((searchState.romId[rom_byte_number] & rom_byte_mask) > 0)
IanBenzMaxim 32:bce180b544ed 323 search_direction = DIRECTION_WRITE_ONE;
IanBenzMaxim 32:bce180b544ed 324 else
IanBenzMaxim 32:bce180b544ed 325 search_direction = DIRECTION_WRITE_ZERO;
IanBenzMaxim 32:bce180b544ed 326 }
IanBenzMaxim 32:bce180b544ed 327 else
IanBenzMaxim 32:bce180b544ed 328 {
IanBenzMaxim 32:bce180b544ed 329 // if equal to last pick 1, if not then pick 0
IanBenzMaxim 32:bce180b544ed 330 if (id_bit_number == searchState.last_discrepancy)
IanBenzMaxim 32:bce180b544ed 331 search_direction = DIRECTION_WRITE_ONE;
IanBenzMaxim 32:bce180b544ed 332 else
IanBenzMaxim 32:bce180b544ed 333 search_direction = DIRECTION_WRITE_ZERO;
IanBenzMaxim 32:bce180b544ed 334 }
IanBenzMaxim 32:bce180b544ed 335
IanBenzMaxim 32:bce180b544ed 336 // Peform a triple operation on the DS2465 which will perform 2 read bits and 1 write bit
IanBenzMaxim 32:bce180b544ed 337 result = OWTriplet(search_direction, id_bit, cmp_id_bit);
IanBenzMaxim 32:bce180b544ed 338 if (result != OneWireMaster::Success)
IanBenzMaxim 32:bce180b544ed 339 return result;
IanBenzMaxim 32:bce180b544ed 340
IanBenzMaxim 32:bce180b544ed 341 // check for no devices on 1-wire
IanBenzMaxim 32:bce180b544ed 342 if ((id_bit) && (cmp_id_bit))
IanBenzMaxim 32:bce180b544ed 343 break;
IanBenzMaxim 32:bce180b544ed 344 else
IanBenzMaxim 32:bce180b544ed 345 {
IanBenzMaxim 32:bce180b544ed 346 if ((!id_bit) && (!cmp_id_bit) && (search_direction == DIRECTION_WRITE_ZERO))
IanBenzMaxim 32:bce180b544ed 347 {
IanBenzMaxim 32:bce180b544ed 348 last_zero = id_bit_number;
IanBenzMaxim 32:bce180b544ed 349
IanBenzMaxim 32:bce180b544ed 350 // check for Last discrepancy in family
IanBenzMaxim 32:bce180b544ed 351 if (last_zero < 9)
IanBenzMaxim 32:bce180b544ed 352 searchState.last_family_discrepancy = last_zero;
IanBenzMaxim 32:bce180b544ed 353 }
IanBenzMaxim 32:bce180b544ed 354
IanBenzMaxim 32:bce180b544ed 355 // set or clear the bit in the ROM byte rom_byte_number
IanBenzMaxim 32:bce180b544ed 356 // with mask rom_byte_mask
IanBenzMaxim 32:bce180b544ed 357 if (search_direction == DIRECTION_WRITE_ONE)
IanBenzMaxim 32:bce180b544ed 358 searchState.romId[rom_byte_number] |= rom_byte_mask;
IanBenzMaxim 32:bce180b544ed 359 else
IanBenzMaxim 73:2cecc1372acc 360 searchState.romId[rom_byte_number] &= (uint8_t)~rom_byte_mask;
IanBenzMaxim 32:bce180b544ed 361
IanBenzMaxim 32:bce180b544ed 362 // increment the byte counter id_bit_number
IanBenzMaxim 32:bce180b544ed 363 // and shift the mask rom_byte_mask
IanBenzMaxim 32:bce180b544ed 364 id_bit_number++;
IanBenzMaxim 32:bce180b544ed 365 rom_byte_mask <<= 1;
IanBenzMaxim 32:bce180b544ed 366
IanBenzMaxim 32:bce180b544ed 367 // if the mask is 0 then go to new SerialNum byte rom_byte_number and reset mask
IanBenzMaxim 32:bce180b544ed 368 if (rom_byte_mask == 0)
IanBenzMaxim 32:bce180b544ed 369 {
IanBenzMaxim 32:bce180b544ed 370 crc8 = RomId::calculateCRC8(crc8, searchState.romId[rom_byte_number]); // accumulate the CRC
IanBenzMaxim 32:bce180b544ed 371 rom_byte_number++;
IanBenzMaxim 32:bce180b544ed 372 rom_byte_mask = 1;
IanBenzMaxim 32:bce180b544ed 373 }
IanBenzMaxim 32:bce180b544ed 374 }
IanBenzMaxim 32:bce180b544ed 375 }
IanBenzMaxim 32:bce180b544ed 376 while(rom_byte_number < RomId::byteLen); // loop until through all ROM bytes 0-7
IanBenzMaxim 32:bce180b544ed 377
IanBenzMaxim 32:bce180b544ed 378 // if the search was successful then
IanBenzMaxim 32:bce180b544ed 379 if (!((id_bit_number <= (RomId::byteLen * 8)) || (crc8 != 0)))
IanBenzMaxim 32:bce180b544ed 380 {
IanBenzMaxim 32:bce180b544ed 381 // search successful so set m_last_discrepancy,m_last_device_flag,search_result
IanBenzMaxim 32:bce180b544ed 382 searchState.last_discrepancy = last_zero;
IanBenzMaxim 32:bce180b544ed 383
IanBenzMaxim 32:bce180b544ed 384 // check for last device
IanBenzMaxim 32:bce180b544ed 385 if (searchState.last_discrepancy == 0)
IanBenzMaxim 32:bce180b544ed 386 searchState.last_device_flag = true;
IanBenzMaxim 32:bce180b544ed 387
IanBenzMaxim 32:bce180b544ed 388 search_result = true;
IanBenzMaxim 32:bce180b544ed 389 }
IanBenzMaxim 32:bce180b544ed 390 }
IanBenzMaxim 32:bce180b544ed 391
IanBenzMaxim 32:bce180b544ed 392 // if no device found then reset counters so next 'search' will be like a first
IanBenzMaxim 32:bce180b544ed 393 if (!search_result || (searchState.romId.familyCode() == 0))
IanBenzMaxim 32:bce180b544ed 394 {
IanBenzMaxim 32:bce180b544ed 395 searchState.reset();
IanBenzMaxim 32:bce180b544ed 396 search_result = false;
IanBenzMaxim 32:bce180b544ed 397 }
IanBenzMaxim 32:bce180b544ed 398
IanBenzMaxim 32:bce180b544ed 399 return search_result ? OneWireMaster::Success : OneWireMaster::OperationFailure;
IanBenzMaxim 32:bce180b544ed 400 }
IanBenzMaxim 32:bce180b544ed 401
IanBenzMaxim 32:bce180b544ed 402
IanBenzMaxim 73:2cecc1372acc 403 OneWireMaster::CmdResult OneWireMaster::OWTriplet(SearchDirection & search_direction, uint8_t & sbr, uint8_t & tsb)
IanBenzMaxim 32:bce180b544ed 404 {
IanBenzMaxim 32:bce180b544ed 405 CmdResult result;
IanBenzMaxim 32:bce180b544ed 406 result = OWReadBit(sbr);
IanBenzMaxim 32:bce180b544ed 407 if (result == Success)
IanBenzMaxim 32:bce180b544ed 408 result = OWReadBit(tsb);
IanBenzMaxim 32:bce180b544ed 409 if (result == Success)
IanBenzMaxim 32:bce180b544ed 410 {
IanBenzMaxim 57:1635f247ceae 411 if (sbr)
IanBenzMaxim 32:bce180b544ed 412 search_direction = DIRECTION_WRITE_ONE;
IanBenzMaxim 57:1635f247ceae 413 else if (tsb)
IanBenzMaxim 32:bce180b544ed 414 search_direction = DIRECTION_WRITE_ZERO;
IanBenzMaxim 32:bce180b544ed 415 // else: use search_direction parameter
IanBenzMaxim 32:bce180b544ed 416
IanBenzMaxim 32:bce180b544ed 417 result = OWWriteBit((search_direction == DIRECTION_WRITE_ONE) ? 1 : 0);
IanBenzMaxim 32:bce180b544ed 418 }
IanBenzMaxim 32:bce180b544ed 419 return result;
IanBenzMaxim 32:bce180b544ed 420 }
IanBenzMaxim 32:bce180b544ed 421
IanBenzMaxim 32:bce180b544ed 422
j3 17:b646b1e3970b 423 uint16_t OneWireMaster::calculateCRC16(uint16_t CRC16, uint16_t data)
j3 15:f6cb0d906fb6 424 {
IanBenzMaxim 43:23017dcd2ec3 425 const uint16_t _oddparity[] = { 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0 };
IanBenzMaxim 43:23017dcd2ec3 426
j3 17:b646b1e3970b 427 data = (data ^ (CRC16 & 0xff)) & 0xff;
j3 17:b646b1e3970b 428 CRC16 >>= 8;
j3 17:b646b1e3970b 429
j3 17:b646b1e3970b 430 if (_oddparity[data & 0xf] ^ _oddparity[data >> 4])
j3 17:b646b1e3970b 431 CRC16 ^= 0xc001;
j3 17:b646b1e3970b 432
j3 17:b646b1e3970b 433 data <<= 6;
j3 17:b646b1e3970b 434 CRC16 ^= data;
j3 17:b646b1e3970b 435 data <<= 1;
j3 17:b646b1e3970b 436 CRC16 ^= data;
j3 17:b646b1e3970b 437
j3 17:b646b1e3970b 438 return CRC16;
j3 15:f6cb0d906fb6 439 }
j3 15:f6cb0d906fb6 440
IanBenzMaxim 48:6f9208ae280e 441
j3 17:b646b1e3970b 442 uint16_t OneWireMaster::calculateCRC16(const uint8_t * data, size_t data_offset, size_t data_len, uint16_t crc)
j3 17:b646b1e3970b 443 {
j3 17:b646b1e3970b 444 for (size_t i = data_offset; i < (data_len + data_offset); i++)
j3 17:b646b1e3970b 445 crc = calculateCRC16(crc, data[i]);
j3 17:b646b1e3970b 446 return crc;
IanBenzMaxim 48:6f9208ae280e 447 }