Implementation of 1-Wire with added Alarm Search Functionality

Dependents:   Max32630_One_Wire_Interface

Committer:
IanBenzMaxim
Date:
Fri May 13 14:52:50 2016 -0500
Revision:
75:8b627804927c
Parent:
74:23be10c32fa3
Child:
76:84e6c4994e29
Code cleanup.

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 75:8b627804927c 38 enum OwRomCmd
IanBenzMaxim 43:23017dcd2ec3 39 {
IanBenzMaxim 75:8b627804927c 40 ReadRomCmd = 0x33,
IanBenzMaxim 75:8b627804927c 41 MatchRomCmd = 0x55,
IanBenzMaxim 75:8b627804927c 42 SearchRomCmd = 0xF0,
IanBenzMaxim 75:8b627804927c 43 SkipRomCmd = 0xCC,
IanBenzMaxim 75:8b627804927c 44 ResumeCmd = 0xA5,
IanBenzMaxim 75:8b627804927c 45 OverdriveSkipRomCmd = 0x3C,
IanBenzMaxim 75:8b627804927c 46 OverdriveMatchRomCmd = 0x69
IanBenzMaxim 43:23017dcd2ec3 47 };
j3 15:f6cb0d906fb6 48
IanBenzMaxim 75:8b627804927c 49 OneWireMaster::CmdResult OneWireMaster::OWWriteBlock(const uint8_t *sendBuf, uint8_t sendLen)
IanBenzMaxim 71:562f5c702094 50 {
IanBenzMaxim 71:562f5c702094 51 CmdResult result;
IanBenzMaxim 74:23be10c32fa3 52
IanBenzMaxim 75:8b627804927c 53 for (uint8_t idx = 0; idx < sendLen; idx++)
IanBenzMaxim 71:562f5c702094 54 {
IanBenzMaxim 75:8b627804927c 55 result = OWWriteByte(sendBuf[idx]);
IanBenzMaxim 74:23be10c32fa3 56 if (result != Success)
IanBenzMaxim 71:562f5c702094 57 {
IanBenzMaxim 71:562f5c702094 58 break;
IanBenzMaxim 71:562f5c702094 59 }
IanBenzMaxim 71:562f5c702094 60 }
IanBenzMaxim 74:23be10c32fa3 61
IanBenzMaxim 71:562f5c702094 62 return result;
IanBenzMaxim 71:562f5c702094 63 }
IanBenzMaxim 71:562f5c702094 64
IanBenzMaxim 75:8b627804927c 65 OneWireMaster::CmdResult OneWireMaster::OWReadBlock(uint8_t *recvBuf, uint8_t recvLen)
IanBenzMaxim 71:562f5c702094 66 {
IanBenzMaxim 71:562f5c702094 67 CmdResult result;
IanBenzMaxim 74:23be10c32fa3 68
IanBenzMaxim 75:8b627804927c 69 for (uint8_t idx = 0; idx < recvLen; idx++)
IanBenzMaxim 71:562f5c702094 70 {
IanBenzMaxim 75:8b627804927c 71 result = OWReadByte(recvBuf[idx]);
IanBenzMaxim 74:23be10c32fa3 72 if (result != Success)
IanBenzMaxim 71:562f5c702094 73 {
IanBenzMaxim 71:562f5c702094 74 break;
IanBenzMaxim 71:562f5c702094 75 }
IanBenzMaxim 71:562f5c702094 76 }
IanBenzMaxim 74:23be10c32fa3 77
IanBenzMaxim 71:562f5c702094 78 return result;
IanBenzMaxim 71:562f5c702094 79 }
IanBenzMaxim 71:562f5c702094 80
IanBenzMaxim 71:562f5c702094 81
IanBenzMaxim 32:bce180b544ed 82 OneWireMaster::CmdResult OneWireMaster::OWFirst(SearchState & searchState)
j3 15:f6cb0d906fb6 83 {
IanBenzMaxim 75:8b627804927c 84 // Reset and begin a new search
IanBenzMaxim 32:bce180b544ed 85 searchState.reset();
IanBenzMaxim 74:23be10c32fa3 86 return OWSearch(searchState);
j3 15:f6cb0d906fb6 87 }
j3 15:f6cb0d906fb6 88
IanBenzMaxim 32:bce180b544ed 89 OneWireMaster::CmdResult OneWireMaster::OWNext(SearchState & searchState)
j3 15:f6cb0d906fb6 90 {
IanBenzMaxim 75:8b627804927c 91 // Continue the previous search
IanBenzMaxim 32:bce180b544ed 92 return OWSearch(searchState);
j3 15:f6cb0d906fb6 93 }
j3 15:f6cb0d906fb6 94
j3 23:e8e403d61359 95 OneWireMaster::CmdResult OneWireMaster::OWVerify(const RomId & romId)
j3 15:f6cb0d906fb6 96 {
j3 23:e8e403d61359 97 OneWireMaster::CmdResult result;
IanBenzMaxim 32:bce180b544ed 98 SearchState searchState;
IanBenzMaxim 74:23be10c32fa3 99
j3 42:698635a0d073 100 searchState.romId = romId;
j3 15:f6cb0d906fb6 101
j3 15:f6cb0d906fb6 102 // set search to find the same device
IanBenzMaxim 32:bce180b544ed 103 searchState.last_discrepancy = 64;
IanBenzMaxim 32:bce180b544ed 104 searchState.last_device_flag = false;
j3 15:f6cb0d906fb6 105
IanBenzMaxim 32:bce180b544ed 106 result = OWSearch(searchState);
IanBenzMaxim 74:23be10c32fa3 107 if (result == OneWireMaster::Success)
j3 15:f6cb0d906fb6 108 {
j3 15:f6cb0d906fb6 109 // check if same device found
IanBenzMaxim 74:23be10c32fa3 110 if (romId != searchState.romId)
j3 15:f6cb0d906fb6 111 {
j3 23:e8e403d61359 112 result = OneWireMaster::OperationFailure;
j3 15:f6cb0d906fb6 113 }
j3 15:f6cb0d906fb6 114 }
j3 15:f6cb0d906fb6 115
j3 17:b646b1e3970b 116 return result;
j3 15:f6cb0d906fb6 117 }
j3 15:f6cb0d906fb6 118
IanBenzMaxim 32:bce180b544ed 119 void OneWireMaster::OWTargetSetup(SearchState & searchState)
j3 15:f6cb0d906fb6 120 {
j3 15:f6cb0d906fb6 121 // set the search state to find SearchFamily type devices
IanBenzMaxim 73:2cecc1372acc 122 uint8_t familyCode = searchState.romId.familyCode();
IanBenzMaxim 32:bce180b544ed 123 searchState.reset();
IanBenzMaxim 32:bce180b544ed 124 searchState.romId.setFamilyCode(familyCode);
IanBenzMaxim 32:bce180b544ed 125 searchState.last_discrepancy = 64;
j3 15:f6cb0d906fb6 126 }
j3 5:ce108eeb878d 127
IanBenzMaxim 32:bce180b544ed 128 void OneWireMaster::OWFamilySkipSetup(SearchState & searchState)
j3 15:f6cb0d906fb6 129 {
j3 15:f6cb0d906fb6 130 // set the Last discrepancy to last family discrepancy
IanBenzMaxim 32:bce180b544ed 131 searchState.last_discrepancy = searchState.last_family_discrepancy;
j3 17:b646b1e3970b 132
j3 15:f6cb0d906fb6 133 // clear the last family discrpepancy
IanBenzMaxim 32:bce180b544ed 134 searchState.last_family_discrepancy = 0;
j3 17:b646b1e3970b 135
j3 15:f6cb0d906fb6 136 // check for end of list
IanBenzMaxim 32:bce180b544ed 137 if (searchState.last_discrepancy == 0)
j3 15:f6cb0d906fb6 138 {
IanBenzMaxim 32:bce180b544ed 139 searchState.last_device_flag = true;
j3 15:f6cb0d906fb6 140 }
j3 15:f6cb0d906fb6 141 }
j3 15:f6cb0d906fb6 142
IanBenzMaxim 75:8b627804927c 143 OneWireMaster::CmdResult OneWireMaster::OWReadRom(RomId & romId)
j3 15:f6cb0d906fb6 144 {
j3 23:e8e403d61359 145 OneWireMaster::CmdResult result;
IanBenzMaxim 75:8b627804927c 146 RomId readId;
j3 17:b646b1e3970b 147
j3 17:b646b1e3970b 148 result = OWReset();
j3 23:e8e403d61359 149 if (result == OneWireMaster::Success)
j3 15:f6cb0d906fb6 150 {
IanBenzMaxim 75:8b627804927c 151 result = OWWriteByte(ReadRomCmd);
j3 17:b646b1e3970b 152 }
j3 17:b646b1e3970b 153
j3 17:b646b1e3970b 154 // read the ROM
j3 23:e8e403d61359 155 if (result == OneWireMaster::Success)
j3 17:b646b1e3970b 156 {
IanBenzMaxim 75:8b627804927c 157 result = OWReadBlock(readId, RomId::byteLen);
j3 15:f6cb0d906fb6 158 }
IanBenzMaxim 74:23be10c32fa3 159
j3 17:b646b1e3970b 160 // verify CRC8
IanBenzMaxim 75:8b627804927c 161 if (result == OneWireMaster::Success)
j3 17:b646b1e3970b 162 {
IanBenzMaxim 75:8b627804927c 163 if (readId.crc8Valid())
IanBenzMaxim 75:8b627804927c 164 {
IanBenzMaxim 75:8b627804927c 165 romId = readId;
IanBenzMaxim 75:8b627804927c 166 }
IanBenzMaxim 75:8b627804927c 167 else
IanBenzMaxim 75:8b627804927c 168 {
IanBenzMaxim 75:8b627804927c 169 result = OneWireMaster::OperationFailure;
IanBenzMaxim 75:8b627804927c 170 }
j3 17:b646b1e3970b 171 }
j3 17:b646b1e3970b 172
j3 17:b646b1e3970b 173 return result;
j3 15:f6cb0d906fb6 174 }
IanBenzMaxim 75:8b627804927c 175
IanBenzMaxim 75:8b627804927c 176 OneWireMaster::CmdResult OneWireMaster::OWSkipRom()
j3 15:f6cb0d906fb6 177 {
j3 23:e8e403d61359 178 OneWireMaster::CmdResult result;
IanBenzMaxim 74:23be10c32fa3 179
j3 17:b646b1e3970b 180 result = OWReset();
IanBenzMaxim 74:23be10c32fa3 181 if (result == OneWireMaster::Success)
j3 15:f6cb0d906fb6 182 {
IanBenzMaxim 75:8b627804927c 183 result = OWWriteByte(SkipRomCmd);
j3 15:f6cb0d906fb6 184 }
IanBenzMaxim 74:23be10c32fa3 185
j3 17:b646b1e3970b 186 return result;
j3 15:f6cb0d906fb6 187 }
IanBenzMaxim 75:8b627804927c 188
IanBenzMaxim 75:8b627804927c 189 OneWireMaster::CmdResult OneWireMaster::OWMatchRom(const RomId & romId)
j3 15:f6cb0d906fb6 190 {
j3 23:e8e403d61359 191 OneWireMaster::CmdResult result;
IanBenzMaxim 74:23be10c32fa3 192
j3 17:b646b1e3970b 193 uint8_t buf[1 + RomId::byteLen];
j3 17:b646b1e3970b 194
j3 17:b646b1e3970b 195 // use MatchROM
j3 17:b646b1e3970b 196 result = OWReset();
IanBenzMaxim 74:23be10c32fa3 197 if (result == OneWireMaster::Success)
j3 15:f6cb0d906fb6 198 {
IanBenzMaxim 75:8b627804927c 199 buf[0] = MatchRomCmd;
j3 17:b646b1e3970b 200 std::memcpy(&buf[1], romId, RomId::byteLen);
j3 17:b646b1e3970b 201 // send command and rom
j3 17:b646b1e3970b 202 result = OWWriteBlock(buf, 1 + RomId::byteLen);
j3 15:f6cb0d906fb6 203 }
j3 17:b646b1e3970b 204
j3 17:b646b1e3970b 205 return result;
j3 15:f6cb0d906fb6 206 }
IanBenzMaxim 75:8b627804927c 207
IanBenzMaxim 75:8b627804927c 208 OneWireMaster::CmdResult OneWireMaster::OWOverdriveSkipRom()
j3 15:f6cb0d906fb6 209 {
IanBenzMaxim 75:8b627804927c 210 OneWireMaster::CmdResult result = OWSetSpeed(StandardSpeed);
IanBenzMaxim 74:23be10c32fa3 211
j3 23:e8e403d61359 212 if (result == OneWireMaster::Success)
j3 15:f6cb0d906fb6 213 {
j3 17:b646b1e3970b 214 result = OWReset();
j3 15:f6cb0d906fb6 215 }
IanBenzMaxim 74:23be10c32fa3 216
j3 23:e8e403d61359 217 if (result == OneWireMaster::Success)
j3 17:b646b1e3970b 218 {
IanBenzMaxim 75:8b627804927c 219 result = OWWriteByte(OverdriveSkipRomCmd);
j3 17:b646b1e3970b 220 }
IanBenzMaxim 74:23be10c32fa3 221
j3 23:e8e403d61359 222 if (result == OneWireMaster::Success)
j3 17:b646b1e3970b 223 {
IanBenzMaxim 75:8b627804927c 224 result = OWSetSpeed(OverdriveSpeed);
j3 17:b646b1e3970b 225 }
IanBenzMaxim 74:23be10c32fa3 226
j3 17:b646b1e3970b 227 return result;
j3 15:f6cb0d906fb6 228 }
IanBenzMaxim 75:8b627804927c 229
IanBenzMaxim 75:8b627804927c 230 OneWireMaster::CmdResult OneWireMaster::OWOverdriveMatchRom(const RomId & romId)
j3 15:f6cb0d906fb6 231 {
j3 23:e8e403d61359 232 OneWireMaster::CmdResult result;
IanBenzMaxim 74:23be10c32fa3 233
j3 17:b646b1e3970b 234 // use overdrive MatchROM
IanBenzMaxim 75:8b627804927c 235 OWSetSpeed(StandardSpeed);
IanBenzMaxim 74:23be10c32fa3 236
j3 17:b646b1e3970b 237 result = OWReset();
IanBenzMaxim 74:23be10c32fa3 238 if (result == OneWireMaster::Success)
j3 15:f6cb0d906fb6 239 {
IanBenzMaxim 75:8b627804927c 240 result = OWWriteByte(OverdriveMatchRomCmd);
IanBenzMaxim 74:23be10c32fa3 241 if (result == OneWireMaster::Success)
j3 15:f6cb0d906fb6 242 {
IanBenzMaxim 75:8b627804927c 243 OWSetSpeed(OverdriveSpeed);
j3 17:b646b1e3970b 244 // send ROM
j3 17:b646b1e3970b 245 result = OWWriteBlock(romId, RomId::byteLen);
j3 15:f6cb0d906fb6 246 }
j3 15:f6cb0d906fb6 247 }
j3 17:b646b1e3970b 248 return result;
j3 15:f6cb0d906fb6 249 }
IanBenzMaxim 75:8b627804927c 250
IanBenzMaxim 75:8b627804927c 251 OneWireMaster::CmdResult OneWireMaster::OWResume()
j3 15:f6cb0d906fb6 252 {
j3 23:e8e403d61359 253 OneWireMaster::CmdResult result;
IanBenzMaxim 74:23be10c32fa3 254
j3 17:b646b1e3970b 255 result = OWReset();
IanBenzMaxim 74:23be10c32fa3 256 if (result == OneWireMaster::Success)
j3 15:f6cb0d906fb6 257 {
IanBenzMaxim 75:8b627804927c 258 result = OWWriteByte(ResumeCmd);
j3 15:f6cb0d906fb6 259 }
IanBenzMaxim 74:23be10c32fa3 260
j3 17:b646b1e3970b 261 return result;
j3 15:f6cb0d906fb6 262 }
j3 15:f6cb0d906fb6 263
IanBenzMaxim 32:bce180b544ed 264 OneWireMaster::CmdResult OneWireMaster::OWSearch(SearchState & searchState)
IanBenzMaxim 32:bce180b544ed 265 {
IanBenzMaxim 74:23be10c32fa3 266 uint8_t id_bit_number;
IanBenzMaxim 74:23be10c32fa3 267 uint8_t last_zero, rom_byte_number;
IanBenzMaxim 74:23be10c32fa3 268 uint8_t id_bit, cmp_id_bit;
IanBenzMaxim 74:23be10c32fa3 269 uint8_t rom_byte_mask;
IanBenzMaxim 74:23be10c32fa3 270 bool search_result;
IanBenzMaxim 74:23be10c32fa3 271 uint8_t crc8 = 0;
IanBenzMaxim 74:23be10c32fa3 272 SearchDirection search_direction;
IanBenzMaxim 32:bce180b544ed 273
IanBenzMaxim 74:23be10c32fa3 274 // initialize for search
IanBenzMaxim 74:23be10c32fa3 275 id_bit_number = 1;
IanBenzMaxim 74:23be10c32fa3 276 last_zero = 0;
IanBenzMaxim 74:23be10c32fa3 277 rom_byte_number = 0;
IanBenzMaxim 74:23be10c32fa3 278 rom_byte_mask = 1;
IanBenzMaxim 74:23be10c32fa3 279 search_result = false;
IanBenzMaxim 32:bce180b544ed 280
IanBenzMaxim 74:23be10c32fa3 281 // if the last call was not the last one
IanBenzMaxim 74:23be10c32fa3 282 if (!searchState.last_device_flag)
IanBenzMaxim 74:23be10c32fa3 283 {
IanBenzMaxim 74:23be10c32fa3 284 // 1-Wire reset
IanBenzMaxim 74:23be10c32fa3 285 OneWireMaster::CmdResult result = OWReset();
IanBenzMaxim 74:23be10c32fa3 286 if (result != OneWireMaster::Success)
IanBenzMaxim 74:23be10c32fa3 287 {
IanBenzMaxim 74:23be10c32fa3 288 // reset the search
IanBenzMaxim 74:23be10c32fa3 289 searchState.reset();
IanBenzMaxim 74:23be10c32fa3 290 return result;
IanBenzMaxim 74:23be10c32fa3 291 }
IanBenzMaxim 32:bce180b544ed 292
IanBenzMaxim 74:23be10c32fa3 293 // issue the search command
IanBenzMaxim 75:8b627804927c 294 OneWireMaster::OWWriteByte(SearchRomCmd);
IanBenzMaxim 32:bce180b544ed 295
IanBenzMaxim 74:23be10c32fa3 296 // loop to do the search
IanBenzMaxim 74:23be10c32fa3 297 do
IanBenzMaxim 74:23be10c32fa3 298 {
IanBenzMaxim 74:23be10c32fa3 299 // if this discrepancy if before the Last Discrepancy
IanBenzMaxim 74:23be10c32fa3 300 // on a previous next then pick the same as last time
IanBenzMaxim 74:23be10c32fa3 301 if (id_bit_number < searchState.last_discrepancy)
IanBenzMaxim 74:23be10c32fa3 302 {
IanBenzMaxim 74:23be10c32fa3 303 if ((searchState.romId[rom_byte_number] & rom_byte_mask) > 0)
IanBenzMaxim 74:23be10c32fa3 304 {
IanBenzMaxim 75:8b627804927c 305 search_direction = WriteOne;
IanBenzMaxim 74:23be10c32fa3 306 }
IanBenzMaxim 74:23be10c32fa3 307 else
IanBenzMaxim 74:23be10c32fa3 308 {
IanBenzMaxim 75:8b627804927c 309 search_direction = WriteZero;
IanBenzMaxim 74:23be10c32fa3 310 }
IanBenzMaxim 74:23be10c32fa3 311 }
IanBenzMaxim 32:bce180b544ed 312 else
IanBenzMaxim 74:23be10c32fa3 313 {
IanBenzMaxim 74:23be10c32fa3 314 // if equal to last pick 1, if not then pick 0
IanBenzMaxim 74:23be10c32fa3 315 if (id_bit_number == searchState.last_discrepancy)
IanBenzMaxim 74:23be10c32fa3 316 {
IanBenzMaxim 75:8b627804927c 317 search_direction = WriteOne;
IanBenzMaxim 74:23be10c32fa3 318 }
IanBenzMaxim 74:23be10c32fa3 319 else
IanBenzMaxim 74:23be10c32fa3 320 {
IanBenzMaxim 75:8b627804927c 321 search_direction = WriteZero;
IanBenzMaxim 74:23be10c32fa3 322 }
IanBenzMaxim 74:23be10c32fa3 323 }
IanBenzMaxim 32:bce180b544ed 324
IanBenzMaxim 74:23be10c32fa3 325 // Peform a triple operation on the DS2465 which will perform 2 read bits and 1 write bit
IanBenzMaxim 74:23be10c32fa3 326 result = OWTriplet(search_direction, id_bit, cmp_id_bit);
IanBenzMaxim 74:23be10c32fa3 327 if (result != OneWireMaster::Success)
IanBenzMaxim 32:bce180b544ed 328 {
IanBenzMaxim 74:23be10c32fa3 329 return result;
IanBenzMaxim 32:bce180b544ed 330 }
IanBenzMaxim 32:bce180b544ed 331
IanBenzMaxim 74:23be10c32fa3 332 // check for no devices on 1-wire
IanBenzMaxim 74:23be10c32fa3 333 if ((id_bit) && (cmp_id_bit))
IanBenzMaxim 74:23be10c32fa3 334 {
IanBenzMaxim 74:23be10c32fa3 335 break;
IanBenzMaxim 74:23be10c32fa3 336 }
IanBenzMaxim 32:bce180b544ed 337 else
IanBenzMaxim 74:23be10c32fa3 338 {
IanBenzMaxim 75:8b627804927c 339 if ((!id_bit) && (!cmp_id_bit) && (search_direction == WriteZero))
IanBenzMaxim 74:23be10c32fa3 340 {
IanBenzMaxim 74:23be10c32fa3 341 last_zero = id_bit_number;
IanBenzMaxim 32:bce180b544ed 342
IanBenzMaxim 74:23be10c32fa3 343 // check for Last discrepancy in family
IanBenzMaxim 74:23be10c32fa3 344 if (last_zero < 9)
IanBenzMaxim 74:23be10c32fa3 345 {
IanBenzMaxim 74:23be10c32fa3 346 searchState.last_family_discrepancy = last_zero;
IanBenzMaxim 74:23be10c32fa3 347 }
IanBenzMaxim 74:23be10c32fa3 348 }
IanBenzMaxim 32:bce180b544ed 349
IanBenzMaxim 74:23be10c32fa3 350 // set or clear the bit in the ROM byte rom_byte_number
IanBenzMaxim 74:23be10c32fa3 351 // with mask rom_byte_mask
IanBenzMaxim 75:8b627804927c 352 if (search_direction == WriteOne)
IanBenzMaxim 74:23be10c32fa3 353 {
IanBenzMaxim 74:23be10c32fa3 354 searchState.romId[rom_byte_number] |= rom_byte_mask;
IanBenzMaxim 74:23be10c32fa3 355 }
IanBenzMaxim 74:23be10c32fa3 356 else
IanBenzMaxim 74:23be10c32fa3 357 {
IanBenzMaxim 74:23be10c32fa3 358 searchState.romId[rom_byte_number] &= (uint8_t)~rom_byte_mask;
IanBenzMaxim 74:23be10c32fa3 359 }
IanBenzMaxim 74:23be10c32fa3 360
IanBenzMaxim 74:23be10c32fa3 361 // increment the byte counter id_bit_number
IanBenzMaxim 74:23be10c32fa3 362 // and shift the mask rom_byte_mask
IanBenzMaxim 74:23be10c32fa3 363 id_bit_number++;
IanBenzMaxim 74:23be10c32fa3 364 rom_byte_mask <<= 1;
IanBenzMaxim 32:bce180b544ed 365
IanBenzMaxim 74:23be10c32fa3 366 // if the mask is 0 then go to new SerialNum byte rom_byte_number and reset mask
IanBenzMaxim 74:23be10c32fa3 367 if (rom_byte_mask == 0)
IanBenzMaxim 74:23be10c32fa3 368 {
IanBenzMaxim 75:8b627804927c 369 crc8 = RomId::calculateCrc8(crc8, searchState.romId[rom_byte_number]); // accumulate the CRC
IanBenzMaxim 74:23be10c32fa3 370 rom_byte_number++;
IanBenzMaxim 74:23be10c32fa3 371 rom_byte_mask = 1;
IanBenzMaxim 74:23be10c32fa3 372 }
IanBenzMaxim 74:23be10c32fa3 373 }
IanBenzMaxim 74:23be10c32fa3 374 } while (rom_byte_number < RomId::byteLen); // loop until through all ROM bytes 0-7
IanBenzMaxim 32:bce180b544ed 375
IanBenzMaxim 74:23be10c32fa3 376 // if the search was successful then
IanBenzMaxim 74:23be10c32fa3 377 if (!((id_bit_number <= (RomId::byteLen * 8)) || (crc8 != 0)))
IanBenzMaxim 74:23be10c32fa3 378 {
IanBenzMaxim 74:23be10c32fa3 379 // search successful so set m_last_discrepancy,m_last_device_flag,search_result
IanBenzMaxim 74:23be10c32fa3 380 searchState.last_discrepancy = last_zero;
IanBenzMaxim 32:bce180b544ed 381
IanBenzMaxim 74:23be10c32fa3 382 // check for last device
IanBenzMaxim 74:23be10c32fa3 383 if (searchState.last_discrepancy == 0)
IanBenzMaxim 74:23be10c32fa3 384 {
IanBenzMaxim 74:23be10c32fa3 385 searchState.last_device_flag = true;
IanBenzMaxim 74:23be10c32fa3 386 }
IanBenzMaxim 32:bce180b544ed 387
IanBenzMaxim 74:23be10c32fa3 388 search_result = true;
IanBenzMaxim 74:23be10c32fa3 389 }
IanBenzMaxim 74:23be10c32fa3 390 }
IanBenzMaxim 32:bce180b544ed 391
IanBenzMaxim 74:23be10c32fa3 392 // if no device found then reset counters so next 'search' will be like a first
IanBenzMaxim 74:23be10c32fa3 393 if (!search_result || (searchState.romId.familyCode() == 0))
IanBenzMaxim 74:23be10c32fa3 394 {
IanBenzMaxim 74:23be10c32fa3 395 searchState.reset();
IanBenzMaxim 74:23be10c32fa3 396 search_result = false;
IanBenzMaxim 74:23be10c32fa3 397 }
IanBenzMaxim 74:23be10c32fa3 398
IanBenzMaxim 74:23be10c32fa3 399 return (search_result ? OneWireMaster::Success : OneWireMaster::OperationFailure);
IanBenzMaxim 32:bce180b544ed 400 }
IanBenzMaxim 32:bce180b544ed 401
IanBenzMaxim 75:8b627804927c 402 OneWireMaster::CmdResult OneWireMaster::OWTriplet(SearchDirection & searchDirection, uint8_t & sbr, uint8_t & tsb)
IanBenzMaxim 32:bce180b544ed 403 {
IanBenzMaxim 32:bce180b544ed 404 CmdResult result;
IanBenzMaxim 32:bce180b544ed 405 result = OWReadBit(sbr);
IanBenzMaxim 32:bce180b544ed 406 if (result == Success)
IanBenzMaxim 74:23be10c32fa3 407 {
IanBenzMaxim 32:bce180b544ed 408 result = OWReadBit(tsb);
IanBenzMaxim 74:23be10c32fa3 409 }
IanBenzMaxim 32:bce180b544ed 410 if (result == Success)
IanBenzMaxim 32:bce180b544ed 411 {
IanBenzMaxim 57:1635f247ceae 412 if (sbr)
IanBenzMaxim 74:23be10c32fa3 413 {
IanBenzMaxim 75:8b627804927c 414 searchDirection = WriteOne;
IanBenzMaxim 74:23be10c32fa3 415 }
IanBenzMaxim 57:1635f247ceae 416 else if (tsb)
IanBenzMaxim 74:23be10c32fa3 417 {
IanBenzMaxim 75:8b627804927c 418 searchDirection = WriteZero;
IanBenzMaxim 74:23be10c32fa3 419 }
IanBenzMaxim 32:bce180b544ed 420 // else: use search_direction parameter
IanBenzMaxim 74:23be10c32fa3 421
IanBenzMaxim 75:8b627804927c 422 result = OWWriteBit((searchDirection == WriteOne) ? 1 : 0);
IanBenzMaxim 32:bce180b544ed 423 }
IanBenzMaxim 32:bce180b544ed 424 return result;
IanBenzMaxim 32:bce180b544ed 425 }
IanBenzMaxim 32:bce180b544ed 426
IanBenzMaxim 75:8b627804927c 427 uint16_t OneWireMaster::calculateCrc16(uint16_t crc16, uint16_t data)
j3 15:f6cb0d906fb6 428 {
IanBenzMaxim 75:8b627804927c 429 const uint16_t oddparity[] = { 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0 };
IanBenzMaxim 74:23be10c32fa3 430
IanBenzMaxim 75:8b627804927c 431 data = (data ^ (crc16 & 0xff)) & 0xff;
IanBenzMaxim 75:8b627804927c 432 crc16 >>= 8;
j3 17:b646b1e3970b 433
IanBenzMaxim 75:8b627804927c 434 if (oddparity[data & 0xf] ^ oddparity[data >> 4])
IanBenzMaxim 74:23be10c32fa3 435 {
IanBenzMaxim 75:8b627804927c 436 crc16 ^= 0xc001;
IanBenzMaxim 74:23be10c32fa3 437 }
j3 17:b646b1e3970b 438
IanBenzMaxim 74:23be10c32fa3 439 data <<= 6;
IanBenzMaxim 75:8b627804927c 440 crc16 ^= data;
IanBenzMaxim 74:23be10c32fa3 441 data <<= 1;
IanBenzMaxim 75:8b627804927c 442 crc16 ^= data;
j3 17:b646b1e3970b 443
IanBenzMaxim 75:8b627804927c 444 return crc16;
j3 15:f6cb0d906fb6 445 }
j3 15:f6cb0d906fb6 446
IanBenzMaxim 75:8b627804927c 447 uint16_t OneWireMaster::calculateCrc16(const uint8_t * data, size_t dataOffset, size_t dataLen, uint16_t crc)
IanBenzMaxim 74:23be10c32fa3 448 {
IanBenzMaxim 75:8b627804927c 449 for (size_t i = dataOffset; i < (dataLen + dataOffset); i++)
IanBenzMaxim 74:23be10c32fa3 450 {
IanBenzMaxim 75:8b627804927c 451 crc = calculateCrc16(crc, data[i]);
IanBenzMaxim 74:23be10c32fa3 452 }
IanBenzMaxim 74:23be10c32fa3 453 return crc;
IanBenzMaxim 48:6f9208ae280e 454 }