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 8:5ea891c7d1a1 33 #ifndef MaximInterfaceDevices_DS9481P_300_hpp
IanBenzMaxim 8:5ea891c7d1a1 34 #define MaximInterfaceDevices_DS9481P_300_hpp
IanBenzMaxim 7:9cd16581b578 35
IanBenzMaxim 7:9cd16581b578 36 #include <MaximInterfaceCore/I2CMasterDecorator.hpp>
IanBenzMaxim 7:9cd16581b578 37 #include <MaximInterfaceCore/OneWireMasterDecorator.hpp>
IanBenzMaxim 7:9cd16581b578 38 #include <MaximInterfaceCore/SerialPort.hpp>
IanBenzMaxim 7:9cd16581b578 39 #include <MaximInterfaceCore/Sleep.hpp>
IanBenzMaxim 7:9cd16581b578 40 #include "DS2480B.hpp"
IanBenzMaxim 7:9cd16581b578 41 #include "DS9400.hpp"
IanBenzMaxim 7:9cd16581b578 42 #include "Config.hpp"
IanBenzMaxim 7:9cd16581b578 43
IanBenzMaxim 7:9cd16581b578 44 namespace MaximInterfaceDevices {
IanBenzMaxim 7:9cd16581b578 45
IanBenzMaxim 7:9cd16581b578 46 /// DS9481P-300 USB to 1-Wire and I2C adapter.
IanBenzMaxim 7:9cd16581b578 47 class DS9481P_300 {
IanBenzMaxim 7:9cd16581b578 48 public:
IanBenzMaxim 7:9cd16581b578 49 MaximInterfaceDevices_EXPORT DS9481P_300(Core::Sleep & sleep,
IanBenzMaxim 7:9cd16581b578 50 Core::SerialPort & serialPort);
IanBenzMaxim 7:9cd16581b578 51
IanBenzMaxim 7:9cd16581b578 52 void setSleep(Core::Sleep & sleep) { ds2480b.setSleep(sleep); }
IanBenzMaxim 7:9cd16581b578 53
IanBenzMaxim 7:9cd16581b578 54 void setSerialPort(Core::SerialPort & serialPort) {
IanBenzMaxim 7:9cd16581b578 55 this->serialPort = &serialPort;
IanBenzMaxim 7:9cd16581b578 56 ds2480b.setUart(serialPort);
IanBenzMaxim 7:9cd16581b578 57 ds9400.setUart(serialPort);
IanBenzMaxim 7:9cd16581b578 58 }
IanBenzMaxim 7:9cd16581b578 59
IanBenzMaxim 8:5ea891c7d1a1 60 MaximInterfaceDevices_EXPORT Core::Result<void>
IanBenzMaxim 7:9cd16581b578 61 connect(const std::string & portName);
IanBenzMaxim 7:9cd16581b578 62
IanBenzMaxim 8:5ea891c7d1a1 63 MaximInterfaceDevices_EXPORT Core::Result<void> disconnect();
IanBenzMaxim 7:9cd16581b578 64
IanBenzMaxim 7:9cd16581b578 65 /// Access the 1-Wire master when connected to an adapter.
IanBenzMaxim 7:9cd16581b578 66 Core::OneWireMaster & oneWireMaster() { return oneWireMaster_; }
IanBenzMaxim 7:9cd16581b578 67
IanBenzMaxim 7:9cd16581b578 68 /// Access the I2C master when connected to an adapter.
IanBenzMaxim 7:9cd16581b578 69 Core::I2CMaster & i2cMaster() { return i2cMaster_; }
IanBenzMaxim 7:9cd16581b578 70
IanBenzMaxim 7:9cd16581b578 71 private:
IanBenzMaxim 7:9cd16581b578 72 class OneWireMaster : public Core::OneWireMasterDecorator {
IanBenzMaxim 7:9cd16581b578 73 public:
IanBenzMaxim 7:9cd16581b578 74 explicit OneWireMaster(DS9481P_300 & parent)
IanBenzMaxim 7:9cd16581b578 75 : OneWireMasterDecorator(parent.ds2480b), parent(&parent) {}
IanBenzMaxim 7:9cd16581b578 76
IanBenzMaxim 8:5ea891c7d1a1 77 MaximInterfaceDevices_EXPORT virtual Core::Result<void> reset();
IanBenzMaxim 7:9cd16581b578 78
IanBenzMaxim 8:5ea891c7d1a1 79 MaximInterfaceDevices_EXPORT virtual Core::Result<bool>
IanBenzMaxim 8:5ea891c7d1a1 80 touchBitSetLevel(bool sendBit, Level afterLevel);
IanBenzMaxim 7:9cd16581b578 81
IanBenzMaxim 8:5ea891c7d1a1 82 MaximInterfaceDevices_EXPORT virtual Core::Result<void>
IanBenzMaxim 7:9cd16581b578 83 writeByteSetLevel(uint_least8_t sendByte, Level afterLevel);
IanBenzMaxim 7:9cd16581b578 84
IanBenzMaxim 8:5ea891c7d1a1 85 MaximInterfaceDevices_EXPORT virtual Core::Result<uint_least8_t>
IanBenzMaxim 8:5ea891c7d1a1 86 readByteSetLevel(Level afterLevel);
IanBenzMaxim 7:9cd16581b578 87
IanBenzMaxim 8:5ea891c7d1a1 88 MaximInterfaceDevices_EXPORT virtual Core::Result<void>
IanBenzMaxim 7:9cd16581b578 89 writeBlock(Core::span<const uint_least8_t> sendBuf);
IanBenzMaxim 7:9cd16581b578 90
IanBenzMaxim 8:5ea891c7d1a1 91 MaximInterfaceDevices_EXPORT virtual Core::Result<void>
IanBenzMaxim 7:9cd16581b578 92 readBlock(Core::span<uint_least8_t> recvBuf);
IanBenzMaxim 7:9cd16581b578 93
IanBenzMaxim 8:5ea891c7d1a1 94 MaximInterfaceDevices_EXPORT virtual Core::Result<void>
IanBenzMaxim 7:9cd16581b578 95 setSpeed(Speed newSpeed);
IanBenzMaxim 7:9cd16581b578 96
IanBenzMaxim 8:5ea891c7d1a1 97 MaximInterfaceDevices_EXPORT virtual Core::Result<void>
IanBenzMaxim 7:9cd16581b578 98 setLevel(Level newLevel);
IanBenzMaxim 7:9cd16581b578 99
IanBenzMaxim 8:5ea891c7d1a1 100 MaximInterfaceDevices_EXPORT virtual Core::Result<TripletData>
IanBenzMaxim 8:5ea891c7d1a1 101 triplet(bool sendBit);
IanBenzMaxim 7:9cd16581b578 102
IanBenzMaxim 7:9cd16581b578 103 private:
IanBenzMaxim 7:9cd16581b578 104 DS9481P_300 * parent;
IanBenzMaxim 7:9cd16581b578 105 };
IanBenzMaxim 7:9cd16581b578 106
IanBenzMaxim 7:9cd16581b578 107 class I2CMaster : public Core::I2CMasterDecorator {
IanBenzMaxim 7:9cd16581b578 108 public:
IanBenzMaxim 7:9cd16581b578 109 explicit I2CMaster(DS9481P_300 & parent)
IanBenzMaxim 7:9cd16581b578 110 : I2CMasterDecorator(parent.ds9400), parent(&parent) {}
IanBenzMaxim 7:9cd16581b578 111
IanBenzMaxim 8:5ea891c7d1a1 112 MaximInterfaceDevices_EXPORT virtual Core::Result<void>
IanBenzMaxim 7:9cd16581b578 113 start(uint_least8_t address);
IanBenzMaxim 7:9cd16581b578 114
IanBenzMaxim 8:5ea891c7d1a1 115 MaximInterfaceDevices_EXPORT virtual Core::Result<void> stop();
IanBenzMaxim 7:9cd16581b578 116
IanBenzMaxim 8:5ea891c7d1a1 117 MaximInterfaceDevices_EXPORT virtual Core::Result<void>
IanBenzMaxim 7:9cd16581b578 118 writeByte(uint_least8_t data);
IanBenzMaxim 7:9cd16581b578 119
IanBenzMaxim 8:5ea891c7d1a1 120 MaximInterfaceDevices_EXPORT virtual Core::Result<void>
IanBenzMaxim 7:9cd16581b578 121 writeBlock(Core::span<const uint_least8_t> data);
IanBenzMaxim 7:9cd16581b578 122
IanBenzMaxim 8:5ea891c7d1a1 123 MaximInterfaceDevices_EXPORT virtual Core::Result<void>
IanBenzMaxim 8:5ea891c7d1a1 124 writePacket(uint_least8_t address, Core::span<const uint_least8_t> data,
IanBenzMaxim 8:5ea891c7d1a1 125 DoStop doStop);
IanBenzMaxim 7:9cd16581b578 126
IanBenzMaxim 8:5ea891c7d1a1 127 MaximInterfaceDevices_EXPORT virtual Core::Result<uint_least8_t>
IanBenzMaxim 8:5ea891c7d1a1 128 readByte(DoAck doAck);
IanBenzMaxim 7:9cd16581b578 129
IanBenzMaxim 8:5ea891c7d1a1 130 MaximInterfaceDevices_EXPORT virtual Core::Result<void>
IanBenzMaxim 8:5ea891c7d1a1 131 readBlock(Core::span<uint_least8_t> data, DoAck doAck);
IanBenzMaxim 7:9cd16581b578 132
IanBenzMaxim 8:5ea891c7d1a1 133 MaximInterfaceDevices_EXPORT virtual Core::Result<void>
IanBenzMaxim 8:5ea891c7d1a1 134 readPacket(uint_least8_t address, Core::span<uint_least8_t> data,
IanBenzMaxim 8:5ea891c7d1a1 135 DoStop doStop);
IanBenzMaxim 7:9cd16581b578 136
IanBenzMaxim 7:9cd16581b578 137 private:
IanBenzMaxim 7:9cd16581b578 138 DS9481P_300 * parent;
IanBenzMaxim 7:9cd16581b578 139 };
IanBenzMaxim 7:9cd16581b578 140
IanBenzMaxim 7:9cd16581b578 141 class DS2480BWithEscape : public DS2480B {
IanBenzMaxim 7:9cd16581b578 142 public:
IanBenzMaxim 7:9cd16581b578 143 DS2480BWithEscape(Core::Sleep & sleep, Core::Uart & uart)
IanBenzMaxim 7:9cd16581b578 144 : DS2480B(sleep, uart) {}
IanBenzMaxim 7:9cd16581b578 145
IanBenzMaxim 8:5ea891c7d1a1 146 Core::Result<void> escape();
IanBenzMaxim 7:9cd16581b578 147 };
IanBenzMaxim 7:9cd16581b578 148
IanBenzMaxim 7:9cd16581b578 149 class DS9400WithEscape : public DS9400 {
IanBenzMaxim 7:9cd16581b578 150 public:
IanBenzMaxim 7:9cd16581b578 151 explicit DS9400WithEscape(Core::Uart & uart) : DS9400(uart) {}
IanBenzMaxim 7:9cd16581b578 152
IanBenzMaxim 8:5ea891c7d1a1 153 Core::Result<void> escape();
IanBenzMaxim 7:9cd16581b578 154 };
IanBenzMaxim 7:9cd16581b578 155
IanBenzMaxim 7:9cd16581b578 156 enum Bus { OneWire, I2C };
IanBenzMaxim 7:9cd16581b578 157
IanBenzMaxim 7:9cd16581b578 158 Core::SerialPort * serialPort;
IanBenzMaxim 7:9cd16581b578 159 Bus currentBus;
IanBenzMaxim 7:9cd16581b578 160 DS2480BWithEscape ds2480b;
IanBenzMaxim 7:9cd16581b578 161 OneWireMaster oneWireMaster_;
IanBenzMaxim 7:9cd16581b578 162 DS9400WithEscape ds9400;
IanBenzMaxim 7:9cd16581b578 163 I2CMaster i2cMaster_;
IanBenzMaxim 7:9cd16581b578 164
IanBenzMaxim 8:5ea891c7d1a1 165 Core::Result<void> selectOneWire();
IanBenzMaxim 8:5ea891c7d1a1 166 Core::Result<void> selectBus(Bus newBus);
IanBenzMaxim 7:9cd16581b578 167
IanBenzMaxim 7:9cd16581b578 168 friend class OneWireMaster;
IanBenzMaxim 7:9cd16581b578 169 friend class I2CMaster;
IanBenzMaxim 7:9cd16581b578 170 };
IanBenzMaxim 7:9cd16581b578 171
IanBenzMaxim 7:9cd16581b578 172 } // namespace MaximInterfaceDevices
IanBenzMaxim 7:9cd16581b578 173
IanBenzMaxim 7:9cd16581b578 174 #endif