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_DS2431_hpp
IanBenzMaxim 8:5ea891c7d1a1 34 #define MaximInterfaceDevices_DS2431_hpp
IanBenzMaxim 7:9cd16581b578 35
IanBenzMaxim 8:5ea891c7d1a1 36 #include <utility>
IanBenzMaxim 7:9cd16581b578 37 #include <MaximInterfaceCore/array_span.hpp>
IanBenzMaxim 7:9cd16581b578 38 #include <MaximInterfaceCore/SelectRom.hpp>
IanBenzMaxim 7:9cd16581b578 39 #include <MaximInterfaceCore/Sleep.hpp>
IanBenzMaxim 7:9cd16581b578 40 #include "Config.hpp"
IanBenzMaxim 7:9cd16581b578 41
IanBenzMaxim 7:9cd16581b578 42 namespace MaximInterfaceDevices {
IanBenzMaxim 7:9cd16581b578 43
IanBenzMaxim 7:9cd16581b578 44 /// @brief DS2431 1024-bit 1-Wire EEPROM
IanBenzMaxim 7:9cd16581b578 45 /// @details The DS2431 is a 1024-bit, 1-Wire® EEPROM chip organized
IanBenzMaxim 7:9cd16581b578 46 /// as four memory pages of 256 bits each. Data is written to an 8-byte
IanBenzMaxim 7:9cd16581b578 47 /// scratchpad, verified, and then copied to the EEPROM memory. As a
IanBenzMaxim 7:9cd16581b578 48 /// special feature, the four memory pages can individually be write
IanBenzMaxim 7:9cd16581b578 49 /// protected or put in EPROM-emulation mode, where bits can only be
IanBenzMaxim 7:9cd16581b578 50 /// changed from a 1 to a 0 state. The DS2431 communicates over the
IanBenzMaxim 7:9cd16581b578 51 /// single-conductor 1-Wire bus. The communication follows the standard
IanBenzMaxim 7:9cd16581b578 52 /// 1-Wire protocol. Each device has its own unalterable and unique
IanBenzMaxim 7:9cd16581b578 53 /// 64-bit ROM registration number that is factory lasered into the chip.
IanBenzMaxim 7:9cd16581b578 54 /// The registration number is used to address the device in a multidrop,
IanBenzMaxim 7:9cd16581b578 55 /// 1-Wire net environment.
IanBenzMaxim 7:9cd16581b578 56 class DS2431 {
IanBenzMaxim 7:9cd16581b578 57 public:
IanBenzMaxim 7:9cd16581b578 58 enum ErrorValue { CrcError = 1, OperationFailure };
IanBenzMaxim 7:9cd16581b578 59
IanBenzMaxim 7:9cd16581b578 60 typedef Core::array_span<uint_least8_t, 8> Scratchpad;
IanBenzMaxim 7:9cd16581b578 61
IanBenzMaxim 7:9cd16581b578 62 DS2431(Core::Sleep & sleep, Core::OneWireMaster & master,
IanBenzMaxim 7:9cd16581b578 63 const Core::SelectRom & selectRom)
IanBenzMaxim 7:9cd16581b578 64 : selectRom(selectRom), master(&master), sleep(&sleep) {}
IanBenzMaxim 7:9cd16581b578 65
IanBenzMaxim 7:9cd16581b578 66 void setSleep(Core::Sleep & sleep) { this->sleep = &sleep; }
IanBenzMaxim 7:9cd16581b578 67
IanBenzMaxim 7:9cd16581b578 68 void setMaster(Core::OneWireMaster & master) { this->master = &master; }
IanBenzMaxim 7:9cd16581b578 69
IanBenzMaxim 7:9cd16581b578 70 void setSelectRom(const Core::SelectRom & selectRom) {
IanBenzMaxim 7:9cd16581b578 71 this->selectRom = selectRom;
IanBenzMaxim 7:9cd16581b578 72 }
IanBenzMaxim 7:9cd16581b578 73
IanBenzMaxim 7:9cd16581b578 74 /// @brief Reads block of data from EEPROM memory.
IanBenzMaxim 7:9cd16581b578 75 /// @param[in] beginAddress EEPROM memory address to start reading from.
IanBenzMaxim 7:9cd16581b578 76 /// @param[out] data EEPROM data read from the device.
IanBenzMaxim 8:5ea891c7d1a1 77 MaximInterfaceDevices_EXPORT Core::Result<void>
IanBenzMaxim 8:5ea891c7d1a1 78 readMemory(uint_least8_t beginAddress, Core::span<uint_least8_t> data) const;
IanBenzMaxim 7:9cd16581b578 79
IanBenzMaxim 7:9cd16581b578 80 /// @brief Writes 8 bytes to the scratchpad.
IanBenzMaxim 7:9cd16581b578 81 /// @param[in] targetAddress
IanBenzMaxim 7:9cd16581b578 82 /// EEPROM memory address that this data will be copied to.
IanBenzMaxim 7:9cd16581b578 83 /// Must be on row boundary.
IanBenzMaxim 7:9cd16581b578 84 /// @param[in] data Data to write to scratchpad.
IanBenzMaxim 8:5ea891c7d1a1 85 MaximInterfaceDevices_EXPORT Core::Result<void>
IanBenzMaxim 8:5ea891c7d1a1 86 writeScratchpad(uint_least8_t targetAddress, Scratchpad::const_span data);
IanBenzMaxim 7:9cd16581b578 87
IanBenzMaxim 7:9cd16581b578 88 /// @brief Reads contents of scratchpad.
IanBenzMaxim 8:5ea891c7d1a1 89 /// @returns E/S byte and scratchpad data.
IanBenzMaxim 8:5ea891c7d1a1 90 MaximInterfaceDevices_EXPORT
IanBenzMaxim 8:5ea891c7d1a1 91 Core::Result<std::pair<uint_least8_t, Scratchpad::array> >
IanBenzMaxim 8:5ea891c7d1a1 92 readScratchpad() const;
IanBenzMaxim 7:9cd16581b578 93
IanBenzMaxim 7:9cd16581b578 94 /// @brief Copies contents of scratchpad to EEPROM.
IanBenzMaxim 7:9cd16581b578 95 /// @param[in] targetAddress EEPROM memory address that scratchpad
IanBenzMaxim 7:9cd16581b578 96 /// will be copied to. Must be on row boundary.
IanBenzMaxim 7:9cd16581b578 97 /// @param[in] esByte E/S byte from preceding Read Scratchpad command.
IanBenzMaxim 8:5ea891c7d1a1 98 MaximInterfaceDevices_EXPORT Core::Result<void>
IanBenzMaxim 8:5ea891c7d1a1 99 copyScratchpad(uint_least8_t targetAddress, uint_least8_t esByte);
IanBenzMaxim 7:9cd16581b578 100
IanBenzMaxim 7:9cd16581b578 101 MaximInterfaceDevices_EXPORT static const Core::error_category &
IanBenzMaxim 7:9cd16581b578 102 errorCategory();
IanBenzMaxim 7:9cd16581b578 103
IanBenzMaxim 7:9cd16581b578 104 private:
IanBenzMaxim 7:9cd16581b578 105 Core::SelectRom selectRom;
IanBenzMaxim 7:9cd16581b578 106 Core::OneWireMaster * master;
IanBenzMaxim 7:9cd16581b578 107 const Core::Sleep * sleep;
IanBenzMaxim 7:9cd16581b578 108 };
IanBenzMaxim 7:9cd16581b578 109
IanBenzMaxim 7:9cd16581b578 110 /// @brief
IanBenzMaxim 7:9cd16581b578 111 /// Writes data to EEPROM using Write Scratchpad, Read Scratchpad,
IanBenzMaxim 7:9cd16581b578 112 /// and Copy Scratchpad commands.
IanBenzMaxim 7:9cd16581b578 113 /// @param device Device to write.
IanBenzMaxim 7:9cd16581b578 114 /// @param[in] targetAddress EEPROM memory address to start writing at.
IanBenzMaxim 7:9cd16581b578 115 /// @param[in] data Data to write to EEPROM.
IanBenzMaxim 8:5ea891c7d1a1 116 MaximInterfaceDevices_EXPORT Core::Result<void>
IanBenzMaxim 8:5ea891c7d1a1 117 writeMemory(DS2431 & device, uint_least8_t targetAddress,
IanBenzMaxim 7:9cd16581b578 118 DS2431::Scratchpad::const_span data);
IanBenzMaxim 7:9cd16581b578 119
IanBenzMaxim 8:5ea891c7d1a1 120 } // namespace MaximInterfaceDevices
IanBenzMaxim 8:5ea891c7d1a1 121 namespace MaximInterfaceCore {
IanBenzMaxim 8:5ea891c7d1a1 122
IanBenzMaxim 8:5ea891c7d1a1 123 template <>
IanBenzMaxim 8:5ea891c7d1a1 124 struct is_error_code_enum<MaximInterfaceDevices::DS2431::ErrorValue>
IanBenzMaxim 8:5ea891c7d1a1 125 : true_type {};
IanBenzMaxim 8:5ea891c7d1a1 126
IanBenzMaxim 8:5ea891c7d1a1 127 } // namespace MaximInterfaceCore
IanBenzMaxim 8:5ea891c7d1a1 128 namespace MaximInterfaceDevices {
IanBenzMaxim 8:5ea891c7d1a1 129
IanBenzMaxim 7:9cd16581b578 130 inline Core::error_code make_error_code(DS2431::ErrorValue e) {
IanBenzMaxim 7:9cd16581b578 131 return Core::error_code(e, DS2431::errorCategory());
IanBenzMaxim 7:9cd16581b578 132 }
IanBenzMaxim 7:9cd16581b578 133
IanBenzMaxim 7:9cd16581b578 134 } // namespace MaximInterfaceDevices
IanBenzMaxim 7:9cd16581b578 135
IanBenzMaxim 7:9cd16581b578 136 #endif