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 7:9cd16581b578 33 #include <utility>
IanBenzMaxim 8:5ea891c7d1a1 34 #include "HexString.hpp"
IanBenzMaxim 7:9cd16581b578 35 #include "LoggingI2CMaster.hpp"
IanBenzMaxim 7:9cd16581b578 36
IanBenzMaxim 7:9cd16581b578 37 using std::string;
IanBenzMaxim 7:9cd16581b578 38
IanBenzMaxim 7:9cd16581b578 39 namespace MaximInterfaceCore {
IanBenzMaxim 7:9cd16581b578 40
IanBenzMaxim 7:9cd16581b578 41 static const char startString[] = "S ";
IanBenzMaxim 7:9cd16581b578 42 static const char stopString[] = "P";
IanBenzMaxim 7:9cd16581b578 43
IanBenzMaxim 7:9cd16581b578 44 static string formatDataString(span<const uint_least8_t> data, bool read) {
IanBenzMaxim 7:9cd16581b578 45 string dataBuilder;
IanBenzMaxim 7:9cd16581b578 46 for (span<const uint_least8_t>::index_type i = 0; i < data.size(); ++i) {
IanBenzMaxim 7:9cd16581b578 47 if (read) {
IanBenzMaxim 7:9cd16581b578 48 dataBuilder.append(1, '[');
IanBenzMaxim 7:9cd16581b578 49 }
IanBenzMaxim 8:5ea891c7d1a1 50 dataBuilder.append(toHexString(data.subspan(i, 1)));
IanBenzMaxim 7:9cd16581b578 51 if (read) {
IanBenzMaxim 7:9cd16581b578 52 dataBuilder.append(1, ']');
IanBenzMaxim 7:9cd16581b578 53 }
IanBenzMaxim 7:9cd16581b578 54 dataBuilder.append(1, ' ');
IanBenzMaxim 7:9cd16581b578 55 }
IanBenzMaxim 7:9cd16581b578 56 return dataBuilder;
IanBenzMaxim 7:9cd16581b578 57 }
IanBenzMaxim 7:9cd16581b578 58
IanBenzMaxim 7:9cd16581b578 59 void LoggingI2CMaster::tryWriteMessage() {
IanBenzMaxim 7:9cd16581b578 60 if (writeMessage) {
IanBenzMaxim 7:9cd16581b578 61 writeMessage(messageBuilder);
IanBenzMaxim 7:9cd16581b578 62 }
IanBenzMaxim 7:9cd16581b578 63 messageBuilder.clear();
IanBenzMaxim 7:9cd16581b578 64 }
IanBenzMaxim 7:9cd16581b578 65
IanBenzMaxim 8:5ea891c7d1a1 66 Result<void> LoggingI2CMaster::start(uint_least8_t address) {
IanBenzMaxim 7:9cd16581b578 67 messageBuilder.append(startString);
IanBenzMaxim 7:9cd16581b578 68 messageBuilder.append(formatDataString(make_span(&address, 1), false));
IanBenzMaxim 7:9cd16581b578 69 return I2CMasterDecorator::start(address);
IanBenzMaxim 7:9cd16581b578 70 }
IanBenzMaxim 7:9cd16581b578 71
IanBenzMaxim 8:5ea891c7d1a1 72 Result<void> LoggingI2CMaster::stop() {
IanBenzMaxim 7:9cd16581b578 73 messageBuilder.append(stopString);
IanBenzMaxim 7:9cd16581b578 74 tryWriteMessage();
IanBenzMaxim 7:9cd16581b578 75 return I2CMasterDecorator::stop();
IanBenzMaxim 7:9cd16581b578 76 }
IanBenzMaxim 7:9cd16581b578 77
IanBenzMaxim 8:5ea891c7d1a1 78 Result<void> LoggingI2CMaster::writeByte(uint_least8_t data) {
IanBenzMaxim 7:9cd16581b578 79 messageBuilder.append(formatDataString(make_span(&data, 1), false));
IanBenzMaxim 7:9cd16581b578 80 return I2CMasterDecorator::writeByte(data);
IanBenzMaxim 7:9cd16581b578 81 }
IanBenzMaxim 7:9cd16581b578 82
IanBenzMaxim 8:5ea891c7d1a1 83 Result<void> LoggingI2CMaster::writeBlock(span<const uint_least8_t> data) {
IanBenzMaxim 7:9cd16581b578 84 messageBuilder.append(formatDataString(data, false));
IanBenzMaxim 7:9cd16581b578 85 return I2CMasterDecorator::writeBlock(data);
IanBenzMaxim 7:9cd16581b578 86 }
IanBenzMaxim 7:9cd16581b578 87
IanBenzMaxim 8:5ea891c7d1a1 88 Result<void> LoggingI2CMaster::writePacket(uint_least8_t address,
IanBenzMaxim 7:9cd16581b578 89 span<const uint_least8_t> data,
IanBenzMaxim 8:5ea891c7d1a1 90 DoStop doStop) {
IanBenzMaxim 7:9cd16581b578 91 messageBuilder.append(startString);
IanBenzMaxim 7:9cd16581b578 92 messageBuilder.append(formatDataString(make_span(&address, 1), false));
IanBenzMaxim 8:5ea891c7d1a1 93 const Result<void> result =
IanBenzMaxim 8:5ea891c7d1a1 94 I2CMasterDecorator::writePacket(address, data, doStop);
IanBenzMaxim 8:5ea891c7d1a1 95 if (result) {
IanBenzMaxim 7:9cd16581b578 96 messageBuilder.append(formatDataString(data, false));
IanBenzMaxim 7:9cd16581b578 97 }
IanBenzMaxim 8:5ea891c7d1a1 98 if (doStop == Stop || (doStop == StopOnError && !result)) {
IanBenzMaxim 7:9cd16581b578 99 messageBuilder.append(stopString);
IanBenzMaxim 7:9cd16581b578 100 tryWriteMessage();
IanBenzMaxim 7:9cd16581b578 101 }
IanBenzMaxim 7:9cd16581b578 102 return result;
IanBenzMaxim 7:9cd16581b578 103 }
IanBenzMaxim 7:9cd16581b578 104
IanBenzMaxim 8:5ea891c7d1a1 105 Result<uint_least8_t> LoggingI2CMaster::readByte(DoAck doAck) {
IanBenzMaxim 8:5ea891c7d1a1 106 const Result<uint_least8_t> data = I2CMasterDecorator::readByte(doAck);
IanBenzMaxim 8:5ea891c7d1a1 107 if (data) {
IanBenzMaxim 8:5ea891c7d1a1 108 messageBuilder.append(formatDataString(make_span(&data.value(), 1), true));
IanBenzMaxim 7:9cd16581b578 109 }
IanBenzMaxim 8:5ea891c7d1a1 110 return data;
IanBenzMaxim 7:9cd16581b578 111 }
IanBenzMaxim 7:9cd16581b578 112
IanBenzMaxim 8:5ea891c7d1a1 113 Result<void> LoggingI2CMaster::readBlock(span<uint_least8_t> data,
IanBenzMaxim 8:5ea891c7d1a1 114 DoAck doAck) {
IanBenzMaxim 8:5ea891c7d1a1 115 const Result<void> result = I2CMasterDecorator::readBlock(data, doAck);
IanBenzMaxim 8:5ea891c7d1a1 116 if (result) {
IanBenzMaxim 7:9cd16581b578 117 messageBuilder.append(formatDataString(data, true));
IanBenzMaxim 7:9cd16581b578 118 }
IanBenzMaxim 7:9cd16581b578 119 return result;
IanBenzMaxim 7:9cd16581b578 120 }
IanBenzMaxim 7:9cd16581b578 121
IanBenzMaxim 8:5ea891c7d1a1 122 Result<void> LoggingI2CMaster::readPacket(uint_least8_t address,
IanBenzMaxim 7:9cd16581b578 123 span<uint_least8_t> data,
IanBenzMaxim 8:5ea891c7d1a1 124 DoStop doStop) {
IanBenzMaxim 7:9cd16581b578 125 messageBuilder.append(startString);
IanBenzMaxim 7:9cd16581b578 126 messageBuilder.append(formatDataString(make_span(&address, 1), false));
IanBenzMaxim 8:5ea891c7d1a1 127 const Result<void> result =
IanBenzMaxim 8:5ea891c7d1a1 128 I2CMasterDecorator::readPacket(address, data, doStop);
IanBenzMaxim 8:5ea891c7d1a1 129 if (result) {
IanBenzMaxim 7:9cd16581b578 130 messageBuilder.append(formatDataString(data, true));
IanBenzMaxim 7:9cd16581b578 131 }
IanBenzMaxim 8:5ea891c7d1a1 132 if (doStop == Stop || (doStop == StopOnError && !result)) {
IanBenzMaxim 7:9cd16581b578 133 messageBuilder.append(stopString);
IanBenzMaxim 7:9cd16581b578 134 tryWriteMessage();
IanBenzMaxim 7:9cd16581b578 135 }
IanBenzMaxim 7:9cd16581b578 136 return result;
IanBenzMaxim 7:9cd16581b578 137 }
IanBenzMaxim 7:9cd16581b578 138
IanBenzMaxim 7:9cd16581b578 139 } // namespace MaximInterfaceCore