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 MaximInterfaceDevices_DS28E17
IanBenzMaxim 7:9cd16581b578 34 #define MaximInterfaceDevices_DS28E17
IanBenzMaxim 7:9cd16581b578 35
IanBenzMaxim 7:9cd16581b578 36 #include <stdint.h>
IanBenzMaxim 7:9cd16581b578 37 #include <MaximInterfaceCore/SelectRom.hpp>
IanBenzMaxim 7:9cd16581b578 38 #include <MaximInterfaceCore/span.hpp>
IanBenzMaxim 7:9cd16581b578 39 #include "Config.hpp"
IanBenzMaxim 7:9cd16581b578 40
IanBenzMaxim 7:9cd16581b578 41 namespace MaximInterfaceDevices {
IanBenzMaxim 7:9cd16581b578 42
IanBenzMaxim 7:9cd16581b578 43 /// @brief DS28E17 1-Wire®-to-I2C Master Bridge
IanBenzMaxim 7:9cd16581b578 44 /// @details The DS28E17 is a 1-Wire slave to I2C master bridge
IanBenzMaxim 7:9cd16581b578 45 /// device that interfaces directly to I2C slaves at standard
IanBenzMaxim 7:9cd16581b578 46 /// (100kHz max) or fast (400kHz max). Data transfers serially by
IanBenzMaxim 7:9cd16581b578 47 /// means of the 1-Wire® protocol, which requires only a single data
IanBenzMaxim 7:9cd16581b578 48 /// lead and a ground return. Every DS28E17 is guaranteed to have a
IanBenzMaxim 7:9cd16581b578 49 /// unique 64-bit ROM registration number that serves as a node
IanBenzMaxim 7:9cd16581b578 50 /// address in the 1-Wire network. Multiple DS28E17 devices can
IanBenzMaxim 7:9cd16581b578 51 /// coexist with other devices in the 1-Wire network and be accessed
IanBenzMaxim 7:9cd16581b578 52 /// individually without affecting other devices. The DS28E17 allows
IanBenzMaxim 7:9cd16581b578 53 /// using complex I2C devices such as display controllers, ADCs, DACs,
IanBenzMaxim 7:9cd16581b578 54 /// I2C sensors, etc. in a 1-Wire environment. Each self-timed DS28E17
IanBenzMaxim 7:9cd16581b578 55 /// provides 1-Wire access for a single I2C interface.
IanBenzMaxim 7:9cd16581b578 56 class DS28E17 {
IanBenzMaxim 7:9cd16581b578 57 public:
IanBenzMaxim 7:9cd16581b578 58 enum ErrorValue {
IanBenzMaxim 7:9cd16581b578 59 TimeoutError = 1,
IanBenzMaxim 7:9cd16581b578 60 OutOfRangeError,
IanBenzMaxim 7:9cd16581b578 61 InvalidCrc16Error,
IanBenzMaxim 7:9cd16581b578 62 AddressNackError,
IanBenzMaxim 7:9cd16581b578 63 InvalidStartError,
IanBenzMaxim 7:9cd16581b578 64 WriteNackError
IanBenzMaxim 7:9cd16581b578 65 };
IanBenzMaxim 7:9cd16581b578 66
IanBenzMaxim 7:9cd16581b578 67 enum I2CSpeed { Speed100kHz, Speed400kHz, Speed900kHz };
IanBenzMaxim 7:9cd16581b578 68
IanBenzMaxim 7:9cd16581b578 69 DS28E17(Core::OneWireMaster & master, const Core::SelectRom & selectRom)
IanBenzMaxim 7:9cd16581b578 70 : selectRom(selectRom), master(&master) {}
IanBenzMaxim 7:9cd16581b578 71
IanBenzMaxim 7:9cd16581b578 72 void setMaster(Core::OneWireMaster & master) { this->master = &master; }
IanBenzMaxim 7:9cd16581b578 73
IanBenzMaxim 7:9cd16581b578 74 void setSelectRom(const Core::SelectRom & selectRom) {
IanBenzMaxim 7:9cd16581b578 75 this->selectRom = selectRom;
IanBenzMaxim 7:9cd16581b578 76 }
IanBenzMaxim 7:9cd16581b578 77
IanBenzMaxim 7:9cd16581b578 78 /// @brief Write Data With Stop command.
IanBenzMaxim 7:9cd16581b578 79 /// @details Output on I2C: S, Address + Write, Write Data [1-255], P
IanBenzMaxim 7:9cd16581b578 80 /// @param[in] I2C_addr
IanBenzMaxim 7:9cd16581b578 81 /// I2C slave address. The least significant bit of the I2C
IanBenzMaxim 7:9cd16581b578 82 /// address is automatically cleared by the command.
IanBenzMaxim 7:9cd16581b578 83 /// @param[in] data I2C data to write with length 1-255.
IanBenzMaxim 7:9cd16581b578 84 /// @param[out] wr_status
IanBenzMaxim 7:9cd16581b578 85 /// Indicates which write byte NACK’d. A value of 00h indicates all bytes
IanBenzMaxim 7:9cd16581b578 86 /// were acknowledged by the slave. A non-zero value indicates the byte number
IanBenzMaxim 7:9cd16581b578 87 /// that NACK’d. May be set to NULL.
IanBenzMaxim 7:9cd16581b578 88 MaximInterfaceDevices_EXPORT Core::error_code
IanBenzMaxim 7:9cd16581b578 89 writeDataWithStop(uint_least8_t I2C_addr,
IanBenzMaxim 7:9cd16581b578 90 Core::span<const uint_least8_t> data,
IanBenzMaxim 7:9cd16581b578 91 uint_least8_t * wr_status = NULL);
IanBenzMaxim 7:9cd16581b578 92
IanBenzMaxim 7:9cd16581b578 93 /// @brief Write Data No Stop command.
IanBenzMaxim 7:9cd16581b578 94 /// @details Output on I2C: S, Address + Write, Write Data [1-255]
IanBenzMaxim 7:9cd16581b578 95 /// @param[in] I2C_addr
IanBenzMaxim 7:9cd16581b578 96 /// I2C slave address. The least significant bit of the I2C address
IanBenzMaxim 7:9cd16581b578 97 /// is automatically cleared by the command.
IanBenzMaxim 7:9cd16581b578 98 /// @param[in] data I2C data to write with length 1-255.
IanBenzMaxim 7:9cd16581b578 99 /// @param[out] wr_status
IanBenzMaxim 7:9cd16581b578 100 /// Indicates which write byte NACK’d. A value of 00h indicates all bytes
IanBenzMaxim 7:9cd16581b578 101 /// were acknowledged by the slave. A non-zero value indicates the byte number
IanBenzMaxim 7:9cd16581b578 102 /// that NACK’d. May be set to NULL.
IanBenzMaxim 7:9cd16581b578 103 MaximInterfaceDevices_EXPORT Core::error_code
IanBenzMaxim 7:9cd16581b578 104 writeDataNoStop(uint_least8_t I2C_addr, Core::span<const uint_least8_t> data,
IanBenzMaxim 7:9cd16581b578 105 uint_least8_t * wr_status = NULL);
IanBenzMaxim 7:9cd16581b578 106
IanBenzMaxim 7:9cd16581b578 107 /// @brief Write Data Only command.
IanBenzMaxim 7:9cd16581b578 108 /// @details Output on I2C: Write Data [1-255]
IanBenzMaxim 7:9cd16581b578 109 /// @param[in] data I2C data to write with length 1-255.
IanBenzMaxim 7:9cd16581b578 110 /// @param[out] wr_status
IanBenzMaxim 7:9cd16581b578 111 /// Indicates which write byte NACK’d. A value of 00h indicates all bytes
IanBenzMaxim 7:9cd16581b578 112 /// were acknowledged by the slave. A non-zero value indicates the byte number
IanBenzMaxim 7:9cd16581b578 113 /// that NACK’d. May be set to NULL.
IanBenzMaxim 7:9cd16581b578 114 MaximInterfaceDevices_EXPORT Core::error_code
IanBenzMaxim 7:9cd16581b578 115 writeDataOnly(Core::span<const uint_least8_t> data,
IanBenzMaxim 7:9cd16581b578 116 uint_least8_t * wr_status = NULL);
IanBenzMaxim 7:9cd16581b578 117
IanBenzMaxim 7:9cd16581b578 118 /// @brief Write Data Only With Stop command.
IanBenzMaxim 7:9cd16581b578 119 /// @details Output on I2C: Write Data [1-255], P
IanBenzMaxim 7:9cd16581b578 120 /// @param[in] data I2C data to write with length 1-255.
IanBenzMaxim 7:9cd16581b578 121 /// @param[out] wr_status
IanBenzMaxim 7:9cd16581b578 122 /// Indicates which write byte NACK’d. A value of 00h indicates all bytes
IanBenzMaxim 7:9cd16581b578 123 /// were acknowledged by the slave. A non-zero value indicates the byte number
IanBenzMaxim 7:9cd16581b578 124 /// that NACK’d. May be set to NULL.
IanBenzMaxim 7:9cd16581b578 125 MaximInterfaceDevices_EXPORT Core::error_code
IanBenzMaxim 7:9cd16581b578 126 writeDataOnlyWithStop(Core::span<const uint_least8_t> data,
IanBenzMaxim 7:9cd16581b578 127 uint_least8_t * wr_status = NULL);
IanBenzMaxim 7:9cd16581b578 128
IanBenzMaxim 7:9cd16581b578 129 /// @brief Write, Read Data With Stop command.
IanBenzMaxim 7:9cd16581b578 130 /// @details Output on I2C:
IanBenzMaxim 7:9cd16581b578 131 /// S, Slave Address + Write, Write Data [1-255],
IanBenzMaxim 7:9cd16581b578 132 /// Sr, Address + Read, Read Data [1-255], P (NACK last read byte)
IanBenzMaxim 7:9cd16581b578 133 /// @param[in] I2C_addr
IanBenzMaxim 7:9cd16581b578 134 /// I2C slave address. The least significant bit of the I2C address
IanBenzMaxim 7:9cd16581b578 135 /// is automatically cleared and set by the command.
IanBenzMaxim 7:9cd16581b578 136 /// @param[in] write_data I2C data to write with length 1-255.
IanBenzMaxim 7:9cd16581b578 137 /// @param[out] read_data I2C data that was read with length 1-255.
IanBenzMaxim 7:9cd16581b578 138 /// @param[out] wr_status
IanBenzMaxim 7:9cd16581b578 139 /// Indicates which write byte NACK’d. A value of 00h indicates all bytes
IanBenzMaxim 7:9cd16581b578 140 /// were acknowledged by the slave. A non-zero value indicates the byte number
IanBenzMaxim 7:9cd16581b578 141 /// that NACK’d. May be set to NULL.
IanBenzMaxim 7:9cd16581b578 142 MaximInterfaceDevices_EXPORT Core::error_code writeReadDataWithStop(
IanBenzMaxim 7:9cd16581b578 143 uint_least8_t I2C_addr, Core::span<const uint_least8_t> write_data,
IanBenzMaxim 7:9cd16581b578 144 Core::span<uint_least8_t> read_data, uint_least8_t * wr_status = NULL);
IanBenzMaxim 7:9cd16581b578 145
IanBenzMaxim 7:9cd16581b578 146 /// @brief Read Data With Stop command.
IanBenzMaxim 7:9cd16581b578 147 /// @details Output on I2C:
IanBenzMaxim 7:9cd16581b578 148 /// S, Slave Address + Read, Read Data [1-255], P (NACK last read byte)
IanBenzMaxim 7:9cd16581b578 149 /// @param[in] I2C_addr
IanBenzMaxim 7:9cd16581b578 150 /// I2C slave address. The least significant bit of the I2C address
IanBenzMaxim 7:9cd16581b578 151 /// is automatically set by the command.
IanBenzMaxim 7:9cd16581b578 152 /// @param[out] data I2C data that was read with length 1-255.
IanBenzMaxim 7:9cd16581b578 153 MaximInterfaceDevices_EXPORT Core::error_code
IanBenzMaxim 7:9cd16581b578 154 readDataWithStop(uint_least8_t I2C_addr, Core::span<uint_least8_t> data);
IanBenzMaxim 7:9cd16581b578 155
IanBenzMaxim 7:9cd16581b578 156 /// Write to Configuration Register of DS28E17.
IanBenzMaxim 7:9cd16581b578 157 MaximInterfaceDevices_EXPORT Core::error_code writeConfigReg(I2CSpeed speed);
IanBenzMaxim 7:9cd16581b578 158
IanBenzMaxim 7:9cd16581b578 159 /// @brief Read the Configuration Register of DS28E17.
IanBenzMaxim 7:9cd16581b578 160 /// @param[out] speed Speed read from configuration register.
IanBenzMaxim 7:9cd16581b578 161 MaximInterfaceDevices_EXPORT Core::error_code readConfigReg(I2CSpeed & speed);
IanBenzMaxim 7:9cd16581b578 162
IanBenzMaxim 7:9cd16581b578 163 /// @brief Put the device into a low current mode.
IanBenzMaxim 7:9cd16581b578 164 /// @details All 1-Wire communication is ignored until woken up. Immediately
IanBenzMaxim 7:9cd16581b578 165 /// after the command, the device monitors the WAKEUP input pin and exits
IanBenzMaxim 7:9cd16581b578 166 /// sleep mode on a rising edge.
IanBenzMaxim 7:9cd16581b578 167 MaximInterfaceDevices_EXPORT Core::error_code enableSleepMode();
IanBenzMaxim 7:9cd16581b578 168
IanBenzMaxim 7:9cd16581b578 169 /// @brief Read the Device Revision of DS28E17.
IanBenzMaxim 7:9cd16581b578 170 /// @details The upper nibble is the major revision,
IanBenzMaxim 7:9cd16581b578 171 /// and the lower nibble is the minor revision.
IanBenzMaxim 7:9cd16581b578 172 /// @param[out] rev Device Revision.
IanBenzMaxim 7:9cd16581b578 173 MaximInterfaceDevices_EXPORT Core::error_code
IanBenzMaxim 7:9cd16581b578 174 readDeviceRevision(uint_least8_t & rev);
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 private:
IanBenzMaxim 7:9cd16581b578 180 enum Command {
IanBenzMaxim 7:9cd16581b578 181 WriteDataWithStopCmd = 0x4B,
IanBenzMaxim 7:9cd16581b578 182 WriteDataNoStopCmd = 0x5A,
IanBenzMaxim 7:9cd16581b578 183 WriteDataOnlyCmd = 0x69,
IanBenzMaxim 7:9cd16581b578 184 WriteDataOnlyWithStopCmd = 0x78,
IanBenzMaxim 7:9cd16581b578 185 ReadDataWithStopCmd = 0x87,
IanBenzMaxim 7:9cd16581b578 186 WriteReadDataWithStopCmd = 0x2D,
IanBenzMaxim 7:9cd16581b578 187 WriteConfigurationCmd = 0xD2,
IanBenzMaxim 7:9cd16581b578 188 ReadConfigurationCmd = 0xE1,
IanBenzMaxim 7:9cd16581b578 189 EnableSleepModeCmd = 0x1E,
IanBenzMaxim 7:9cd16581b578 190 ReadDeviceRevisionCmd = 0xC3
IanBenzMaxim 7:9cd16581b578 191 };
IanBenzMaxim 7:9cd16581b578 192
IanBenzMaxim 7:9cd16581b578 193 Core::error_code sendPacket(Command command, const uint_least8_t * I2C_addr,
IanBenzMaxim 7:9cd16581b578 194 Core::span<const uint_least8_t> write_data,
IanBenzMaxim 7:9cd16581b578 195 Core::span<uint_least8_t> read_data,
IanBenzMaxim 7:9cd16581b578 196 uint_least8_t * wr_status);
IanBenzMaxim 7:9cd16581b578 197
IanBenzMaxim 7:9cd16581b578 198 Core::SelectRom selectRom;
IanBenzMaxim 7:9cd16581b578 199 Core::OneWireMaster * master;
IanBenzMaxim 7:9cd16581b578 200 };
IanBenzMaxim 7:9cd16581b578 201
IanBenzMaxim 7:9cd16581b578 202 inline Core::error_code make_error_code(DS28E17::ErrorValue e) {
IanBenzMaxim 7:9cd16581b578 203 return Core::error_code(e, DS28E17::errorCategory());
IanBenzMaxim 7:9cd16581b578 204 }
IanBenzMaxim 7:9cd16581b578 205
IanBenzMaxim 7:9cd16581b578 206 } // namespace MaximInterfaceDevices
IanBenzMaxim 7:9cd16581b578 207
IanBenzMaxim 7:9cd16581b578 208 #endif