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