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:
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?

UserRevisionLine numberNew 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 <MaximInterfaceCore/Error.hpp>
IanBenzMaxim 7:9cd16581b578 34 #include "DS9481P_300.hpp"
IanBenzMaxim 7:9cd16581b578 35
IanBenzMaxim 8:5ea891c7d1a1 36 #define TRY MaximInterfaceCore_TRY
IanBenzMaxim 8:5ea891c7d1a1 37
IanBenzMaxim 7:9cd16581b578 38 namespace MaximInterfaceDevices {
IanBenzMaxim 7:9cd16581b578 39
IanBenzMaxim 7:9cd16581b578 40 using namespace Core;
IanBenzMaxim 7:9cd16581b578 41
IanBenzMaxim 7:9cd16581b578 42 DS9481P_300::DS9481P_300(Sleep & sleep, SerialPort & serialPort)
IanBenzMaxim 7:9cd16581b578 43 : serialPort(&serialPort), currentBus(OneWire), ds2480b(sleep, serialPort),
IanBenzMaxim 7:9cd16581b578 44 oneWireMaster_(*this), ds9400(serialPort), i2cMaster_(*this) {}
IanBenzMaxim 7:9cd16581b578 45
IanBenzMaxim 8:5ea891c7d1a1 46 Result<void> DS9481P_300::connect(const std::string & portName) {
IanBenzMaxim 8:5ea891c7d1a1 47 Result<void> result = serialPort->connect(portName);
IanBenzMaxim 8:5ea891c7d1a1 48 if (result) {
IanBenzMaxim 7:9cd16581b578 49 result = selectOneWire();
IanBenzMaxim 7:9cd16581b578 50 if (result) {
IanBenzMaxim 8:5ea891c7d1a1 51 currentBus = OneWire;
IanBenzMaxim 7:9cd16581b578 52 } else {
IanBenzMaxim 8:5ea891c7d1a1 53 serialPort->disconnect();
IanBenzMaxim 7:9cd16581b578 54 }
IanBenzMaxim 7:9cd16581b578 55 }
IanBenzMaxim 7:9cd16581b578 56 return result;
IanBenzMaxim 7:9cd16581b578 57 }
IanBenzMaxim 7:9cd16581b578 58
IanBenzMaxim 8:5ea891c7d1a1 59 Result<void> DS9481P_300::disconnect() { return serialPort->disconnect(); }
IanBenzMaxim 8:5ea891c7d1a1 60
IanBenzMaxim 8:5ea891c7d1a1 61 Result<void> DS9481P_300::selectOneWire() {
IanBenzMaxim 8:5ea891c7d1a1 62 // Escape DS9400 mode.
IanBenzMaxim 8:5ea891c7d1a1 63 TRY(ds9400.escape());
IanBenzMaxim 8:5ea891c7d1a1 64 TRY(ds2480b.initialize());
IanBenzMaxim 8:5ea891c7d1a1 65 return none;
IanBenzMaxim 7:9cd16581b578 66 }
IanBenzMaxim 7:9cd16581b578 67
IanBenzMaxim 8:5ea891c7d1a1 68 Result<void> DS9481P_300::selectBus(Bus newBus) {
IanBenzMaxim 8:5ea891c7d1a1 69 if (currentBus != newBus) {
IanBenzMaxim 8:5ea891c7d1a1 70 switch (currentBus) {
IanBenzMaxim 8:5ea891c7d1a1 71 case OneWire: // Next bus I2C.
IanBenzMaxim 8:5ea891c7d1a1 72 // Escape DS2480 Mode.
IanBenzMaxim 8:5ea891c7d1a1 73 TRY(ds2480b.escape());
IanBenzMaxim 8:5ea891c7d1a1 74 // Wait for awake notification.
IanBenzMaxim 8:5ea891c7d1a1 75 TRY(ds9400.waitAwake());
IanBenzMaxim 8:5ea891c7d1a1 76 break;
IanBenzMaxim 8:5ea891c7d1a1 77
IanBenzMaxim 8:5ea891c7d1a1 78 case I2C: // Next bus OneWire.
IanBenzMaxim 8:5ea891c7d1a1 79 TRY(selectOneWire());
IanBenzMaxim 8:5ea891c7d1a1 80 break;
IanBenzMaxim 8:5ea891c7d1a1 81 }
IanBenzMaxim 8:5ea891c7d1a1 82 currentBus = newBus;
IanBenzMaxim 7:9cd16581b578 83 }
IanBenzMaxim 8:5ea891c7d1a1 84 return none;
IanBenzMaxim 7:9cd16581b578 85 }
IanBenzMaxim 7:9cd16581b578 86
IanBenzMaxim 8:5ea891c7d1a1 87 Result<void> DS9481P_300::OneWireMaster::reset() {
IanBenzMaxim 8:5ea891c7d1a1 88 TRY(parent->selectBus(OneWire));
IanBenzMaxim 8:5ea891c7d1a1 89 return OneWireMasterDecorator::reset();
IanBenzMaxim 7:9cd16581b578 90 }
IanBenzMaxim 7:9cd16581b578 91
IanBenzMaxim 8:5ea891c7d1a1 92 Result<bool> DS9481P_300::OneWireMaster::touchBitSetLevel(bool sendBit,
IanBenzMaxim 8:5ea891c7d1a1 93 Level afterLevel) {
IanBenzMaxim 8:5ea891c7d1a1 94 TRY(parent->selectBus(OneWire));
IanBenzMaxim 8:5ea891c7d1a1 95 return OneWireMasterDecorator::touchBitSetLevel(sendBit, afterLevel);
IanBenzMaxim 7:9cd16581b578 96 }
IanBenzMaxim 7:9cd16581b578 97
IanBenzMaxim 8:5ea891c7d1a1 98 Result<void>
IanBenzMaxim 8:5ea891c7d1a1 99 DS9481P_300::OneWireMaster::writeByteSetLevel(uint_least8_t sendByte,
IanBenzMaxim 8:5ea891c7d1a1 100 Level afterLevel) {
IanBenzMaxim 8:5ea891c7d1a1 101 TRY(parent->selectBus(OneWire));
IanBenzMaxim 8:5ea891c7d1a1 102 return OneWireMasterDecorator::writeByteSetLevel(sendByte, afterLevel);
IanBenzMaxim 7:9cd16581b578 103 }
IanBenzMaxim 7:9cd16581b578 104
IanBenzMaxim 8:5ea891c7d1a1 105 Result<uint_least8_t>
IanBenzMaxim 8:5ea891c7d1a1 106 DS9481P_300::OneWireMaster::readByteSetLevel(Level afterLevel) {
IanBenzMaxim 8:5ea891c7d1a1 107 TRY(parent->selectBus(OneWire));
IanBenzMaxim 8:5ea891c7d1a1 108 return OneWireMasterDecorator::readByteSetLevel(afterLevel);
IanBenzMaxim 7:9cd16581b578 109 }
IanBenzMaxim 7:9cd16581b578 110
IanBenzMaxim 8:5ea891c7d1a1 111 Result<void>
IanBenzMaxim 8:5ea891c7d1a1 112 DS9481P_300::OneWireMaster::writeBlock(span<const uint_least8_t> sendBuf) {
IanBenzMaxim 8:5ea891c7d1a1 113 TRY(parent->selectBus(OneWire));
IanBenzMaxim 8:5ea891c7d1a1 114 return OneWireMasterDecorator::writeBlock(sendBuf);
IanBenzMaxim 8:5ea891c7d1a1 115 }
IanBenzMaxim 8:5ea891c7d1a1 116
IanBenzMaxim 8:5ea891c7d1a1 117 Result<void>
IanBenzMaxim 8:5ea891c7d1a1 118 DS9481P_300::OneWireMaster::readBlock(span<uint_least8_t> recvBuf) {
IanBenzMaxim 8:5ea891c7d1a1 119 TRY(parent->selectBus(OneWire));
IanBenzMaxim 8:5ea891c7d1a1 120 return OneWireMasterDecorator::readBlock(recvBuf);
IanBenzMaxim 7:9cd16581b578 121 }
IanBenzMaxim 7:9cd16581b578 122
IanBenzMaxim 8:5ea891c7d1a1 123 Result<void> DS9481P_300::OneWireMaster::setSpeed(Speed newSpeed) {
IanBenzMaxim 8:5ea891c7d1a1 124 TRY(parent->selectBus(OneWire));
IanBenzMaxim 8:5ea891c7d1a1 125 return OneWireMasterDecorator::setSpeed(newSpeed);
IanBenzMaxim 7:9cd16581b578 126 }
IanBenzMaxim 7:9cd16581b578 127
IanBenzMaxim 8:5ea891c7d1a1 128 Result<void> DS9481P_300::OneWireMaster::setLevel(Level newLevel) {
IanBenzMaxim 8:5ea891c7d1a1 129 TRY(parent->selectBus(OneWire));
IanBenzMaxim 8:5ea891c7d1a1 130 return OneWireMasterDecorator::setLevel(newLevel);
IanBenzMaxim 7:9cd16581b578 131 }
IanBenzMaxim 7:9cd16581b578 132
IanBenzMaxim 8:5ea891c7d1a1 133 Result<OneWireMaster::TripletData>
IanBenzMaxim 8:5ea891c7d1a1 134 DS9481P_300::OneWireMaster::triplet(bool sendBit) {
IanBenzMaxim 8:5ea891c7d1a1 135 TRY(parent->selectBus(OneWire));
IanBenzMaxim 8:5ea891c7d1a1 136 return OneWireMasterDecorator::triplet(sendBit);
IanBenzMaxim 7:9cd16581b578 137 }
IanBenzMaxim 7:9cd16581b578 138
IanBenzMaxim 8:5ea891c7d1a1 139 Result<void> DS9481P_300::I2CMaster::start(uint_least8_t address) {
IanBenzMaxim 8:5ea891c7d1a1 140 TRY(parent->selectBus(I2C));
IanBenzMaxim 8:5ea891c7d1a1 141 return I2CMasterDecorator::start(address);
IanBenzMaxim 7:9cd16581b578 142 }
IanBenzMaxim 7:9cd16581b578 143
IanBenzMaxim 8:5ea891c7d1a1 144 Result<void> DS9481P_300::I2CMaster::stop() {
IanBenzMaxim 8:5ea891c7d1a1 145 TRY(parent->selectBus(I2C));
IanBenzMaxim 8:5ea891c7d1a1 146 return I2CMasterDecorator::stop();
IanBenzMaxim 8:5ea891c7d1a1 147 }
IanBenzMaxim 8:5ea891c7d1a1 148
IanBenzMaxim 8:5ea891c7d1a1 149 Result<void> DS9481P_300::I2CMaster::writeByte(uint_least8_t data) {
IanBenzMaxim 8:5ea891c7d1a1 150 TRY(parent->selectBus(I2C));
IanBenzMaxim 8:5ea891c7d1a1 151 return I2CMasterDecorator::writeByte(data);
IanBenzMaxim 7:9cd16581b578 152 }
IanBenzMaxim 7:9cd16581b578 153
IanBenzMaxim 8:5ea891c7d1a1 154 Result<void>
IanBenzMaxim 8:5ea891c7d1a1 155 DS9481P_300::I2CMaster::writeBlock(span<const uint_least8_t> data) {
IanBenzMaxim 8:5ea891c7d1a1 156 TRY(parent->selectBus(I2C));
IanBenzMaxim 8:5ea891c7d1a1 157 return I2CMasterDecorator::writeBlock(data);
IanBenzMaxim 7:9cd16581b578 158 }
IanBenzMaxim 7:9cd16581b578 159
IanBenzMaxim 8:5ea891c7d1a1 160 Result<void> DS9481P_300::I2CMaster::writePacket(uint_least8_t address,
IanBenzMaxim 8:5ea891c7d1a1 161 span<const uint_least8_t> data,
IanBenzMaxim 8:5ea891c7d1a1 162 DoStop doStop) {
IanBenzMaxim 8:5ea891c7d1a1 163 TRY(parent->selectBus(I2C));
IanBenzMaxim 8:5ea891c7d1a1 164 return I2CMasterDecorator::writePacket(address, data, doStop);
IanBenzMaxim 7:9cd16581b578 165 }
IanBenzMaxim 7:9cd16581b578 166
IanBenzMaxim 8:5ea891c7d1a1 167 Result<uint_least8_t> DS9481P_300::I2CMaster::readByte(DoAck doAck) {
IanBenzMaxim 8:5ea891c7d1a1 168 TRY(parent->selectBus(I2C));
IanBenzMaxim 8:5ea891c7d1a1 169 return I2CMasterDecorator::readByte(doAck);
IanBenzMaxim 7:9cd16581b578 170 }
IanBenzMaxim 7:9cd16581b578 171
IanBenzMaxim 8:5ea891c7d1a1 172 Result<void> DS9481P_300::I2CMaster::readBlock(span<uint_least8_t> data,
IanBenzMaxim 8:5ea891c7d1a1 173 DoAck doAck) {
IanBenzMaxim 8:5ea891c7d1a1 174 TRY(parent->selectBus(I2C));
IanBenzMaxim 8:5ea891c7d1a1 175 return I2CMasterDecorator::readBlock(data, doAck);
IanBenzMaxim 7:9cd16581b578 176 }
IanBenzMaxim 7:9cd16581b578 177
IanBenzMaxim 8:5ea891c7d1a1 178 Result<void> DS9481P_300::I2CMaster::readPacket(uint_least8_t address,
IanBenzMaxim 8:5ea891c7d1a1 179 span<uint_least8_t> data,
IanBenzMaxim 8:5ea891c7d1a1 180 DoStop doStop) {
IanBenzMaxim 8:5ea891c7d1a1 181 TRY(parent->selectBus(I2C));
IanBenzMaxim 8:5ea891c7d1a1 182 return I2CMasterDecorator::readPacket(address, data, doStop);
IanBenzMaxim 8:5ea891c7d1a1 183 }
IanBenzMaxim 8:5ea891c7d1a1 184
IanBenzMaxim 8:5ea891c7d1a1 185 Result<void> DS9481P_300::DS2480BWithEscape::escape() {
IanBenzMaxim 7:9cd16581b578 186 return sendCommand(0xE5);
IanBenzMaxim 7:9cd16581b578 187 }
IanBenzMaxim 7:9cd16581b578 188
IanBenzMaxim 8:5ea891c7d1a1 189 Result<void> DS9481P_300::DS9400WithEscape::escape() { return configure('O'); }
IanBenzMaxim 7:9cd16581b578 190
IanBenzMaxim 7:9cd16581b578 191 } // namespace MaximInterfaceDevices