Device interface library for multiple platforms including Mbed.
Dependents: DeepCover Embedded Security in IoT MaximInterface MAXREFDES155#
Maxim Interface is a library framework focused on providing flexible and expressive hardware interfaces. Both communication interfaces such as I2C and 1-Wire and device interfaces such as DS18B20 are supported. Modern C++ concepts are used extensively while keeping compatibility with C++98/C++03 and requiring no external dependencies. The embedded-friendly design does not depend on exceptions or RTTI.
The full version of the project is hosted on GitLab: https://gitlab.com/iabenz/MaximInterface
MaximInterfaceCore/RomCommands.cpp@12:7eb41621ba22, 2020-05-29 (annotated)
- Committer:
- IanBenzMaxim
- Date:
- Fri May 29 16:19:22 2020 -0500
- Revision:
- 12:7eb41621ba22
- Parent:
- 8:5ea891c7d1a1
Updated to version 2.2.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
IanBenzMaxim | 7:9cd16581b578 | 1 | /******************************************************************************* |
IanBenzMaxim | 8:5ea891c7d1a1 | 2 | * Copyright (C) Maxim Integrated Products, Inc., All Rights Reserved. |
IanBenzMaxim | 7:9cd16581b578 | 3 | * |
IanBenzMaxim | 7:9cd16581b578 | 4 | * Permission is hereby granted, free of charge, to any person obtaining a |
IanBenzMaxim | 7:9cd16581b578 | 5 | * copy of this software and associated documentation files (the "Software"), |
IanBenzMaxim | 7:9cd16581b578 | 6 | * to deal in the Software without restriction, including without limitation |
IanBenzMaxim | 7:9cd16581b578 | 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
IanBenzMaxim | 7:9cd16581b578 | 8 | * and/or sell copies of the Software, and to permit persons to whom the |
IanBenzMaxim | 7:9cd16581b578 | 9 | * Software is furnished to do so, subject to the following conditions: |
IanBenzMaxim | 7:9cd16581b578 | 10 | * |
IanBenzMaxim | 7:9cd16581b578 | 11 | * The above copyright notice and this permission notice shall be included |
IanBenzMaxim | 7:9cd16581b578 | 12 | * in all copies or substantial portions of the Software. |
IanBenzMaxim | 7:9cd16581b578 | 13 | * |
IanBenzMaxim | 7:9cd16581b578 | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
IanBenzMaxim | 7:9cd16581b578 | 15 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
IanBenzMaxim | 7:9cd16581b578 | 16 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
IanBenzMaxim | 7:9cd16581b578 | 17 | * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES |
IanBenzMaxim | 7:9cd16581b578 | 18 | * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
IanBenzMaxim | 7:9cd16581b578 | 19 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
IanBenzMaxim | 7:9cd16581b578 | 20 | * OTHER DEALINGS IN THE SOFTWARE. |
IanBenzMaxim | 7:9cd16581b578 | 21 | * |
IanBenzMaxim | 7:9cd16581b578 | 22 | * Except as contained in this notice, the name of Maxim Integrated |
IanBenzMaxim | 7:9cd16581b578 | 23 | * Products, Inc. shall not be used except as stated in the Maxim Integrated |
IanBenzMaxim | 7:9cd16581b578 | 24 | * Products, Inc. Branding Policy. |
IanBenzMaxim | 7:9cd16581b578 | 25 | * |
IanBenzMaxim | 7:9cd16581b578 | 26 | * The mere transfer of this software does not imply any licenses |
IanBenzMaxim | 7:9cd16581b578 | 27 | * of trade secrets, proprietary technology, copyrights, patents, |
IanBenzMaxim | 7:9cd16581b578 | 28 | * trademarks, maskwork rights, or any other form of intellectual |
IanBenzMaxim | 7:9cd16581b578 | 29 | * property whatsoever. Maxim Integrated Products, Inc. retains all |
IanBenzMaxim | 7:9cd16581b578 | 30 | * ownership rights. |
IanBenzMaxim | 7:9cd16581b578 | 31 | *******************************************************************************/ |
IanBenzMaxim | 7:9cd16581b578 | 32 | |
IanBenzMaxim | 7:9cd16581b578 | 33 | #include "RomCommands.hpp" |
IanBenzMaxim | 7:9cd16581b578 | 34 | |
IanBenzMaxim | 7:9cd16581b578 | 35 | namespace MaximInterfaceCore { |
IanBenzMaxim | 7:9cd16581b578 | 36 | |
IanBenzMaxim | 7:9cd16581b578 | 37 | void skipCurrentFamily(SearchRomState & searchState) { |
IanBenzMaxim | 7:9cd16581b578 | 38 | // Set the last discrepancy to last family discrepancy. |
IanBenzMaxim | 7:9cd16581b578 | 39 | searchState.lastDiscrepancy = searchState.lastFamilyDiscrepancy; |
IanBenzMaxim | 7:9cd16581b578 | 40 | // Clear the last family discrepancy. |
IanBenzMaxim | 7:9cd16581b578 | 41 | searchState.lastFamilyDiscrepancy = 0; |
IanBenzMaxim | 7:9cd16581b578 | 42 | // Check for end of list. |
IanBenzMaxim | 7:9cd16581b578 | 43 | if (searchState.lastDiscrepancy == 0) { |
IanBenzMaxim | 7:9cd16581b578 | 44 | searchState.lastDevice = true; |
IanBenzMaxim | 7:9cd16581b578 | 45 | } |
IanBenzMaxim | 7:9cd16581b578 | 46 | } |
IanBenzMaxim | 7:9cd16581b578 | 47 | |
IanBenzMaxim | 8:5ea891c7d1a1 | 48 | Result<void> verifyRom(OneWireMaster & master, RomId::const_span romId) { |
IanBenzMaxim | 7:9cd16581b578 | 49 | SearchRomState searchState(romId); |
IanBenzMaxim | 8:5ea891c7d1a1 | 50 | Result<void> result = searchRom(master, searchState); |
IanBenzMaxim | 8:5ea891c7d1a1 | 51 | if (!result) { |
IanBenzMaxim | 7:9cd16581b578 | 52 | return result; |
IanBenzMaxim | 7:9cd16581b578 | 53 | } |
IanBenzMaxim | 7:9cd16581b578 | 54 | // Check if same device found. |
IanBenzMaxim | 7:9cd16581b578 | 55 | if (!equal(romId, make_span(searchState.romId))) { |
IanBenzMaxim | 8:5ea891c7d1a1 | 56 | result = OneWireMaster::NoSlaveError; |
IanBenzMaxim | 7:9cd16581b578 | 57 | } |
IanBenzMaxim | 7:9cd16581b578 | 58 | return result; |
IanBenzMaxim | 7:9cd16581b578 | 59 | } |
IanBenzMaxim | 7:9cd16581b578 | 60 | |
IanBenzMaxim | 8:5ea891c7d1a1 | 61 | Result<RomId::array> readRom(OneWireMaster & master) { |
IanBenzMaxim | 8:5ea891c7d1a1 | 62 | Result<void> result = master.reset(); |
IanBenzMaxim | 8:5ea891c7d1a1 | 63 | if (!result) { |
IanBenzMaxim | 8:5ea891c7d1a1 | 64 | return result.error(); |
IanBenzMaxim | 8:5ea891c7d1a1 | 65 | } |
IanBenzMaxim | 8:5ea891c7d1a1 | 66 | result = master.writeByte(0x33); |
IanBenzMaxim | 8:5ea891c7d1a1 | 67 | if (!result) { |
IanBenzMaxim | 8:5ea891c7d1a1 | 68 | return result.error(); |
IanBenzMaxim | 8:5ea891c7d1a1 | 69 | } |
IanBenzMaxim | 8:5ea891c7d1a1 | 70 | RomId::array romId; |
IanBenzMaxim | 8:5ea891c7d1a1 | 71 | result = master.readBlock(romId); |
IanBenzMaxim | 8:5ea891c7d1a1 | 72 | if (!result) { |
IanBenzMaxim | 8:5ea891c7d1a1 | 73 | return result.error(); |
IanBenzMaxim | 8:5ea891c7d1a1 | 74 | } |
IanBenzMaxim | 8:5ea891c7d1a1 | 75 | if (!valid(romId)) { |
IanBenzMaxim | 8:5ea891c7d1a1 | 76 | return OneWireMaster::NoSlaveError; |
IanBenzMaxim | 8:5ea891c7d1a1 | 77 | } |
IanBenzMaxim | 8:5ea891c7d1a1 | 78 | return romId; |
IanBenzMaxim | 8:5ea891c7d1a1 | 79 | } |
IanBenzMaxim | 8:5ea891c7d1a1 | 80 | |
IanBenzMaxim | 8:5ea891c7d1a1 | 81 | Result<void> skipRom(OneWireMaster & master) { |
IanBenzMaxim | 8:5ea891c7d1a1 | 82 | Result<void> result = master.reset(); |
IanBenzMaxim | 8:5ea891c7d1a1 | 83 | if (!result) { |
IanBenzMaxim | 7:9cd16581b578 | 84 | return result; |
IanBenzMaxim | 7:9cd16581b578 | 85 | } |
IanBenzMaxim | 8:5ea891c7d1a1 | 86 | result = master.writeByte(0xCC); |
IanBenzMaxim | 7:9cd16581b578 | 87 | return result; |
IanBenzMaxim | 7:9cd16581b578 | 88 | } |
IanBenzMaxim | 7:9cd16581b578 | 89 | |
IanBenzMaxim | 8:5ea891c7d1a1 | 90 | Result<void> matchRom(OneWireMaster & master, RomId::const_span romId) { |
IanBenzMaxim | 8:5ea891c7d1a1 | 91 | Result<void> result = master.reset(); |
IanBenzMaxim | 8:5ea891c7d1a1 | 92 | if (!result) { |
IanBenzMaxim | 7:9cd16581b578 | 93 | return result; |
IanBenzMaxim | 7:9cd16581b578 | 94 | } |
IanBenzMaxim | 8:5ea891c7d1a1 | 95 | result = master.writeByte(0x55); |
IanBenzMaxim | 8:5ea891c7d1a1 | 96 | if (!result) { |
IanBenzMaxim | 7:9cd16581b578 | 97 | return result; |
IanBenzMaxim | 7:9cd16581b578 | 98 | } |
IanBenzMaxim | 7:9cd16581b578 | 99 | result = master.writeBlock(romId); |
IanBenzMaxim | 7:9cd16581b578 | 100 | return result; |
IanBenzMaxim | 7:9cd16581b578 | 101 | } |
IanBenzMaxim | 7:9cd16581b578 | 102 | |
IanBenzMaxim | 8:5ea891c7d1a1 | 103 | Result<void> overdriveSkipRom(OneWireMaster & master) { |
IanBenzMaxim | 8:5ea891c7d1a1 | 104 | Result<void> result = master.reset(); |
IanBenzMaxim | 8:5ea891c7d1a1 | 105 | if (!result) { |
IanBenzMaxim | 7:9cd16581b578 | 106 | return result; |
IanBenzMaxim | 7:9cd16581b578 | 107 | } |
IanBenzMaxim | 8:5ea891c7d1a1 | 108 | result = master.writeByte(0x3C); |
IanBenzMaxim | 8:5ea891c7d1a1 | 109 | if (!result) { |
IanBenzMaxim | 7:9cd16581b578 | 110 | return result; |
IanBenzMaxim | 7:9cd16581b578 | 111 | } |
IanBenzMaxim | 7:9cd16581b578 | 112 | result = master.setSpeed(OneWireMaster::OverdriveSpeed); |
IanBenzMaxim | 7:9cd16581b578 | 113 | return result; |
IanBenzMaxim | 7:9cd16581b578 | 114 | } |
IanBenzMaxim | 7:9cd16581b578 | 115 | |
IanBenzMaxim | 8:5ea891c7d1a1 | 116 | Result<void> overdriveMatchRom(OneWireMaster & master, |
IanBenzMaxim | 8:5ea891c7d1a1 | 117 | RomId::const_span romId) { |
IanBenzMaxim | 8:5ea891c7d1a1 | 118 | Result<void> result = master.reset(); |
IanBenzMaxim | 8:5ea891c7d1a1 | 119 | if (!result) { |
IanBenzMaxim | 7:9cd16581b578 | 120 | return result; |
IanBenzMaxim | 7:9cd16581b578 | 121 | } |
IanBenzMaxim | 8:5ea891c7d1a1 | 122 | result = master.writeByte(0x69); |
IanBenzMaxim | 8:5ea891c7d1a1 | 123 | if (!result) { |
IanBenzMaxim | 7:9cd16581b578 | 124 | return result; |
IanBenzMaxim | 7:9cd16581b578 | 125 | } |
IanBenzMaxim | 7:9cd16581b578 | 126 | result = master.setSpeed(OneWireMaster::OverdriveSpeed); |
IanBenzMaxim | 8:5ea891c7d1a1 | 127 | if (!result) { |
IanBenzMaxim | 7:9cd16581b578 | 128 | return result; |
IanBenzMaxim | 7:9cd16581b578 | 129 | } |
IanBenzMaxim | 7:9cd16581b578 | 130 | result = master.writeBlock(romId); |
IanBenzMaxim | 7:9cd16581b578 | 131 | return result; |
IanBenzMaxim | 7:9cd16581b578 | 132 | } |
IanBenzMaxim | 7:9cd16581b578 | 133 | |
IanBenzMaxim | 8:5ea891c7d1a1 | 134 | Result<void> resumeRom(OneWireMaster & master) { |
IanBenzMaxim | 8:5ea891c7d1a1 | 135 | Result<void> result = master.reset(); |
IanBenzMaxim | 8:5ea891c7d1a1 | 136 | if (!result) { |
IanBenzMaxim | 7:9cd16581b578 | 137 | return result; |
IanBenzMaxim | 7:9cd16581b578 | 138 | } |
IanBenzMaxim | 8:5ea891c7d1a1 | 139 | result = master.writeByte(0xA5); |
IanBenzMaxim | 7:9cd16581b578 | 140 | return result; |
IanBenzMaxim | 7:9cd16581b578 | 141 | } |
IanBenzMaxim | 7:9cd16581b578 | 142 | |
IanBenzMaxim | 8:5ea891c7d1a1 | 143 | Result<void> searchRom(OneWireMaster & master, SearchRomState & searchState) { |
IanBenzMaxim | 7:9cd16581b578 | 144 | if (searchState.lastDevice) { |
IanBenzMaxim | 7:9cd16581b578 | 145 | searchState = SearchRomState(); |
IanBenzMaxim | 7:9cd16581b578 | 146 | } |
IanBenzMaxim | 7:9cd16581b578 | 147 | |
IanBenzMaxim | 8:5ea891c7d1a1 | 148 | Result<void> result = master.reset(); |
IanBenzMaxim | 8:5ea891c7d1a1 | 149 | if (!result) { |
IanBenzMaxim | 7:9cd16581b578 | 150 | return result; |
IanBenzMaxim | 7:9cd16581b578 | 151 | } |
IanBenzMaxim | 8:5ea891c7d1a1 | 152 | result = master.writeByte(0xF0); |
IanBenzMaxim | 8:5ea891c7d1a1 | 153 | if (!result) { |
IanBenzMaxim | 7:9cd16581b578 | 154 | return result; |
IanBenzMaxim | 7:9cd16581b578 | 155 | } |
IanBenzMaxim | 7:9cd16581b578 | 156 | |
IanBenzMaxim | 7:9cd16581b578 | 157 | SearchRomState newSearchState; |
IanBenzMaxim | 7:9cd16581b578 | 158 | newSearchState.lastFamilyDiscrepancy = searchState.lastFamilyDiscrepancy; |
IanBenzMaxim | 7:9cd16581b578 | 159 | for (int idBitNumber = 1; idBitNumber <= 64; ++idBitNumber) { |
IanBenzMaxim | 7:9cd16581b578 | 160 | const int idByteNumber = (idBitNumber - 1) / 8; |
IanBenzMaxim | 7:9cd16581b578 | 161 | const unsigned int idBitMask = 1 << ((idBitNumber - 1) % 8); |
IanBenzMaxim | 7:9cd16581b578 | 162 | |
IanBenzMaxim | 7:9cd16581b578 | 163 | OneWireMaster::TripletData tripletData; |
IanBenzMaxim | 7:9cd16581b578 | 164 | if (idBitNumber == searchState.lastDiscrepancy) { |
IanBenzMaxim | 7:9cd16581b578 | 165 | tripletData.writeBit = 1; |
IanBenzMaxim | 7:9cd16581b578 | 166 | } else if (idBitNumber > searchState.lastDiscrepancy) { |
IanBenzMaxim | 7:9cd16581b578 | 167 | tripletData.writeBit = 0; |
IanBenzMaxim | 7:9cd16581b578 | 168 | } else { // idBitNumber < searchState.lastDiscrepancy |
IanBenzMaxim | 7:9cd16581b578 | 169 | tripletData.writeBit = |
IanBenzMaxim | 7:9cd16581b578 | 170 | (searchState.romId[idByteNumber] & idBitMask) == idBitMask; |
IanBenzMaxim | 7:9cd16581b578 | 171 | } |
IanBenzMaxim | 7:9cd16581b578 | 172 | |
IanBenzMaxim | 8:5ea891c7d1a1 | 173 | MaximInterfaceCore_TRY_VALUE(tripletData, |
IanBenzMaxim | 8:5ea891c7d1a1 | 174 | master.triplet(tripletData.writeBit)); |
IanBenzMaxim | 7:9cd16581b578 | 175 | if (tripletData.readBit && tripletData.readBitComplement) { |
IanBenzMaxim | 8:5ea891c7d1a1 | 176 | return OneWireMaster::NoSlaveError; |
IanBenzMaxim | 7:9cd16581b578 | 177 | } |
IanBenzMaxim | 7:9cd16581b578 | 178 | |
IanBenzMaxim | 7:9cd16581b578 | 179 | if (tripletData.writeBit) { |
IanBenzMaxim | 7:9cd16581b578 | 180 | newSearchState.romId[idByteNumber] |= idBitMask; |
IanBenzMaxim | 7:9cd16581b578 | 181 | } else if (!tripletData.readBit && !tripletData.readBitComplement) { |
IanBenzMaxim | 7:9cd16581b578 | 182 | newSearchState.lastDiscrepancy = idBitNumber; |
IanBenzMaxim | 7:9cd16581b578 | 183 | if (idBitNumber <= 8) { |
IanBenzMaxim | 7:9cd16581b578 | 184 | newSearchState.lastFamilyDiscrepancy = idBitNumber; |
IanBenzMaxim | 7:9cd16581b578 | 185 | } |
IanBenzMaxim | 7:9cd16581b578 | 186 | } |
IanBenzMaxim | 7:9cd16581b578 | 187 | } |
IanBenzMaxim | 7:9cd16581b578 | 188 | |
IanBenzMaxim | 7:9cd16581b578 | 189 | if (valid(newSearchState.romId)) { |
IanBenzMaxim | 7:9cd16581b578 | 190 | if (newSearchState.lastDiscrepancy == 0) { |
IanBenzMaxim | 7:9cd16581b578 | 191 | newSearchState.lastDevice = true; |
IanBenzMaxim | 7:9cd16581b578 | 192 | } |
IanBenzMaxim | 7:9cd16581b578 | 193 | searchState = newSearchState; |
IanBenzMaxim | 7:9cd16581b578 | 194 | } else { |
IanBenzMaxim | 8:5ea891c7d1a1 | 195 | result = OneWireMaster::NoSlaveError; |
IanBenzMaxim | 7:9cd16581b578 | 196 | } |
IanBenzMaxim | 7:9cd16581b578 | 197 | return result; |
IanBenzMaxim | 7:9cd16581b578 | 198 | } |
IanBenzMaxim | 7:9cd16581b578 | 199 | |
IanBenzMaxim | 7:9cd16581b578 | 200 | } // namespace MaximInterfaceCore |