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 #ifndef MaximInterfaceCore_OneWireMaster
IanBenzMaxim 7:9cd16581b578 34 #define MaximInterfaceCore_OneWireMaster
IanBenzMaxim 7:9cd16581b578 35
IanBenzMaxim 7:9cd16581b578 36 #include <stdint.h>
IanBenzMaxim 7:9cd16581b578 37 #include "Config.hpp"
IanBenzMaxim 7:9cd16581b578 38 #include "span.hpp"
IanBenzMaxim 7:9cd16581b578 39 #include "system_error.hpp"
IanBenzMaxim 7:9cd16581b578 40
IanBenzMaxim 7:9cd16581b578 41 namespace MaximInterfaceCore {
IanBenzMaxim 7:9cd16581b578 42
IanBenzMaxim 7:9cd16581b578 43 /// 1-Wire master interface.
IanBenzMaxim 7:9cd16581b578 44 class OneWireMaster {
IanBenzMaxim 7:9cd16581b578 45 public:
IanBenzMaxim 7:9cd16581b578 46 /// Speed of the 1-Wire bus.
IanBenzMaxim 7:9cd16581b578 47 enum Speed { StandardSpeed = 0x00, OverdriveSpeed = 0x01 };
IanBenzMaxim 7:9cd16581b578 48
IanBenzMaxim 7:9cd16581b578 49 /// Level of the 1-Wire bus.
IanBenzMaxim 7:9cd16581b578 50 enum Level { NormalLevel = 0x00, StrongLevel = 0x02 };
IanBenzMaxim 7:9cd16581b578 51
IanBenzMaxim 7:9cd16581b578 52 /// Result of all 1-Wire commands.
IanBenzMaxim 7:9cd16581b578 53 enum ErrorValue {
IanBenzMaxim 7:9cd16581b578 54 NoSlaveError = 1, ///< Slave not detected, typically due to no presence pulse.
IanBenzMaxim 7:9cd16581b578 55 ShortDetectedError,
IanBenzMaxim 7:9cd16581b578 56 InvalidSpeedError,
IanBenzMaxim 7:9cd16581b578 57 InvalidLevelError
IanBenzMaxim 7:9cd16581b578 58 };
IanBenzMaxim 7:9cd16581b578 59
IanBenzMaxim 7:9cd16581b578 60 struct TripletData {
IanBenzMaxim 7:9cd16581b578 61 bool writeBit;
IanBenzMaxim 7:9cd16581b578 62 bool readBit;
IanBenzMaxim 7:9cd16581b578 63 bool readBitComplement;
IanBenzMaxim 7:9cd16581b578 64 };
IanBenzMaxim 7:9cd16581b578 65
IanBenzMaxim 7:9cd16581b578 66 virtual ~OneWireMaster() {}
IanBenzMaxim 7:9cd16581b578 67
IanBenzMaxim 7:9cd16581b578 68 /// @brief
IanBenzMaxim 7:9cd16581b578 69 /// Reset all of the devices on the 1-Wire bus and check for a presence pulse.
IanBenzMaxim 7:9cd16581b578 70 /// @returns
IanBenzMaxim 7:9cd16581b578 71 /// NoSlaveError if reset was performed but no presence pulse was detected.
IanBenzMaxim 7:9cd16581b578 72 virtual error_code reset() = 0;
IanBenzMaxim 7:9cd16581b578 73
IanBenzMaxim 7:9cd16581b578 74 /// @brief
IanBenzMaxim 7:9cd16581b578 75 /// Send and receive one bit of communication and set a new level on the
IanBenzMaxim 7:9cd16581b578 76 /// 1-Wire bus.
IanBenzMaxim 7:9cd16581b578 77 /// @param[in,out] sendRecvBit
IanBenzMaxim 7:9cd16581b578 78 /// Input containing the bit to send and output containing the received bit.
IanBenzMaxim 7:9cd16581b578 79 /// @param afterLevel Level to set the 1-Wire bus to after communication.
IanBenzMaxim 7:9cd16581b578 80 virtual error_code touchBitSetLevel(bool & sendRecvBit, Level afterLevel) = 0;
IanBenzMaxim 7:9cd16581b578 81
IanBenzMaxim 7:9cd16581b578 82 /// @brief
IanBenzMaxim 7:9cd16581b578 83 /// Send one byte of communication and set a new level on the 1-Wire bus.
IanBenzMaxim 7:9cd16581b578 84 /// @param sendByte Byte to send on the 1-Wire bus.
IanBenzMaxim 7:9cd16581b578 85 /// @param afterLevel Level to set the 1-Wire bus to after communication.
IanBenzMaxim 7:9cd16581b578 86 MaximInterfaceCore_EXPORT virtual error_code
IanBenzMaxim 7:9cd16581b578 87 writeByteSetLevel(uint_least8_t sendByte, Level afterLevel);
IanBenzMaxim 7:9cd16581b578 88
IanBenzMaxim 7:9cd16581b578 89 /// @brief
IanBenzMaxim 7:9cd16581b578 90 /// Receive one byte of communication and set a new level on the 1-Wire bus.
IanBenzMaxim 7:9cd16581b578 91 /// @param recvByte Buffer to receive the data from the 1-Wire bus.
IanBenzMaxim 7:9cd16581b578 92 /// @param afterLevel Level to set the 1-Wire bus to after communication.
IanBenzMaxim 7:9cd16581b578 93 MaximInterfaceCore_EXPORT virtual error_code
IanBenzMaxim 7:9cd16581b578 94 readByteSetLevel(uint_least8_t & recvByte, Level afterLevel);
IanBenzMaxim 7:9cd16581b578 95
IanBenzMaxim 7:9cd16581b578 96 /// @brief Send a block of communication on the 1-Wire bus.
IanBenzMaxim 7:9cd16581b578 97 /// @param[in] sendBuf Buffer to send on the 1-Wire bus.
IanBenzMaxim 7:9cd16581b578 98 MaximInterfaceCore_EXPORT virtual error_code
IanBenzMaxim 7:9cd16581b578 99 writeBlock(span<const uint_least8_t> sendBuf);
IanBenzMaxim 7:9cd16581b578 100
IanBenzMaxim 7:9cd16581b578 101 /// @brief Receive a block of communication on the 1-Wire bus.
IanBenzMaxim 7:9cd16581b578 102 /// @param[out] recvBuf Buffer to receive the data from the 1-Wire bus.
IanBenzMaxim 7:9cd16581b578 103 MaximInterfaceCore_EXPORT virtual error_code
IanBenzMaxim 7:9cd16581b578 104 readBlock(span<uint_least8_t> recvBuf);
IanBenzMaxim 7:9cd16581b578 105
IanBenzMaxim 7:9cd16581b578 106 /// Set the 1-Wire bus communication speed.
IanBenzMaxim 7:9cd16581b578 107 virtual error_code setSpeed(Speed newSpeed) = 0;
IanBenzMaxim 7:9cd16581b578 108
IanBenzMaxim 7:9cd16581b578 109 /// Set the 1-Wire bus level.
IanBenzMaxim 7:9cd16581b578 110 virtual error_code setLevel(Level newLevel) = 0;
IanBenzMaxim 7:9cd16581b578 111
IanBenzMaxim 7:9cd16581b578 112 /// @brief 1-Wire Triplet operation.
IanBenzMaxim 7:9cd16581b578 113 /// @details Perform one bit of a 1-Wire search. This command
IanBenzMaxim 7:9cd16581b578 114 /// does two read bits and one write bit. The write bit is either
IanBenzMaxim 7:9cd16581b578 115 /// the default direction (all devices have same bit) or in case
IanBenzMaxim 7:9cd16581b578 116 /// of a discrepancy, the data.writeBit parameter is used.
IanBenzMaxim 7:9cd16581b578 117 /// @param[in,out] data
IanBenzMaxim 7:9cd16581b578 118 /// Input with desired writeBit in case both read bits are zero.
IanBenzMaxim 7:9cd16581b578 119 /// Output with all data fields set.
IanBenzMaxim 7:9cd16581b578 120 MaximInterfaceCore_EXPORT virtual error_code triplet(TripletData & data);
IanBenzMaxim 7:9cd16581b578 121
IanBenzMaxim 7:9cd16581b578 122 /// @brief
IanBenzMaxim 7:9cd16581b578 123 /// Send one bit of communication and set a new level on the 1-Wire bus.
IanBenzMaxim 7:9cd16581b578 124 /// @param sendBit Bit to send on the 1-Wire bus.
IanBenzMaxim 7:9cd16581b578 125 /// @param afterLevel Level to set the 1-Wire bus to after communication.
IanBenzMaxim 7:9cd16581b578 126 error_code writeBitSetLevel(bool sendBit, Level afterLevel) {
IanBenzMaxim 7:9cd16581b578 127 return touchBitSetLevel(sendBit, afterLevel);
IanBenzMaxim 7:9cd16581b578 128 }
IanBenzMaxim 7:9cd16581b578 129
IanBenzMaxim 7:9cd16581b578 130 /// @brief
IanBenzMaxim 7:9cd16581b578 131 /// Receive one bit of communication and set a new level on the 1-Wire bus.
IanBenzMaxim 7:9cd16581b578 132 /// @param[out] recvBit Received data from the 1-Wire bus.
IanBenzMaxim 7:9cd16581b578 133 /// @param afterLevel Level to set the 1-Wire bus to after communication.
IanBenzMaxim 7:9cd16581b578 134 error_code readBitSetLevel(bool & recvBit, Level afterLevel) {
IanBenzMaxim 7:9cd16581b578 135 recvBit = 1;
IanBenzMaxim 7:9cd16581b578 136 return touchBitSetLevel(recvBit, afterLevel);
IanBenzMaxim 7:9cd16581b578 137 }
IanBenzMaxim 7:9cd16581b578 138
IanBenzMaxim 7:9cd16581b578 139 // Alternate forms of the read and write functions.
IanBenzMaxim 7:9cd16581b578 140
IanBenzMaxim 7:9cd16581b578 141 error_code touchBit(bool & sendRecvBit) {
IanBenzMaxim 7:9cd16581b578 142 return touchBitSetLevel(sendRecvBit, NormalLevel);
IanBenzMaxim 7:9cd16581b578 143 }
IanBenzMaxim 7:9cd16581b578 144
IanBenzMaxim 7:9cd16581b578 145 error_code writeBit(bool sendBit) {
IanBenzMaxim 7:9cd16581b578 146 return writeBitSetLevel(sendBit, NormalLevel);
IanBenzMaxim 7:9cd16581b578 147 }
IanBenzMaxim 7:9cd16581b578 148
IanBenzMaxim 7:9cd16581b578 149 error_code readBit(bool & recvBit) {
IanBenzMaxim 7:9cd16581b578 150 return readBitSetLevel(recvBit, NormalLevel);
IanBenzMaxim 7:9cd16581b578 151 }
IanBenzMaxim 7:9cd16581b578 152
IanBenzMaxim 7:9cd16581b578 153 error_code writeBitPower(bool sendBit) {
IanBenzMaxim 7:9cd16581b578 154 return writeBitSetLevel(sendBit, StrongLevel);
IanBenzMaxim 7:9cd16581b578 155 }
IanBenzMaxim 7:9cd16581b578 156
IanBenzMaxim 7:9cd16581b578 157 error_code readBitPower(bool & recvBit) {
IanBenzMaxim 7:9cd16581b578 158 return readBitSetLevel(recvBit, StrongLevel);
IanBenzMaxim 7:9cd16581b578 159 }
IanBenzMaxim 7:9cd16581b578 160
IanBenzMaxim 7:9cd16581b578 161 error_code writeByte(uint_least8_t sendByte) {
IanBenzMaxim 7:9cd16581b578 162 return writeByteSetLevel(sendByte, NormalLevel);
IanBenzMaxim 7:9cd16581b578 163 }
IanBenzMaxim 7:9cd16581b578 164
IanBenzMaxim 7:9cd16581b578 165 error_code readByte(uint_least8_t & recvByte) {
IanBenzMaxim 7:9cd16581b578 166 return readByteSetLevel(recvByte, NormalLevel);
IanBenzMaxim 7:9cd16581b578 167 }
IanBenzMaxim 7:9cd16581b578 168
IanBenzMaxim 7:9cd16581b578 169 error_code writeBytePower(uint_least8_t sendByte) {
IanBenzMaxim 7:9cd16581b578 170 return writeByteSetLevel(sendByte, StrongLevel);
IanBenzMaxim 7:9cd16581b578 171 }
IanBenzMaxim 7:9cd16581b578 172
IanBenzMaxim 7:9cd16581b578 173 error_code readBytePower(uint_least8_t & recvByte) {
IanBenzMaxim 7:9cd16581b578 174 return readByteSetLevel(recvByte, StrongLevel);
IanBenzMaxim 7:9cd16581b578 175 }
IanBenzMaxim 7:9cd16581b578 176
IanBenzMaxim 7:9cd16581b578 177 MaximInterfaceCore_EXPORT static const error_category & errorCategory();
IanBenzMaxim 7:9cd16581b578 178 };
IanBenzMaxim 7:9cd16581b578 179
IanBenzMaxim 7:9cd16581b578 180 inline error_code make_error_code(OneWireMaster::ErrorValue e) {
IanBenzMaxim 7:9cd16581b578 181 return error_code(e, OneWireMaster::errorCategory());
IanBenzMaxim 7:9cd16581b578 182 }
IanBenzMaxim 7:9cd16581b578 183
IanBenzMaxim 7:9cd16581b578 184 inline error_condition make_error_condition(OneWireMaster::ErrorValue e) {
IanBenzMaxim 7:9cd16581b578 185 return error_condition(e, OneWireMaster::errorCategory());
IanBenzMaxim 7:9cd16581b578 186 }
IanBenzMaxim 7:9cd16581b578 187
IanBenzMaxim 7:9cd16581b578 188 } // namespace MaximInterfaceCore
IanBenzMaxim 7:9cd16581b578 189
IanBenzMaxim 7:9cd16581b578 190 #endif