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_I2CMaster
IanBenzMaxim 7:9cd16581b578 34 #define MaximInterfaceCore_I2CMaster
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 /// I2C master interface.
IanBenzMaxim 7:9cd16581b578 44 class I2CMaster {
IanBenzMaxim 7:9cd16581b578 45 public:
IanBenzMaxim 7:9cd16581b578 46 enum ErrorValue {
IanBenzMaxim 7:9cd16581b578 47 NackError = 1 ///< Transaction stopped due to a NACK from the slave device.
IanBenzMaxim 7:9cd16581b578 48 };
IanBenzMaxim 7:9cd16581b578 49
IanBenzMaxim 7:9cd16581b578 50 enum AckStatus { Nack, Ack };
IanBenzMaxim 7:9cd16581b578 51
IanBenzMaxim 7:9cd16581b578 52 virtual ~I2CMaster() {}
IanBenzMaxim 7:9cd16581b578 53
IanBenzMaxim 7:9cd16581b578 54 /// @brief Send start condition and address on the bus.
IanBenzMaxim 7:9cd16581b578 55 /// @param address Address with R/W bit.
IanBenzMaxim 7:9cd16581b578 56 virtual error_code start(uint_least8_t address) = 0;
IanBenzMaxim 7:9cd16581b578 57
IanBenzMaxim 7:9cd16581b578 58 /// Send stop condition on the bus.
IanBenzMaxim 7:9cd16581b578 59 virtual error_code stop() = 0;
IanBenzMaxim 7:9cd16581b578 60
IanBenzMaxim 7:9cd16581b578 61 /// Write data byte to the bus.
IanBenzMaxim 7:9cd16581b578 62 virtual error_code writeByte(uint_least8_t data) = 0;
IanBenzMaxim 7:9cd16581b578 63
IanBenzMaxim 7:9cd16581b578 64 /// Write data block to the bus.
IanBenzMaxim 7:9cd16581b578 65 MaximInterfaceCore_EXPORT virtual error_code
IanBenzMaxim 7:9cd16581b578 66 writeBlock(span<const uint_least8_t> data);
IanBenzMaxim 7:9cd16581b578 67
IanBenzMaxim 7:9cd16581b578 68 /// @brief
IanBenzMaxim 7:9cd16581b578 69 /// Perform a complete write transaction on the bus with optional stop
IanBenzMaxim 7:9cd16581b578 70 /// condition.
IanBenzMaxim 7:9cd16581b578 71 /// @param address Address in 8-bit format.
IanBenzMaxim 7:9cd16581b578 72 /// @param data Data to write to the bus.
IanBenzMaxim 7:9cd16581b578 73 /// @param sendStop
IanBenzMaxim 7:9cd16581b578 74 /// True to send a stop condition or false to set up a repeated start.
IanBenzMaxim 7:9cd16581b578 75 error_code writePacket(uint_least8_t address, span<const uint_least8_t> data,
IanBenzMaxim 7:9cd16581b578 76 bool sendStop = true) {
IanBenzMaxim 7:9cd16581b578 77 return doWritePacket(address, data, sendStop);
IanBenzMaxim 7:9cd16581b578 78 }
IanBenzMaxim 7:9cd16581b578 79
IanBenzMaxim 7:9cd16581b578 80 /// @brief Read data byte from the bus.
IanBenzMaxim 7:9cd16581b578 81 /// @param status Determines whether an ACK or NACK is sent after reading.
IanBenzMaxim 7:9cd16581b578 82 /// @param[out] data Data read from the bus if successful.
IanBenzMaxim 7:9cd16581b578 83 virtual error_code readByte(AckStatus status, uint_least8_t & data) = 0;
IanBenzMaxim 7:9cd16581b578 84
IanBenzMaxim 7:9cd16581b578 85 /// @brief Read data block from the bus.
IanBenzMaxim 7:9cd16581b578 86 /// @param status Determines whether an ACK or NACK is sent after reading.
IanBenzMaxim 7:9cd16581b578 87 /// @param[out] data Data read from the bus if successful.
IanBenzMaxim 7:9cd16581b578 88 MaximInterfaceCore_EXPORT virtual error_code
IanBenzMaxim 7:9cd16581b578 89 readBlock(AckStatus status, span<uint_least8_t> data);
IanBenzMaxim 7:9cd16581b578 90
IanBenzMaxim 7:9cd16581b578 91 /// @brief
IanBenzMaxim 7:9cd16581b578 92 /// Perform a complete read transaction on the bus with optional stop
IanBenzMaxim 7:9cd16581b578 93 /// condition.
IanBenzMaxim 7:9cd16581b578 94 /// @param address Address in 8-bit format.
IanBenzMaxim 7:9cd16581b578 95 /// @param[out] data Data read from the bus if successful.
IanBenzMaxim 7:9cd16581b578 96 /// @param sendStop
IanBenzMaxim 7:9cd16581b578 97 /// True to send a stop condition or false to set up a repeated start.
IanBenzMaxim 7:9cd16581b578 98 error_code readPacket(uint_least8_t address, span<uint_least8_t> data,
IanBenzMaxim 7:9cd16581b578 99 bool sendStop = true) {
IanBenzMaxim 7:9cd16581b578 100 return doReadPacket(address, data, sendStop);
IanBenzMaxim 7:9cd16581b578 101 }
IanBenzMaxim 7:9cd16581b578 102
IanBenzMaxim 7:9cd16581b578 103 MaximInterfaceCore_EXPORT static const error_category & errorCategory();
IanBenzMaxim 7:9cd16581b578 104
IanBenzMaxim 7:9cd16581b578 105 protected:
IanBenzMaxim 7:9cd16581b578 106 MaximInterfaceCore_EXPORT virtual error_code
IanBenzMaxim 7:9cd16581b578 107 doWritePacket(uint_least8_t address, span<const uint_least8_t> data,
IanBenzMaxim 7:9cd16581b578 108 bool sendStop);
IanBenzMaxim 7:9cd16581b578 109
IanBenzMaxim 7:9cd16581b578 110 MaximInterfaceCore_EXPORT virtual error_code
IanBenzMaxim 7:9cd16581b578 111 doReadPacket(uint_least8_t address, span<uint_least8_t> data, bool sendStop);
IanBenzMaxim 7:9cd16581b578 112 };
IanBenzMaxim 7:9cd16581b578 113
IanBenzMaxim 7:9cd16581b578 114 inline error_code make_error_code(I2CMaster::ErrorValue e) {
IanBenzMaxim 7:9cd16581b578 115 return error_code(e, I2CMaster::errorCategory());
IanBenzMaxim 7:9cd16581b578 116 }
IanBenzMaxim 7:9cd16581b578 117
IanBenzMaxim 7:9cd16581b578 118 inline error_condition make_error_condition(I2CMaster::ErrorValue e) {
IanBenzMaxim 7:9cd16581b578 119 return error_condition(e, I2CMaster::errorCategory());
IanBenzMaxim 7:9cd16581b578 120 }
IanBenzMaxim 7:9cd16581b578 121
IanBenzMaxim 7:9cd16581b578 122 } // namespace MaximInterfaceCore
IanBenzMaxim 7:9cd16581b578 123
IanBenzMaxim 7:9cd16581b578 124 #endif