Implementation of 1-Wire with added Alarm Search Functionality

Dependents:   Max32630_One_Wire_Interface

Committer:
IanBenzMaxim
Date:
Wed Mar 30 16:50:29 2016 -0500
Revision:
32:bce180b544ed
Parent:
27:d5aaefa252f1
Child:
36:b6b5985a5e40
1. Move the implementation of OWSearch() into OneWireMaster since it is a fairly complex algorithm that shouldn?t be implemented over and over. It will also match all other ROM function that are implemented there.
2. Create a new member function, OWTriplet(), in OneWireMaster to handle the virtual section of OWSearch(). Create a default implementation of OWTriplet() that uses OWReadByte() and OWWriteByte(). Masters only need to implement this function if they have a search accelerator of some sort.
3. Create type SearchState that will encapsulate all persistent data used by the search ROM functions. This will also make it easy to not have the search state part of the permanent OneWireMaster class data.
4. Rename OWSpeed() to OWSetSpeed() and OWLevel() to OWSetLevel() for naming consistency.

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