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
MaximInterfaceCore/RunCommand.cpp@12:7eb41621ba22, 2020-05-29 (annotated)
- 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?
User | Revision | Line number | New 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 | #include "Crc.hpp" |
IanBenzMaxim | 7:9cd16581b578 | 34 | #include "Error.hpp" |
IanBenzMaxim | 7:9cd16581b578 | 35 | #include "I2CMaster.hpp" |
IanBenzMaxim | 7:9cd16581b578 | 36 | #include "OneWireMaster.hpp" |
IanBenzMaxim | 7:9cd16581b578 | 37 | #include "RunCommand.hpp" |
IanBenzMaxim | 7:9cd16581b578 | 38 | #include "Sleep.hpp" |
IanBenzMaxim | 7:9cd16581b578 | 39 | |
IanBenzMaxim | 7:9cd16581b578 | 40 | namespace MaximInterfaceCore { |
IanBenzMaxim | 7:9cd16581b578 | 41 | |
IanBenzMaxim | 7:9cd16581b578 | 42 | const error_category & RunCommandWithOneWireMaster::errorCategory() { |
IanBenzMaxim | 7:9cd16581b578 | 43 | static class : public error_category { |
IanBenzMaxim | 7:9cd16581b578 | 44 | public: |
IanBenzMaxim | 11:3f3bf6bf5e6c | 45 | virtual const char * name() const { |
IanBenzMaxim | 11:3f3bf6bf5e6c | 46 | return "MaximInterfaceCore.RunCommandWithOneWireMaster"; |
IanBenzMaxim | 11:3f3bf6bf5e6c | 47 | } |
IanBenzMaxim | 7:9cd16581b578 | 48 | |
IanBenzMaxim | 7:9cd16581b578 | 49 | virtual std::string message(int condition) const { |
IanBenzMaxim | 7:9cd16581b578 | 50 | switch (condition) { |
IanBenzMaxim | 7:9cd16581b578 | 51 | case CrcError: |
IanBenzMaxim | 7:9cd16581b578 | 52 | return "CRC Error"; |
IanBenzMaxim | 7:9cd16581b578 | 53 | |
IanBenzMaxim | 7:9cd16581b578 | 54 | case InvalidResponseError: |
IanBenzMaxim | 7:9cd16581b578 | 55 | return "Invalid Response Error"; |
IanBenzMaxim | 7:9cd16581b578 | 56 | } |
IanBenzMaxim | 7:9cd16581b578 | 57 | return defaultErrorMessage(condition); |
IanBenzMaxim | 7:9cd16581b578 | 58 | } |
IanBenzMaxim | 7:9cd16581b578 | 59 | } instance; |
IanBenzMaxim | 7:9cd16581b578 | 60 | return instance; |
IanBenzMaxim | 7:9cd16581b578 | 61 | } |
IanBenzMaxim | 7:9cd16581b578 | 62 | |
IanBenzMaxim | 8:5ea891c7d1a1 | 63 | Result<span<uint_least8_t> > RunCommandWithOneWireMaster:: |
IanBenzMaxim | 7:9cd16581b578 | 64 | operator()(span<const uint_least8_t> request, int delayTime, |
IanBenzMaxim | 8:5ea891c7d1a1 | 65 | span<uint_least8_t> response) const { |
IanBenzMaxim | 7:9cd16581b578 | 66 | // Write request. |
IanBenzMaxim | 8:5ea891c7d1a1 | 67 | Result<void> result = selectRom(*master); |
IanBenzMaxim | 8:5ea891c7d1a1 | 68 | if (!result) { |
IanBenzMaxim | 8:5ea891c7d1a1 | 69 | return result.error(); |
IanBenzMaxim | 7:9cd16581b578 | 70 | } |
IanBenzMaxim | 7:9cd16581b578 | 71 | uint_least8_t xpcBuffer[2] = {0x66, |
IanBenzMaxim | 7:9cd16581b578 | 72 | static_cast<uint_least8_t>(request.size())}; |
IanBenzMaxim | 7:9cd16581b578 | 73 | result = master->writeBlock(xpcBuffer); |
IanBenzMaxim | 8:5ea891c7d1a1 | 74 | if (!result) { |
IanBenzMaxim | 8:5ea891c7d1a1 | 75 | return result.error(); |
IanBenzMaxim | 7:9cd16581b578 | 76 | } |
IanBenzMaxim | 7:9cd16581b578 | 77 | result = master->writeBlock(request); |
IanBenzMaxim | 8:5ea891c7d1a1 | 78 | if (!result) { |
IanBenzMaxim | 8:5ea891c7d1a1 | 79 | return result.error(); |
IanBenzMaxim | 7:9cd16581b578 | 80 | } |
IanBenzMaxim | 7:9cd16581b578 | 81 | uint_fast16_t expectedCrc = |
IanBenzMaxim | 10:947d3f44e0a0 | 82 | calculateCrc16(calculateCrc16(xpcBuffer), request) ^ 0xFFFF; |
IanBenzMaxim | 7:9cd16581b578 | 83 | result = master->readBlock(xpcBuffer); |
IanBenzMaxim | 8:5ea891c7d1a1 | 84 | if (!result) { |
IanBenzMaxim | 8:5ea891c7d1a1 | 85 | return result.error(); |
IanBenzMaxim | 7:9cd16581b578 | 86 | } |
IanBenzMaxim | 7:9cd16581b578 | 87 | if (expectedCrc != |
IanBenzMaxim | 7:9cd16581b578 | 88 | ((static_cast<uint_fast16_t>(xpcBuffer[1]) << 8) | xpcBuffer[0])) { |
IanBenzMaxim | 8:5ea891c7d1a1 | 89 | return CrcError; |
IanBenzMaxim | 7:9cd16581b578 | 90 | } |
IanBenzMaxim | 7:9cd16581b578 | 91 | result = master->writeBytePower(0xAA); |
IanBenzMaxim | 8:5ea891c7d1a1 | 92 | if (!result) { |
IanBenzMaxim | 8:5ea891c7d1a1 | 93 | return result.error(); |
IanBenzMaxim | 7:9cd16581b578 | 94 | } |
IanBenzMaxim | 7:9cd16581b578 | 95 | |
IanBenzMaxim | 7:9cd16581b578 | 96 | // Wait for device to process. |
IanBenzMaxim | 7:9cd16581b578 | 97 | sleep->invoke(delayTime); |
IanBenzMaxim | 7:9cd16581b578 | 98 | |
IanBenzMaxim | 7:9cd16581b578 | 99 | // Read response. |
IanBenzMaxim | 7:9cd16581b578 | 100 | result = master->setLevel(OneWireMaster::NormalLevel); |
IanBenzMaxim | 8:5ea891c7d1a1 | 101 | if (!result) { |
IanBenzMaxim | 8:5ea891c7d1a1 | 102 | return result.error(); |
IanBenzMaxim | 7:9cd16581b578 | 103 | } |
IanBenzMaxim | 7:9cd16581b578 | 104 | result = master->readBlock(xpcBuffer); |
IanBenzMaxim | 8:5ea891c7d1a1 | 105 | if (!result) { |
IanBenzMaxim | 8:5ea891c7d1a1 | 106 | return result.error(); |
IanBenzMaxim | 7:9cd16581b578 | 107 | } |
IanBenzMaxim | 7:9cd16581b578 | 108 | if (xpcBuffer[1] > response.size()) { |
IanBenzMaxim | 8:5ea891c7d1a1 | 109 | return InvalidResponseError; |
IanBenzMaxim | 7:9cd16581b578 | 110 | } |
IanBenzMaxim | 7:9cd16581b578 | 111 | response = response.first(xpcBuffer[1]); |
IanBenzMaxim | 7:9cd16581b578 | 112 | result = master->readBlock(response); |
IanBenzMaxim | 8:5ea891c7d1a1 | 113 | if (!result) { |
IanBenzMaxim | 8:5ea891c7d1a1 | 114 | return result.error(); |
IanBenzMaxim | 7:9cd16581b578 | 115 | } |
IanBenzMaxim | 7:9cd16581b578 | 116 | expectedCrc = |
IanBenzMaxim | 7:9cd16581b578 | 117 | calculateCrc16(calculateCrc16(make_span(xpcBuffer + 1, 1)), response) ^ |
IanBenzMaxim | 10:947d3f44e0a0 | 118 | 0xFFFF; |
IanBenzMaxim | 7:9cd16581b578 | 119 | result = master->readBlock(xpcBuffer); |
IanBenzMaxim | 8:5ea891c7d1a1 | 120 | if (!result) { |
IanBenzMaxim | 8:5ea891c7d1a1 | 121 | return result.error(); |
IanBenzMaxim | 7:9cd16581b578 | 122 | } |
IanBenzMaxim | 7:9cd16581b578 | 123 | if (expectedCrc != |
IanBenzMaxim | 7:9cd16581b578 | 124 | ((static_cast<uint_fast16_t>(xpcBuffer[1]) << 8) | xpcBuffer[0])) { |
IanBenzMaxim | 8:5ea891c7d1a1 | 125 | return CrcError; |
IanBenzMaxim | 7:9cd16581b578 | 126 | } |
IanBenzMaxim | 8:5ea891c7d1a1 | 127 | return response; |
IanBenzMaxim | 7:9cd16581b578 | 128 | } |
IanBenzMaxim | 7:9cd16581b578 | 129 | |
IanBenzMaxim | 7:9cd16581b578 | 130 | const error_category & RunCommandWithI2CMaster::errorCategory() { |
IanBenzMaxim | 7:9cd16581b578 | 131 | static class : public error_category { |
IanBenzMaxim | 7:9cd16581b578 | 132 | public: |
IanBenzMaxim | 11:3f3bf6bf5e6c | 133 | virtual const char * name() const { |
IanBenzMaxim | 11:3f3bf6bf5e6c | 134 | return "MaximInterfaceCore.RunCommandWithI2CMaster"; |
IanBenzMaxim | 11:3f3bf6bf5e6c | 135 | } |
IanBenzMaxim | 7:9cd16581b578 | 136 | |
IanBenzMaxim | 7:9cd16581b578 | 137 | virtual std::string message(int condition) const { |
IanBenzMaxim | 7:9cd16581b578 | 138 | switch (condition) { |
IanBenzMaxim | 7:9cd16581b578 | 139 | case InvalidResponseError: |
IanBenzMaxim | 7:9cd16581b578 | 140 | return "Invalid Response Error"; |
IanBenzMaxim | 7:9cd16581b578 | 141 | } |
IanBenzMaxim | 7:9cd16581b578 | 142 | return defaultErrorMessage(condition); |
IanBenzMaxim | 7:9cd16581b578 | 143 | } |
IanBenzMaxim | 7:9cd16581b578 | 144 | } instance; |
IanBenzMaxim | 7:9cd16581b578 | 145 | return instance; |
IanBenzMaxim | 7:9cd16581b578 | 146 | } |
IanBenzMaxim | 7:9cd16581b578 | 147 | |
IanBenzMaxim | 8:5ea891c7d1a1 | 148 | Result<span<uint_least8_t> > RunCommandWithI2CMaster:: |
IanBenzMaxim | 7:9cd16581b578 | 149 | operator()(span<const uint_least8_t> request, int delayTime, |
IanBenzMaxim | 8:5ea891c7d1a1 | 150 | span<uint_least8_t> response) const { |
IanBenzMaxim | 7:9cd16581b578 | 151 | // Write request. |
IanBenzMaxim | 8:5ea891c7d1a1 | 152 | Result<void> result = master->start(address_); |
IanBenzMaxim | 8:5ea891c7d1a1 | 153 | if (!result && result.error() == make_error_condition(I2CMaster::NackError) && |
IanBenzMaxim | 8:5ea891c7d1a1 | 154 | address_ != 0) { |
IanBenzMaxim | 7:9cd16581b578 | 155 | result = master->start(0); |
IanBenzMaxim | 7:9cd16581b578 | 156 | } |
IanBenzMaxim | 8:5ea891c7d1a1 | 157 | if (!result) { |
IanBenzMaxim | 7:9cd16581b578 | 158 | master->stop(); |
IanBenzMaxim | 8:5ea891c7d1a1 | 159 | return result.error(); |
IanBenzMaxim | 7:9cd16581b578 | 160 | } |
IanBenzMaxim | 7:9cd16581b578 | 161 | if (!request.empty()) { |
IanBenzMaxim | 7:9cd16581b578 | 162 | result = master->writeByte(request[0]); |
IanBenzMaxim | 8:5ea891c7d1a1 | 163 | if (!result) { |
IanBenzMaxim | 7:9cd16581b578 | 164 | master->stop(); |
IanBenzMaxim | 8:5ea891c7d1a1 | 165 | return result.error(); |
IanBenzMaxim | 7:9cd16581b578 | 166 | } |
IanBenzMaxim | 7:9cd16581b578 | 167 | request = request.subspan(1); |
IanBenzMaxim | 7:9cd16581b578 | 168 | if (!request.empty()) { |
IanBenzMaxim | 7:9cd16581b578 | 169 | result = master->writeByte(static_cast<uint_least8_t>(request.size())); |
IanBenzMaxim | 8:5ea891c7d1a1 | 170 | if (!result) { |
IanBenzMaxim | 7:9cd16581b578 | 171 | master->stop(); |
IanBenzMaxim | 8:5ea891c7d1a1 | 172 | return result.error(); |
IanBenzMaxim | 7:9cd16581b578 | 173 | } |
IanBenzMaxim | 7:9cd16581b578 | 174 | result = master->writeBlock(request); |
IanBenzMaxim | 8:5ea891c7d1a1 | 175 | if (!result) { |
IanBenzMaxim | 7:9cd16581b578 | 176 | master->stop(); |
IanBenzMaxim | 8:5ea891c7d1a1 | 177 | return result.error(); |
IanBenzMaxim | 7:9cd16581b578 | 178 | } |
IanBenzMaxim | 7:9cd16581b578 | 179 | } |
IanBenzMaxim | 7:9cd16581b578 | 180 | } |
IanBenzMaxim | 7:9cd16581b578 | 181 | result = master->stop(); |
IanBenzMaxim | 8:5ea891c7d1a1 | 182 | if (!result) { |
IanBenzMaxim | 8:5ea891c7d1a1 | 183 | return result.error(); |
IanBenzMaxim | 7:9cd16581b578 | 184 | } |
IanBenzMaxim | 7:9cd16581b578 | 185 | |
IanBenzMaxim | 7:9cd16581b578 | 186 | // Wait for device to process. |
IanBenzMaxim | 7:9cd16581b578 | 187 | sleep->invoke(delayTime); |
IanBenzMaxim | 7:9cd16581b578 | 188 | |
IanBenzMaxim | 7:9cd16581b578 | 189 | // Read response. |
IanBenzMaxim | 7:9cd16581b578 | 190 | result = master->start(address_ | 1); |
IanBenzMaxim | 8:5ea891c7d1a1 | 191 | if (!result) { |
IanBenzMaxim | 7:9cd16581b578 | 192 | master->stop(); |
IanBenzMaxim | 8:5ea891c7d1a1 | 193 | return result.error(); |
IanBenzMaxim | 7:9cd16581b578 | 194 | } |
IanBenzMaxim | 7:9cd16581b578 | 195 | uint_least8_t length; |
IanBenzMaxim | 8:5ea891c7d1a1 | 196 | if (const Result<uint_least8_t> result = master->readByte(I2CMaster::Ack)) { |
IanBenzMaxim | 8:5ea891c7d1a1 | 197 | length = result.value(); |
IanBenzMaxim | 8:5ea891c7d1a1 | 198 | } else { |
IanBenzMaxim | 7:9cd16581b578 | 199 | master->stop(); |
IanBenzMaxim | 8:5ea891c7d1a1 | 200 | return result.error(); |
IanBenzMaxim | 7:9cd16581b578 | 201 | } |
IanBenzMaxim | 7:9cd16581b578 | 202 | if (length > response.size()) { |
IanBenzMaxim | 7:9cd16581b578 | 203 | master->stop(); |
IanBenzMaxim | 8:5ea891c7d1a1 | 204 | return InvalidResponseError; |
IanBenzMaxim | 7:9cd16581b578 | 205 | } |
IanBenzMaxim | 7:9cd16581b578 | 206 | response = response.first(length); |
IanBenzMaxim | 8:5ea891c7d1a1 | 207 | result = master->readBlock(response, I2CMaster::Nack); |
IanBenzMaxim | 8:5ea891c7d1a1 | 208 | if (!result) { |
IanBenzMaxim | 7:9cd16581b578 | 209 | master->stop(); |
IanBenzMaxim | 8:5ea891c7d1a1 | 210 | return result.error(); |
IanBenzMaxim | 7:9cd16581b578 | 211 | } |
IanBenzMaxim | 7:9cd16581b578 | 212 | result = master->stop(); |
IanBenzMaxim | 8:5ea891c7d1a1 | 213 | if (!result) { |
IanBenzMaxim | 8:5ea891c7d1a1 | 214 | return result.error(); |
IanBenzMaxim | 8:5ea891c7d1a1 | 215 | } |
IanBenzMaxim | 8:5ea891c7d1a1 | 216 | return response; |
IanBenzMaxim | 7:9cd16581b578 | 217 | } |
IanBenzMaxim | 7:9cd16581b578 | 218 | |
IanBenzMaxim | 7:9cd16581b578 | 219 | } // namespace MaximInterfaceCore |