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:
11:3f3bf6bf5e6c
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 "Error.hpp"
IanBenzMaxim 7:9cd16581b578 34 #include "OneWireMaster.hpp"
IanBenzMaxim 7:9cd16581b578 35
IanBenzMaxim 8:5ea891c7d1a1 36 #define TRY MaximInterfaceCore_TRY
IanBenzMaxim 8:5ea891c7d1a1 37 #define TRY_VALUE MaximInterfaceCore_TRY_VALUE
IanBenzMaxim 8:5ea891c7d1a1 38
IanBenzMaxim 7:9cd16581b578 39 namespace MaximInterfaceCore {
IanBenzMaxim 7:9cd16581b578 40
IanBenzMaxim 7:9cd16581b578 41 static const int maxBitNum = 7;
IanBenzMaxim 7:9cd16581b578 42
IanBenzMaxim 8:5ea891c7d1a1 43 Result<void> OneWireMaster::writeByteSetLevel(uint_least8_t sendByte,
IanBenzMaxim 8:5ea891c7d1a1 44 Level afterLevel) {
IanBenzMaxim 8:5ea891c7d1a1 45 for (int bitNum = 0; bitNum <= maxBitNum; ++bitNum) {
IanBenzMaxim 8:5ea891c7d1a1 46 TRY(writeBitSetLevel(((sendByte >> bitNum) & 1) == 1,
IanBenzMaxim 8:5ea891c7d1a1 47 (bitNum == maxBitNum) ? afterLevel : NormalLevel));
IanBenzMaxim 7:9cd16581b578 48 }
IanBenzMaxim 8:5ea891c7d1a1 49 return none;
IanBenzMaxim 7:9cd16581b578 50 }
IanBenzMaxim 7:9cd16581b578 51
IanBenzMaxim 8:5ea891c7d1a1 52 Result<uint_least8_t> OneWireMaster::readByteSetLevel(Level afterLevel) {
IanBenzMaxim 8:5ea891c7d1a1 53 uint_least8_t recvByte = 0;
IanBenzMaxim 7:9cd16581b578 54 for (int bitNum = 0; bitNum <= maxBitNum; ++bitNum) {
IanBenzMaxim 7:9cd16581b578 55 bool recvBit;
IanBenzMaxim 8:5ea891c7d1a1 56 TRY_VALUE(recvBit, readBitSetLevel((bitNum == maxBitNum) ? afterLevel
IanBenzMaxim 8:5ea891c7d1a1 57 : NormalLevel));
IanBenzMaxim 7:9cd16581b578 58 if (recvBit) {
IanBenzMaxim 7:9cd16581b578 59 recvByte |= (1 << bitNum);
IanBenzMaxim 7:9cd16581b578 60 }
IanBenzMaxim 7:9cd16581b578 61 }
IanBenzMaxim 8:5ea891c7d1a1 62 return recvByte;
IanBenzMaxim 7:9cd16581b578 63 }
IanBenzMaxim 7:9cd16581b578 64
IanBenzMaxim 8:5ea891c7d1a1 65 Result<void> OneWireMaster::writeBlock(span<const uint_least8_t> sendBuf) {
IanBenzMaxim 8:5ea891c7d1a1 66 for (span<const uint_least8_t>::index_type i = 0; i < sendBuf.size(); ++i) {
IanBenzMaxim 8:5ea891c7d1a1 67 TRY(writeByte(sendBuf[i]));
IanBenzMaxim 7:9cd16581b578 68 }
IanBenzMaxim 8:5ea891c7d1a1 69 return none;
IanBenzMaxim 7:9cd16581b578 70 }
IanBenzMaxim 7:9cd16581b578 71
IanBenzMaxim 8:5ea891c7d1a1 72 Result<void> OneWireMaster::readBlock(span<uint_least8_t> recvBuf) {
IanBenzMaxim 8:5ea891c7d1a1 73 for (span<uint_least8_t>::index_type i = 0; i < recvBuf.size(); ++i) {
IanBenzMaxim 8:5ea891c7d1a1 74 TRY_VALUE(recvBuf[i], readByte());
IanBenzMaxim 7:9cd16581b578 75 }
IanBenzMaxim 8:5ea891c7d1a1 76 return none;
IanBenzMaxim 7:9cd16581b578 77 }
IanBenzMaxim 7:9cd16581b578 78
IanBenzMaxim 8:5ea891c7d1a1 79 Result<OneWireMaster::TripletData> OneWireMaster::triplet(bool sendBit) {
IanBenzMaxim 8:5ea891c7d1a1 80 TripletData data;
IanBenzMaxim 8:5ea891c7d1a1 81 TRY_VALUE(data.readBit, readBit());
IanBenzMaxim 8:5ea891c7d1a1 82 TRY_VALUE(data.readBitComplement, readBit());
IanBenzMaxim 8:5ea891c7d1a1 83 if (data.readBit) {
IanBenzMaxim 8:5ea891c7d1a1 84 data.writeBit = 1;
IanBenzMaxim 8:5ea891c7d1a1 85 } else if (data.readBitComplement) {
IanBenzMaxim 8:5ea891c7d1a1 86 data.writeBit = 0;
IanBenzMaxim 8:5ea891c7d1a1 87 } else {
IanBenzMaxim 8:5ea891c7d1a1 88 data.writeBit = sendBit;
IanBenzMaxim 7:9cd16581b578 89 }
IanBenzMaxim 8:5ea891c7d1a1 90 TRY(writeBit(data.writeBit));
IanBenzMaxim 8:5ea891c7d1a1 91 return data;
IanBenzMaxim 7:9cd16581b578 92 }
IanBenzMaxim 7:9cd16581b578 93
IanBenzMaxim 7:9cd16581b578 94 const error_category & OneWireMaster::errorCategory() {
IanBenzMaxim 7:9cd16581b578 95 static class : public error_category {
IanBenzMaxim 7:9cd16581b578 96 public:
IanBenzMaxim 11:3f3bf6bf5e6c 97 virtual const char * name() const {
IanBenzMaxim 11:3f3bf6bf5e6c 98 return "MaximInterfaceCore.OneWireMaster";
IanBenzMaxim 11:3f3bf6bf5e6c 99 }
IanBenzMaxim 7:9cd16581b578 100
IanBenzMaxim 7:9cd16581b578 101 virtual std::string message(int condition) const {
IanBenzMaxim 7:9cd16581b578 102 switch (condition) {
IanBenzMaxim 7:9cd16581b578 103 case NoSlaveError:
IanBenzMaxim 7:9cd16581b578 104 return "No Slave Error";
IanBenzMaxim 7:9cd16581b578 105
IanBenzMaxim 7:9cd16581b578 106 case ShortDetectedError:
IanBenzMaxim 7:9cd16581b578 107 return "Short Detected Error";
IanBenzMaxim 7:9cd16581b578 108
IanBenzMaxim 7:9cd16581b578 109 case InvalidSpeedError:
IanBenzMaxim 7:9cd16581b578 110 return "Invalid Speed Error";
IanBenzMaxim 7:9cd16581b578 111
IanBenzMaxim 7:9cd16581b578 112 case InvalidLevelError:
IanBenzMaxim 7:9cd16581b578 113 return "Invalid Level Error";
IanBenzMaxim 7:9cd16581b578 114 }
IanBenzMaxim 8:5ea891c7d1a1 115 return defaultErrorMessage(condition);
IanBenzMaxim 7:9cd16581b578 116 }
IanBenzMaxim 7:9cd16581b578 117 } instance;
IanBenzMaxim 7:9cd16581b578 118 return instance;
IanBenzMaxim 7:9cd16581b578 119 }
IanBenzMaxim 7:9cd16581b578 120
IanBenzMaxim 7:9cd16581b578 121 } // namespace MaximInterfaceCore