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
MaximInterfaceDevices/DS28E15_22_25.cpp@8:5ea891c7d1a1, 2019-09-16 (annotated)
- Committer:
- IanBenzMaxim
- Date:
- Mon Sep 16 11:13:37 2019 -0500
- Revision:
- 8:5ea891c7d1a1
- Parent:
- 7:9cd16581b578
- Child:
- 10:947d3f44e0a0
Updated to version 2.0.
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 | 7:9cd16581b578 | 33 | #include <algorithm> |
IanBenzMaxim | 8:5ea891c7d1a1 | 34 | #include <MaximInterfaceCore/Crc.hpp> |
IanBenzMaxim | 7:9cd16581b578 | 35 | #include <MaximInterfaceCore/Error.hpp> |
IanBenzMaxim | 7:9cd16581b578 | 36 | #include "DS28E15_22_25.hpp" |
IanBenzMaxim | 7:9cd16581b578 | 37 | |
IanBenzMaxim | 8:5ea891c7d1a1 | 38 | #define TRY MaximInterfaceCore_TRY |
IanBenzMaxim | 8:5ea891c7d1a1 | 39 | #define TRY_VALUE MaximInterfaceCore_TRY_VALUE |
IanBenzMaxim | 8:5ea891c7d1a1 | 40 | |
IanBenzMaxim | 7:9cd16581b578 | 41 | namespace MaximInterfaceDevices { |
IanBenzMaxim | 7:9cd16581b578 | 42 | |
IanBenzMaxim | 7:9cd16581b578 | 43 | using namespace Core; |
IanBenzMaxim | 7:9cd16581b578 | 44 | using std::copy; |
IanBenzMaxim | 7:9cd16581b578 | 45 | |
IanBenzMaxim | 7:9cd16581b578 | 46 | static const int shaComputationDelayMs = 3; |
IanBenzMaxim | 7:9cd16581b578 | 47 | static const int eepromWriteDelayMs = 10; |
IanBenzMaxim | 7:9cd16581b578 | 48 | static inline int secretEepromWriteDelayMs(bool lowPower) { |
IanBenzMaxim | 7:9cd16581b578 | 49 | return lowPower ? 200 : 100; |
IanBenzMaxim | 7:9cd16581b578 | 50 | } |
IanBenzMaxim | 7:9cd16581b578 | 51 | |
IanBenzMaxim | 7:9cd16581b578 | 52 | static const int ds28e22_25_pagesPerBlock = 2; |
IanBenzMaxim | 7:9cd16581b578 | 53 | |
IanBenzMaxim | 8:5ea891c7d1a1 | 54 | static Result<void> |
IanBenzMaxim | 7:9cd16581b578 | 55 | writeDataWithCrc(OneWireMaster & master, span<const uint_least8_t> data, |
IanBenzMaxim | 7:9cd16581b578 | 56 | OneWireMaster::Level level = OneWireMaster::NormalLevel, |
IanBenzMaxim | 7:9cd16581b578 | 57 | uint_fast16_t crcStart = 0) { |
IanBenzMaxim | 8:5ea891c7d1a1 | 58 | TRY(master.writeBlock(data)); |
IanBenzMaxim | 7:9cd16581b578 | 59 | uint_least8_t response[2]; |
IanBenzMaxim | 8:5ea891c7d1a1 | 60 | TRY_VALUE(response[0], master.readByte()); |
IanBenzMaxim | 8:5ea891c7d1a1 | 61 | TRY_VALUE(response[1], master.readByteSetLevel(level)); |
IanBenzMaxim | 8:5ea891c7d1a1 | 62 | if (calculateCrc16(calculateCrc16(crcStart, data), response) != 0xB001u) { |
IanBenzMaxim | 8:5ea891c7d1a1 | 63 | return DS28E15_22_25::CrcError; |
IanBenzMaxim | 7:9cd16581b578 | 64 | } |
IanBenzMaxim | 8:5ea891c7d1a1 | 65 | return none; |
IanBenzMaxim | 7:9cd16581b578 | 66 | } |
IanBenzMaxim | 7:9cd16581b578 | 67 | |
IanBenzMaxim | 7:9cd16581b578 | 68 | const int DS28E15_22_25::segmentsPerPage; |
IanBenzMaxim | 7:9cd16581b578 | 69 | |
IanBenzMaxim | 7:9cd16581b578 | 70 | DS28E15_22_25::AuthenticationData & |
IanBenzMaxim | 7:9cd16581b578 | 71 | DS28E15_22_25::AuthenticationData::setAnonymousRomId() { |
IanBenzMaxim | 7:9cd16581b578 | 72 | std::fill(romId().begin(), romId().end(), 0xFF); |
IanBenzMaxim | 7:9cd16581b578 | 73 | return *this; |
IanBenzMaxim | 7:9cd16581b578 | 74 | } |
IanBenzMaxim | 7:9cd16581b578 | 75 | |
IanBenzMaxim | 8:5ea891c7d1a1 | 76 | Result<void> |
IanBenzMaxim | 7:9cd16581b578 | 77 | DS28E15_22_25::writeCommandWithCrc(Command command, uint_least8_t parameter, |
IanBenzMaxim | 7:9cd16581b578 | 78 | OneWireMaster::Level level) const { |
IanBenzMaxim | 8:5ea891c7d1a1 | 79 | Result<void> result = selectRom(*master); |
IanBenzMaxim | 8:5ea891c7d1a1 | 80 | if (result) { |
IanBenzMaxim | 7:9cd16581b578 | 81 | const uint_least8_t data[] = {static_cast<uint_least8_t>(command), |
IanBenzMaxim | 7:9cd16581b578 | 82 | parameter}; |
IanBenzMaxim | 7:9cd16581b578 | 83 | result = writeDataWithCrc(*master, data, level); |
IanBenzMaxim | 7:9cd16581b578 | 84 | } |
IanBenzMaxim | 7:9cd16581b578 | 85 | return result; |
IanBenzMaxim | 7:9cd16581b578 | 86 | } |
IanBenzMaxim | 7:9cd16581b578 | 87 | |
IanBenzMaxim | 8:5ea891c7d1a1 | 88 | static Result<void> readDataWithCrc(OneWireMaster & master, |
IanBenzMaxim | 8:5ea891c7d1a1 | 89 | span<uint_least8_t> data) { |
IanBenzMaxim | 8:5ea891c7d1a1 | 90 | Result<void> result = master.readBlock(data); |
IanBenzMaxim | 8:5ea891c7d1a1 | 91 | if (!result) { |
IanBenzMaxim | 7:9cd16581b578 | 92 | return result; |
IanBenzMaxim | 7:9cd16581b578 | 93 | } |
IanBenzMaxim | 7:9cd16581b578 | 94 | uint_least8_t response[2]; |
IanBenzMaxim | 7:9cd16581b578 | 95 | result = master.readBlock(response); |
IanBenzMaxim | 8:5ea891c7d1a1 | 96 | if (!result) { |
IanBenzMaxim | 7:9cd16581b578 | 97 | return result; |
IanBenzMaxim | 7:9cd16581b578 | 98 | } |
IanBenzMaxim | 8:5ea891c7d1a1 | 99 | if (calculateCrc16(calculateCrc16(data), response) != 0xB001u) { |
IanBenzMaxim | 8:5ea891c7d1a1 | 100 | result = DS28E15_22_25::CrcError; |
IanBenzMaxim | 7:9cd16581b578 | 101 | } |
IanBenzMaxim | 7:9cd16581b578 | 102 | return result; |
IanBenzMaxim | 7:9cd16581b578 | 103 | } |
IanBenzMaxim | 7:9cd16581b578 | 104 | |
IanBenzMaxim | 8:5ea891c7d1a1 | 105 | static Result<void> readCsByte(OneWireMaster & master) { |
IanBenzMaxim | 8:5ea891c7d1a1 | 106 | const Result<uint_least8_t> response = master.readByte(); |
IanBenzMaxim | 8:5ea891c7d1a1 | 107 | if (!response) { |
IanBenzMaxim | 8:5ea891c7d1a1 | 108 | return response.error(); |
IanBenzMaxim | 7:9cd16581b578 | 109 | } |
IanBenzMaxim | 8:5ea891c7d1a1 | 110 | return (response.value() == 0xAA) ? makeResult(none) |
IanBenzMaxim | 8:5ea891c7d1a1 | 111 | : DS28E15_22_25::OperationFailure; |
IanBenzMaxim | 7:9cd16581b578 | 112 | } |
IanBenzMaxim | 7:9cd16581b578 | 113 | |
IanBenzMaxim | 8:5ea891c7d1a1 | 114 | static Result<void> releaseSequence(OneWireMaster & master, Sleep & sleep, |
IanBenzMaxim | 8:5ea891c7d1a1 | 115 | int delayTimeMs) { |
IanBenzMaxim | 8:5ea891c7d1a1 | 116 | Result<void> result = master.writeBytePower(0xAA); |
IanBenzMaxim | 8:5ea891c7d1a1 | 117 | if (!result) { |
IanBenzMaxim | 7:9cd16581b578 | 118 | return result; |
IanBenzMaxim | 7:9cd16581b578 | 119 | } |
IanBenzMaxim | 7:9cd16581b578 | 120 | sleep(delayTimeMs); |
IanBenzMaxim | 7:9cd16581b578 | 121 | result = master.setLevel(OneWireMaster::NormalLevel); |
IanBenzMaxim | 8:5ea891c7d1a1 | 122 | if (!result) { |
IanBenzMaxim | 7:9cd16581b578 | 123 | return result; |
IanBenzMaxim | 7:9cd16581b578 | 124 | } |
IanBenzMaxim | 7:9cd16581b578 | 125 | return readCsByte(master); |
IanBenzMaxim | 7:9cd16581b578 | 126 | } |
IanBenzMaxim | 7:9cd16581b578 | 127 | |
IanBenzMaxim | 7:9cd16581b578 | 128 | DS28E15_22_25::BlockProtection & |
IanBenzMaxim | 7:9cd16581b578 | 129 | DS28E15_22_25::BlockProtection::setBlockNum(int blockNum) { |
IanBenzMaxim | 7:9cd16581b578 | 130 | status &= ~blockNumMask; |
IanBenzMaxim | 7:9cd16581b578 | 131 | status |= (blockNum & blockNumMask); |
IanBenzMaxim | 7:9cd16581b578 | 132 | return *this; |
IanBenzMaxim | 7:9cd16581b578 | 133 | } |
IanBenzMaxim | 7:9cd16581b578 | 134 | |
IanBenzMaxim | 7:9cd16581b578 | 135 | bool DS28E15_22_25::BlockProtection::noProtection() const { |
IanBenzMaxim | 7:9cd16581b578 | 136 | return !readProtection() && !writeProtection() && !eepromEmulation() && |
IanBenzMaxim | 7:9cd16581b578 | 137 | !authProtection(); |
IanBenzMaxim | 7:9cd16581b578 | 138 | } |
IanBenzMaxim | 7:9cd16581b578 | 139 | |
IanBenzMaxim | 7:9cd16581b578 | 140 | DS28E15_22_25::BlockProtection & |
IanBenzMaxim | 7:9cd16581b578 | 141 | DS28E15_22_25::BlockProtection::setReadProtection(bool readProtection) { |
IanBenzMaxim | 7:9cd16581b578 | 142 | if (readProtection) { |
IanBenzMaxim | 7:9cd16581b578 | 143 | status |= readProtectionMask; |
IanBenzMaxim | 7:9cd16581b578 | 144 | } else { |
IanBenzMaxim | 7:9cd16581b578 | 145 | status &= ~readProtectionMask; |
IanBenzMaxim | 7:9cd16581b578 | 146 | } |
IanBenzMaxim | 7:9cd16581b578 | 147 | return *this; |
IanBenzMaxim | 7:9cd16581b578 | 148 | } |
IanBenzMaxim | 7:9cd16581b578 | 149 | |
IanBenzMaxim | 7:9cd16581b578 | 150 | DS28E15_22_25::BlockProtection & |
IanBenzMaxim | 7:9cd16581b578 | 151 | DS28E15_22_25::BlockProtection::setWriteProtection(bool writeProtection) { |
IanBenzMaxim | 7:9cd16581b578 | 152 | if (writeProtection) { |
IanBenzMaxim | 7:9cd16581b578 | 153 | status |= writeProtectionMask; |
IanBenzMaxim | 7:9cd16581b578 | 154 | } else { |
IanBenzMaxim | 7:9cd16581b578 | 155 | status &= ~writeProtectionMask; |
IanBenzMaxim | 7:9cd16581b578 | 156 | } |
IanBenzMaxim | 7:9cd16581b578 | 157 | return *this; |
IanBenzMaxim | 7:9cd16581b578 | 158 | } |
IanBenzMaxim | 7:9cd16581b578 | 159 | |
IanBenzMaxim | 7:9cd16581b578 | 160 | DS28E15_22_25::BlockProtection & |
IanBenzMaxim | 7:9cd16581b578 | 161 | DS28E15_22_25::BlockProtection::setEepromEmulation(bool eepromEmulation) { |
IanBenzMaxim | 7:9cd16581b578 | 162 | if (eepromEmulation) { |
IanBenzMaxim | 7:9cd16581b578 | 163 | status |= eepromEmulationMask; |
IanBenzMaxim | 7:9cd16581b578 | 164 | } else { |
IanBenzMaxim | 7:9cd16581b578 | 165 | status &= ~eepromEmulationMask; |
IanBenzMaxim | 7:9cd16581b578 | 166 | } |
IanBenzMaxim | 7:9cd16581b578 | 167 | return *this; |
IanBenzMaxim | 7:9cd16581b578 | 168 | } |
IanBenzMaxim | 7:9cd16581b578 | 169 | |
IanBenzMaxim | 7:9cd16581b578 | 170 | DS28E15_22_25::BlockProtection & |
IanBenzMaxim | 7:9cd16581b578 | 171 | DS28E15_22_25::BlockProtection::setAuthProtection(bool authProtection) { |
IanBenzMaxim | 7:9cd16581b578 | 172 | if (authProtection) { |
IanBenzMaxim | 7:9cd16581b578 | 173 | status |= authProtectionMask; |
IanBenzMaxim | 7:9cd16581b578 | 174 | } else { |
IanBenzMaxim | 7:9cd16581b578 | 175 | status &= ~authProtectionMask; |
IanBenzMaxim | 7:9cd16581b578 | 176 | } |
IanBenzMaxim | 7:9cd16581b578 | 177 | return *this; |
IanBenzMaxim | 7:9cd16581b578 | 178 | } |
IanBenzMaxim | 7:9cd16581b578 | 179 | |
IanBenzMaxim | 7:9cd16581b578 | 180 | DS28E15_22_25::ProtectionWriteMacData::ProtectionWriteMacData() |
IanBenzMaxim | 7:9cd16581b578 | 181 | : result_(), oldProtection_(), newProtection_() { |
IanBenzMaxim | 7:9cd16581b578 | 182 | setOldProtection(oldProtection_); |
IanBenzMaxim | 7:9cd16581b578 | 183 | setNewProtection(newProtection_); |
IanBenzMaxim | 7:9cd16581b578 | 184 | } |
IanBenzMaxim | 7:9cd16581b578 | 185 | |
IanBenzMaxim | 7:9cd16581b578 | 186 | DS28E15_22_25::ProtectionWriteMacData & |
IanBenzMaxim | 7:9cd16581b578 | 187 | DS28E15_22_25::ProtectionWriteMacData::setOldProtection( |
IanBenzMaxim | 7:9cd16581b578 | 188 | BlockProtection oldProtection) { |
IanBenzMaxim | 7:9cd16581b578 | 189 | result_[oldProtectionIdx] = oldProtection.authProtection() ? 1 : 0; |
IanBenzMaxim | 7:9cd16581b578 | 190 | result_[oldProtectionIdx + 1] = oldProtection.eepromEmulation() ? 1 : 0; |
IanBenzMaxim | 7:9cd16581b578 | 191 | result_[oldProtectionIdx + 2] = oldProtection.writeProtection() ? 1 : 0; |
IanBenzMaxim | 7:9cd16581b578 | 192 | result_[oldProtectionIdx + 3] = oldProtection.readProtection() ? 1 : 0; |
IanBenzMaxim | 7:9cd16581b578 | 193 | oldProtection_ = oldProtection; |
IanBenzMaxim | 7:9cd16581b578 | 194 | return *this; |
IanBenzMaxim | 7:9cd16581b578 | 195 | } |
IanBenzMaxim | 7:9cd16581b578 | 196 | |
IanBenzMaxim | 7:9cd16581b578 | 197 | DS28E15_22_25::ProtectionWriteMacData & |
IanBenzMaxim | 7:9cd16581b578 | 198 | DS28E15_22_25::ProtectionWriteMacData::setNewProtection( |
IanBenzMaxim | 7:9cd16581b578 | 199 | BlockProtection newProtection) { |
IanBenzMaxim | 7:9cd16581b578 | 200 | result_[blockNumIdx] = newProtection.blockNum(); |
IanBenzMaxim | 7:9cd16581b578 | 201 | result_[newProtectionIdx] = newProtection.authProtection() ? 1 : 0; |
IanBenzMaxim | 7:9cd16581b578 | 202 | result_[newProtectionIdx + 1] = newProtection.eepromEmulation() ? 1 : 0; |
IanBenzMaxim | 7:9cd16581b578 | 203 | result_[newProtectionIdx + 2] = newProtection.writeProtection() ? 1 : 0; |
IanBenzMaxim | 7:9cd16581b578 | 204 | result_[newProtectionIdx + 3] = newProtection.readProtection() ? 1 : 0; |
IanBenzMaxim | 7:9cd16581b578 | 205 | newProtection_ = newProtection; |
IanBenzMaxim | 7:9cd16581b578 | 206 | return *this; |
IanBenzMaxim | 7:9cd16581b578 | 207 | } |
IanBenzMaxim | 7:9cd16581b578 | 208 | |
IanBenzMaxim | 8:5ea891c7d1a1 | 209 | Result<void> |
IanBenzMaxim | 7:9cd16581b578 | 210 | DS28E15_22_25::writeAuthBlockProtection(BlockProtection newProtection, |
IanBenzMaxim | 7:9cd16581b578 | 211 | Page::const_span mac) { |
IanBenzMaxim | 8:5ea891c7d1a1 | 212 | Result<void> result = |
IanBenzMaxim | 7:9cd16581b578 | 213 | writeCommandWithCrc(AuthWriteBlockProtection, newProtection.statusByte(), |
IanBenzMaxim | 7:9cd16581b578 | 214 | OneWireMaster::StrongLevel); |
IanBenzMaxim | 8:5ea891c7d1a1 | 215 | if (!result) { |
IanBenzMaxim | 7:9cd16581b578 | 216 | return result; |
IanBenzMaxim | 7:9cd16581b578 | 217 | } |
IanBenzMaxim | 7:9cd16581b578 | 218 | |
IanBenzMaxim | 7:9cd16581b578 | 219 | sleep->invoke(shaComputationDelayMs); |
IanBenzMaxim | 7:9cd16581b578 | 220 | result = master->setLevel(OneWireMaster::NormalLevel); |
IanBenzMaxim | 8:5ea891c7d1a1 | 221 | if (!result) { |
IanBenzMaxim | 7:9cd16581b578 | 222 | return result; |
IanBenzMaxim | 7:9cd16581b578 | 223 | } |
IanBenzMaxim | 7:9cd16581b578 | 224 | |
IanBenzMaxim | 7:9cd16581b578 | 225 | result = writeDataWithCrc(*master, mac); |
IanBenzMaxim | 8:5ea891c7d1a1 | 226 | if (!result) { |
IanBenzMaxim | 7:9cd16581b578 | 227 | return result; |
IanBenzMaxim | 7:9cd16581b578 | 228 | } |
IanBenzMaxim | 7:9cd16581b578 | 229 | |
IanBenzMaxim | 7:9cd16581b578 | 230 | result = readCsByte(*master); |
IanBenzMaxim | 8:5ea891c7d1a1 | 231 | if (!result) { |
IanBenzMaxim | 7:9cd16581b578 | 232 | return result; |
IanBenzMaxim | 7:9cd16581b578 | 233 | } |
IanBenzMaxim | 7:9cd16581b578 | 234 | |
IanBenzMaxim | 7:9cd16581b578 | 235 | result = releaseSequence(*master, *sleep, eepromWriteDelayMs); |
IanBenzMaxim | 7:9cd16581b578 | 236 | return result; |
IanBenzMaxim | 7:9cd16581b578 | 237 | } |
IanBenzMaxim | 7:9cd16581b578 | 238 | |
IanBenzMaxim | 8:5ea891c7d1a1 | 239 | Result<void> DS28E15_22_25::writeBlockProtection(BlockProtection protection) { |
IanBenzMaxim | 8:5ea891c7d1a1 | 240 | Result<void> result = |
IanBenzMaxim | 7:9cd16581b578 | 241 | writeCommandWithCrc(WriteBlockProtection, protection.statusByte()); |
IanBenzMaxim | 8:5ea891c7d1a1 | 242 | if (!result) { |
IanBenzMaxim | 7:9cd16581b578 | 243 | return result; |
IanBenzMaxim | 7:9cd16581b578 | 244 | } |
IanBenzMaxim | 7:9cd16581b578 | 245 | |
IanBenzMaxim | 7:9cd16581b578 | 246 | result = releaseSequence(*master, *sleep, eepromWriteDelayMs); |
IanBenzMaxim | 7:9cd16581b578 | 247 | return result; |
IanBenzMaxim | 7:9cd16581b578 | 248 | } |
IanBenzMaxim | 7:9cd16581b578 | 249 | |
IanBenzMaxim | 8:5ea891c7d1a1 | 250 | Result<DS28E15_22_25::BlockProtection> |
IanBenzMaxim | 8:5ea891c7d1a1 | 251 | DS28E15_22_25::doReadBlockProtection(int blockNum, Variant variant) const { |
IanBenzMaxim | 7:9cd16581b578 | 252 | uint_least8_t buffer = blockNum; |
IanBenzMaxim | 7:9cd16581b578 | 253 | if (variant == DS28E22 || variant == DS28E25) { |
IanBenzMaxim | 7:9cd16581b578 | 254 | buffer *= ds28e22_25_pagesPerBlock; |
IanBenzMaxim | 7:9cd16581b578 | 255 | } |
IanBenzMaxim | 8:5ea891c7d1a1 | 256 | TRY(writeCommandWithCrc(ReadStatus, buffer)); |
IanBenzMaxim | 8:5ea891c7d1a1 | 257 | TRY_VALUE(buffer, master->readByte()); |
IanBenzMaxim | 8:5ea891c7d1a1 | 258 | return BlockProtection(buffer); |
IanBenzMaxim | 7:9cd16581b578 | 259 | } |
IanBenzMaxim | 7:9cd16581b578 | 260 | |
IanBenzMaxim | 8:5ea891c7d1a1 | 261 | Result<DS28E15_22_25::Page::array> |
IanBenzMaxim | 8:5ea891c7d1a1 | 262 | DS28E15_22_25::computeReadPageMac(int page_num, bool anon) const { |
IanBenzMaxim | 8:5ea891c7d1a1 | 263 | Result<void> result = |
IanBenzMaxim | 7:9cd16581b578 | 264 | writeCommandWithCrc(ComputePageMac, (anon ? 0xE0 : 0x00) | page_num, |
IanBenzMaxim | 7:9cd16581b578 | 265 | OneWireMaster::StrongLevel); |
IanBenzMaxim | 8:5ea891c7d1a1 | 266 | if (!result) { |
IanBenzMaxim | 8:5ea891c7d1a1 | 267 | return result.error(); |
IanBenzMaxim | 7:9cd16581b578 | 268 | } |
IanBenzMaxim | 7:9cd16581b578 | 269 | |
IanBenzMaxim | 7:9cd16581b578 | 270 | sleep->invoke(shaComputationDelayMs * 2); |
IanBenzMaxim | 7:9cd16581b578 | 271 | result = master->setLevel(OneWireMaster::NormalLevel); |
IanBenzMaxim | 8:5ea891c7d1a1 | 272 | if (!result) { |
IanBenzMaxim | 8:5ea891c7d1a1 | 273 | return result.error(); |
IanBenzMaxim | 7:9cd16581b578 | 274 | } |
IanBenzMaxim | 7:9cd16581b578 | 275 | |
IanBenzMaxim | 7:9cd16581b578 | 276 | result = readCsByte(*master); |
IanBenzMaxim | 8:5ea891c7d1a1 | 277 | if (!result) { |
IanBenzMaxim | 8:5ea891c7d1a1 | 278 | return result.error(); |
IanBenzMaxim | 7:9cd16581b578 | 279 | } |
IanBenzMaxim | 7:9cd16581b578 | 280 | |
IanBenzMaxim | 8:5ea891c7d1a1 | 281 | Page::array mac; |
IanBenzMaxim | 7:9cd16581b578 | 282 | result = readDataWithCrc(*master, mac); |
IanBenzMaxim | 8:5ea891c7d1a1 | 283 | if (!result) { |
IanBenzMaxim | 8:5ea891c7d1a1 | 284 | return result.error(); |
IanBenzMaxim | 8:5ea891c7d1a1 | 285 | } |
IanBenzMaxim | 8:5ea891c7d1a1 | 286 | |
IanBenzMaxim | 8:5ea891c7d1a1 | 287 | return mac; |
IanBenzMaxim | 7:9cd16581b578 | 288 | } |
IanBenzMaxim | 7:9cd16581b578 | 289 | |
IanBenzMaxim | 8:5ea891c7d1a1 | 290 | Result<void> DS28E15_22_25::doComputeSecret(int page_num, bool lock, |
IanBenzMaxim | 8:5ea891c7d1a1 | 291 | bool lowPower) { |
IanBenzMaxim | 8:5ea891c7d1a1 | 292 | Result<void> result = writeCommandWithCrc( |
IanBenzMaxim | 8:5ea891c7d1a1 | 293 | ComputeAndLockSecret, lock ? (0xE0 | page_num) : page_num); |
IanBenzMaxim | 8:5ea891c7d1a1 | 294 | if (!result) { |
IanBenzMaxim | 7:9cd16581b578 | 295 | return result; |
IanBenzMaxim | 7:9cd16581b578 | 296 | } |
IanBenzMaxim | 7:9cd16581b578 | 297 | |
IanBenzMaxim | 7:9cd16581b578 | 298 | result = releaseSequence(*master, *sleep, |
IanBenzMaxim | 7:9cd16581b578 | 299 | shaComputationDelayMs * 2 + |
IanBenzMaxim | 7:9cd16581b578 | 300 | secretEepromWriteDelayMs(lowPower)); |
IanBenzMaxim | 7:9cd16581b578 | 301 | return result; |
IanBenzMaxim | 7:9cd16581b578 | 302 | } |
IanBenzMaxim | 7:9cd16581b578 | 303 | |
IanBenzMaxim | 8:5ea891c7d1a1 | 304 | Result<void> DS28E15_22_25::doWriteScratchpad(Page::const_span data, |
IanBenzMaxim | 8:5ea891c7d1a1 | 305 | Variant variant) { |
IanBenzMaxim | 7:9cd16581b578 | 306 | const uint_least8_t parameter = |
IanBenzMaxim | 7:9cd16581b578 | 307 | (variant == DS28E22 || variant == DS28E25) ? 0x20 : 0x00; |
IanBenzMaxim | 8:5ea891c7d1a1 | 308 | Result<void> result = writeCommandWithCrc(ReadWriteScratchpad, parameter); |
IanBenzMaxim | 8:5ea891c7d1a1 | 309 | if (!result) { |
IanBenzMaxim | 7:9cd16581b578 | 310 | return result; |
IanBenzMaxim | 7:9cd16581b578 | 311 | } |
IanBenzMaxim | 7:9cd16581b578 | 312 | |
IanBenzMaxim | 7:9cd16581b578 | 313 | result = writeDataWithCrc(*master, data); |
IanBenzMaxim | 7:9cd16581b578 | 314 | return result; |
IanBenzMaxim | 7:9cd16581b578 | 315 | } |
IanBenzMaxim | 7:9cd16581b578 | 316 | |
IanBenzMaxim | 8:5ea891c7d1a1 | 317 | Result<DS28E15_22_25::Page::array> |
IanBenzMaxim | 8:5ea891c7d1a1 | 318 | DS28E15_22_25::doReadScratchpad(Variant variant) const { |
IanBenzMaxim | 7:9cd16581b578 | 319 | const uint_least8_t parameter = |
IanBenzMaxim | 7:9cd16581b578 | 320 | (variant == DS28E22 || variant == DS28E25) ? 0x2F : 0x0F; |
IanBenzMaxim | 8:5ea891c7d1a1 | 321 | Result<void> result = writeCommandWithCrc(ReadWriteScratchpad, parameter); |
IanBenzMaxim | 8:5ea891c7d1a1 | 322 | if (!result) { |
IanBenzMaxim | 8:5ea891c7d1a1 | 323 | return result.error(); |
IanBenzMaxim | 7:9cd16581b578 | 324 | } |
IanBenzMaxim | 7:9cd16581b578 | 325 | |
IanBenzMaxim | 8:5ea891c7d1a1 | 326 | Page::array data; |
IanBenzMaxim | 7:9cd16581b578 | 327 | result = readDataWithCrc(*master, data); |
IanBenzMaxim | 8:5ea891c7d1a1 | 328 | if (!result) { |
IanBenzMaxim | 8:5ea891c7d1a1 | 329 | return result.error(); |
IanBenzMaxim | 8:5ea891c7d1a1 | 330 | } |
IanBenzMaxim | 8:5ea891c7d1a1 | 331 | |
IanBenzMaxim | 8:5ea891c7d1a1 | 332 | return data; |
IanBenzMaxim | 7:9cd16581b578 | 333 | } |
IanBenzMaxim | 7:9cd16581b578 | 334 | |
IanBenzMaxim | 8:5ea891c7d1a1 | 335 | Result<void> DS28E15_22_25::doLoadSecret(bool lock, bool lowPower) { |
IanBenzMaxim | 8:5ea891c7d1a1 | 336 | Result<void> result = |
IanBenzMaxim | 7:9cd16581b578 | 337 | writeCommandWithCrc(LoadAndLockSecret, lock ? 0xE0 : 0x00); |
IanBenzMaxim | 8:5ea891c7d1a1 | 338 | if (!result) { |
IanBenzMaxim | 7:9cd16581b578 | 339 | return result; |
IanBenzMaxim | 7:9cd16581b578 | 340 | } |
IanBenzMaxim | 7:9cd16581b578 | 341 | |
IanBenzMaxim | 7:9cd16581b578 | 342 | result = releaseSequence(*master, *sleep, secretEepromWriteDelayMs(lowPower)); |
IanBenzMaxim | 7:9cd16581b578 | 343 | return result; |
IanBenzMaxim | 7:9cd16581b578 | 344 | } |
IanBenzMaxim | 7:9cd16581b578 | 345 | |
IanBenzMaxim | 8:5ea891c7d1a1 | 346 | Result<DS28E15_22_25::Page::array> DS28E15_22_25::readPage(int page) const { |
IanBenzMaxim | 8:5ea891c7d1a1 | 347 | const Result<void> result = writeCommandWithCrc(ReadMemory, page); |
IanBenzMaxim | 8:5ea891c7d1a1 | 348 | if (!result) { |
IanBenzMaxim | 8:5ea891c7d1a1 | 349 | return result.error(); |
IanBenzMaxim | 7:9cd16581b578 | 350 | } |
IanBenzMaxim | 7:9cd16581b578 | 351 | |
IanBenzMaxim | 8:5ea891c7d1a1 | 352 | return continueReadPage(); |
IanBenzMaxim | 7:9cd16581b578 | 353 | } |
IanBenzMaxim | 7:9cd16581b578 | 354 | |
IanBenzMaxim | 8:5ea891c7d1a1 | 355 | Result<DS28E15_22_25::Page::array> DS28E15_22_25::continueReadPage() const { |
IanBenzMaxim | 8:5ea891c7d1a1 | 356 | Page::array rdbuf; |
IanBenzMaxim | 8:5ea891c7d1a1 | 357 | TRY(readDataWithCrc(*master, rdbuf)); |
IanBenzMaxim | 8:5ea891c7d1a1 | 358 | return rdbuf; |
IanBenzMaxim | 7:9cd16581b578 | 359 | } |
IanBenzMaxim | 7:9cd16581b578 | 360 | |
IanBenzMaxim | 8:5ea891c7d1a1 | 361 | Result<void> DS28E15_22_25::doWriteAuthSegment(Segment::const_span newData, |
IanBenzMaxim | 8:5ea891c7d1a1 | 362 | Page::const_span mac, |
IanBenzMaxim | 8:5ea891c7d1a1 | 363 | Variant variant, |
IanBenzMaxim | 8:5ea891c7d1a1 | 364 | bool continuing) { |
IanBenzMaxim | 7:9cd16581b578 | 365 | // CRC gets calculated with CS byte when continuing on DS28E22 and DS28E25. |
IanBenzMaxim | 7:9cd16581b578 | 366 | const uint_fast16_t crcStart = |
IanBenzMaxim | 7:9cd16581b578 | 367 | ((variant == DS28E22 || variant == DS28E25) && continuing) |
IanBenzMaxim | 7:9cd16581b578 | 368 | ? calculateCrc16(0xAA) |
IanBenzMaxim | 7:9cd16581b578 | 369 | : 0; |
IanBenzMaxim | 8:5ea891c7d1a1 | 370 | Result<void> result = |
IanBenzMaxim | 7:9cd16581b578 | 371 | writeDataWithCrc(*master, newData, OneWireMaster::StrongLevel, crcStart); |
IanBenzMaxim | 8:5ea891c7d1a1 | 372 | if (!result) { |
IanBenzMaxim | 7:9cd16581b578 | 373 | return result; |
IanBenzMaxim | 7:9cd16581b578 | 374 | } |
IanBenzMaxim | 7:9cd16581b578 | 375 | |
IanBenzMaxim | 7:9cd16581b578 | 376 | sleep->invoke(shaComputationDelayMs); |
IanBenzMaxim | 7:9cd16581b578 | 377 | result = master->setLevel(OneWireMaster::NormalLevel); |
IanBenzMaxim | 8:5ea891c7d1a1 | 378 | if (!result) { |
IanBenzMaxim | 7:9cd16581b578 | 379 | return result; |
IanBenzMaxim | 7:9cd16581b578 | 380 | } |
IanBenzMaxim | 7:9cd16581b578 | 381 | |
IanBenzMaxim | 7:9cd16581b578 | 382 | result = writeDataWithCrc(*master, mac); |
IanBenzMaxim | 8:5ea891c7d1a1 | 383 | if (!result) { |
IanBenzMaxim | 7:9cd16581b578 | 384 | return result; |
IanBenzMaxim | 7:9cd16581b578 | 385 | } |
IanBenzMaxim | 7:9cd16581b578 | 386 | |
IanBenzMaxim | 7:9cd16581b578 | 387 | result = readCsByte(*master); |
IanBenzMaxim | 8:5ea891c7d1a1 | 388 | if (!result) { |
IanBenzMaxim | 7:9cd16581b578 | 389 | return result; |
IanBenzMaxim | 7:9cd16581b578 | 390 | } |
IanBenzMaxim | 7:9cd16581b578 | 391 | |
IanBenzMaxim | 7:9cd16581b578 | 392 | result = releaseSequence(*master, *sleep, eepromWriteDelayMs); |
IanBenzMaxim | 7:9cd16581b578 | 393 | return result; |
IanBenzMaxim | 7:9cd16581b578 | 394 | } |
IanBenzMaxim | 7:9cd16581b578 | 395 | |
IanBenzMaxim | 8:5ea891c7d1a1 | 396 | Result<void> DS28E15_22_25::doWriteAuthSegment(int pageNum, int segmentNum, |
IanBenzMaxim | 8:5ea891c7d1a1 | 397 | Segment::const_span newData, |
IanBenzMaxim | 8:5ea891c7d1a1 | 398 | Page::const_span mac, |
IanBenzMaxim | 8:5ea891c7d1a1 | 399 | Variant variant) { |
IanBenzMaxim | 8:5ea891c7d1a1 | 400 | Result<void> result = |
IanBenzMaxim | 7:9cd16581b578 | 401 | writeCommandWithCrc(AuthWriteMemory, (segmentNum << 5) | pageNum); |
IanBenzMaxim | 8:5ea891c7d1a1 | 402 | if (!result) { |
IanBenzMaxim | 7:9cd16581b578 | 403 | return result; |
IanBenzMaxim | 7:9cd16581b578 | 404 | } |
IanBenzMaxim | 7:9cd16581b578 | 405 | |
IanBenzMaxim | 7:9cd16581b578 | 406 | result = doWriteAuthSegment(newData, mac, variant, false); |
IanBenzMaxim | 7:9cd16581b578 | 407 | return result; |
IanBenzMaxim | 7:9cd16581b578 | 408 | } |
IanBenzMaxim | 7:9cd16581b578 | 409 | |
IanBenzMaxim | 8:5ea891c7d1a1 | 410 | Result<void> DS28E15_22_25::doContinueWriteAuthSegment( |
IanBenzMaxim | 7:9cd16581b578 | 411 | Segment::const_span newData, Page::const_span mac, Variant variant) { |
IanBenzMaxim | 7:9cd16581b578 | 412 | return doWriteAuthSegment(newData, mac, variant, true); |
IanBenzMaxim | 7:9cd16581b578 | 413 | } |
IanBenzMaxim | 7:9cd16581b578 | 414 | |
IanBenzMaxim | 8:5ea891c7d1a1 | 415 | Result<DS28E15_22_25::Segment::array> |
IanBenzMaxim | 8:5ea891c7d1a1 | 416 | DS28E15_22_25::readSegment(int page, int segment) const { |
IanBenzMaxim | 8:5ea891c7d1a1 | 417 | Result<void> result = writeCommandWithCrc(ReadMemory, (segment << 5) | page); |
IanBenzMaxim | 8:5ea891c7d1a1 | 418 | if (!result) { |
IanBenzMaxim | 8:5ea891c7d1a1 | 419 | return result.error(); |
IanBenzMaxim | 7:9cd16581b578 | 420 | } |
IanBenzMaxim | 7:9cd16581b578 | 421 | |
IanBenzMaxim | 8:5ea891c7d1a1 | 422 | return continueReadSegment(); |
IanBenzMaxim | 7:9cd16581b578 | 423 | } |
IanBenzMaxim | 7:9cd16581b578 | 424 | |
IanBenzMaxim | 8:5ea891c7d1a1 | 425 | Result<DS28E15_22_25::Segment::array> |
IanBenzMaxim | 8:5ea891c7d1a1 | 426 | DS28E15_22_25::continueReadSegment() const { |
IanBenzMaxim | 8:5ea891c7d1a1 | 427 | Segment::array data; |
IanBenzMaxim | 8:5ea891c7d1a1 | 428 | TRY(master->readBlock(data)); |
IanBenzMaxim | 8:5ea891c7d1a1 | 429 | return data; |
IanBenzMaxim | 7:9cd16581b578 | 430 | } |
IanBenzMaxim | 7:9cd16581b578 | 431 | |
IanBenzMaxim | 8:5ea891c7d1a1 | 432 | Result<void> DS28E15_22_25::writeSegment(int page, int block, |
IanBenzMaxim | 8:5ea891c7d1a1 | 433 | Segment::const_span data) { |
IanBenzMaxim | 8:5ea891c7d1a1 | 434 | Result<void> result = writeCommandWithCrc(WriteMemory, (block << 5) | page); |
IanBenzMaxim | 8:5ea891c7d1a1 | 435 | if (!result) { |
IanBenzMaxim | 7:9cd16581b578 | 436 | return result; |
IanBenzMaxim | 7:9cd16581b578 | 437 | } |
IanBenzMaxim | 7:9cd16581b578 | 438 | |
IanBenzMaxim | 7:9cd16581b578 | 439 | result = continueWriteSegment(data); |
IanBenzMaxim | 7:9cd16581b578 | 440 | return result; |
IanBenzMaxim | 7:9cd16581b578 | 441 | } |
IanBenzMaxim | 7:9cd16581b578 | 442 | |
IanBenzMaxim | 8:5ea891c7d1a1 | 443 | Result<void> DS28E15_22_25::continueWriteSegment(Segment::const_span data) { |
IanBenzMaxim | 8:5ea891c7d1a1 | 444 | Result<void> result = writeDataWithCrc(*master, data); |
IanBenzMaxim | 8:5ea891c7d1a1 | 445 | if (!result) { |
IanBenzMaxim | 7:9cd16581b578 | 446 | return result; |
IanBenzMaxim | 7:9cd16581b578 | 447 | } |
IanBenzMaxim | 7:9cd16581b578 | 448 | |
IanBenzMaxim | 7:9cd16581b578 | 449 | result = releaseSequence(*master, *sleep, eepromWriteDelayMs); |
IanBenzMaxim | 7:9cd16581b578 | 450 | return result; |
IanBenzMaxim | 7:9cd16581b578 | 451 | } |
IanBenzMaxim | 7:9cd16581b578 | 452 | |
IanBenzMaxim | 8:5ea891c7d1a1 | 453 | Result<void> |
IanBenzMaxim | 7:9cd16581b578 | 454 | DS28E15_22_25::doReadAllBlockProtection(span<BlockProtection> protection, |
IanBenzMaxim | 7:9cd16581b578 | 455 | Variant variant) const { |
IanBenzMaxim | 8:5ea891c7d1a1 | 456 | Result<void> result = writeCommandWithCrc(ReadStatus, 0); |
IanBenzMaxim | 7:9cd16581b578 | 457 | if (!result) { |
IanBenzMaxim | 7:9cd16581b578 | 458 | if (variant == DS28E22 || variant == DS28E25) { |
IanBenzMaxim | 7:9cd16581b578 | 459 | // Need to read extra data on DS28E22 to get CRC16. |
IanBenzMaxim | 7:9cd16581b578 | 460 | uint_least8_t buf[DS28E25::memoryPages]; |
IanBenzMaxim | 7:9cd16581b578 | 461 | result = readDataWithCrc(*master, buf); |
IanBenzMaxim | 7:9cd16581b578 | 462 | if (!result) { |
IanBenzMaxim | 7:9cd16581b578 | 463 | const int blocks = ((variant == DS28E22) ? DS28E22::memoryPages |
IanBenzMaxim | 7:9cd16581b578 | 464 | : DS28E25::memoryPages) / |
IanBenzMaxim | 7:9cd16581b578 | 465 | ds28e22_25_pagesPerBlock; |
IanBenzMaxim | 7:9cd16581b578 | 466 | for (span<BlockProtection>::index_type i = 0; |
IanBenzMaxim | 7:9cd16581b578 | 467 | i < std::min<span<BlockProtection>::index_type>(protection.size(), |
IanBenzMaxim | 7:9cd16581b578 | 468 | blocks); |
IanBenzMaxim | 7:9cd16581b578 | 469 | ++i) { |
IanBenzMaxim | 7:9cd16581b578 | 470 | protection[i].setStatusByte( |
IanBenzMaxim | 7:9cd16581b578 | 471 | (buf[i * ds28e22_25_pagesPerBlock] & 0xF0) | // Upper nibble |
IanBenzMaxim | 7:9cd16581b578 | 472 | ((buf[i * ds28e22_25_pagesPerBlock] & 0x0F) / |
IanBenzMaxim | 7:9cd16581b578 | 473 | ds28e22_25_pagesPerBlock)); // Lower nibble |
IanBenzMaxim | 7:9cd16581b578 | 474 | } |
IanBenzMaxim | 7:9cd16581b578 | 475 | } |
IanBenzMaxim | 7:9cd16581b578 | 476 | } else { // DS28E15 |
IanBenzMaxim | 7:9cd16581b578 | 477 | uint_least8_t buf[DS28E15::protectionBlocks]; |
IanBenzMaxim | 7:9cd16581b578 | 478 | result = readDataWithCrc(*master, buf); |
IanBenzMaxim | 7:9cd16581b578 | 479 | if (!result) { |
IanBenzMaxim | 7:9cd16581b578 | 480 | for (span<BlockProtection>::index_type i = 0; |
IanBenzMaxim | 7:9cd16581b578 | 481 | i < std::min<span<BlockProtection>::index_type>( |
IanBenzMaxim | 7:9cd16581b578 | 482 | protection.size(), DS28E15::protectionBlocks); |
IanBenzMaxim | 7:9cd16581b578 | 483 | ++i) { |
IanBenzMaxim | 7:9cd16581b578 | 484 | protection[i].setStatusByte(buf[i]); |
IanBenzMaxim | 7:9cd16581b578 | 485 | } |
IanBenzMaxim | 7:9cd16581b578 | 486 | } |
IanBenzMaxim | 7:9cd16581b578 | 487 | } |
IanBenzMaxim | 7:9cd16581b578 | 488 | } |
IanBenzMaxim | 7:9cd16581b578 | 489 | return result; |
IanBenzMaxim | 7:9cd16581b578 | 490 | } |
IanBenzMaxim | 7:9cd16581b578 | 491 | |
IanBenzMaxim | 7:9cd16581b578 | 492 | const error_category & DS28E15_22_25::errorCategory() { |
IanBenzMaxim | 7:9cd16581b578 | 493 | static class : public error_category { |
IanBenzMaxim | 7:9cd16581b578 | 494 | public: |
IanBenzMaxim | 7:9cd16581b578 | 495 | virtual const char * name() const { return "DS28E15_22_25"; } |
IanBenzMaxim | 7:9cd16581b578 | 496 | |
IanBenzMaxim | 7:9cd16581b578 | 497 | virtual std::string message(int condition) const { |
IanBenzMaxim | 7:9cd16581b578 | 498 | switch (condition) { |
IanBenzMaxim | 7:9cd16581b578 | 499 | case CrcError: |
IanBenzMaxim | 7:9cd16581b578 | 500 | return "CRC Error"; |
IanBenzMaxim | 7:9cd16581b578 | 501 | |
IanBenzMaxim | 7:9cd16581b578 | 502 | case OperationFailure: |
IanBenzMaxim | 7:9cd16581b578 | 503 | return "Operation Failure"; |
IanBenzMaxim | 7:9cd16581b578 | 504 | } |
IanBenzMaxim | 8:5ea891c7d1a1 | 505 | return defaultErrorMessage(condition); |
IanBenzMaxim | 7:9cd16581b578 | 506 | } |
IanBenzMaxim | 7:9cd16581b578 | 507 | } instance; |
IanBenzMaxim | 7:9cd16581b578 | 508 | return instance; |
IanBenzMaxim | 7:9cd16581b578 | 509 | } |
IanBenzMaxim | 7:9cd16581b578 | 510 | |
IanBenzMaxim | 8:5ea891c7d1a1 | 511 | Result<void> DS28E15_22_25::loadSecret(bool lock) { |
IanBenzMaxim | 7:9cd16581b578 | 512 | // Use worst-case low power timing if the device type is not known. |
IanBenzMaxim | 7:9cd16581b578 | 513 | return doLoadSecret(lock, true); |
IanBenzMaxim | 7:9cd16581b578 | 514 | } |
IanBenzMaxim | 7:9cd16581b578 | 515 | |
IanBenzMaxim | 8:5ea891c7d1a1 | 516 | Result<void> DS28E15_22_25::computeSecret(int pageNum, bool lock) { |
IanBenzMaxim | 7:9cd16581b578 | 517 | // Use worst-case low power timing if the device type is not known. |
IanBenzMaxim | 7:9cd16581b578 | 518 | return doComputeSecret(pageNum, lock, true); |
IanBenzMaxim | 7:9cd16581b578 | 519 | } |
IanBenzMaxim | 7:9cd16581b578 | 520 | |
IanBenzMaxim | 8:5ea891c7d1a1 | 521 | Result<DS28E15_22_25::Personality> DS28E15_22_25::readPersonality() const { |
IanBenzMaxim | 8:5ea891c7d1a1 | 522 | Result<void> result = writeCommandWithCrc(ReadStatus, 0xE0); |
IanBenzMaxim | 8:5ea891c7d1a1 | 523 | if (!result) { |
IanBenzMaxim | 8:5ea891c7d1a1 | 524 | return result.error(); |
IanBenzMaxim | 8:5ea891c7d1a1 | 525 | } |
IanBenzMaxim | 8:5ea891c7d1a1 | 526 | |
IanBenzMaxim | 8:5ea891c7d1a1 | 527 | uint_least8_t data[4]; |
IanBenzMaxim | 8:5ea891c7d1a1 | 528 | result = readDataWithCrc(*master, data); |
IanBenzMaxim | 7:9cd16581b578 | 529 | if (!result) { |
IanBenzMaxim | 8:5ea891c7d1a1 | 530 | return result.error(); |
IanBenzMaxim | 7:9cd16581b578 | 531 | } |
IanBenzMaxim | 8:5ea891c7d1a1 | 532 | |
IanBenzMaxim | 8:5ea891c7d1a1 | 533 | Personality personality; |
IanBenzMaxim | 8:5ea891c7d1a1 | 534 | personality.PB1 = data[0]; |
IanBenzMaxim | 8:5ea891c7d1a1 | 535 | personality.PB2 = data[1]; |
IanBenzMaxim | 8:5ea891c7d1a1 | 536 | personality.manId[0] = data[2]; |
IanBenzMaxim | 8:5ea891c7d1a1 | 537 | personality.manId[1] = data[3]; |
IanBenzMaxim | 8:5ea891c7d1a1 | 538 | return personality; |
IanBenzMaxim | 7:9cd16581b578 | 539 | } |
IanBenzMaxim | 7:9cd16581b578 | 540 | |
IanBenzMaxim | 7:9cd16581b578 | 541 | const int DS28EL15::memoryPages; |
IanBenzMaxim | 7:9cd16581b578 | 542 | const int DS28EL15::protectionBlocks; |
IanBenzMaxim | 7:9cd16581b578 | 543 | |
IanBenzMaxim | 8:5ea891c7d1a1 | 544 | Result<void> DS28EL15::writeScratchpad(Page::const_span data) { |
IanBenzMaxim | 7:9cd16581b578 | 545 | return doWriteScratchpad(data, DS28E15); |
IanBenzMaxim | 7:9cd16581b578 | 546 | } |
IanBenzMaxim | 7:9cd16581b578 | 547 | |
IanBenzMaxim | 8:5ea891c7d1a1 | 548 | Result<DS28E15_22_25::Page::array> DS28EL15::readScratchpad() const { |
IanBenzMaxim | 8:5ea891c7d1a1 | 549 | return doReadScratchpad(DS28E15); |
IanBenzMaxim | 7:9cd16581b578 | 550 | } |
IanBenzMaxim | 7:9cd16581b578 | 551 | |
IanBenzMaxim | 8:5ea891c7d1a1 | 552 | Result<DS28E15_22_25::BlockProtection> |
IanBenzMaxim | 8:5ea891c7d1a1 | 553 | DS28EL15::readBlockProtection(int blockNum) const { |
IanBenzMaxim | 8:5ea891c7d1a1 | 554 | return doReadBlockProtection(blockNum, DS28E15); |
IanBenzMaxim | 7:9cd16581b578 | 555 | } |
IanBenzMaxim | 7:9cd16581b578 | 556 | |
IanBenzMaxim | 8:5ea891c7d1a1 | 557 | Result<void> DS28EL15::writeAuthSegment(int pageNum, int segmentNum, |
IanBenzMaxim | 8:5ea891c7d1a1 | 558 | Segment::const_span newData, |
IanBenzMaxim | 8:5ea891c7d1a1 | 559 | Page::const_span mac) { |
IanBenzMaxim | 7:9cd16581b578 | 560 | return doWriteAuthSegment(pageNum, segmentNum, newData, mac, DS28E15); |
IanBenzMaxim | 7:9cd16581b578 | 561 | } |
IanBenzMaxim | 7:9cd16581b578 | 562 | |
IanBenzMaxim | 8:5ea891c7d1a1 | 563 | Result<void> DS28EL15::continueWriteAuthSegment(Segment::const_span newData, |
IanBenzMaxim | 8:5ea891c7d1a1 | 564 | Page::const_span mac) { |
IanBenzMaxim | 7:9cd16581b578 | 565 | return doContinueWriteAuthSegment(newData, mac, DS28E15); |
IanBenzMaxim | 7:9cd16581b578 | 566 | } |
IanBenzMaxim | 7:9cd16581b578 | 567 | |
IanBenzMaxim | 8:5ea891c7d1a1 | 568 | Result<array<DS28E15_22_25::BlockProtection, DS28E15::protectionBlocks> > |
IanBenzMaxim | 8:5ea891c7d1a1 | 569 | DS28EL15::readAllBlockProtection() const { |
IanBenzMaxim | 8:5ea891c7d1a1 | 570 | array<BlockProtection, protectionBlocks> protection; |
IanBenzMaxim | 8:5ea891c7d1a1 | 571 | TRY(doReadAllBlockProtection(protection, DS28E15)); |
IanBenzMaxim | 8:5ea891c7d1a1 | 572 | return protection; |
IanBenzMaxim | 7:9cd16581b578 | 573 | } |
IanBenzMaxim | 7:9cd16581b578 | 574 | |
IanBenzMaxim | 8:5ea891c7d1a1 | 575 | Result<void> DS28E15::loadSecret(bool lock) { |
IanBenzMaxim | 8:5ea891c7d1a1 | 576 | return doLoadSecret(lock, false); |
IanBenzMaxim | 8:5ea891c7d1a1 | 577 | } |
IanBenzMaxim | 7:9cd16581b578 | 578 | |
IanBenzMaxim | 8:5ea891c7d1a1 | 579 | Result<void> DS28E15::computeSecret(int pageNum, bool lock) { |
IanBenzMaxim | 7:9cd16581b578 | 580 | return doComputeSecret(pageNum, lock, false); |
IanBenzMaxim | 7:9cd16581b578 | 581 | } |
IanBenzMaxim | 7:9cd16581b578 | 582 | |
IanBenzMaxim | 7:9cd16581b578 | 583 | const int DS28EL22::memoryPages; |
IanBenzMaxim | 7:9cd16581b578 | 584 | const int DS28EL22::protectionBlocks; |
IanBenzMaxim | 7:9cd16581b578 | 585 | |
IanBenzMaxim | 8:5ea891c7d1a1 | 586 | Result<void> DS28EL22::writeScratchpad(Page::const_span data) { |
IanBenzMaxim | 7:9cd16581b578 | 587 | return doWriteScratchpad(data, DS28E22); |
IanBenzMaxim | 7:9cd16581b578 | 588 | } |
IanBenzMaxim | 7:9cd16581b578 | 589 | |
IanBenzMaxim | 8:5ea891c7d1a1 | 590 | Result<DS28E15_22_25::Page::array> DS28EL22::readScratchpad() const { |
IanBenzMaxim | 8:5ea891c7d1a1 | 591 | return doReadScratchpad(DS28E22); |
IanBenzMaxim | 7:9cd16581b578 | 592 | } |
IanBenzMaxim | 7:9cd16581b578 | 593 | |
IanBenzMaxim | 8:5ea891c7d1a1 | 594 | Result<DS28E15_22_25::BlockProtection> |
IanBenzMaxim | 8:5ea891c7d1a1 | 595 | DS28EL22::readBlockProtection(int blockNum) const { |
IanBenzMaxim | 8:5ea891c7d1a1 | 596 | return doReadBlockProtection(blockNum, DS28E22); |
IanBenzMaxim | 7:9cd16581b578 | 597 | } |
IanBenzMaxim | 7:9cd16581b578 | 598 | |
IanBenzMaxim | 8:5ea891c7d1a1 | 599 | Result<void> DS28EL22::writeAuthSegment(int pageNum, int segmentNum, |
IanBenzMaxim | 8:5ea891c7d1a1 | 600 | Segment::const_span newData, |
IanBenzMaxim | 8:5ea891c7d1a1 | 601 | Page::const_span mac) { |
IanBenzMaxim | 7:9cd16581b578 | 602 | return doWriteAuthSegment(pageNum, segmentNum, newData, mac, DS28E22); |
IanBenzMaxim | 7:9cd16581b578 | 603 | } |
IanBenzMaxim | 7:9cd16581b578 | 604 | |
IanBenzMaxim | 8:5ea891c7d1a1 | 605 | Result<void> DS28EL22::continueWriteAuthSegment(Segment::const_span newData, |
IanBenzMaxim | 8:5ea891c7d1a1 | 606 | Page::const_span mac) { |
IanBenzMaxim | 7:9cd16581b578 | 607 | return doContinueWriteAuthSegment(newData, mac, DS28E22); |
IanBenzMaxim | 7:9cd16581b578 | 608 | } |
IanBenzMaxim | 7:9cd16581b578 | 609 | |
IanBenzMaxim | 8:5ea891c7d1a1 | 610 | Result<array<DS28E15_22_25::BlockProtection, DS28E22::protectionBlocks> > |
IanBenzMaxim | 8:5ea891c7d1a1 | 611 | DS28EL22::readAllBlockProtection() const { |
IanBenzMaxim | 8:5ea891c7d1a1 | 612 | array<BlockProtection, protectionBlocks> protection; |
IanBenzMaxim | 8:5ea891c7d1a1 | 613 | TRY(doReadAllBlockProtection(protection, DS28E22)); |
IanBenzMaxim | 8:5ea891c7d1a1 | 614 | return protection; |
IanBenzMaxim | 7:9cd16581b578 | 615 | } |
IanBenzMaxim | 7:9cd16581b578 | 616 | |
IanBenzMaxim | 8:5ea891c7d1a1 | 617 | Result<void> DS28E22::loadSecret(bool lock) { |
IanBenzMaxim | 8:5ea891c7d1a1 | 618 | return doLoadSecret(lock, false); |
IanBenzMaxim | 8:5ea891c7d1a1 | 619 | } |
IanBenzMaxim | 7:9cd16581b578 | 620 | |
IanBenzMaxim | 8:5ea891c7d1a1 | 621 | Result<void> DS28E22::computeSecret(int pageNum, bool lock) { |
IanBenzMaxim | 7:9cd16581b578 | 622 | return doComputeSecret(pageNum, lock, false); |
IanBenzMaxim | 7:9cd16581b578 | 623 | } |
IanBenzMaxim | 7:9cd16581b578 | 624 | |
IanBenzMaxim | 7:9cd16581b578 | 625 | const int DS28EL25::memoryPages; |
IanBenzMaxim | 7:9cd16581b578 | 626 | const int DS28EL25::protectionBlocks; |
IanBenzMaxim | 7:9cd16581b578 | 627 | |
IanBenzMaxim | 8:5ea891c7d1a1 | 628 | Result<void> DS28EL25::writeScratchpad(Page::const_span data) { |
IanBenzMaxim | 7:9cd16581b578 | 629 | return doWriteScratchpad(data, DS28E25); |
IanBenzMaxim | 7:9cd16581b578 | 630 | } |
IanBenzMaxim | 7:9cd16581b578 | 631 | |
IanBenzMaxim | 8:5ea891c7d1a1 | 632 | Result<DS28E15_22_25::Page::array> DS28EL25::readScratchpad() const { |
IanBenzMaxim | 8:5ea891c7d1a1 | 633 | return doReadScratchpad(DS28E25); |
IanBenzMaxim | 7:9cd16581b578 | 634 | } |
IanBenzMaxim | 7:9cd16581b578 | 635 | |
IanBenzMaxim | 8:5ea891c7d1a1 | 636 | Result<DS28E15_22_25::BlockProtection> |
IanBenzMaxim | 8:5ea891c7d1a1 | 637 | DS28EL25::readBlockProtection(int blockNum) const { |
IanBenzMaxim | 8:5ea891c7d1a1 | 638 | return doReadBlockProtection(blockNum, DS28E25); |
IanBenzMaxim | 7:9cd16581b578 | 639 | } |
IanBenzMaxim | 7:9cd16581b578 | 640 | |
IanBenzMaxim | 8:5ea891c7d1a1 | 641 | Result<void> DS28EL25::writeAuthSegment(int pageNum, int segmentNum, |
IanBenzMaxim | 8:5ea891c7d1a1 | 642 | Segment::const_span newData, |
IanBenzMaxim | 8:5ea891c7d1a1 | 643 | Page::const_span mac) { |
IanBenzMaxim | 7:9cd16581b578 | 644 | return doWriteAuthSegment(pageNum, segmentNum, newData, mac, DS28E25); |
IanBenzMaxim | 7:9cd16581b578 | 645 | } |
IanBenzMaxim | 7:9cd16581b578 | 646 | |
IanBenzMaxim | 8:5ea891c7d1a1 | 647 | Result<void> DS28EL25::continueWriteAuthSegment(Segment::const_span newData, |
IanBenzMaxim | 8:5ea891c7d1a1 | 648 | Page::const_span mac) { |
IanBenzMaxim | 7:9cd16581b578 | 649 | return doContinueWriteAuthSegment(newData, mac, DS28E25); |
IanBenzMaxim | 7:9cd16581b578 | 650 | } |
IanBenzMaxim | 7:9cd16581b578 | 651 | |
IanBenzMaxim | 8:5ea891c7d1a1 | 652 | Result<array<DS28E15_22_25::BlockProtection, DS28E25::protectionBlocks> > |
IanBenzMaxim | 8:5ea891c7d1a1 | 653 | DS28EL25::readAllBlockProtection() const { |
IanBenzMaxim | 8:5ea891c7d1a1 | 654 | array<BlockProtection, protectionBlocks> protection; |
IanBenzMaxim | 8:5ea891c7d1a1 | 655 | TRY(doReadAllBlockProtection(protection, DS28E25)); |
IanBenzMaxim | 8:5ea891c7d1a1 | 656 | return protection; |
IanBenzMaxim | 7:9cd16581b578 | 657 | } |
IanBenzMaxim | 7:9cd16581b578 | 658 | |
IanBenzMaxim | 8:5ea891c7d1a1 | 659 | Result<void> DS28E25::loadSecret(bool lock) { |
IanBenzMaxim | 8:5ea891c7d1a1 | 660 | return doLoadSecret(lock, false); |
IanBenzMaxim | 8:5ea891c7d1a1 | 661 | } |
IanBenzMaxim | 7:9cd16581b578 | 662 | |
IanBenzMaxim | 8:5ea891c7d1a1 | 663 | Result<void> DS28E25::computeSecret(int pageNum, bool lock) { |
IanBenzMaxim | 7:9cd16581b578 | 664 | return doComputeSecret(pageNum, lock, false); |
IanBenzMaxim | 7:9cd16581b578 | 665 | } |
IanBenzMaxim | 7:9cd16581b578 | 666 | |
IanBenzMaxim | 7:9cd16581b578 | 667 | } // namespace MaximInterfaceDevices |