Implementation of 1-Wire with added Alarm Search Functionality
Dependents: Max32630_One_Wire_Interface
RomId/RomCommands.cpp@142:85b71cfd617e, 2019-08-13 (annotated)
- Committer:
- mfruge
- Date:
- Tue Aug 13 14:42:37 2019 +0000
- Revision:
- 142:85b71cfd617e
- Parent:
- 139:f0e0a7976846
Added functions to ROMCommands to add Alarm Search functionality
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
j3 | 104:3f48daed532b | 1 | /******************************************************************//** |
j3 | 104:3f48daed532b | 2 | * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved. |
j3 | 104:3f48daed532b | 3 | * |
j3 | 104:3f48daed532b | 4 | * Permission is hereby granted, free of charge, to any person obtaining a |
j3 | 104:3f48daed532b | 5 | * copy of this software and associated documentation files (the "Software"), |
j3 | 104:3f48daed532b | 6 | * to deal in the Software without restriction, including without limitation |
j3 | 104:3f48daed532b | 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
j3 | 104:3f48daed532b | 8 | * and/or sell copies of the Software, and to permit persons to whom the |
j3 | 104:3f48daed532b | 9 | * Software is furnished to do so, subject to the following conditions: |
j3 | 104:3f48daed532b | 10 | * |
j3 | 104:3f48daed532b | 11 | * The above copyright notice and this permission notice shall be included |
j3 | 104:3f48daed532b | 12 | * in all copies or substantial portions of the Software. |
j3 | 104:3f48daed532b | 13 | * |
j3 | 104:3f48daed532b | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
j3 | 104:3f48daed532b | 15 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
j3 | 104:3f48daed532b | 16 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
j3 | 104:3f48daed532b | 17 | * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES |
j3 | 104:3f48daed532b | 18 | * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
j3 | 104:3f48daed532b | 19 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
j3 | 104:3f48daed532b | 20 | * OTHER DEALINGS IN THE SOFTWARE. |
j3 | 104:3f48daed532b | 21 | * |
j3 | 104:3f48daed532b | 22 | * Except as contained in this notice, the name of Maxim Integrated |
j3 | 104:3f48daed532b | 23 | * Products, Inc. shall not be used except as stated in the Maxim Integrated |
j3 | 104:3f48daed532b | 24 | * Products, Inc. Branding Policy. |
j3 | 104:3f48daed532b | 25 | * |
j3 | 104:3f48daed532b | 26 | * The mere transfer of this software does not imply any licenses |
j3 | 104:3f48daed532b | 27 | * of trade secrets, proprietary technology, copyrights, patents, |
j3 | 104:3f48daed532b | 28 | * trademarks, maskwork rights, or any other form of intellectual |
j3 | 104:3f48daed532b | 29 | * property whatsoever. Maxim Integrated Products, Inc. retains all |
j3 | 104:3f48daed532b | 30 | * ownership rights. |
j3 | 104:3f48daed532b | 31 | **********************************************************************/ |
j3 | 104:3f48daed532b | 32 | |
j3 | 104:3f48daed532b | 33 | #include "RomId/RomCommands.h" |
IanBenzMaxim | 90:c233d1c265ff | 34 | |
IanBenzMaxim | 90:c233d1c265ff | 35 | using namespace OneWire::crc; |
IanBenzMaxim | 90:c233d1c265ff | 36 | |
IanBenzMaxim | 90:c233d1c265ff | 37 | namespace OneWire |
IanBenzMaxim | 90:c233d1c265ff | 38 | { |
IanBenzMaxim | 90:c233d1c265ff | 39 | namespace RomCommands |
IanBenzMaxim | 90:c233d1c265ff | 40 | { |
IanBenzMaxim | 90:c233d1c265ff | 41 | enum OwRomCmd |
IanBenzMaxim | 90:c233d1c265ff | 42 | { |
IanBenzMaxim | 90:c233d1c265ff | 43 | ReadRomCmd = 0x33, |
IanBenzMaxim | 90:c233d1c265ff | 44 | MatchRomCmd = 0x55, |
IanBenzMaxim | 90:c233d1c265ff | 45 | SearchRomCmd = 0xF0, |
IanBenzMaxim | 90:c233d1c265ff | 46 | SkipRomCmd = 0xCC, |
IanBenzMaxim | 90:c233d1c265ff | 47 | ResumeCmd = 0xA5, |
IanBenzMaxim | 90:c233d1c265ff | 48 | OverdriveSkipRomCmd = 0x3C, |
mfruge | 142:85b71cfd617e | 49 | OverdriveMatchRomCmd = 0x69, |
mfruge | 142:85b71cfd617e | 50 | AlarmSearchCmd = 0xEC |
IanBenzMaxim | 90:c233d1c265ff | 51 | }; |
IanBenzMaxim | 90:c233d1c265ff | 52 | |
IanBenzMaxim | 90:c233d1c265ff | 53 | void SearchState::reset() |
IanBenzMaxim | 90:c233d1c265ff | 54 | { |
IanBenzMaxim | 90:c233d1c265ff | 55 | last_discrepancy = 0; |
IanBenzMaxim | 90:c233d1c265ff | 56 | last_device_flag = false; |
IanBenzMaxim | 90:c233d1c265ff | 57 | last_family_discrepancy = 0; |
j3 | 139:f0e0a7976846 | 58 | romId = RomId(); |
IanBenzMaxim | 90:c233d1c265ff | 59 | } |
IanBenzMaxim | 90:c233d1c265ff | 60 | |
IanBenzMaxim | 90:c233d1c265ff | 61 | void SearchState::findFamily(uint8_t familyCode) |
IanBenzMaxim | 90:c233d1c265ff | 62 | { |
IanBenzMaxim | 90:c233d1c265ff | 63 | reset(); |
j3 | 139:f0e0a7976846 | 64 | romId.familyCode() = familyCode; |
IanBenzMaxim | 90:c233d1c265ff | 65 | last_discrepancy = 64; |
IanBenzMaxim | 90:c233d1c265ff | 66 | } |
IanBenzMaxim | 90:c233d1c265ff | 67 | |
IanBenzMaxim | 90:c233d1c265ff | 68 | void SearchState::skipCurrentFamily() |
IanBenzMaxim | 90:c233d1c265ff | 69 | { |
IanBenzMaxim | 90:c233d1c265ff | 70 | // set the Last discrepancy to last family discrepancy |
IanBenzMaxim | 90:c233d1c265ff | 71 | last_discrepancy = last_family_discrepancy; |
IanBenzMaxim | 90:c233d1c265ff | 72 | |
IanBenzMaxim | 90:c233d1c265ff | 73 | // clear the last family discrpepancy |
IanBenzMaxim | 90:c233d1c265ff | 74 | last_family_discrepancy = 0; |
IanBenzMaxim | 90:c233d1c265ff | 75 | |
IanBenzMaxim | 90:c233d1c265ff | 76 | // check for end of list |
IanBenzMaxim | 90:c233d1c265ff | 77 | if (last_discrepancy == 0) |
IanBenzMaxim | 90:c233d1c265ff | 78 | { |
IanBenzMaxim | 90:c233d1c265ff | 79 | last_device_flag = true; |
IanBenzMaxim | 90:c233d1c265ff | 80 | } |
IanBenzMaxim | 90:c233d1c265ff | 81 | } |
IanBenzMaxim | 90:c233d1c265ff | 82 | |
IanBenzMaxim | 90:c233d1c265ff | 83 | OneWireMaster::CmdResult OWFirst(OneWireMaster & master, SearchState & searchState) |
IanBenzMaxim | 90:c233d1c265ff | 84 | { |
IanBenzMaxim | 90:c233d1c265ff | 85 | // Reset and begin a new search |
IanBenzMaxim | 90:c233d1c265ff | 86 | searchState.reset(); |
IanBenzMaxim | 90:c233d1c265ff | 87 | return OWSearch(master, searchState); |
IanBenzMaxim | 90:c233d1c265ff | 88 | } |
IanBenzMaxim | 90:c233d1c265ff | 89 | |
IanBenzMaxim | 90:c233d1c265ff | 90 | OneWireMaster::CmdResult OWNext(OneWireMaster & master, SearchState & searchState) |
IanBenzMaxim | 90:c233d1c265ff | 91 | { |
IanBenzMaxim | 90:c233d1c265ff | 92 | // Continue the previous search |
IanBenzMaxim | 90:c233d1c265ff | 93 | return OWSearch(master, searchState); |
IanBenzMaxim | 90:c233d1c265ff | 94 | } |
IanBenzMaxim | 90:c233d1c265ff | 95 | |
IanBenzMaxim | 90:c233d1c265ff | 96 | OneWireMaster::CmdResult OWVerify(OneWireMaster & master, const RomId & romId) |
IanBenzMaxim | 90:c233d1c265ff | 97 | { |
IanBenzMaxim | 90:c233d1c265ff | 98 | OneWireMaster::CmdResult result; |
IanBenzMaxim | 90:c233d1c265ff | 99 | SearchState searchState; |
IanBenzMaxim | 90:c233d1c265ff | 100 | |
IanBenzMaxim | 90:c233d1c265ff | 101 | searchState.romId = romId; |
IanBenzMaxim | 90:c233d1c265ff | 102 | |
IanBenzMaxim | 90:c233d1c265ff | 103 | // set search to find the same device |
IanBenzMaxim | 90:c233d1c265ff | 104 | searchState.last_discrepancy = 64; |
IanBenzMaxim | 90:c233d1c265ff | 105 | searchState.last_device_flag = false; |
IanBenzMaxim | 90:c233d1c265ff | 106 | |
IanBenzMaxim | 90:c233d1c265ff | 107 | result = OWSearch(master, searchState); |
IanBenzMaxim | 90:c233d1c265ff | 108 | if (result == OneWireMaster::Success) |
IanBenzMaxim | 90:c233d1c265ff | 109 | { |
IanBenzMaxim | 90:c233d1c265ff | 110 | // check if same device found |
IanBenzMaxim | 90:c233d1c265ff | 111 | if (romId != searchState.romId) |
IanBenzMaxim | 90:c233d1c265ff | 112 | { |
IanBenzMaxim | 90:c233d1c265ff | 113 | result = OneWireMaster::OperationFailure; |
IanBenzMaxim | 90:c233d1c265ff | 114 | } |
IanBenzMaxim | 90:c233d1c265ff | 115 | } |
IanBenzMaxim | 90:c233d1c265ff | 116 | |
IanBenzMaxim | 90:c233d1c265ff | 117 | return result; |
IanBenzMaxim | 90:c233d1c265ff | 118 | } |
IanBenzMaxim | 90:c233d1c265ff | 119 | |
IanBenzMaxim | 90:c233d1c265ff | 120 | OneWireMaster::CmdResult OWReadRom(OneWireMaster & master, RomId & romId) |
IanBenzMaxim | 90:c233d1c265ff | 121 | { |
IanBenzMaxim | 90:c233d1c265ff | 122 | OneWireMaster::CmdResult result; |
IanBenzMaxim | 90:c233d1c265ff | 123 | RomId readId; |
IanBenzMaxim | 90:c233d1c265ff | 124 | |
IanBenzMaxim | 90:c233d1c265ff | 125 | result = master.OWReset(); |
IanBenzMaxim | 90:c233d1c265ff | 126 | if (result == OneWireMaster::Success) |
IanBenzMaxim | 90:c233d1c265ff | 127 | { |
IanBenzMaxim | 90:c233d1c265ff | 128 | result = master.OWWriteByte(ReadRomCmd); |
IanBenzMaxim | 90:c233d1c265ff | 129 | } |
IanBenzMaxim | 90:c233d1c265ff | 130 | |
IanBenzMaxim | 90:c233d1c265ff | 131 | // read the ROM |
IanBenzMaxim | 90:c233d1c265ff | 132 | if (result == OneWireMaster::Success) |
IanBenzMaxim | 90:c233d1c265ff | 133 | { |
j3 | 139:f0e0a7976846 | 134 | result = master.OWReadBlock(readId.buffer.data(), readId.buffer.size()); |
IanBenzMaxim | 90:c233d1c265ff | 135 | } |
IanBenzMaxim | 90:c233d1c265ff | 136 | |
IanBenzMaxim | 90:c233d1c265ff | 137 | // verify CRC8 |
IanBenzMaxim | 90:c233d1c265ff | 138 | if (result == OneWireMaster::Success) |
IanBenzMaxim | 90:c233d1c265ff | 139 | { |
j3 | 139:f0e0a7976846 | 140 | if (readId.valid()) |
IanBenzMaxim | 90:c233d1c265ff | 141 | { |
IanBenzMaxim | 90:c233d1c265ff | 142 | romId = readId; |
IanBenzMaxim | 90:c233d1c265ff | 143 | } |
IanBenzMaxim | 90:c233d1c265ff | 144 | else |
IanBenzMaxim | 90:c233d1c265ff | 145 | { |
IanBenzMaxim | 90:c233d1c265ff | 146 | result = OneWireMaster::OperationFailure; |
IanBenzMaxim | 90:c233d1c265ff | 147 | } |
IanBenzMaxim | 90:c233d1c265ff | 148 | } |
IanBenzMaxim | 90:c233d1c265ff | 149 | |
IanBenzMaxim | 90:c233d1c265ff | 150 | return result; |
IanBenzMaxim | 90:c233d1c265ff | 151 | } |
j3 | 139:f0e0a7976846 | 152 | |
IanBenzMaxim | 90:c233d1c265ff | 153 | OneWireMaster::CmdResult OWSkipRom(OneWireMaster & master) |
IanBenzMaxim | 90:c233d1c265ff | 154 | { |
IanBenzMaxim | 90:c233d1c265ff | 155 | OneWireMaster::CmdResult result; |
IanBenzMaxim | 90:c233d1c265ff | 156 | |
IanBenzMaxim | 90:c233d1c265ff | 157 | result = master.OWReset(); |
IanBenzMaxim | 90:c233d1c265ff | 158 | if (result == OneWireMaster::Success) |
IanBenzMaxim | 90:c233d1c265ff | 159 | { |
IanBenzMaxim | 90:c233d1c265ff | 160 | result = master.OWWriteByte(SkipRomCmd); |
IanBenzMaxim | 90:c233d1c265ff | 161 | } |
IanBenzMaxim | 90:c233d1c265ff | 162 | |
IanBenzMaxim | 90:c233d1c265ff | 163 | return result; |
IanBenzMaxim | 90:c233d1c265ff | 164 | } |
j3 | 139:f0e0a7976846 | 165 | |
IanBenzMaxim | 90:c233d1c265ff | 166 | OneWireMaster::CmdResult OWMatchRom(OneWireMaster & master, const RomId & romId) |
IanBenzMaxim | 90:c233d1c265ff | 167 | { |
IanBenzMaxim | 90:c233d1c265ff | 168 | OneWireMaster::CmdResult result; |
IanBenzMaxim | 90:c233d1c265ff | 169 | |
j3 | 139:f0e0a7976846 | 170 | uint8_t buf[1 + RomId::Buffer::csize]; |
IanBenzMaxim | 90:c233d1c265ff | 171 | |
IanBenzMaxim | 90:c233d1c265ff | 172 | // use MatchROM |
IanBenzMaxim | 90:c233d1c265ff | 173 | result = master.OWReset(); |
IanBenzMaxim | 90:c233d1c265ff | 174 | if (result == OneWireMaster::Success) |
IanBenzMaxim | 90:c233d1c265ff | 175 | { |
IanBenzMaxim | 90:c233d1c265ff | 176 | buf[0] = MatchRomCmd; |
j3 | 139:f0e0a7976846 | 177 | std::memcpy(&buf[1], romId.buffer.data(), romId.buffer.size()); |
IanBenzMaxim | 90:c233d1c265ff | 178 | // send command and rom |
j3 | 139:f0e0a7976846 | 179 | result = master.OWWriteBlock(buf, 1 + romId.buffer.size()); |
IanBenzMaxim | 90:c233d1c265ff | 180 | } |
IanBenzMaxim | 90:c233d1c265ff | 181 | |
IanBenzMaxim | 90:c233d1c265ff | 182 | return result; |
IanBenzMaxim | 90:c233d1c265ff | 183 | } |
j3 | 139:f0e0a7976846 | 184 | |
IanBenzMaxim | 90:c233d1c265ff | 185 | OneWireMaster::CmdResult OWOverdriveSkipRom(OneWireMaster & master) |
IanBenzMaxim | 90:c233d1c265ff | 186 | { |
IanBenzMaxim | 90:c233d1c265ff | 187 | OneWireMaster::CmdResult result = master.OWSetSpeed(OneWireMaster::StandardSpeed); |
IanBenzMaxim | 90:c233d1c265ff | 188 | |
IanBenzMaxim | 90:c233d1c265ff | 189 | if (result == OneWireMaster::Success) |
IanBenzMaxim | 90:c233d1c265ff | 190 | { |
IanBenzMaxim | 90:c233d1c265ff | 191 | result = master.OWReset(); |
IanBenzMaxim | 90:c233d1c265ff | 192 | } |
IanBenzMaxim | 90:c233d1c265ff | 193 | |
IanBenzMaxim | 90:c233d1c265ff | 194 | if (result == OneWireMaster::Success) |
IanBenzMaxim | 90:c233d1c265ff | 195 | { |
IanBenzMaxim | 90:c233d1c265ff | 196 | result = master.OWWriteByte(OverdriveSkipRomCmd); |
IanBenzMaxim | 90:c233d1c265ff | 197 | } |
IanBenzMaxim | 90:c233d1c265ff | 198 | |
IanBenzMaxim | 90:c233d1c265ff | 199 | if (result == OneWireMaster::Success) |
IanBenzMaxim | 90:c233d1c265ff | 200 | { |
IanBenzMaxim | 90:c233d1c265ff | 201 | result = master.OWSetSpeed(OneWireMaster::OverdriveSpeed); |
IanBenzMaxim | 90:c233d1c265ff | 202 | } |
IanBenzMaxim | 90:c233d1c265ff | 203 | |
IanBenzMaxim | 90:c233d1c265ff | 204 | return result; |
IanBenzMaxim | 90:c233d1c265ff | 205 | } |
IanBenzMaxim | 90:c233d1c265ff | 206 | |
IanBenzMaxim | 90:c233d1c265ff | 207 | OneWireMaster::CmdResult OWOverdriveMatchRom(OneWireMaster & master, const RomId & romId) |
IanBenzMaxim | 90:c233d1c265ff | 208 | { |
IanBenzMaxim | 90:c233d1c265ff | 209 | OneWireMaster::CmdResult result; |
IanBenzMaxim | 90:c233d1c265ff | 210 | |
IanBenzMaxim | 90:c233d1c265ff | 211 | // use overdrive MatchROM |
IanBenzMaxim | 90:c233d1c265ff | 212 | master.OWSetSpeed(OneWireMaster::StandardSpeed); |
IanBenzMaxim | 90:c233d1c265ff | 213 | |
IanBenzMaxim | 90:c233d1c265ff | 214 | result = master.OWReset(); |
IanBenzMaxim | 90:c233d1c265ff | 215 | if (result == OneWireMaster::Success) |
IanBenzMaxim | 90:c233d1c265ff | 216 | { |
IanBenzMaxim | 90:c233d1c265ff | 217 | result = master.OWWriteByte(OverdriveMatchRomCmd); |
IanBenzMaxim | 90:c233d1c265ff | 218 | if (result == OneWireMaster::Success) |
IanBenzMaxim | 90:c233d1c265ff | 219 | { |
IanBenzMaxim | 90:c233d1c265ff | 220 | master.OWSetSpeed(OneWireMaster::OverdriveSpeed); |
IanBenzMaxim | 90:c233d1c265ff | 221 | // send ROM |
j3 | 139:f0e0a7976846 | 222 | result = master.OWWriteBlock(romId.buffer.data(), romId.buffer.size()); |
IanBenzMaxim | 90:c233d1c265ff | 223 | } |
IanBenzMaxim | 90:c233d1c265ff | 224 | } |
IanBenzMaxim | 90:c233d1c265ff | 225 | return result; |
IanBenzMaxim | 90:c233d1c265ff | 226 | } |
j3 | 139:f0e0a7976846 | 227 | |
IanBenzMaxim | 90:c233d1c265ff | 228 | OneWireMaster::CmdResult OWResume(OneWireMaster & master) |
IanBenzMaxim | 90:c233d1c265ff | 229 | { |
IanBenzMaxim | 90:c233d1c265ff | 230 | OneWireMaster::CmdResult result; |
IanBenzMaxim | 90:c233d1c265ff | 231 | |
IanBenzMaxim | 90:c233d1c265ff | 232 | result = master.OWReset(); |
IanBenzMaxim | 90:c233d1c265ff | 233 | if (result == OneWireMaster::Success) |
IanBenzMaxim | 90:c233d1c265ff | 234 | { |
IanBenzMaxim | 90:c233d1c265ff | 235 | result = master.OWWriteByte(ResumeCmd); |
IanBenzMaxim | 90:c233d1c265ff | 236 | } |
IanBenzMaxim | 90:c233d1c265ff | 237 | |
IanBenzMaxim | 90:c233d1c265ff | 238 | return result; |
IanBenzMaxim | 90:c233d1c265ff | 239 | } |
IanBenzMaxim | 90:c233d1c265ff | 240 | |
IanBenzMaxim | 90:c233d1c265ff | 241 | OneWireMaster::CmdResult OWSearch(OneWireMaster & master, SearchState & searchState) |
IanBenzMaxim | 90:c233d1c265ff | 242 | { |
mfruge | 142:85b71cfd617e | 243 | return OWSearchAll(master, searchState, false); |
mfruge | 142:85b71cfd617e | 244 | } |
mfruge | 142:85b71cfd617e | 245 | |
mfruge | 142:85b71cfd617e | 246 | OneWireMaster::CmdResult OWAlarmSearch(OneWireMaster & master, SearchState & searchState) |
mfruge | 142:85b71cfd617e | 247 | { |
mfruge | 142:85b71cfd617e | 248 | return OWSearchAll(master, searchState, true); |
mfruge | 142:85b71cfd617e | 249 | } |
mfruge | 142:85b71cfd617e | 250 | |
mfruge | 142:85b71cfd617e | 251 | OneWireMaster::CmdResult OWFirstAlarm(OneWireMaster & master, SearchState & searchState) |
mfruge | 142:85b71cfd617e | 252 | { |
mfruge | 142:85b71cfd617e | 253 | searchState.reset(); |
mfruge | 142:85b71cfd617e | 254 | return OWAlarmSearch(master, searchState); |
mfruge | 142:85b71cfd617e | 255 | } |
mfruge | 142:85b71cfd617e | 256 | |
mfruge | 142:85b71cfd617e | 257 | OneWireMaster::CmdResult OWNextAlarm(OneWireMaster & master, SearchState & searchState) |
mfruge | 142:85b71cfd617e | 258 | { |
mfruge | 142:85b71cfd617e | 259 | return OWAlarmSearch(master, searchState); |
mfruge | 142:85b71cfd617e | 260 | } |
mfruge | 142:85b71cfd617e | 261 | |
mfruge | 142:85b71cfd617e | 262 | OneWireMaster::CmdResult OWSearchAll(OneWireMaster & master, SearchState & searchState, bool alarmSearch) |
mfruge | 142:85b71cfd617e | 263 | { |
IanBenzMaxim | 90:c233d1c265ff | 264 | uint8_t id_bit_number; |
IanBenzMaxim | 90:c233d1c265ff | 265 | uint8_t last_zero, rom_byte_number; |
IanBenzMaxim | 90:c233d1c265ff | 266 | uint8_t id_bit, cmp_id_bit; |
IanBenzMaxim | 90:c233d1c265ff | 267 | uint8_t rom_byte_mask; |
IanBenzMaxim | 90:c233d1c265ff | 268 | bool search_result; |
IanBenzMaxim | 90:c233d1c265ff | 269 | uint8_t crc8 = 0; |
IanBenzMaxim | 90:c233d1c265ff | 270 | OneWireMaster::SearchDirection search_direction; |
IanBenzMaxim | 90:c233d1c265ff | 271 | |
IanBenzMaxim | 90:c233d1c265ff | 272 | // initialize for search |
IanBenzMaxim | 90:c233d1c265ff | 273 | id_bit_number = 1; |
IanBenzMaxim | 90:c233d1c265ff | 274 | last_zero = 0; |
IanBenzMaxim | 90:c233d1c265ff | 275 | rom_byte_number = 0; |
IanBenzMaxim | 90:c233d1c265ff | 276 | rom_byte_mask = 1; |
IanBenzMaxim | 90:c233d1c265ff | 277 | search_result = false; |
IanBenzMaxim | 90:c233d1c265ff | 278 | |
IanBenzMaxim | 90:c233d1c265ff | 279 | // if the last call was not the last one |
IanBenzMaxim | 90:c233d1c265ff | 280 | if (!searchState.last_device_flag) |
IanBenzMaxim | 90:c233d1c265ff | 281 | { |
IanBenzMaxim | 90:c233d1c265ff | 282 | // 1-Wire reset |
IanBenzMaxim | 90:c233d1c265ff | 283 | OneWireMaster::CmdResult result = master.OWReset(); |
IanBenzMaxim | 90:c233d1c265ff | 284 | if (result != OneWireMaster::Success) |
IanBenzMaxim | 90:c233d1c265ff | 285 | { |
IanBenzMaxim | 90:c233d1c265ff | 286 | // reset the search |
IanBenzMaxim | 90:c233d1c265ff | 287 | searchState.reset(); |
IanBenzMaxim | 90:c233d1c265ff | 288 | return result; |
IanBenzMaxim | 90:c233d1c265ff | 289 | } |
IanBenzMaxim | 90:c233d1c265ff | 290 | |
IanBenzMaxim | 90:c233d1c265ff | 291 | // issue the search command |
mfruge | 142:85b71cfd617e | 292 | if(alarmSearch){ |
mfruge | 142:85b71cfd617e | 293 | master.OWWriteByte(AlarmSearchCmd); |
mfruge | 142:85b71cfd617e | 294 | } |
mfruge | 142:85b71cfd617e | 295 | else{ |
mfruge | 142:85b71cfd617e | 296 | master.OWWriteByte(SearchRomCmd); |
mfruge | 142:85b71cfd617e | 297 | } |
IanBenzMaxim | 90:c233d1c265ff | 298 | |
IanBenzMaxim | 90:c233d1c265ff | 299 | // loop to do the search |
IanBenzMaxim | 90:c233d1c265ff | 300 | do |
IanBenzMaxim | 90:c233d1c265ff | 301 | { |
IanBenzMaxim | 90:c233d1c265ff | 302 | // if this discrepancy if before the Last Discrepancy |
IanBenzMaxim | 90:c233d1c265ff | 303 | // on a previous next then pick the same as last time |
IanBenzMaxim | 90:c233d1c265ff | 304 | if (id_bit_number < searchState.last_discrepancy) |
IanBenzMaxim | 90:c233d1c265ff | 305 | { |
j3 | 139:f0e0a7976846 | 306 | if ((searchState.romId.buffer[rom_byte_number] & rom_byte_mask) > 0) |
IanBenzMaxim | 90:c233d1c265ff | 307 | { |
IanBenzMaxim | 90:c233d1c265ff | 308 | search_direction = OneWireMaster::WriteOne; |
IanBenzMaxim | 90:c233d1c265ff | 309 | } |
IanBenzMaxim | 90:c233d1c265ff | 310 | else |
IanBenzMaxim | 90:c233d1c265ff | 311 | { |
IanBenzMaxim | 90:c233d1c265ff | 312 | search_direction = OneWireMaster::WriteZero; |
IanBenzMaxim | 90:c233d1c265ff | 313 | } |
IanBenzMaxim | 90:c233d1c265ff | 314 | } |
IanBenzMaxim | 90:c233d1c265ff | 315 | else |
IanBenzMaxim | 90:c233d1c265ff | 316 | { |
IanBenzMaxim | 90:c233d1c265ff | 317 | // if equal to last pick 1, if not then pick 0 |
IanBenzMaxim | 90:c233d1c265ff | 318 | if (id_bit_number == searchState.last_discrepancy) |
IanBenzMaxim | 90:c233d1c265ff | 319 | { |
IanBenzMaxim | 90:c233d1c265ff | 320 | search_direction = OneWireMaster::WriteOne; |
IanBenzMaxim | 90:c233d1c265ff | 321 | } |
IanBenzMaxim | 90:c233d1c265ff | 322 | else |
IanBenzMaxim | 90:c233d1c265ff | 323 | { |
IanBenzMaxim | 90:c233d1c265ff | 324 | search_direction = OneWireMaster::WriteZero; |
IanBenzMaxim | 90:c233d1c265ff | 325 | } |
IanBenzMaxim | 90:c233d1c265ff | 326 | } |
IanBenzMaxim | 90:c233d1c265ff | 327 | |
IanBenzMaxim | 90:c233d1c265ff | 328 | // Peform a triple operation on the DS2465 which will perform 2 read bits and 1 write bit |
IanBenzMaxim | 90:c233d1c265ff | 329 | result = master.OWTriplet(search_direction, id_bit, cmp_id_bit); |
IanBenzMaxim | 90:c233d1c265ff | 330 | if (result != OneWireMaster::Success) |
IanBenzMaxim | 90:c233d1c265ff | 331 | { |
IanBenzMaxim | 90:c233d1c265ff | 332 | return result; |
IanBenzMaxim | 90:c233d1c265ff | 333 | } |
IanBenzMaxim | 90:c233d1c265ff | 334 | |
IanBenzMaxim | 90:c233d1c265ff | 335 | // check for no devices on 1-wire |
IanBenzMaxim | 90:c233d1c265ff | 336 | if (id_bit && cmp_id_bit) |
IanBenzMaxim | 90:c233d1c265ff | 337 | { |
IanBenzMaxim | 90:c233d1c265ff | 338 | break; |
IanBenzMaxim | 90:c233d1c265ff | 339 | } |
IanBenzMaxim | 90:c233d1c265ff | 340 | else |
IanBenzMaxim | 90:c233d1c265ff | 341 | { |
IanBenzMaxim | 90:c233d1c265ff | 342 | if (!id_bit && !cmp_id_bit && (search_direction == OneWireMaster::WriteZero)) |
IanBenzMaxim | 90:c233d1c265ff | 343 | { |
IanBenzMaxim | 90:c233d1c265ff | 344 | last_zero = id_bit_number; |
IanBenzMaxim | 90:c233d1c265ff | 345 | |
IanBenzMaxim | 90:c233d1c265ff | 346 | // check for Last discrepancy in family |
IanBenzMaxim | 90:c233d1c265ff | 347 | if (last_zero < 9) |
IanBenzMaxim | 90:c233d1c265ff | 348 | { |
IanBenzMaxim | 90:c233d1c265ff | 349 | searchState.last_family_discrepancy = last_zero; |
IanBenzMaxim | 90:c233d1c265ff | 350 | } |
IanBenzMaxim | 90:c233d1c265ff | 351 | } |
IanBenzMaxim | 90:c233d1c265ff | 352 | |
IanBenzMaxim | 90:c233d1c265ff | 353 | // set or clear the bit in the ROM byte rom_byte_number |
IanBenzMaxim | 90:c233d1c265ff | 354 | // with mask rom_byte_mask |
IanBenzMaxim | 90:c233d1c265ff | 355 | if (search_direction == OneWireMaster::WriteOne) |
IanBenzMaxim | 90:c233d1c265ff | 356 | { |
j3 | 139:f0e0a7976846 | 357 | searchState.romId.buffer[rom_byte_number] |= rom_byte_mask; |
IanBenzMaxim | 90:c233d1c265ff | 358 | } |
IanBenzMaxim | 90:c233d1c265ff | 359 | else |
IanBenzMaxim | 90:c233d1c265ff | 360 | { |
j3 | 139:f0e0a7976846 | 361 | searchState.romId.buffer[rom_byte_number] &= (uint8_t)~rom_byte_mask; |
IanBenzMaxim | 90:c233d1c265ff | 362 | } |
IanBenzMaxim | 90:c233d1c265ff | 363 | |
IanBenzMaxim | 90:c233d1c265ff | 364 | // increment the byte counter id_bit_number |
IanBenzMaxim | 90:c233d1c265ff | 365 | // and shift the mask rom_byte_mask |
IanBenzMaxim | 90:c233d1c265ff | 366 | id_bit_number++; |
IanBenzMaxim | 90:c233d1c265ff | 367 | rom_byte_mask <<= 1; |
IanBenzMaxim | 90:c233d1c265ff | 368 | |
IanBenzMaxim | 90:c233d1c265ff | 369 | // if the mask is 0 then go to new SerialNum byte rom_byte_number and reset mask |
IanBenzMaxim | 90:c233d1c265ff | 370 | if (rom_byte_mask == 0) |
IanBenzMaxim | 90:c233d1c265ff | 371 | { |
j3 | 139:f0e0a7976846 | 372 | crc8 = calculateCrc8(crc8, searchState.romId.buffer[rom_byte_number]); // accumulate the CRC |
IanBenzMaxim | 90:c233d1c265ff | 373 | rom_byte_number++; |
IanBenzMaxim | 90:c233d1c265ff | 374 | rom_byte_mask = 1; |
IanBenzMaxim | 90:c233d1c265ff | 375 | } |
IanBenzMaxim | 90:c233d1c265ff | 376 | } |
j3 | 139:f0e0a7976846 | 377 | } while (rom_byte_number < searchState.romId.buffer.size()); // loop until through all ROM bytes 0-7 |
IanBenzMaxim | 90:c233d1c265ff | 378 | |
IanBenzMaxim | 90:c233d1c265ff | 379 | // if the search was successful then |
j3 | 139:f0e0a7976846 | 380 | if (!((id_bit_number <= (searchState.romId.buffer.size() * 8)) || (crc8 != 0))) |
IanBenzMaxim | 90:c233d1c265ff | 381 | { |
IanBenzMaxim | 90:c233d1c265ff | 382 | // search successful so set m_last_discrepancy,m_last_device_flag,search_result |
IanBenzMaxim | 90:c233d1c265ff | 383 | searchState.last_discrepancy = last_zero; |
IanBenzMaxim | 90:c233d1c265ff | 384 | |
IanBenzMaxim | 90:c233d1c265ff | 385 | // check for last device |
IanBenzMaxim | 90:c233d1c265ff | 386 | if (searchState.last_discrepancy == 0) |
IanBenzMaxim | 90:c233d1c265ff | 387 | { |
IanBenzMaxim | 90:c233d1c265ff | 388 | searchState.last_device_flag = true; |
IanBenzMaxim | 90:c233d1c265ff | 389 | } |
IanBenzMaxim | 90:c233d1c265ff | 390 | |
IanBenzMaxim | 90:c233d1c265ff | 391 | search_result = true; |
IanBenzMaxim | 90:c233d1c265ff | 392 | } |
IanBenzMaxim | 90:c233d1c265ff | 393 | } |
IanBenzMaxim | 90:c233d1c265ff | 394 | |
IanBenzMaxim | 90:c233d1c265ff | 395 | // if no device found then reset counters so next 'search' will be like a first |
IanBenzMaxim | 90:c233d1c265ff | 396 | if (!search_result || (searchState.romId.familyCode() == 0)) |
IanBenzMaxim | 90:c233d1c265ff | 397 | { |
IanBenzMaxim | 90:c233d1c265ff | 398 | searchState.reset(); |
IanBenzMaxim | 90:c233d1c265ff | 399 | search_result = false; |
IanBenzMaxim | 90:c233d1c265ff | 400 | } |
IanBenzMaxim | 90:c233d1c265ff | 401 | |
IanBenzMaxim | 90:c233d1c265ff | 402 | return (search_result ? OneWireMaster::Success : OneWireMaster::OperationFailure); |
IanBenzMaxim | 90:c233d1c265ff | 403 | } |
IanBenzMaxim | 90:c233d1c265ff | 404 | } |
IanBenzMaxim | 90:c233d1c265ff | 405 | } |