Extended MaximInterface
Dependents: mbed_DS28EC20_GPIO
Devices/DS28E38.cpp@10:de4b8812877d, 2020-01-06 (annotated)
- Committer:
- reARMnimator
- Date:
- Mon Jan 06 15:54:55 2020 +0000
- Revision:
- 10:de4b8812877d
- Parent:
- 6:a8c83a2e6fa4
Fixed inappropriate include path.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
IanBenzMaxim | 6:a8c83a2e6fa4 | 1 | /******************************************************************************* |
IanBenzMaxim | 6:a8c83a2e6fa4 | 2 | * Copyright (C) 2017 Maxim Integrated Products, Inc., All Rights Reserved. |
IanBenzMaxim | 6:a8c83a2e6fa4 | 3 | * |
IanBenzMaxim | 6:a8c83a2e6fa4 | 4 | * Permission is hereby granted, free of charge, to any person obtaining a |
IanBenzMaxim | 6:a8c83a2e6fa4 | 5 | * copy of this software and associated documentation files (the "Software"), |
IanBenzMaxim | 6:a8c83a2e6fa4 | 6 | * to deal in the Software without restriction, including without limitation |
IanBenzMaxim | 6:a8c83a2e6fa4 | 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
IanBenzMaxim | 6:a8c83a2e6fa4 | 8 | * and/or sell copies of the Software, and to permit persons to whom the |
IanBenzMaxim | 6:a8c83a2e6fa4 | 9 | * Software is furnished to do so, subject to the following conditions: |
IanBenzMaxim | 6:a8c83a2e6fa4 | 10 | * |
IanBenzMaxim | 6:a8c83a2e6fa4 | 11 | * The above copyright notice and this permission notice shall be included |
IanBenzMaxim | 6:a8c83a2e6fa4 | 12 | * in all copies or substantial portions of the Software. |
IanBenzMaxim | 6:a8c83a2e6fa4 | 13 | * |
IanBenzMaxim | 6:a8c83a2e6fa4 | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
IanBenzMaxim | 6:a8c83a2e6fa4 | 15 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
IanBenzMaxim | 6:a8c83a2e6fa4 | 16 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
IanBenzMaxim | 6:a8c83a2e6fa4 | 17 | * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES |
IanBenzMaxim | 6:a8c83a2e6fa4 | 18 | * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
IanBenzMaxim | 6:a8c83a2e6fa4 | 19 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
IanBenzMaxim | 6:a8c83a2e6fa4 | 20 | * OTHER DEALINGS IN THE SOFTWARE. |
IanBenzMaxim | 6:a8c83a2e6fa4 | 21 | * |
IanBenzMaxim | 6:a8c83a2e6fa4 | 22 | * Except as contained in this notice, the name of Maxim Integrated |
IanBenzMaxim | 6:a8c83a2e6fa4 | 23 | * Products, Inc. shall not be used except as stated in the Maxim Integrated |
IanBenzMaxim | 6:a8c83a2e6fa4 | 24 | * Products, Inc. Branding Policy. |
IanBenzMaxim | 6:a8c83a2e6fa4 | 25 | * |
IanBenzMaxim | 6:a8c83a2e6fa4 | 26 | * The mere transfer of this software does not imply any licenses |
IanBenzMaxim | 6:a8c83a2e6fa4 | 27 | * of trade secrets, proprietary technology, copyrights, patents, |
IanBenzMaxim | 6:a8c83a2e6fa4 | 28 | * trademarks, maskwork rights, or any other form of intellectual |
IanBenzMaxim | 6:a8c83a2e6fa4 | 29 | * property whatsoever. Maxim Integrated Products, Inc. retains all |
IanBenzMaxim | 6:a8c83a2e6fa4 | 30 | * ownership rights. |
IanBenzMaxim | 6:a8c83a2e6fa4 | 31 | *******************************************************************************/ |
IanBenzMaxim | 6:a8c83a2e6fa4 | 32 | |
IanBenzMaxim | 6:a8c83a2e6fa4 | 33 | #include <stddef.h> |
IanBenzMaxim | 6:a8c83a2e6fa4 | 34 | #include <algorithm> |
IanBenzMaxim | 6:a8c83a2e6fa4 | 35 | #include <MaximInterface/Utilities/Algorithm.hpp> |
IanBenzMaxim | 6:a8c83a2e6fa4 | 36 | #include <MaximInterface/Utilities/Error.hpp> |
IanBenzMaxim | 6:a8c83a2e6fa4 | 37 | #include "DS28E38.hpp" |
IanBenzMaxim | 6:a8c83a2e6fa4 | 38 | |
IanBenzMaxim | 6:a8c83a2e6fa4 | 39 | namespace MaximInterface { |
IanBenzMaxim | 6:a8c83a2e6fa4 | 40 | |
IanBenzMaxim | 6:a8c83a2e6fa4 | 41 | using std::copy; |
IanBenzMaxim | 6:a8c83a2e6fa4 | 42 | |
IanBenzMaxim | 6:a8c83a2e6fa4 | 43 | static const int readMemoryTimeMs = 30; |
IanBenzMaxim | 6:a8c83a2e6fa4 | 44 | static const int writeMemoryTimeMs = 65; |
IanBenzMaxim | 6:a8c83a2e6fa4 | 45 | static const int writeStateTimeMs = 15; |
IanBenzMaxim | 6:a8c83a2e6fa4 | 46 | static const int generateEccKeyPairTimeMs = 200; |
IanBenzMaxim | 6:a8c83a2e6fa4 | 47 | static const int generateEcdsaSignatureTimeMs = 130; |
IanBenzMaxim | 6:a8c83a2e6fa4 | 48 | static const int trngOnDemandCheckTimeMs = 20; |
IanBenzMaxim | 6:a8c83a2e6fa4 | 49 | static const int trngGenerationTimeMs = 10; |
IanBenzMaxim | 6:a8c83a2e6fa4 | 50 | |
IanBenzMaxim | 6:a8c83a2e6fa4 | 51 | const int DS28E38::decrementCounterPage; |
IanBenzMaxim | 6:a8c83a2e6fa4 | 52 | const int DS28E38::publicKeyXPage; |
IanBenzMaxim | 6:a8c83a2e6fa4 | 53 | const int DS28E38::publicKeyYPage; |
IanBenzMaxim | 6:a8c83a2e6fa4 | 54 | const int DS28E38::privateKeyPage; |
IanBenzMaxim | 6:a8c83a2e6fa4 | 55 | const int DS28E38::memoryPages; |
IanBenzMaxim | 6:a8c83a2e6fa4 | 56 | |
IanBenzMaxim | 6:a8c83a2e6fa4 | 57 | error_code DS28E38::writeMemory(int pageNum, Page::const_span page) { |
IanBenzMaxim | 6:a8c83a2e6fa4 | 58 | if (pageNum < 0 || pageNum >= memoryPages) { |
IanBenzMaxim | 6:a8c83a2e6fa4 | 59 | return make_error_code(InvalidParameterError); |
IanBenzMaxim | 6:a8c83a2e6fa4 | 60 | } |
IanBenzMaxim | 6:a8c83a2e6fa4 | 61 | |
IanBenzMaxim | 6:a8c83a2e6fa4 | 62 | uint_least8_t command[2 + Page::size]; |
IanBenzMaxim | 6:a8c83a2e6fa4 | 63 | command[0] = 0x96; |
IanBenzMaxim | 6:a8c83a2e6fa4 | 64 | command[1] = pageNum; |
IanBenzMaxim | 6:a8c83a2e6fa4 | 65 | copy(page.begin(), page.end(), command + 2); |
IanBenzMaxim | 6:a8c83a2e6fa4 | 66 | return runCommand(command, writeMemoryTimeMs); |
IanBenzMaxim | 6:a8c83a2e6fa4 | 67 | } |
IanBenzMaxim | 6:a8c83a2e6fa4 | 68 | |
IanBenzMaxim | 6:a8c83a2e6fa4 | 69 | error_code DS28E38::readMemory(int pageNum, Page::span page) { |
IanBenzMaxim | 6:a8c83a2e6fa4 | 70 | if (pageNum < 0 || pageNum >= memoryPages) { |
IanBenzMaxim | 6:a8c83a2e6fa4 | 71 | return make_error_code(InvalidParameterError); |
IanBenzMaxim | 6:a8c83a2e6fa4 | 72 | } |
IanBenzMaxim | 6:a8c83a2e6fa4 | 73 | |
IanBenzMaxim | 6:a8c83a2e6fa4 | 74 | uint_least8_t buffer[1 + Page::size]; |
IanBenzMaxim | 6:a8c83a2e6fa4 | 75 | buffer[0] = 0x44; |
IanBenzMaxim | 6:a8c83a2e6fa4 | 76 | buffer[1] = pageNum; |
IanBenzMaxim | 6:a8c83a2e6fa4 | 77 | span<uint_least8_t> response(buffer); |
IanBenzMaxim | 6:a8c83a2e6fa4 | 78 | const error_code result = |
IanBenzMaxim | 6:a8c83a2e6fa4 | 79 | runCommand(make_span(buffer, 2), readMemoryTimeMs, response); |
IanBenzMaxim | 6:a8c83a2e6fa4 | 80 | if (!result) { |
IanBenzMaxim | 6:a8c83a2e6fa4 | 81 | copy(response.begin(), response.end(), page.begin()); |
IanBenzMaxim | 6:a8c83a2e6fa4 | 82 | } |
IanBenzMaxim | 6:a8c83a2e6fa4 | 83 | return result; |
IanBenzMaxim | 6:a8c83a2e6fa4 | 84 | } |
IanBenzMaxim | 6:a8c83a2e6fa4 | 85 | |
IanBenzMaxim | 6:a8c83a2e6fa4 | 86 | error_code DS28E38::readStatus(bool entropyHealthTest, Status & status) { |
IanBenzMaxim | 6:a8c83a2e6fa4 | 87 | int delay = readMemoryTimeMs; |
IanBenzMaxim | 6:a8c83a2e6fa4 | 88 | if (entropyHealthTest) { |
IanBenzMaxim | 6:a8c83a2e6fa4 | 89 | delay += trngOnDemandCheckTimeMs; |
IanBenzMaxim | 6:a8c83a2e6fa4 | 90 | } |
IanBenzMaxim | 6:a8c83a2e6fa4 | 91 | uint_least8_t buffer[Status::PageProtectionList::csize + ManId::size + |
IanBenzMaxim | 6:a8c83a2e6fa4 | 92 | Status::RomVersion::csize + 2]; |
IanBenzMaxim | 6:a8c83a2e6fa4 | 93 | buffer[0] = 0xAA; |
IanBenzMaxim | 6:a8c83a2e6fa4 | 94 | buffer[1] = entropyHealthTest ? 0x01 : 0x00; |
IanBenzMaxim | 6:a8c83a2e6fa4 | 95 | span<uint_least8_t> response(buffer); |
IanBenzMaxim | 6:a8c83a2e6fa4 | 96 | error_code result = runCommand(make_span(buffer, 2), delay, response); |
IanBenzMaxim | 6:a8c83a2e6fa4 | 97 | if (!result) { |
IanBenzMaxim | 6:a8c83a2e6fa4 | 98 | span<uint_least8_t>::const_iterator responseIt = response.begin(); |
IanBenzMaxim | 6:a8c83a2e6fa4 | 99 | for (Status::PageProtectionList::iterator it = |
IanBenzMaxim | 6:a8c83a2e6fa4 | 100 | status.pageProtection.begin(); |
IanBenzMaxim | 6:a8c83a2e6fa4 | 101 | it != status.pageProtection.end(); ++it) { |
IanBenzMaxim | 6:a8c83a2e6fa4 | 102 | *it = *responseIt; |
IanBenzMaxim | 6:a8c83a2e6fa4 | 103 | ++responseIt; |
IanBenzMaxim | 6:a8c83a2e6fa4 | 104 | } |
IanBenzMaxim | 6:a8c83a2e6fa4 | 105 | span<uint_least8_t>::const_iterator responseItEnd = |
IanBenzMaxim | 6:a8c83a2e6fa4 | 106 | responseIt + status.manId.size(); |
IanBenzMaxim | 6:a8c83a2e6fa4 | 107 | copy(responseIt, responseItEnd, status.manId.begin()); |
IanBenzMaxim | 6:a8c83a2e6fa4 | 108 | responseIt = responseItEnd; |
IanBenzMaxim | 6:a8c83a2e6fa4 | 109 | responseItEnd = responseIt + status.romVersion.size(); |
IanBenzMaxim | 6:a8c83a2e6fa4 | 110 | copy(responseIt, responseItEnd, status.romVersion.begin()); |
IanBenzMaxim | 6:a8c83a2e6fa4 | 111 | responseIt = responseItEnd; |
IanBenzMaxim | 6:a8c83a2e6fa4 | 112 | switch (*responseIt) { |
IanBenzMaxim | 6:a8c83a2e6fa4 | 113 | case Status::TestNotPerformed: |
IanBenzMaxim | 6:a8c83a2e6fa4 | 114 | case Status::EntropyHealthy: |
IanBenzMaxim | 6:a8c83a2e6fa4 | 115 | case Status::EntropyNotHealthy: |
IanBenzMaxim | 6:a8c83a2e6fa4 | 116 | status.entropyHealthTestStatus = |
IanBenzMaxim | 6:a8c83a2e6fa4 | 117 | static_cast<Status::EntropyHealthTestStatus>(*responseIt); |
IanBenzMaxim | 6:a8c83a2e6fa4 | 118 | break; |
IanBenzMaxim | 6:a8c83a2e6fa4 | 119 | |
IanBenzMaxim | 6:a8c83a2e6fa4 | 120 | default: |
IanBenzMaxim | 6:a8c83a2e6fa4 | 121 | result = make_error_code(InvalidResponseError); |
IanBenzMaxim | 6:a8c83a2e6fa4 | 122 | break; |
IanBenzMaxim | 6:a8c83a2e6fa4 | 123 | } |
IanBenzMaxim | 6:a8c83a2e6fa4 | 124 | } |
IanBenzMaxim | 6:a8c83a2e6fa4 | 125 | return result; |
IanBenzMaxim | 6:a8c83a2e6fa4 | 126 | } |
IanBenzMaxim | 6:a8c83a2e6fa4 | 127 | |
IanBenzMaxim | 6:a8c83a2e6fa4 | 128 | error_code DS28E38::setPageProtection(int pageNum, |
IanBenzMaxim | 6:a8c83a2e6fa4 | 129 | const PageProtection & protection) { |
IanBenzMaxim | 6:a8c83a2e6fa4 | 130 | if (pageNum < 0 || pageNum >= memoryPages) { |
IanBenzMaxim | 6:a8c83a2e6fa4 | 131 | return make_error_code(InvalidParameterError); |
IanBenzMaxim | 6:a8c83a2e6fa4 | 132 | } |
IanBenzMaxim | 6:a8c83a2e6fa4 | 133 | |
IanBenzMaxim | 6:a8c83a2e6fa4 | 134 | int delay = writeStateTimeMs; |
IanBenzMaxim | 6:a8c83a2e6fa4 | 135 | if (pageNum == decrementCounterPage) { |
IanBenzMaxim | 6:a8c83a2e6fa4 | 136 | delay += writeMemoryTimeMs; |
IanBenzMaxim | 6:a8c83a2e6fa4 | 137 | } |
IanBenzMaxim | 6:a8c83a2e6fa4 | 138 | const uint_least8_t command[] = { |
IanBenzMaxim | 6:a8c83a2e6fa4 | 139 | 0xC3, static_cast<uint_least8_t>(pageNum), |
IanBenzMaxim | 6:a8c83a2e6fa4 | 140 | static_cast<uint_least8_t>(protection.to_ulong())}; |
IanBenzMaxim | 6:a8c83a2e6fa4 | 141 | return runCommand(command, delay); |
IanBenzMaxim | 6:a8c83a2e6fa4 | 142 | } |
IanBenzMaxim | 6:a8c83a2e6fa4 | 143 | |
IanBenzMaxim | 6:a8c83a2e6fa4 | 144 | error_code |
IanBenzMaxim | 6:a8c83a2e6fa4 | 145 | DS28E38::computeAndReadPageAuthentication(int pageNum, bool anonymous, |
IanBenzMaxim | 6:a8c83a2e6fa4 | 146 | Page::const_span challenge, |
IanBenzMaxim | 6:a8c83a2e6fa4 | 147 | Ecc256::Signature::span signature) { |
IanBenzMaxim | 6:a8c83a2e6fa4 | 148 | if (pageNum < 0 || pageNum >= memoryPages) { |
IanBenzMaxim | 6:a8c83a2e6fa4 | 149 | return make_error_code(InvalidParameterError); |
IanBenzMaxim | 6:a8c83a2e6fa4 | 150 | } |
IanBenzMaxim | 6:a8c83a2e6fa4 | 151 | |
IanBenzMaxim | 6:a8c83a2e6fa4 | 152 | const size_t commandSize = 2 + Page::size; |
IanBenzMaxim | 6:a8c83a2e6fa4 | 153 | const size_t responseSize = 1 + 2 * Ecc256::Scalar::size; |
IanBenzMaxim | 6:a8c83a2e6fa4 | 154 | uint_least8_t buffer[MaximInterface_MAX(commandSize, responseSize)]; |
IanBenzMaxim | 6:a8c83a2e6fa4 | 155 | buffer[0] = 0xA5; |
IanBenzMaxim | 6:a8c83a2e6fa4 | 156 | buffer[1] = pageNum | (anonymous ? 0xE0 : 0x00); |
IanBenzMaxim | 6:a8c83a2e6fa4 | 157 | copy(challenge.begin(), challenge.end(), buffer + 2); |
IanBenzMaxim | 6:a8c83a2e6fa4 | 158 | span<uint_least8_t> response(buffer, responseSize); |
IanBenzMaxim | 6:a8c83a2e6fa4 | 159 | const error_code result = runCommand(make_span(buffer, commandSize), |
IanBenzMaxim | 6:a8c83a2e6fa4 | 160 | generateEcdsaSignatureTimeMs, response); |
IanBenzMaxim | 6:a8c83a2e6fa4 | 161 | if (!result) { |
IanBenzMaxim | 6:a8c83a2e6fa4 | 162 | span<uint_least8_t>::const_iterator begin = response.begin(); |
IanBenzMaxim | 6:a8c83a2e6fa4 | 163 | span<uint_least8_t>::const_iterator end = begin + signature.s.size(); |
IanBenzMaxim | 6:a8c83a2e6fa4 | 164 | copy(begin, end, signature.s.begin()); |
IanBenzMaxim | 6:a8c83a2e6fa4 | 165 | begin = end; |
IanBenzMaxim | 6:a8c83a2e6fa4 | 166 | end = begin + signature.r.size(); |
IanBenzMaxim | 6:a8c83a2e6fa4 | 167 | copy(begin, end, signature.r.begin()); |
IanBenzMaxim | 6:a8c83a2e6fa4 | 168 | } |
IanBenzMaxim | 6:a8c83a2e6fa4 | 169 | return result; |
IanBenzMaxim | 6:a8c83a2e6fa4 | 170 | } |
IanBenzMaxim | 6:a8c83a2e6fa4 | 171 | |
IanBenzMaxim | 6:a8c83a2e6fa4 | 172 | error_code DS28E38::decrementCounter() { |
IanBenzMaxim | 6:a8c83a2e6fa4 | 173 | const uint_least8_t command = 0xC9; |
IanBenzMaxim | 6:a8c83a2e6fa4 | 174 | return runCommand(make_span(&command, 1), writeMemoryTimeMs); |
IanBenzMaxim | 6:a8c83a2e6fa4 | 175 | } |
IanBenzMaxim | 6:a8c83a2e6fa4 | 176 | |
IanBenzMaxim | 6:a8c83a2e6fa4 | 177 | error_code DS28E38::disableDevice() { |
IanBenzMaxim | 6:a8c83a2e6fa4 | 178 | const uint_least8_t command[] = {0x33, 0x9E, 0xA7, 0x49, 0xFB, |
IanBenzMaxim | 6:a8c83a2e6fa4 | 179 | 0x10, 0x62, 0x0A, 0x26}; |
IanBenzMaxim | 6:a8c83a2e6fa4 | 180 | return runCommand(command, writeStateTimeMs); |
IanBenzMaxim | 6:a8c83a2e6fa4 | 181 | } |
IanBenzMaxim | 6:a8c83a2e6fa4 | 182 | |
IanBenzMaxim | 6:a8c83a2e6fa4 | 183 | error_code DS28E38::generateEcc256KeyPair(bool privateKeyPuf, |
IanBenzMaxim | 6:a8c83a2e6fa4 | 184 | bool writeProtectEnable) { |
IanBenzMaxim | 6:a8c83a2e6fa4 | 185 | int delay = generateEccKeyPairTimeMs; |
IanBenzMaxim | 6:a8c83a2e6fa4 | 186 | if (writeProtectEnable) { |
IanBenzMaxim | 6:a8c83a2e6fa4 | 187 | delay += writeStateTimeMs; |
IanBenzMaxim | 6:a8c83a2e6fa4 | 188 | } |
IanBenzMaxim | 6:a8c83a2e6fa4 | 189 | uint_least8_t command[] = {0xCB, 0x00}; |
IanBenzMaxim | 6:a8c83a2e6fa4 | 190 | if (privateKeyPuf) { |
IanBenzMaxim | 6:a8c83a2e6fa4 | 191 | command[1] |= 0x01; |
IanBenzMaxim | 6:a8c83a2e6fa4 | 192 | } |
IanBenzMaxim | 6:a8c83a2e6fa4 | 193 | if (writeProtectEnable) { |
IanBenzMaxim | 6:a8c83a2e6fa4 | 194 | command[1] |= 0x80; |
IanBenzMaxim | 6:a8c83a2e6fa4 | 195 | } |
IanBenzMaxim | 6:a8c83a2e6fa4 | 196 | return runCommand(command, delay); |
IanBenzMaxim | 6:a8c83a2e6fa4 | 197 | } |
IanBenzMaxim | 6:a8c83a2e6fa4 | 198 | |
IanBenzMaxim | 6:a8c83a2e6fa4 | 199 | error_code DS28E38::readRng(span<uint_least8_t> data) { |
IanBenzMaxim | 6:a8c83a2e6fa4 | 200 | const span<uint_least8_t>::index_type maxDataSize = 64; |
IanBenzMaxim | 6:a8c83a2e6fa4 | 201 | if ((data.size() < 1) || (data.size() > maxDataSize)) { |
IanBenzMaxim | 6:a8c83a2e6fa4 | 202 | return make_error_code(InvalidParameterError); |
IanBenzMaxim | 6:a8c83a2e6fa4 | 203 | } |
IanBenzMaxim | 6:a8c83a2e6fa4 | 204 | |
IanBenzMaxim | 6:a8c83a2e6fa4 | 205 | uint_least8_t buffer[1 + maxDataSize]; |
IanBenzMaxim | 6:a8c83a2e6fa4 | 206 | buffer[0] = 0xD2; |
IanBenzMaxim | 6:a8c83a2e6fa4 | 207 | buffer[1] = data.size() - 1; |
IanBenzMaxim | 6:a8c83a2e6fa4 | 208 | span<uint_least8_t> response(buffer, 1 + data.size()); |
IanBenzMaxim | 6:a8c83a2e6fa4 | 209 | const error_code result = |
IanBenzMaxim | 6:a8c83a2e6fa4 | 210 | runCommand(make_span(buffer, 2), trngGenerationTimeMs, response); |
IanBenzMaxim | 6:a8c83a2e6fa4 | 211 | if (!result) { |
IanBenzMaxim | 6:a8c83a2e6fa4 | 212 | copy(response.begin(), response.end(), data.begin()); |
IanBenzMaxim | 6:a8c83a2e6fa4 | 213 | } |
IanBenzMaxim | 6:a8c83a2e6fa4 | 214 | return result; |
IanBenzMaxim | 6:a8c83a2e6fa4 | 215 | } |
IanBenzMaxim | 6:a8c83a2e6fa4 | 216 | |
IanBenzMaxim | 6:a8c83a2e6fa4 | 217 | error_code DS28E38::runCommand(span<const uint_least8_t> command, int delayTime, |
IanBenzMaxim | 6:a8c83a2e6fa4 | 218 | span<uint_least8_t> & response) { |
IanBenzMaxim | 6:a8c83a2e6fa4 | 219 | const span<const uint_least8_t>::index_type responseInputSize = |
IanBenzMaxim | 6:a8c83a2e6fa4 | 220 | response.size(); |
IanBenzMaxim | 6:a8c83a2e6fa4 | 221 | error_code result = doRunCommand(command, delayTime, response); |
IanBenzMaxim | 6:a8c83a2e6fa4 | 222 | if (result) { |
IanBenzMaxim | 6:a8c83a2e6fa4 | 223 | return result; |
IanBenzMaxim | 6:a8c83a2e6fa4 | 224 | } |
IanBenzMaxim | 6:a8c83a2e6fa4 | 225 | if (response.empty()) { |
IanBenzMaxim | 6:a8c83a2e6fa4 | 226 | return make_error_code(InvalidResponseError); |
IanBenzMaxim | 6:a8c83a2e6fa4 | 227 | } |
IanBenzMaxim | 6:a8c83a2e6fa4 | 228 | // Parse command result byte. |
IanBenzMaxim | 6:a8c83a2e6fa4 | 229 | switch (response[0]) { |
IanBenzMaxim | 6:a8c83a2e6fa4 | 230 | case 0xAA: |
IanBenzMaxim | 6:a8c83a2e6fa4 | 231 | // Success response. |
IanBenzMaxim | 6:a8c83a2e6fa4 | 232 | if (response.size() != responseInputSize) { |
IanBenzMaxim | 6:a8c83a2e6fa4 | 233 | result = make_error_code(InvalidResponseError); |
IanBenzMaxim | 6:a8c83a2e6fa4 | 234 | } |
IanBenzMaxim | 6:a8c83a2e6fa4 | 235 | break; |
IanBenzMaxim | 6:a8c83a2e6fa4 | 236 | |
IanBenzMaxim | 6:a8c83a2e6fa4 | 237 | case 0x00: |
IanBenzMaxim | 6:a8c83a2e6fa4 | 238 | result = make_error_code(InvalidResponseError); |
IanBenzMaxim | 6:a8c83a2e6fa4 | 239 | break; |
IanBenzMaxim | 6:a8c83a2e6fa4 | 240 | |
IanBenzMaxim | 6:a8c83a2e6fa4 | 241 | default: |
IanBenzMaxim | 6:a8c83a2e6fa4 | 242 | result.assign(response[0], errorCategory()); |
IanBenzMaxim | 6:a8c83a2e6fa4 | 243 | break; |
IanBenzMaxim | 6:a8c83a2e6fa4 | 244 | } |
IanBenzMaxim | 6:a8c83a2e6fa4 | 245 | response = response.subspan(1); |
IanBenzMaxim | 6:a8c83a2e6fa4 | 246 | return result; |
IanBenzMaxim | 6:a8c83a2e6fa4 | 247 | } |
IanBenzMaxim | 6:a8c83a2e6fa4 | 248 | |
IanBenzMaxim | 6:a8c83a2e6fa4 | 249 | error_code DS28E38::runCommand(span<const uint_least8_t> command, |
IanBenzMaxim | 6:a8c83a2e6fa4 | 250 | int delayTime) { |
IanBenzMaxim | 6:a8c83a2e6fa4 | 251 | uint_least8_t buffer; |
IanBenzMaxim | 6:a8c83a2e6fa4 | 252 | span<uint_least8_t> response(&buffer, 1); |
IanBenzMaxim | 6:a8c83a2e6fa4 | 253 | return runCommand(command, delayTime, response); |
IanBenzMaxim | 6:a8c83a2e6fa4 | 254 | } |
IanBenzMaxim | 6:a8c83a2e6fa4 | 255 | |
IanBenzMaxim | 6:a8c83a2e6fa4 | 256 | const error_category & DS28E38::errorCategory() { |
IanBenzMaxim | 6:a8c83a2e6fa4 | 257 | static class : public error_category { |
IanBenzMaxim | 6:a8c83a2e6fa4 | 258 | public: |
IanBenzMaxim | 6:a8c83a2e6fa4 | 259 | virtual const char * name() const { return "DS28E38"; } |
IanBenzMaxim | 6:a8c83a2e6fa4 | 260 | |
IanBenzMaxim | 6:a8c83a2e6fa4 | 261 | virtual std::string message(int condition) const { |
IanBenzMaxim | 6:a8c83a2e6fa4 | 262 | switch (condition) { |
IanBenzMaxim | 6:a8c83a2e6fa4 | 263 | case InvalidOperationError: |
IanBenzMaxim | 6:a8c83a2e6fa4 | 264 | return "Invalid Operation Error"; |
IanBenzMaxim | 6:a8c83a2e6fa4 | 265 | |
IanBenzMaxim | 6:a8c83a2e6fa4 | 266 | case InvalidParameterError: |
IanBenzMaxim | 6:a8c83a2e6fa4 | 267 | return "Invalid Parameter Error"; |
IanBenzMaxim | 6:a8c83a2e6fa4 | 268 | |
IanBenzMaxim | 6:a8c83a2e6fa4 | 269 | case InvalidSequenceError: |
IanBenzMaxim | 6:a8c83a2e6fa4 | 270 | return "Invalid Sequence Error"; |
IanBenzMaxim | 6:a8c83a2e6fa4 | 271 | |
IanBenzMaxim | 6:a8c83a2e6fa4 | 272 | case InternalError: |
IanBenzMaxim | 6:a8c83a2e6fa4 | 273 | return "Internal Error"; |
IanBenzMaxim | 6:a8c83a2e6fa4 | 274 | |
IanBenzMaxim | 6:a8c83a2e6fa4 | 275 | case DeviceDisabledError: |
IanBenzMaxim | 6:a8c83a2e6fa4 | 276 | return "Device Disabled Error"; |
IanBenzMaxim | 6:a8c83a2e6fa4 | 277 | |
IanBenzMaxim | 6:a8c83a2e6fa4 | 278 | case InvalidResponseError: |
IanBenzMaxim | 6:a8c83a2e6fa4 | 279 | return "Invalid Response Error"; |
IanBenzMaxim | 6:a8c83a2e6fa4 | 280 | } |
IanBenzMaxim | 6:a8c83a2e6fa4 | 281 | return defaultErrorMessage(condition); |
IanBenzMaxim | 6:a8c83a2e6fa4 | 282 | } |
IanBenzMaxim | 6:a8c83a2e6fa4 | 283 | } instance; |
IanBenzMaxim | 6:a8c83a2e6fa4 | 284 | return instance; |
IanBenzMaxim | 6:a8c83a2e6fa4 | 285 | } |
IanBenzMaxim | 6:a8c83a2e6fa4 | 286 | |
IanBenzMaxim | 6:a8c83a2e6fa4 | 287 | error_code readManId(DS28E38 & ds28e38, ManId::span manId) { |
IanBenzMaxim | 6:a8c83a2e6fa4 | 288 | DS28E38::Status status; |
IanBenzMaxim | 6:a8c83a2e6fa4 | 289 | const error_code result = ds28e38.readStatus(false, status); |
IanBenzMaxim | 6:a8c83a2e6fa4 | 290 | if (!result) { |
IanBenzMaxim | 6:a8c83a2e6fa4 | 291 | copy(make_span(status.manId), manId); |
IanBenzMaxim | 6:a8c83a2e6fa4 | 292 | } |
IanBenzMaxim | 6:a8c83a2e6fa4 | 293 | return result; |
IanBenzMaxim | 6:a8c83a2e6fa4 | 294 | } |
IanBenzMaxim | 6:a8c83a2e6fa4 | 295 | |
IanBenzMaxim | 6:a8c83a2e6fa4 | 296 | DS28E38::PageAuthenticationData & |
IanBenzMaxim | 6:a8c83a2e6fa4 | 297 | DS28E38::PageAuthenticationData::setAnonymousRomId() { |
IanBenzMaxim | 6:a8c83a2e6fa4 | 298 | std::fill(romId().begin(), romId().end(), 0xFF); |
IanBenzMaxim | 6:a8c83a2e6fa4 | 299 | return *this; |
IanBenzMaxim | 6:a8c83a2e6fa4 | 300 | } |
IanBenzMaxim | 6:a8c83a2e6fa4 | 301 | |
IanBenzMaxim | 6:a8c83a2e6fa4 | 302 | } // namespace MaximInterface |