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

Committer:
IanBenzMaxim
Date:
Mon Jul 22 11:44:07 2019 -0500
Revision:
7:9cd16581b578
Child:
8:5ea891c7d1a1
Updated to version 1.9.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
IanBenzMaxim 7:9cd16581b578 1 /*******************************************************************************
IanBenzMaxim 7:9cd16581b578 2 * Copyright (C) 2017 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 enum RomCmd {
IanBenzMaxim 7:9cd16581b578 38 ReadRomCmd = 0x33,
IanBenzMaxim 7:9cd16581b578 39 MatchRomCmd = 0x55,
IanBenzMaxim 7:9cd16581b578 40 SearchRomCmd = 0xF0,
IanBenzMaxim 7:9cd16581b578 41 SkipRomCmd = 0xCC,
IanBenzMaxim 7:9cd16581b578 42 ResumeRomCmd = 0xA5,
IanBenzMaxim 7:9cd16581b578 43 OverdriveSkipRomCmd = 0x3C,
IanBenzMaxim 7:9cd16581b578 44 OverdriveMatchRomCmd = 0x69
IanBenzMaxim 7:9cd16581b578 45 };
IanBenzMaxim 7:9cd16581b578 46
IanBenzMaxim 7:9cd16581b578 47 void skipCurrentFamily(SearchRomState & searchState) {
IanBenzMaxim 7:9cd16581b578 48 // Set the last discrepancy to last family discrepancy.
IanBenzMaxim 7:9cd16581b578 49 searchState.lastDiscrepancy = searchState.lastFamilyDiscrepancy;
IanBenzMaxim 7:9cd16581b578 50 // Clear the last family discrepancy.
IanBenzMaxim 7:9cd16581b578 51 searchState.lastFamilyDiscrepancy = 0;
IanBenzMaxim 7:9cd16581b578 52 // Check for end of list.
IanBenzMaxim 7:9cd16581b578 53 if (searchState.lastDiscrepancy == 0) {
IanBenzMaxim 7:9cd16581b578 54 searchState.lastDevice = true;
IanBenzMaxim 7:9cd16581b578 55 }
IanBenzMaxim 7:9cd16581b578 56 }
IanBenzMaxim 7:9cd16581b578 57
IanBenzMaxim 7:9cd16581b578 58 error_code verifyRom(OneWireMaster & master, RomId::const_span romId) {
IanBenzMaxim 7:9cd16581b578 59 SearchRomState searchState(romId);
IanBenzMaxim 7:9cd16581b578 60 error_code result = searchRom(master, searchState);
IanBenzMaxim 7:9cd16581b578 61 if (result) {
IanBenzMaxim 7:9cd16581b578 62 return result;
IanBenzMaxim 7:9cd16581b578 63 }
IanBenzMaxim 7:9cd16581b578 64 // Check if same device found.
IanBenzMaxim 7:9cd16581b578 65 if (!equal(romId, make_span(searchState.romId))) {
IanBenzMaxim 7:9cd16581b578 66 result = make_error_code(OneWireMaster::NoSlaveError);
IanBenzMaxim 7:9cd16581b578 67 }
IanBenzMaxim 7:9cd16581b578 68 return result;
IanBenzMaxim 7:9cd16581b578 69 }
IanBenzMaxim 7:9cd16581b578 70
IanBenzMaxim 7:9cd16581b578 71 error_code readRom(OneWireMaster & master, RomId::span romId) {
IanBenzMaxim 7:9cd16581b578 72 error_code result = master.reset();
IanBenzMaxim 7:9cd16581b578 73 if (result) {
IanBenzMaxim 7:9cd16581b578 74 return result;
IanBenzMaxim 7:9cd16581b578 75 }
IanBenzMaxim 7:9cd16581b578 76 result = master.writeByte(ReadRomCmd);
IanBenzMaxim 7:9cd16581b578 77 if (result) {
IanBenzMaxim 7:9cd16581b578 78 return result;
IanBenzMaxim 7:9cd16581b578 79 }
IanBenzMaxim 7:9cd16581b578 80 result = master.readBlock(romId);
IanBenzMaxim 7:9cd16581b578 81 if (result) {
IanBenzMaxim 7:9cd16581b578 82 return result;
IanBenzMaxim 7:9cd16581b578 83 }
IanBenzMaxim 7:9cd16581b578 84 if (!valid(romId)) {
IanBenzMaxim 7:9cd16581b578 85 result = make_error_code(OneWireMaster::NoSlaveError);
IanBenzMaxim 7:9cd16581b578 86 }
IanBenzMaxim 7:9cd16581b578 87 return result;
IanBenzMaxim 7:9cd16581b578 88 }
IanBenzMaxim 7:9cd16581b578 89
IanBenzMaxim 7:9cd16581b578 90 error_code skipRom(OneWireMaster & master) {
IanBenzMaxim 7:9cd16581b578 91 error_code result = master.reset();
IanBenzMaxim 7:9cd16581b578 92 if (result) {
IanBenzMaxim 7:9cd16581b578 93 return result;
IanBenzMaxim 7:9cd16581b578 94 }
IanBenzMaxim 7:9cd16581b578 95 result = master.writeByte(SkipRomCmd);
IanBenzMaxim 7:9cd16581b578 96 return result;
IanBenzMaxim 7:9cd16581b578 97 }
IanBenzMaxim 7:9cd16581b578 98
IanBenzMaxim 7:9cd16581b578 99 error_code matchRom(OneWireMaster & master, RomId::const_span romId) {
IanBenzMaxim 7:9cd16581b578 100 error_code result = master.reset();
IanBenzMaxim 7:9cd16581b578 101 if (result) {
IanBenzMaxim 7:9cd16581b578 102 return result;
IanBenzMaxim 7:9cd16581b578 103 }
IanBenzMaxim 7:9cd16581b578 104 result = master.writeByte(MatchRomCmd);
IanBenzMaxim 7:9cd16581b578 105 if (result) {
IanBenzMaxim 7:9cd16581b578 106 return result;
IanBenzMaxim 7:9cd16581b578 107 }
IanBenzMaxim 7:9cd16581b578 108 result = master.writeBlock(romId);
IanBenzMaxim 7:9cd16581b578 109 return result;
IanBenzMaxim 7:9cd16581b578 110 }
IanBenzMaxim 7:9cd16581b578 111
IanBenzMaxim 7:9cd16581b578 112 error_code overdriveSkipRom(OneWireMaster & master) {
IanBenzMaxim 7:9cd16581b578 113 error_code result = master.reset();
IanBenzMaxim 7:9cd16581b578 114 if (result) {
IanBenzMaxim 7:9cd16581b578 115 return result;
IanBenzMaxim 7:9cd16581b578 116 }
IanBenzMaxim 7:9cd16581b578 117 result = master.writeByte(OverdriveSkipRomCmd);
IanBenzMaxim 7:9cd16581b578 118 if (result) {
IanBenzMaxim 7:9cd16581b578 119 return result;
IanBenzMaxim 7:9cd16581b578 120 }
IanBenzMaxim 7:9cd16581b578 121 result = master.setSpeed(OneWireMaster::OverdriveSpeed);
IanBenzMaxim 7:9cd16581b578 122 return result;
IanBenzMaxim 7:9cd16581b578 123 }
IanBenzMaxim 7:9cd16581b578 124
IanBenzMaxim 7:9cd16581b578 125 error_code overdriveMatchRom(OneWireMaster & master, RomId::const_span romId) {
IanBenzMaxim 7:9cd16581b578 126 error_code result = master.reset();
IanBenzMaxim 7:9cd16581b578 127 if (result) {
IanBenzMaxim 7:9cd16581b578 128 return result;
IanBenzMaxim 7:9cd16581b578 129 }
IanBenzMaxim 7:9cd16581b578 130 result = master.writeByte(OverdriveMatchRomCmd);
IanBenzMaxim 7:9cd16581b578 131 if (result) {
IanBenzMaxim 7:9cd16581b578 132 return result;
IanBenzMaxim 7:9cd16581b578 133 }
IanBenzMaxim 7:9cd16581b578 134 result = master.setSpeed(OneWireMaster::OverdriveSpeed);
IanBenzMaxim 7:9cd16581b578 135 if (result) {
IanBenzMaxim 7:9cd16581b578 136 return result;
IanBenzMaxim 7:9cd16581b578 137 }
IanBenzMaxim 7:9cd16581b578 138 result = master.writeBlock(romId);
IanBenzMaxim 7:9cd16581b578 139 return result;
IanBenzMaxim 7:9cd16581b578 140 }
IanBenzMaxim 7:9cd16581b578 141
IanBenzMaxim 7:9cd16581b578 142 error_code resumeRom(OneWireMaster & master) {
IanBenzMaxim 7:9cd16581b578 143 error_code result = master.reset();
IanBenzMaxim 7:9cd16581b578 144 if (result) {
IanBenzMaxim 7:9cd16581b578 145 return result;
IanBenzMaxim 7:9cd16581b578 146 }
IanBenzMaxim 7:9cd16581b578 147 result = master.writeByte(ResumeRomCmd);
IanBenzMaxim 7:9cd16581b578 148 return result;
IanBenzMaxim 7:9cd16581b578 149 }
IanBenzMaxim 7:9cd16581b578 150
IanBenzMaxim 7:9cd16581b578 151 error_code searchRom(OneWireMaster & master, SearchRomState & searchState) {
IanBenzMaxim 7:9cd16581b578 152 if (searchState.lastDevice) {
IanBenzMaxim 7:9cd16581b578 153 searchState = SearchRomState();
IanBenzMaxim 7:9cd16581b578 154 }
IanBenzMaxim 7:9cd16581b578 155
IanBenzMaxim 7:9cd16581b578 156 error_code result = master.reset();
IanBenzMaxim 7:9cd16581b578 157 if (result) {
IanBenzMaxim 7:9cd16581b578 158 return result;
IanBenzMaxim 7:9cd16581b578 159 }
IanBenzMaxim 7:9cd16581b578 160 result = master.writeByte(SearchRomCmd);
IanBenzMaxim 7:9cd16581b578 161 if (result) {
IanBenzMaxim 7:9cd16581b578 162 return result;
IanBenzMaxim 7:9cd16581b578 163 }
IanBenzMaxim 7:9cd16581b578 164
IanBenzMaxim 7:9cd16581b578 165 SearchRomState newSearchState;
IanBenzMaxim 7:9cd16581b578 166 newSearchState.lastFamilyDiscrepancy = searchState.lastFamilyDiscrepancy;
IanBenzMaxim 7:9cd16581b578 167 for (int idBitNumber = 1; idBitNumber <= 64; ++idBitNumber) {
IanBenzMaxim 7:9cd16581b578 168 const int idByteNumber = (idBitNumber - 1) / 8;
IanBenzMaxim 7:9cd16581b578 169 const unsigned int idBitMask = 1 << ((idBitNumber - 1) % 8);
IanBenzMaxim 7:9cd16581b578 170
IanBenzMaxim 7:9cd16581b578 171 OneWireMaster::TripletData tripletData;
IanBenzMaxim 7:9cd16581b578 172 if (idBitNumber == searchState.lastDiscrepancy) {
IanBenzMaxim 7:9cd16581b578 173 tripletData.writeBit = 1;
IanBenzMaxim 7:9cd16581b578 174 } else if (idBitNumber > searchState.lastDiscrepancy) {
IanBenzMaxim 7:9cd16581b578 175 tripletData.writeBit = 0;
IanBenzMaxim 7:9cd16581b578 176 } else { // idBitNumber < searchState.lastDiscrepancy
IanBenzMaxim 7:9cd16581b578 177 tripletData.writeBit =
IanBenzMaxim 7:9cd16581b578 178 (searchState.romId[idByteNumber] & idBitMask) == idBitMask;
IanBenzMaxim 7:9cd16581b578 179 }
IanBenzMaxim 7:9cd16581b578 180
IanBenzMaxim 7:9cd16581b578 181 result = master.triplet(tripletData);
IanBenzMaxim 7:9cd16581b578 182 if (result) {
IanBenzMaxim 7:9cd16581b578 183 return result;
IanBenzMaxim 7:9cd16581b578 184 }
IanBenzMaxim 7:9cd16581b578 185 if (tripletData.readBit && tripletData.readBitComplement) {
IanBenzMaxim 7:9cd16581b578 186 return make_error_code(OneWireMaster::NoSlaveError);
IanBenzMaxim 7:9cd16581b578 187 }
IanBenzMaxim 7:9cd16581b578 188
IanBenzMaxim 7:9cd16581b578 189 if (tripletData.writeBit) {
IanBenzMaxim 7:9cd16581b578 190 newSearchState.romId[idByteNumber] |= idBitMask;
IanBenzMaxim 7:9cd16581b578 191 } else if (!tripletData.readBit && !tripletData.readBitComplement) {
IanBenzMaxim 7:9cd16581b578 192 newSearchState.lastDiscrepancy = idBitNumber;
IanBenzMaxim 7:9cd16581b578 193 if (idBitNumber <= 8) {
IanBenzMaxim 7:9cd16581b578 194 newSearchState.lastFamilyDiscrepancy = idBitNumber;
IanBenzMaxim 7:9cd16581b578 195 }
IanBenzMaxim 7:9cd16581b578 196 }
IanBenzMaxim 7:9cd16581b578 197 }
IanBenzMaxim 7:9cd16581b578 198
IanBenzMaxim 7:9cd16581b578 199 if (valid(newSearchState.romId)) {
IanBenzMaxim 7:9cd16581b578 200 if (newSearchState.lastDiscrepancy == 0) {
IanBenzMaxim 7:9cd16581b578 201 newSearchState.lastDevice = true;
IanBenzMaxim 7:9cd16581b578 202 }
IanBenzMaxim 7:9cd16581b578 203 searchState = newSearchState;
IanBenzMaxim 7:9cd16581b578 204 } else {
IanBenzMaxim 7:9cd16581b578 205 result = make_error_code(OneWireMaster::NoSlaveError);
IanBenzMaxim 7:9cd16581b578 206 }
IanBenzMaxim 7:9cd16581b578 207 return result;
IanBenzMaxim 7:9cd16581b578 208 }
IanBenzMaxim 7:9cd16581b578 209
IanBenzMaxim 7:9cd16581b578 210 } // namespace MaximInterfaceCore