Implementation of 1-Wire with added Alarm Search Functionality

Dependents:   Max32630_One_Wire_Interface

Committer:
IanBenzMaxim
Date:
Wed Apr 06 08:49:27 2016 -0500
Revision:
43:23017dcd2ec3
Parent:
42:698635a0d073
Child:
48:6f9208ae280e
Add check for self-assignment to array since it could lead to undefined behavior with memcpy. Minor cleanup to OneWireMaster.

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