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_DS2482_DS2484_hpp
IanBenzMaxim 8:5ea891c7d1a1 34 #define MaximInterfaceDevices_DS2482_DS2484_hpp
IanBenzMaxim 7:9cd16581b578 35
IanBenzMaxim 7:9cd16581b578 36 #include <MaximInterfaceCore/I2CMaster.hpp>
IanBenzMaxim 7:9cd16581b578 37 #include <MaximInterfaceCore/OneWireMaster.hpp>
IanBenzMaxim 7:9cd16581b578 38 #include "Config.hpp"
IanBenzMaxim 7:9cd16581b578 39
IanBenzMaxim 7:9cd16581b578 40 namespace MaximInterfaceDevices {
IanBenzMaxim 7:9cd16581b578 41
IanBenzMaxim 7:9cd16581b578 42 /// Interface to the DS2484, DS2482-100, DS2482-101, DS2482-800 1-Wire masters.
IanBenzMaxim 7:9cd16581b578 43 class DS2482_DS2484 : public Core::OneWireMaster {
IanBenzMaxim 7:9cd16581b578 44 public:
IanBenzMaxim 7:9cd16581b578 45 enum ErrorValue { HardwareError = 1, ArgumentOutOfRangeError };
IanBenzMaxim 7:9cd16581b578 46
IanBenzMaxim 7:9cd16581b578 47 /// Represents a device configuration.
IanBenzMaxim 7:9cd16581b578 48 class Config {
IanBenzMaxim 7:9cd16581b578 49 public:
IanBenzMaxim 7:9cd16581b578 50 /// Default construct with power-on config.
IanBenzMaxim 7:9cd16581b578 51 explicit Config(uint_least8_t readByte = optionAPU)
IanBenzMaxim 7:9cd16581b578 52 : readByte_(readByte & 0xF) {}
IanBenzMaxim 7:9cd16581b578 53
IanBenzMaxim 7:9cd16581b578 54 /// @name 1WS
IanBenzMaxim 7:9cd16581b578 55 /// @brief 1-Wire Speed
IanBenzMaxim 7:9cd16581b578 56 /// @{
IanBenzMaxim 7:9cd16581b578 57
IanBenzMaxim 7:9cd16581b578 58 /// Get 1WS bit.
IanBenzMaxim 7:9cd16581b578 59 bool get1WS() const { return (readByte_ & option1WS) == option1WS; }
IanBenzMaxim 7:9cd16581b578 60
IanBenzMaxim 7:9cd16581b578 61 /// Set 1WS bit.
IanBenzMaxim 7:9cd16581b578 62 Config & set1WS(bool new1WS) {
IanBenzMaxim 7:9cd16581b578 63 if (new1WS) {
IanBenzMaxim 7:9cd16581b578 64 readByte_ |= option1WS;
IanBenzMaxim 7:9cd16581b578 65 } else {
IanBenzMaxim 7:9cd16581b578 66 readByte_ &= ~option1WS;
IanBenzMaxim 7:9cd16581b578 67 }
IanBenzMaxim 7:9cd16581b578 68 return *this;
IanBenzMaxim 7:9cd16581b578 69 }
IanBenzMaxim 7:9cd16581b578 70
IanBenzMaxim 7:9cd16581b578 71 /// @}
IanBenzMaxim 7:9cd16581b578 72
IanBenzMaxim 7:9cd16581b578 73 /// @name SPU
IanBenzMaxim 7:9cd16581b578 74 /// @brief Strong Pullup
IanBenzMaxim 7:9cd16581b578 75 /// @{
IanBenzMaxim 7:9cd16581b578 76
IanBenzMaxim 7:9cd16581b578 77 /// Get SPU bit.
IanBenzMaxim 7:9cd16581b578 78 bool getSPU() const { return (readByte_ & optionSPU) == optionSPU; }
IanBenzMaxim 7:9cd16581b578 79
IanBenzMaxim 7:9cd16581b578 80 /// Set SPU bit.
IanBenzMaxim 7:9cd16581b578 81 Config & setSPU(bool newSPU) {
IanBenzMaxim 7:9cd16581b578 82 if (newSPU) {
IanBenzMaxim 7:9cd16581b578 83 readByte_ |= optionSPU;
IanBenzMaxim 7:9cd16581b578 84 } else {
IanBenzMaxim 7:9cd16581b578 85 readByte_ &= ~optionSPU;
IanBenzMaxim 7:9cd16581b578 86 }
IanBenzMaxim 7:9cd16581b578 87 return *this;
IanBenzMaxim 7:9cd16581b578 88 }
IanBenzMaxim 7:9cd16581b578 89
IanBenzMaxim 7:9cd16581b578 90 /// @}
IanBenzMaxim 7:9cd16581b578 91
IanBenzMaxim 7:9cd16581b578 92 /// @name PDN
IanBenzMaxim 7:9cd16581b578 93 /// @brief 1-Wire Power Down
IanBenzMaxim 7:9cd16581b578 94 /// @{
IanBenzMaxim 7:9cd16581b578 95
IanBenzMaxim 7:9cd16581b578 96 /// Get PDN bit.
IanBenzMaxim 7:9cd16581b578 97 bool getPDN() const { return (readByte_ & optionPDN) == optionPDN; }
IanBenzMaxim 7:9cd16581b578 98
IanBenzMaxim 7:9cd16581b578 99 /// Set PDN bit.
IanBenzMaxim 7:9cd16581b578 100 Config & setPDN(bool newPDN) {
IanBenzMaxim 7:9cd16581b578 101 if (newPDN) {
IanBenzMaxim 7:9cd16581b578 102 readByte_ |= optionPDN;
IanBenzMaxim 7:9cd16581b578 103 } else {
IanBenzMaxim 7:9cd16581b578 104 readByte_ &= ~optionPDN;
IanBenzMaxim 7:9cd16581b578 105 }
IanBenzMaxim 7:9cd16581b578 106 return *this;
IanBenzMaxim 7:9cd16581b578 107 }
IanBenzMaxim 7:9cd16581b578 108
IanBenzMaxim 7:9cd16581b578 109 /// @}
IanBenzMaxim 7:9cd16581b578 110
IanBenzMaxim 7:9cd16581b578 111 /// @name APU
IanBenzMaxim 7:9cd16581b578 112 /// @brief Active Pullup
IanBenzMaxim 7:9cd16581b578 113 /// @{
IanBenzMaxim 7:9cd16581b578 114
IanBenzMaxim 7:9cd16581b578 115 /// Get APU bit.
IanBenzMaxim 7:9cd16581b578 116 bool getAPU() const { return (readByte_ & optionAPU) == optionAPU; }
IanBenzMaxim 7:9cd16581b578 117
IanBenzMaxim 7:9cd16581b578 118 /// Set APU bit.
IanBenzMaxim 7:9cd16581b578 119 Config & setAPU(bool newAPU) {
IanBenzMaxim 7:9cd16581b578 120 if (newAPU) {
IanBenzMaxim 7:9cd16581b578 121 readByte_ |= optionAPU;
IanBenzMaxim 7:9cd16581b578 122 } else {
IanBenzMaxim 7:9cd16581b578 123 readByte_ &= ~optionAPU;
IanBenzMaxim 7:9cd16581b578 124 }
IanBenzMaxim 7:9cd16581b578 125 return *this;
IanBenzMaxim 7:9cd16581b578 126 }
IanBenzMaxim 7:9cd16581b578 127
IanBenzMaxim 7:9cd16581b578 128 /// @}
IanBenzMaxim 7:9cd16581b578 129
IanBenzMaxim 7:9cd16581b578 130 /// Byte representation that is read from the device.
IanBenzMaxim 7:9cd16581b578 131 uint_least8_t readByte() const { return readByte_; }
IanBenzMaxim 7:9cd16581b578 132
IanBenzMaxim 7:9cd16581b578 133 private:
IanBenzMaxim 7:9cd16581b578 134 static const unsigned int option1WS = 0x8;
IanBenzMaxim 7:9cd16581b578 135 static const unsigned int optionSPU = 0x4;
IanBenzMaxim 7:9cd16581b578 136 static const unsigned int optionPDN = 0x2;
IanBenzMaxim 7:9cd16581b578 137 static const unsigned int optionAPU = 0x1;
IanBenzMaxim 7:9cd16581b578 138
IanBenzMaxim 7:9cd16581b578 139 uint_least8_t readByte_;
IanBenzMaxim 7:9cd16581b578 140 };
IanBenzMaxim 7:9cd16581b578 141
IanBenzMaxim 7:9cd16581b578 142 void setMaster(Core::I2CMaster & master) { this->master = &master; }
IanBenzMaxim 7:9cd16581b578 143
IanBenzMaxim 7:9cd16581b578 144 uint_least8_t address() const { return address_; }
IanBenzMaxim 7:9cd16581b578 145
IanBenzMaxim 7:9cd16581b578 146 void setAddress(uint_least8_t address) { address_ = address; }
IanBenzMaxim 7:9cd16581b578 147
IanBenzMaxim 7:9cd16581b578 148 /// Initialize hardware for use.
IanBenzMaxim 8:5ea891c7d1a1 149 MaximInterfaceDevices_EXPORT Core::Result<void>
IanBenzMaxim 7:9cd16581b578 150 initialize(Config config = Config());
IanBenzMaxim 7:9cd16581b578 151
IanBenzMaxim 7:9cd16581b578 152 /// @brief Write a new configuration to the device.
IanBenzMaxim 7:9cd16581b578 153 /// @param[in] config New configuration to write.
IanBenzMaxim 8:5ea891c7d1a1 154 MaximInterfaceDevices_EXPORT Core::Result<void> writeConfig(Config config);
IanBenzMaxim 7:9cd16581b578 155
IanBenzMaxim 8:5ea891c7d1a1 156 MaximInterfaceDevices_EXPORT virtual Core::Result<TripletData>
IanBenzMaxim 8:5ea891c7d1a1 157 triplet(bool sendBit);
IanBenzMaxim 7:9cd16581b578 158
IanBenzMaxim 8:5ea891c7d1a1 159 MaximInterfaceDevices_EXPORT virtual Core::Result<void> reset();
IanBenzMaxim 7:9cd16581b578 160
IanBenzMaxim 8:5ea891c7d1a1 161 MaximInterfaceDevices_EXPORT virtual Core::Result<bool>
IanBenzMaxim 8:5ea891c7d1a1 162 touchBitSetLevel(bool sendBit, Level afterLevel);
IanBenzMaxim 7:9cd16581b578 163
IanBenzMaxim 8:5ea891c7d1a1 164 MaximInterfaceDevices_EXPORT virtual Core::Result<uint_least8_t>
IanBenzMaxim 8:5ea891c7d1a1 165 readByteSetLevel(Level afterLevel);
IanBenzMaxim 7:9cd16581b578 166
IanBenzMaxim 8:5ea891c7d1a1 167 MaximInterfaceDevices_EXPORT virtual Core::Result<void>
IanBenzMaxim 7:9cd16581b578 168 writeByteSetLevel(uint_least8_t sendByte, Level afterLevel);
IanBenzMaxim 7:9cd16581b578 169
IanBenzMaxim 8:5ea891c7d1a1 170 MaximInterfaceDevices_EXPORT virtual Core::Result<void>
IanBenzMaxim 7:9cd16581b578 171 setSpeed(Speed newSpeed);
IanBenzMaxim 7:9cd16581b578 172
IanBenzMaxim 8:5ea891c7d1a1 173 MaximInterfaceDevices_EXPORT virtual Core::Result<void>
IanBenzMaxim 7:9cd16581b578 174 setLevel(Level newLevel);
IanBenzMaxim 7:9cd16581b578 175
IanBenzMaxim 7:9cd16581b578 176 MaximInterfaceDevices_EXPORT static const Core::error_category &
IanBenzMaxim 7:9cd16581b578 177 errorCategory();
IanBenzMaxim 7:9cd16581b578 178
IanBenzMaxim 7:9cd16581b578 179 protected:
IanBenzMaxim 7:9cd16581b578 180 DS2482_DS2484(Core::I2CMaster & master, uint_least8_t address)
IanBenzMaxim 7:9cd16581b578 181 : master(&master), address_(address) {}
IanBenzMaxim 7:9cd16581b578 182
IanBenzMaxim 7:9cd16581b578 183 /// @note Allow marking const since not public.
IanBenzMaxim 8:5ea891c7d1a1 184 Core::Result<void> sendCommand(uint_least8_t cmd) const;
IanBenzMaxim 7:9cd16581b578 185
IanBenzMaxim 7:9cd16581b578 186 /// @note Allow marking const since not public.
IanBenzMaxim 8:5ea891c7d1a1 187 Core::Result<void> sendCommand(uint_least8_t cmd, uint_least8_t param) const;
IanBenzMaxim 7:9cd16581b578 188
IanBenzMaxim 7:9cd16581b578 189 /// @brief Reads a register from the device.
IanBenzMaxim 7:9cd16581b578 190 /// @param reg Register to read from.
IanBenzMaxim 8:5ea891c7d1a1 191 /// @returns Read data from register.
IanBenzMaxim 8:5ea891c7d1a1 192 Core::Result<uint_least8_t> readRegister(uint_least8_t reg) const;
IanBenzMaxim 7:9cd16581b578 193
IanBenzMaxim 7:9cd16581b578 194 /// @brief Reads the current register from the device.
IanBenzMaxim 8:5ea891c7d1a1 195 /// @returns Read data from register.
IanBenzMaxim 8:5ea891c7d1a1 196 Core::Result<uint_least8_t> readRegister() const;
IanBenzMaxim 7:9cd16581b578 197
IanBenzMaxim 7:9cd16581b578 198 private:
IanBenzMaxim 7:9cd16581b578 199 /// @brief Performs a soft reset on the device.
IanBenzMaxim 7:9cd16581b578 200 /// @note This is not a 1-Wire Reset.
IanBenzMaxim 8:5ea891c7d1a1 201 Core::Result<void> resetDevice();
IanBenzMaxim 7:9cd16581b578 202
IanBenzMaxim 7:9cd16581b578 203 /// @brief
IanBenzMaxim 7:9cd16581b578 204 /// Polls the device status waiting for the 1-Wire Busy bit (1WB) to be cleared.
IanBenzMaxim 8:5ea891c7d1a1 205 /// @returns Status byte or TimeoutError if poll limit reached.
IanBenzMaxim 8:5ea891c7d1a1 206 Core::Result<uint_least8_t> pollBusy();
IanBenzMaxim 7:9cd16581b578 207
IanBenzMaxim 7:9cd16581b578 208 /// @brief Ensure that the desired 1-Wire level is set in the configuration.
IanBenzMaxim 7:9cd16581b578 209 /// @param level Desired 1-Wire level.
IanBenzMaxim 8:5ea891c7d1a1 210 Core::Result<void> configureLevel(Level level);
IanBenzMaxim 7:9cd16581b578 211
IanBenzMaxim 7:9cd16581b578 212 Core::I2CMaster * master;
IanBenzMaxim 7:9cd16581b578 213 uint_least8_t address_;
IanBenzMaxim 7:9cd16581b578 214 Config curConfig;
IanBenzMaxim 7:9cd16581b578 215 };
IanBenzMaxim 7:9cd16581b578 216
IanBenzMaxim 8:5ea891c7d1a1 217 } // namespace MaximInterfaceDevices
IanBenzMaxim 8:5ea891c7d1a1 218 namespace MaximInterfaceCore {
IanBenzMaxim 8:5ea891c7d1a1 219
IanBenzMaxim 8:5ea891c7d1a1 220 template <>
IanBenzMaxim 8:5ea891c7d1a1 221 struct is_error_code_enum<MaximInterfaceDevices::DS2482_DS2484::ErrorValue>
IanBenzMaxim 8:5ea891c7d1a1 222 : true_type {};
IanBenzMaxim 8:5ea891c7d1a1 223
IanBenzMaxim 8:5ea891c7d1a1 224 } // namespace MaximInterfaceCore
IanBenzMaxim 8:5ea891c7d1a1 225 namespace MaximInterfaceDevices {
IanBenzMaxim 8:5ea891c7d1a1 226
IanBenzMaxim 7:9cd16581b578 227 inline Core::error_code make_error_code(DS2482_DS2484::ErrorValue e) {
IanBenzMaxim 7:9cd16581b578 228 return Core::error_code(e, DS2482_DS2484::errorCategory());
IanBenzMaxim 7:9cd16581b578 229 }
IanBenzMaxim 7:9cd16581b578 230
IanBenzMaxim 7:9cd16581b578 231 class DS2482_100 : public DS2482_DS2484 {
IanBenzMaxim 7:9cd16581b578 232 public:
IanBenzMaxim 7:9cd16581b578 233 DS2482_100(Core::I2CMaster & i2c_bus, uint_least8_t adrs)
IanBenzMaxim 7:9cd16581b578 234 : DS2482_DS2484(i2c_bus, adrs) {}
IanBenzMaxim 7:9cd16581b578 235 };
IanBenzMaxim 7:9cd16581b578 236
IanBenzMaxim 7:9cd16581b578 237 /// DS2482-800 I2C to 1-Wire Master
IanBenzMaxim 7:9cd16581b578 238 class DS2482_800 : public DS2482_DS2484 {
IanBenzMaxim 7:9cd16581b578 239 public:
IanBenzMaxim 7:9cd16581b578 240 DS2482_800(Core::I2CMaster & i2c_bus, uint_least8_t adrs)
IanBenzMaxim 7:9cd16581b578 241 : DS2482_DS2484(i2c_bus, adrs) {}
IanBenzMaxim 7:9cd16581b578 242
IanBenzMaxim 7:9cd16581b578 243 /// @brief Select the active 1-Wire channel.
IanBenzMaxim 7:9cd16581b578 244 /// @param channel Channel number to select from 0 to 7.
IanBenzMaxim 8:5ea891c7d1a1 245 MaximInterfaceDevices_EXPORT Core::Result<void> selectChannel(int channel);
IanBenzMaxim 7:9cd16581b578 246 };
IanBenzMaxim 7:9cd16581b578 247
IanBenzMaxim 7:9cd16581b578 248 /// DS2484 I2C to 1-Wire Master
IanBenzMaxim 7:9cd16581b578 249 class DS2484 : public DS2482_DS2484 {
IanBenzMaxim 7:9cd16581b578 250 public:
IanBenzMaxim 7:9cd16581b578 251 /// @brief 1-Wire port adjustment parameters.
IanBenzMaxim 7:9cd16581b578 252 /// @note See datasheet page 13.
IanBenzMaxim 7:9cd16581b578 253 enum PortParameter {
IanBenzMaxim 7:9cd16581b578 254 tRSTL = 0,
IanBenzMaxim 7:9cd16581b578 255 tRSTL_OD,
IanBenzMaxim 7:9cd16581b578 256 tMSP,
IanBenzMaxim 7:9cd16581b578 257 tMSP_OD,
IanBenzMaxim 7:9cd16581b578 258 tW0L,
IanBenzMaxim 7:9cd16581b578 259 tW0L_OD,
IanBenzMaxim 7:9cd16581b578 260 tREC0, // OD N/A
IanBenzMaxim 7:9cd16581b578 261 RWPU = 8 // OD N/A
IanBenzMaxim 7:9cd16581b578 262 };
IanBenzMaxim 7:9cd16581b578 263
IanBenzMaxim 7:9cd16581b578 264 explicit DS2484(Core::I2CMaster & i2c_bus, uint_least8_t adrs = 0x30)
IanBenzMaxim 7:9cd16581b578 265 : DS2482_DS2484(i2c_bus, adrs) {}
IanBenzMaxim 7:9cd16581b578 266
IanBenzMaxim 7:9cd16581b578 267 /// @brief Adjust 1-Wire port parameters.
IanBenzMaxim 7:9cd16581b578 268 /// @param param Parameter to adjust.
IanBenzMaxim 7:9cd16581b578 269 /// @param val New parameter value to set. Consult datasheet for value mappings.
IanBenzMaxim 8:5ea891c7d1a1 270 MaximInterfaceDevices_EXPORT Core::Result<void>
IanBenzMaxim 8:5ea891c7d1a1 271 adjustPort(PortParameter param, int val);
IanBenzMaxim 7:9cd16581b578 272 };
IanBenzMaxim 7:9cd16581b578 273
IanBenzMaxim 7:9cd16581b578 274 } // namespace MaximInterfaceDevices
IanBenzMaxim 7:9cd16581b578 275
IanBenzMaxim 7:9cd16581b578 276 #endif