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