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 <algorithm>
IanBenzMaxim 7:9cd16581b578 34 #include <MaximInterfaceCore/Error.hpp>
IanBenzMaxim 7:9cd16581b578 35 #include <MaximInterfaceCore/OneWireMaster.hpp>
IanBenzMaxim 7:9cd16581b578 36 #include "DS2431.hpp"
IanBenzMaxim 7:9cd16581b578 37
IanBenzMaxim 7:9cd16581b578 38 namespace MaximInterfaceDevices {
IanBenzMaxim 7:9cd16581b578 39
IanBenzMaxim 7:9cd16581b578 40 using namespace Core;
IanBenzMaxim 7:9cd16581b578 41
IanBenzMaxim 8:5ea891c7d1a1 42 Result<void> writeMemory(DS2431 & device, uint_least8_t targetAddress,
IanBenzMaxim 8:5ea891c7d1a1 43 DS2431::Scratchpad::const_span data) {
IanBenzMaxim 8:5ea891c7d1a1 44 Result<void> result = device.writeScratchpad(targetAddress, data);
IanBenzMaxim 8:5ea891c7d1a1 45 if (!result) {
IanBenzMaxim 7:9cd16581b578 46 return result;
IanBenzMaxim 7:9cd16581b578 47 }
IanBenzMaxim 7:9cd16581b578 48 uint_least8_t esByte;
IanBenzMaxim 8:5ea891c7d1a1 49 if (const Result<std::pair<uint_least8_t, DS2431::Scratchpad::array> >
IanBenzMaxim 8:5ea891c7d1a1 50 scratchpad = device.readScratchpad()) {
IanBenzMaxim 8:5ea891c7d1a1 51 esByte = scratchpad.value().first;
IanBenzMaxim 8:5ea891c7d1a1 52 } else {
IanBenzMaxim 8:5ea891c7d1a1 53 return scratchpad.error();
IanBenzMaxim 7:9cd16581b578 54 }
IanBenzMaxim 7:9cd16581b578 55 result = device.copyScratchpad(targetAddress, esByte);
IanBenzMaxim 7:9cd16581b578 56 return result;
IanBenzMaxim 7:9cd16581b578 57 }
IanBenzMaxim 7:9cd16581b578 58
IanBenzMaxim 8:5ea891c7d1a1 59 Result<void> DS2431::readMemory(uint_least8_t beginAddress,
IanBenzMaxim 8:5ea891c7d1a1 60 span<uint_least8_t> data) const {
IanBenzMaxim 8:5ea891c7d1a1 61 Result<void> result = selectRom(*master);
IanBenzMaxim 8:5ea891c7d1a1 62 if (!result) {
IanBenzMaxim 7:9cd16581b578 63 return result;
IanBenzMaxim 7:9cd16581b578 64 }
IanBenzMaxim 7:9cd16581b578 65 const uint_least8_t sendBlock[] = {0xF0, beginAddress, 0x00};
IanBenzMaxim 7:9cd16581b578 66 result = master->writeBlock(sendBlock);
IanBenzMaxim 8:5ea891c7d1a1 67 if (!result) {
IanBenzMaxim 7:9cd16581b578 68 return result;
IanBenzMaxim 7:9cd16581b578 69 }
IanBenzMaxim 7:9cd16581b578 70 result = master->readBlock(data);
IanBenzMaxim 7:9cd16581b578 71 return result;
IanBenzMaxim 7:9cd16581b578 72 }
IanBenzMaxim 7:9cd16581b578 73
IanBenzMaxim 8:5ea891c7d1a1 74 Result<void> DS2431::writeScratchpad(uint_least8_t targetAddress,
IanBenzMaxim 8:5ea891c7d1a1 75 Scratchpad::const_span data) {
IanBenzMaxim 8:5ea891c7d1a1 76 Result<void> result = selectRom(*master);
IanBenzMaxim 8:5ea891c7d1a1 77 if (!result) {
IanBenzMaxim 7:9cd16581b578 78 return result;
IanBenzMaxim 7:9cd16581b578 79 }
IanBenzMaxim 7:9cd16581b578 80 uint_least8_t block[3 + Scratchpad::size] = {0x0F, targetAddress, 0x00};
IanBenzMaxim 7:9cd16581b578 81 std::copy(data.begin(), data.end(), block + 3);
IanBenzMaxim 7:9cd16581b578 82 result = master->writeBlock(block);
IanBenzMaxim 8:5ea891c7d1a1 83 if (!result) {
IanBenzMaxim 7:9cd16581b578 84 return result;
IanBenzMaxim 7:9cd16581b578 85 }
IanBenzMaxim 10:947d3f44e0a0 86 const uint_fast16_t calculatedCrc = calculateCrc16(block) ^ 0xFFFF;
IanBenzMaxim 7:9cd16581b578 87 result = master->readBlock(make_span(block, 2));
IanBenzMaxim 8:5ea891c7d1a1 88 if (!result) {
IanBenzMaxim 7:9cd16581b578 89 return result;
IanBenzMaxim 7:9cd16581b578 90 }
IanBenzMaxim 7:9cd16581b578 91 if (calculatedCrc !=
IanBenzMaxim 7:9cd16581b578 92 ((static_cast<uint_fast16_t>(block[1]) << 8) | block[0])) {
IanBenzMaxim 8:5ea891c7d1a1 93 result = CrcError;
IanBenzMaxim 7:9cd16581b578 94 }
IanBenzMaxim 7:9cd16581b578 95 return result;
IanBenzMaxim 7:9cd16581b578 96 }
IanBenzMaxim 7:9cd16581b578 97
IanBenzMaxim 8:5ea891c7d1a1 98 Result<std::pair<uint_least8_t, DS2431::Scratchpad::array> >
IanBenzMaxim 8:5ea891c7d1a1 99 DS2431::readScratchpad() const {
IanBenzMaxim 7:9cd16581b578 100 typedef array<uint_least8_t, 6 + Scratchpad::size> Block;
IanBenzMaxim 7:9cd16581b578 101
IanBenzMaxim 8:5ea891c7d1a1 102 Result<void> result = selectRom(*master);
IanBenzMaxim 8:5ea891c7d1a1 103 if (!result) {
IanBenzMaxim 8:5ea891c7d1a1 104 return result.error();
IanBenzMaxim 7:9cd16581b578 105 }
IanBenzMaxim 7:9cd16581b578 106 Block block = {0xAA};
IanBenzMaxim 7:9cd16581b578 107 result = master->writeByte(block.front());
IanBenzMaxim 8:5ea891c7d1a1 108 if (!result) {
IanBenzMaxim 8:5ea891c7d1a1 109 return result.error();
IanBenzMaxim 7:9cd16581b578 110 }
IanBenzMaxim 7:9cd16581b578 111 result = master->readBlock(make_span(block).subspan(1));
IanBenzMaxim 8:5ea891c7d1a1 112 if (!result) {
IanBenzMaxim 8:5ea891c7d1a1 113 return result.error();
IanBenzMaxim 7:9cd16581b578 114 }
IanBenzMaxim 7:9cd16581b578 115 Block::const_iterator blockIt = block.end();
IanBenzMaxim 7:9cd16581b578 116 uint_fast16_t receivedCrc = static_cast<uint_fast16_t>(*(--blockIt)) << 8;
IanBenzMaxim 7:9cd16581b578 117 receivedCrc |= *(--blockIt);
IanBenzMaxim 7:9cd16581b578 118 const uint_fast16_t expectedCrc =
IanBenzMaxim 10:947d3f44e0a0 119 calculateCrc16(make_span(block.data(), block.size() - 2)) ^ 0xFFFF;
IanBenzMaxim 8:5ea891c7d1a1 120 if (expectedCrc != receivedCrc) {
IanBenzMaxim 8:5ea891c7d1a1 121 return CrcError;
IanBenzMaxim 7:9cd16581b578 122 }
IanBenzMaxim 8:5ea891c7d1a1 123 std::pair<uint_least8_t, Scratchpad::array> data;
IanBenzMaxim 8:5ea891c7d1a1 124 Block::const_iterator blockItEnd = blockIt;
IanBenzMaxim 8:5ea891c7d1a1 125 blockIt -= data.second.size();
IanBenzMaxim 8:5ea891c7d1a1 126 std::copy(blockIt, blockItEnd, data.second.begin());
IanBenzMaxim 8:5ea891c7d1a1 127 data.first = *(--blockIt);
IanBenzMaxim 8:5ea891c7d1a1 128 return data;
IanBenzMaxim 7:9cd16581b578 129 }
IanBenzMaxim 7:9cd16581b578 130
IanBenzMaxim 8:5ea891c7d1a1 131 Result<void> DS2431::copyScratchpad(uint_least8_t targetAddress,
IanBenzMaxim 8:5ea891c7d1a1 132 uint_least8_t esByte) {
IanBenzMaxim 8:5ea891c7d1a1 133 Result<void> result = selectRom(*master);
IanBenzMaxim 8:5ea891c7d1a1 134 if (!result) {
IanBenzMaxim 7:9cd16581b578 135 return result;
IanBenzMaxim 7:9cd16581b578 136 }
IanBenzMaxim 7:9cd16581b578 137 uint_least8_t block[] = {0x55, targetAddress, 0x00};
IanBenzMaxim 7:9cd16581b578 138 result = master->writeBlock(block);
IanBenzMaxim 8:5ea891c7d1a1 139 if (!result) {
IanBenzMaxim 7:9cd16581b578 140 return result;
IanBenzMaxim 7:9cd16581b578 141 }
IanBenzMaxim 7:9cd16581b578 142 result = master->writeByteSetLevel(esByte, OneWireMaster::StrongLevel);
IanBenzMaxim 8:5ea891c7d1a1 143 if (!result) {
IanBenzMaxim 7:9cd16581b578 144 return result;
IanBenzMaxim 7:9cd16581b578 145 }
IanBenzMaxim 7:9cd16581b578 146 sleep->invoke(10);
IanBenzMaxim 7:9cd16581b578 147 result = master->setLevel(OneWireMaster::NormalLevel);
IanBenzMaxim 8:5ea891c7d1a1 148 if (!result) {
IanBenzMaxim 7:9cd16581b578 149 return result;
IanBenzMaxim 7:9cd16581b578 150 }
IanBenzMaxim 8:5ea891c7d1a1 151 MaximInterfaceCore_TRY_VALUE(block[0], master->readByte());
IanBenzMaxim 7:9cd16581b578 152 if (block[0] != 0xAA) {
IanBenzMaxim 8:5ea891c7d1a1 153 result = OperationFailure;
IanBenzMaxim 7:9cd16581b578 154 }
IanBenzMaxim 7:9cd16581b578 155 return result;
IanBenzMaxim 7:9cd16581b578 156 }
IanBenzMaxim 7:9cd16581b578 157
IanBenzMaxim 7:9cd16581b578 158 const error_category & DS2431::errorCategory() {
IanBenzMaxim 7:9cd16581b578 159 static class : public error_category {
IanBenzMaxim 7:9cd16581b578 160 public:
IanBenzMaxim 11:3f3bf6bf5e6c 161 virtual const char * name() const { return "MaximInterfaceDevices.DS2431"; }
IanBenzMaxim 7:9cd16581b578 162
IanBenzMaxim 7:9cd16581b578 163 virtual std::string message(int condition) const {
IanBenzMaxim 7:9cd16581b578 164 switch (condition) {
IanBenzMaxim 7:9cd16581b578 165 case CrcError:
IanBenzMaxim 7:9cd16581b578 166 return "CRC Error";
IanBenzMaxim 7:9cd16581b578 167
IanBenzMaxim 7:9cd16581b578 168 case OperationFailure:
IanBenzMaxim 7:9cd16581b578 169 return "Operation Failure";
IanBenzMaxim 7:9cd16581b578 170 }
IanBenzMaxim 7:9cd16581b578 171 return defaultErrorMessage(condition);
IanBenzMaxim 7:9cd16581b578 172 }
IanBenzMaxim 7:9cd16581b578 173 } instance;
IanBenzMaxim 7:9cd16581b578 174 return instance;
IanBenzMaxim 7:9cd16581b578 175 }
IanBenzMaxim 7:9cd16581b578 176
IanBenzMaxim 7:9cd16581b578 177 } // namespace MaximInterfaceDevices