1-Wire® library for mbed. Complete 1-Wire library that supports our silicon masters along with a bit-bang master on the MAX32600MBED platform with one common interface for mbed. Slave support has also been included and more slaves will be added as time permits.

Dependents:   MAXREFDES131_Qt_Demo MAX32630FTHR_iButton_uSD_Logger MAX32630FTHR_DS18B20_uSD_Logger MAXREFDES130_131_Demo ... more

Superseded by MaximInterface.

Committer:
IanBenzMaxim
Date:
Fri May 13 14:52:50 2016 -0500
Revision:
75:8b627804927c
Parent:
74:23be10c32fa3
Child:
76:84e6c4994e29
Code cleanup.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
IanBenzMaxim 75:8b627804927c 1 /******************************************************************//**
IanBenzMaxim 75:8b627804927c 2 * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved.
IanBenzMaxim 75:8b627804927c 3 *
IanBenzMaxim 75:8b627804927c 4 * Permission is hereby granted, free of charge, to any person obtaining a
IanBenzMaxim 75:8b627804927c 5 * copy of this software and associated documentation files (the "Software"),
IanBenzMaxim 75:8b627804927c 6 * to deal in the Software without restriction, including without limitation
IanBenzMaxim 75:8b627804927c 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
IanBenzMaxim 75:8b627804927c 8 * and/or sell copies of the Software, and to permit persons to whom the
IanBenzMaxim 75:8b627804927c 9 * Software is furnished to do so, subject to the following conditions:
IanBenzMaxim 75:8b627804927c 10 *
IanBenzMaxim 75:8b627804927c 11 * The above copyright notice and this permission notice shall be included
IanBenzMaxim 75:8b627804927c 12 * in all copies or substantial portions of the Software.
IanBenzMaxim 75:8b627804927c 13 *
IanBenzMaxim 75:8b627804927c 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
IanBenzMaxim 75:8b627804927c 15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
IanBenzMaxim 75:8b627804927c 16 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IanBenzMaxim 75:8b627804927c 17 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
IanBenzMaxim 75:8b627804927c 18 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
IanBenzMaxim 75:8b627804927c 19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
IanBenzMaxim 75:8b627804927c 20 * OTHER DEALINGS IN THE SOFTWARE.
IanBenzMaxim 75:8b627804927c 21 *
IanBenzMaxim 75:8b627804927c 22 * Except as contained in this notice, the name of Maxim Integrated
IanBenzMaxim 75:8b627804927c 23 * Products, Inc. shall not be used except as stated in the Maxim Integrated
IanBenzMaxim 75:8b627804927c 24 * Products, Inc. Branding Policy.
IanBenzMaxim 75:8b627804927c 25 *
IanBenzMaxim 75:8b627804927c 26 * The mere transfer of this software does not imply any licenses
IanBenzMaxim 75:8b627804927c 27 * of trade secrets, proprietary technology, copyrights, patents,
IanBenzMaxim 75:8b627804927c 28 * trademarks, maskwork rights, or any other form of intellectual
IanBenzMaxim 75:8b627804927c 29 * property whatsoever. Maxim Integrated Products, Inc. retains all
IanBenzMaxim 75:8b627804927c 30 * ownership rights.
IanBenzMaxim 75:8b627804927c 31 **********************************************************************/
IanBenzMaxim 25:bdb1c5a53b58 32
IanBenzMaxim 73:2cecc1372acc 33 #ifndef OneWire_Authenticators_DS28E15_22_25
IanBenzMaxim 73:2cecc1372acc 34 #define OneWire_Authenticators_DS28E15_22_25
IanBenzMaxim 25:bdb1c5a53b58 35
IanBenzMaxim 73:2cecc1372acc 36 #include "array.h"
IanBenzMaxim 73:2cecc1372acc 37 #include "Authenticators/ISha256MacCoproc.h"
IanBenzMaxim 73:2cecc1372acc 38 #include "OneWireSlave.h"
IanBenzMaxim 73:2cecc1372acc 39
IanBenzMaxim 73:2cecc1372acc 40 namespace OneWire
IanBenzMaxim 73:2cecc1372acc 41 {
IanBenzMaxim 74:23be10c32fa3 42 namespace Masters { class OneWireMaster; }
IanBenzMaxim 25:bdb1c5a53b58 43
IanBenzMaxim 74:23be10c32fa3 44 namespace Authenticators
IanBenzMaxim 74:23be10c32fa3 45 {
IanBenzMaxim 74:23be10c32fa3 46 /// Interface to the DS28E15/22/25 series of authenticators
IanBenzMaxim 74:23be10c32fa3 47 /// including low power variants.
IanBenzMaxim 74:23be10c32fa3 48 class DS28E15_22_25 : public OneWireSlave
IanBenzMaxim 73:2cecc1372acc 49 {
IanBenzMaxim 74:23be10c32fa3 50 public:
IanBenzMaxim 74:23be10c32fa3 51 /// Family code for each device.
IanBenzMaxim 74:23be10c32fa3 52 enum FamilyCode
IanBenzMaxim 74:23be10c32fa3 53 {
IanBenzMaxim 75:8b627804927c 54 DS28E25_Family = 0x47,
IanBenzMaxim 75:8b627804927c 55 DS28E22_Family = 0x48,
IanBenzMaxim 75:8b627804927c 56 DS28E15_Family = 0x17,
IanBenzMaxim 75:8b627804927c 57 Unknown_Family = 0
IanBenzMaxim 74:23be10c32fa3 58 };
IanBenzMaxim 74:23be10c32fa3 59
IanBenzMaxim 74:23be10c32fa3 60 /// Number for memory pages for each device.
IanBenzMaxim 74:23be10c32fa3 61 enum MemoryPages
IanBenzMaxim 74:23be10c32fa3 62 {
IanBenzMaxim 75:8b627804927c 63 DS28E25_Pages = 16,
IanBenzMaxim 75:8b627804927c 64 DS28E22_Pages = 8,
IanBenzMaxim 75:8b627804927c 65 DS28E15_Pages = 2,
IanBenzMaxim 75:8b627804927c 66 Unknown_Pages = 0
IanBenzMaxim 74:23be10c32fa3 67 };
IanBenzMaxim 74:23be10c32fa3 68
IanBenzMaxim 74:23be10c32fa3 69 /// Number of protection blocks for each device.
IanBenzMaxim 74:23be10c32fa3 70 enum ProtectionBlocks
IanBenzMaxim 74:23be10c32fa3 71 {
IanBenzMaxim 75:8b627804927c 72 DS28E25_Blocks = 8,
IanBenzMaxim 75:8b627804927c 73 DS28E22_Blocks = 4,
IanBenzMaxim 75:8b627804927c 74 DS28E15_Blocks = 4,
IanBenzMaxim 75:8b627804927c 75 Unknown_Blocks = 0
IanBenzMaxim 74:23be10c32fa3 76 };
IanBenzMaxim 74:23be10c32fa3 77
IanBenzMaxim 74:23be10c32fa3 78 /// Holds the contents of a device memory segment.
IanBenzMaxim 74:23be10c32fa3 79 typedef array<uint8_t, 4> Segment;
IanBenzMaxim 74:23be10c32fa3 80
IanBenzMaxim 74:23be10c32fa3 81 /// Holds the contents of a device memory page.
IanBenzMaxim 74:23be10c32fa3 82 class Page
IanBenzMaxim 74:23be10c32fa3 83 {
IanBenzMaxim 74:23be10c32fa3 84 public:
IanBenzMaxim 74:23be10c32fa3 85 /// Length of the buffer in bytes.
IanBenzMaxim 74:23be10c32fa3 86 static const size_t length = 32;
IanBenzMaxim 74:23be10c32fa3 87
IanBenzMaxim 74:23be10c32fa3 88 private:
IanBenzMaxim 74:23be10c32fa3 89 array<uint8_t, length> m_data;
IanBenzMaxim 74:23be10c32fa3 90
IanBenzMaxim 74:23be10c32fa3 91 public:
IanBenzMaxim 74:23be10c32fa3 92 /// Built-in array representation.
IanBenzMaxim 74:23be10c32fa3 93 typedef array<uint8_t, length>::Buffer Buffer;
IanBenzMaxim 74:23be10c32fa3 94
IanBenzMaxim 74:23be10c32fa3 95 Page() { }
IanBenzMaxim 74:23be10c32fa3 96 Page(const Page & page) : m_data(page.m_data) { }
IanBenzMaxim 74:23be10c32fa3 97 Page(const Buffer & dataBytes) : m_data(dataBytes) { }
IanBenzMaxim 74:23be10c32fa3 98
IanBenzMaxim 74:23be10c32fa3 99 const Page & operator=(const Page & rhs)
IanBenzMaxim 74:23be10c32fa3 100 {
IanBenzMaxim 74:23be10c32fa3 101 this->m_data = rhs.m_data;
IanBenzMaxim 74:23be10c32fa3 102 return rhs;
IanBenzMaxim 74:23be10c32fa3 103 }
IanBenzMaxim 74:23be10c32fa3 104
IanBenzMaxim 74:23be10c32fa3 105 bool operator==(const Page & rhs) const
IanBenzMaxim 74:23be10c32fa3 106 {
IanBenzMaxim 74:23be10c32fa3 107 return (this->m_data == rhs.m_data);
IanBenzMaxim 74:23be10c32fa3 108 }
IanBenzMaxim 74:23be10c32fa3 109
IanBenzMaxim 74:23be10c32fa3 110 bool operator!=(const Page & rhs) const
IanBenzMaxim 74:23be10c32fa3 111 {
IanBenzMaxim 74:23be10c32fa3 112 return !operator==(rhs);
IanBenzMaxim 74:23be10c32fa3 113 }
IanBenzMaxim 74:23be10c32fa3 114
IanBenzMaxim 74:23be10c32fa3 115 /// Conversion to array reference.
IanBenzMaxim 74:23be10c32fa3 116 operator Buffer &()
IanBenzMaxim 74:23be10c32fa3 117 {
IanBenzMaxim 74:23be10c32fa3 118 return m_data;
IanBenzMaxim 74:23be10c32fa3 119 }
IanBenzMaxim 74:23be10c32fa3 120
IanBenzMaxim 74:23be10c32fa3 121 /// Conversion to const array reference.
IanBenzMaxim 74:23be10c32fa3 122 operator const Buffer &() const
IanBenzMaxim 74:23be10c32fa3 123 {
IanBenzMaxim 74:23be10c32fa3 124 return m_data;
IanBenzMaxim 74:23be10c32fa3 125 }
IanBenzMaxim 74:23be10c32fa3 126
IanBenzMaxim 74:23be10c32fa3 127 /// Creates a segment representation from a subsection of the page data.
IanBenzMaxim 74:23be10c32fa3 128 /// @param segmentNum Segment number within page to copy from.
IanBenzMaxim 74:23be10c32fa3 129 /// @returns The copied segment data.
IanBenzMaxim 74:23be10c32fa3 130 Segment toSegment(unsigned int segmentNum) const;
IanBenzMaxim 74:23be10c32fa3 131
IanBenzMaxim 74:23be10c32fa3 132 /// Copies segment data to the page.
IanBenzMaxim 74:23be10c32fa3 133 /// @param segmentNum Segment number within the page to copy to.
IanBenzMaxim 74:23be10c32fa3 134 /// @param[in] segment Segment to copy from.
IanBenzMaxim 74:23be10c32fa3 135 void fromSegment(unsigned int segmentNum, const Segment & segment);
IanBenzMaxim 74:23be10c32fa3 136 };
IanBenzMaxim 74:23be10c32fa3 137
IanBenzMaxim 74:23be10c32fa3 138 /// Holds the contents of the device scratchpad.
IanBenzMaxim 74:23be10c32fa3 139 typedef array<uint8_t, 32> Scratchpad;
IanBenzMaxim 74:23be10c32fa3 140
IanBenzMaxim 74:23be10c32fa3 141 /// Container for a SHA-256 MAC.
IanBenzMaxim 74:23be10c32fa3 142 typedef array<uint8_t, 32> Mac;
IanBenzMaxim 74:23be10c32fa3 143
IanBenzMaxim 74:23be10c32fa3 144 /// Container for a manufacturer ID.
IanBenzMaxim 74:23be10c32fa3 145 typedef array<uint8_t, 2> ManId;
IanBenzMaxim 74:23be10c32fa3 146
IanBenzMaxim 74:23be10c32fa3 147 /// Container for the device personality.
IanBenzMaxim 74:23be10c32fa3 148 union Personality
IanBenzMaxim 74:23be10c32fa3 149 {
IanBenzMaxim 74:23be10c32fa3 150 array<uint8_t, 4>::Buffer bytes;
IanBenzMaxim 74:23be10c32fa3 151 struct
IanBenzMaxim 74:23be10c32fa3 152 {
IanBenzMaxim 74:23be10c32fa3 153 uint8_t PB1;
IanBenzMaxim 74:23be10c32fa3 154 uint8_t PB2;
IanBenzMaxim 74:23be10c32fa3 155 ManId::Buffer manIdBytes;
IanBenzMaxim 74:23be10c32fa3 156
IanBenzMaxim 74:23be10c32fa3 157 bool secretLocked() const { return (PB2 & 0x01); }
IanBenzMaxim 74:23be10c32fa3 158 } fields;
IanBenzMaxim 74:23be10c32fa3 159 };
IanBenzMaxim 74:23be10c32fa3 160
IanBenzMaxim 74:23be10c32fa3 161 /// Represents the status of a memory protection block.
IanBenzMaxim 74:23be10c32fa3 162 class BlockProtection
IanBenzMaxim 74:23be10c32fa3 163 {
IanBenzMaxim 74:23be10c32fa3 164 private:
IanBenzMaxim 74:23be10c32fa3 165 static const uint8_t readProtectionMask = 0x80, writeProtectionMask = 0x40, eepromEmulationMask = 0x20, authProtectionMask = 0x10, blockNumMask = 0x0F;
IanBenzMaxim 74:23be10c32fa3 166 uint8_t m_status;
IanBenzMaxim 74:23be10c32fa3 167
IanBenzMaxim 74:23be10c32fa3 168 public:
IanBenzMaxim 74:23be10c32fa3 169 BlockProtection() : m_status(0x00) { }
IanBenzMaxim 74:23be10c32fa3 170 BlockProtection(bool readProtection, bool writeProtection, bool eepromEmulation, bool authProtection, uint8_t blockNum);
IanBenzMaxim 74:23be10c32fa3 171
IanBenzMaxim 74:23be10c32fa3 172 /// Get the byte representation used by the device.
IanBenzMaxim 74:23be10c32fa3 173 uint8_t statusByte() const { return m_status; }
IanBenzMaxim 74:23be10c32fa3 174 /// Set the byte representation used by the device.
IanBenzMaxim 74:23be10c32fa3 175 void setStatusByte(uint8_t status) { m_status = status; }
IanBenzMaxim 74:23be10c32fa3 176
IanBenzMaxim 74:23be10c32fa3 177 /// Get the Block Number which is indexed from zero.
IanBenzMaxim 74:23be10c32fa3 178 uint8_t blockNum() const { return (m_status & blockNumMask); }
IanBenzMaxim 74:23be10c32fa3 179 /// Set the Block Number which is indexed from zero.
IanBenzMaxim 74:23be10c32fa3 180 void setBlockNum(uint8_t blockNum);
IanBenzMaxim 74:23be10c32fa3 181
IanBenzMaxim 74:23be10c32fa3 182 /// Get the Read Protection status.
IanBenzMaxim 74:23be10c32fa3 183 /// @returns True if Read Protection is enabled.
IanBenzMaxim 74:23be10c32fa3 184 bool readProtection() const { return ((m_status & readProtectionMask) == readProtectionMask); }
IanBenzMaxim 74:23be10c32fa3 185 /// Set the Read Protection status.
IanBenzMaxim 74:23be10c32fa3 186 void setReadProtection(bool readProtection);
IanBenzMaxim 74:23be10c32fa3 187
IanBenzMaxim 74:23be10c32fa3 188 /// Get the Write Protection status.
IanBenzMaxim 74:23be10c32fa3 189 /// @returns True if Write Protection is enabled.
IanBenzMaxim 74:23be10c32fa3 190 bool writeProtection() const { return ((m_status & writeProtectionMask) == writeProtectionMask); }
IanBenzMaxim 74:23be10c32fa3 191 /// Set the Write Protection status.
IanBenzMaxim 74:23be10c32fa3 192 void setWriteProtection(bool writeProtection);
IanBenzMaxim 74:23be10c32fa3 193
IanBenzMaxim 74:23be10c32fa3 194 /// Get the EEPROM Emulation Mode status.
IanBenzMaxim 74:23be10c32fa3 195 /// @returns True if EEPROM Emulation Mode is enabled.
IanBenzMaxim 74:23be10c32fa3 196 bool eepromEmulation() const { return ((m_status & eepromEmulationMask) == eepromEmulationMask); }
IanBenzMaxim 74:23be10c32fa3 197 /// Set the EEPROM Emulation Mode status.
IanBenzMaxim 74:23be10c32fa3 198 void setEepromEmulation(bool eepromEmulation);
IanBenzMaxim 74:23be10c32fa3 199
IanBenzMaxim 74:23be10c32fa3 200 /// Get the Authentication Protection status.
IanBenzMaxim 74:23be10c32fa3 201 /// @returns True if Authentication Protection is enabled.
IanBenzMaxim 74:23be10c32fa3 202 bool authProtection() const { return ((m_status & authProtectionMask) == authProtectionMask); }
IanBenzMaxim 74:23be10c32fa3 203 /// Set the Authentication Protection status.
IanBenzMaxim 74:23be10c32fa3 204 void setAuthProtection(bool authProtection);
IanBenzMaxim 74:23be10c32fa3 205
IanBenzMaxim 74:23be10c32fa3 206 /// Check if no protection options are enabled.
IanBenzMaxim 74:23be10c32fa3 207 /// @returns True if no protection options are enabled.
IanBenzMaxim 74:23be10c32fa3 208 bool noProtection() const;
IanBenzMaxim 74:23be10c32fa3 209
IanBenzMaxim 74:23be10c32fa3 210 bool operator==(const BlockProtection & rhs) const { return (this->m_status == rhs.m_status); }
IanBenzMaxim 74:23be10c32fa3 211 bool operator!=(const BlockProtection & rhs) const { return !operator==(rhs); }
IanBenzMaxim 74:23be10c32fa3 212 };
IanBenzMaxim 74:23be10c32fa3 213
IanBenzMaxim 74:23be10c32fa3 214 /// Compute the MAC for an Authenticated Write to a memory segment.
IanBenzMaxim 74:23be10c32fa3 215 /// @param MacCoproc Coprocessor with Slave Secret to use for the computation.
IanBenzMaxim 74:23be10c32fa3 216 /// @param pageNum Page number for write operation.
IanBenzMaxim 74:23be10c32fa3 217 /// @param segmentNum Segment number within page for write operation.
IanBenzMaxim 74:23be10c32fa3 218 /// @param[in] newData New data to write to the segment.
IanBenzMaxim 74:23be10c32fa3 219 /// @param[in] oldData Existing data contained in the segment.
IanBenzMaxim 74:23be10c32fa3 220 /// @param[in] romId 1-Wire ROM ID of the device.
IanBenzMaxim 74:23be10c32fa3 221 /// @param[in] manId Manufacturer ID of the device.
IanBenzMaxim 74:23be10c32fa3 222 /// @param[out] mac The computed MAC.
IanBenzMaxim 74:23be10c32fa3 223 /// @returns The result code indicated by the coprocessor.
IanBenzMaxim 74:23be10c32fa3 224 static ISha256MacCoproc::CmdResult computeSegmentWriteMac(const ISha256MacCoproc & MacCoproc,
IanBenzMaxim 74:23be10c32fa3 225 unsigned int pageNum,
IanBenzMaxim 74:23be10c32fa3 226 unsigned int segmentNum,
IanBenzMaxim 74:23be10c32fa3 227 const Segment & newData,
IanBenzMaxim 74:23be10c32fa3 228 const Segment & oldData,
IanBenzMaxim 74:23be10c32fa3 229 const RomId & romId,
IanBenzMaxim 74:23be10c32fa3 230 const ManId & manId,
IanBenzMaxim 74:23be10c32fa3 231 Mac & mac);
IanBenzMaxim 74:23be10c32fa3 232
IanBenzMaxim 74:23be10c32fa3 233 /// Compute the MAC for an Authenticated Write to a memory protection block.
IanBenzMaxim 74:23be10c32fa3 234 /// @param MacCoproc Coprocessor with Slave Secret to use for the operation.
IanBenzMaxim 74:23be10c32fa3 235 /// @param[in] newProtection New protection status to write.
IanBenzMaxim 74:23be10c32fa3 236 /// @param[in] oldProtection Existing protection status in device.
IanBenzMaxim 74:23be10c32fa3 237 /// @param[in] romId 1-Wire ROM ID of the device.
IanBenzMaxim 74:23be10c32fa3 238 /// @param[in] manId Manufacturer ID of the device.
IanBenzMaxim 74:23be10c32fa3 239 /// @param[out] mac The computed MAC.
IanBenzMaxim 74:23be10c32fa3 240 /// @returns The result code indicated by the coprocessor.
IanBenzMaxim 74:23be10c32fa3 241 static ISha256MacCoproc::CmdResult computeProtectionWriteMac(const ISha256MacCoproc & MacCoproc,
IanBenzMaxim 74:23be10c32fa3 242 const BlockProtection & newProtection,
IanBenzMaxim 74:23be10c32fa3 243 const BlockProtection & oldProtection,
IanBenzMaxim 74:23be10c32fa3 244 const RomId & romId,
IanBenzMaxim 74:23be10c32fa3 245 const ManId & manId,
IanBenzMaxim 74:23be10c32fa3 246 Mac & mac);
IanBenzMaxim 34:11fffbe98ef9 247
IanBenzMaxim 74:23be10c32fa3 248 /// Compute the next secret from the existing secret.
IanBenzMaxim 74:23be10c32fa3 249 /// @param MacCoproc Coprocessor with Master Secret to use for the operation.
IanBenzMaxim 74:23be10c32fa3 250 /// Slave Secret will be updated with the computation result.
IanBenzMaxim 74:23be10c32fa3 251 /// @param[in] bindingPage Binding data from a device memory page.
IanBenzMaxim 74:23be10c32fa3 252 /// @param bindingPageNum Number of the page where the binding data is from.
IanBenzMaxim 74:23be10c32fa3 253 /// @param[in] partialSecret Partial secret data from the device scratchpad.
IanBenzMaxim 74:23be10c32fa3 254 /// @param[in] romId 1-Wire ROM ID of the device.
IanBenzMaxim 74:23be10c32fa3 255 /// @param[in] manId Manufacturer ID of the device.
IanBenzMaxim 74:23be10c32fa3 256 /// @returns The result code indicated by the coprocessor.
IanBenzMaxim 74:23be10c32fa3 257 static ISha256MacCoproc::CmdResult computeNextSecret(ISha256MacCoproc & MacCoproc,
IanBenzMaxim 74:23be10c32fa3 258 const Page & bindingPage,
IanBenzMaxim 74:23be10c32fa3 259 unsigned int bindingPageNum,
IanBenzMaxim 74:23be10c32fa3 260 const Scratchpad & partialSecret,
IanBenzMaxim 74:23be10c32fa3 261 const RomId & romId,
IanBenzMaxim 74:23be10c32fa3 262 const ManId & manId);
IanBenzMaxim 74:23be10c32fa3 263
IanBenzMaxim 74:23be10c32fa3 264 /// Compute a Page MAC for authentication.
IanBenzMaxim 74:23be10c32fa3 265 /// @param MacCoproc Coprocessor with Slave Secret to use for the operation.
IanBenzMaxim 74:23be10c32fa3 266 /// @param[in] pageData Data from a device memory page.
IanBenzMaxim 74:23be10c32fa3 267 /// @param pageNum Number of the page to use data from.
IanBenzMaxim 74:23be10c32fa3 268 /// @param[in] challenge Random challenge to prevent replay attacks.
IanBenzMaxim 74:23be10c32fa3 269 /// @param[in] romId 1-Wire ROM ID of the device.
IanBenzMaxim 74:23be10c32fa3 270 /// @param[in] manId Manufacturer ID of the device.
IanBenzMaxim 74:23be10c32fa3 271 /// @param[out] mac The computed MAC.
IanBenzMaxim 74:23be10c32fa3 272 static ISha256MacCoproc::CmdResult computeAuthMac(const ISha256MacCoproc & MacCoproc,
IanBenzMaxim 74:23be10c32fa3 273 const Page & pageData,
IanBenzMaxim 74:23be10c32fa3 274 unsigned int pageNum,
IanBenzMaxim 74:23be10c32fa3 275 const Scratchpad & challenge,
IanBenzMaxim 74:23be10c32fa3 276 const RomId & romId,
IanBenzMaxim 74:23be10c32fa3 277 const ManId & manId,
IanBenzMaxim 74:23be10c32fa3 278 Mac & mac);
IanBenzMaxim 74:23be10c32fa3 279
IanBenzMaxim 74:23be10c32fa3 280 /// Compute a Page MAC for authentication using anonymous mode.
IanBenzMaxim 74:23be10c32fa3 281 /// @param MacCoproc Coprocessor with Slave Secret to use for the operation.
IanBenzMaxim 74:23be10c32fa3 282 /// @param[in] pageData Data from a device memory page.
IanBenzMaxim 74:23be10c32fa3 283 /// @param pageNum Number of the page to use data from.
IanBenzMaxim 74:23be10c32fa3 284 /// @param[in] challenge Random challenge to prevent replay attacks.
IanBenzMaxim 74:23be10c32fa3 285 /// @param[in] manId Manufacturer ID of the device.
IanBenzMaxim 74:23be10c32fa3 286 /// @param[out] mac The computed MAC.
IanBenzMaxim 74:23be10c32fa3 287 static ISha256MacCoproc::CmdResult computeAuthMacAnon(const ISha256MacCoproc & MacCoproc,
IanBenzMaxim 74:23be10c32fa3 288 const Page & pageData,
IanBenzMaxim 74:23be10c32fa3 289 unsigned int pageNum,
IanBenzMaxim 74:23be10c32fa3 290 const Scratchpad & challenge,
IanBenzMaxim 74:23be10c32fa3 291 const ManId & manId,
IanBenzMaxim 74:23be10c32fa3 292 Mac & mac);
IanBenzMaxim 74:23be10c32fa3 293
IanBenzMaxim 74:23be10c32fa3 294 /// Number of segments per page.
IanBenzMaxim 74:23be10c32fa3 295 static const unsigned int segmentsPerPage = (Page::length / Segment::length);
IanBenzMaxim 74:23be10c32fa3 296
IanBenzMaxim 74:23be10c32fa3 297 /// Manufacturer ID
IanBenzMaxim 74:23be10c32fa3 298 ManId manId;
IanBenzMaxim 74:23be10c32fa3 299
IanBenzMaxim 74:23be10c32fa3 300 /// Enable low voltage timing
IanBenzMaxim 74:23be10c32fa3 301 bool lowVoltage;
IanBenzMaxim 74:23be10c32fa3 302
IanBenzMaxim 74:23be10c32fa3 303 /// @param OW_master 1-Wire Master to use for communication with DS28E15/22/25.
IanBenzMaxim 74:23be10c32fa3 304 /// @param lowVoltage Enable low voltage timing.
IanBenzMaxim 74:23be10c32fa3 305 DS28E15_22_25(Masters::OneWireMaster & OW_master, bool lowVoltage = false);
IanBenzMaxim 74:23be10c32fa3 306
IanBenzMaxim 74:23be10c32fa3 307 // Const member functions should not affect the state of the memory, block protection, or secret on the DS28Exx.
IanBenzMaxim 74:23be10c32fa3 308 // Scratchpad on the DS28Exx is considered mutable.
IanBenzMaxim 74:23be10c32fa3 309
IanBenzMaxim 74:23be10c32fa3 310 /// Get the number of memory pages from the ROM ID of this device.
IanBenzMaxim 74:23be10c32fa3 311 MemoryPages memoryPages();
IanBenzMaxim 74:23be10c32fa3 312
IanBenzMaxim 74:23be10c32fa3 313 /// Get the number of protection blocks from the ROM ID of this device.
IanBenzMaxim 74:23be10c32fa3 314 ProtectionBlocks protectionBlocks();
IanBenzMaxim 74:23be10c32fa3 315
IanBenzMaxim 74:23be10c32fa3 316 /// Perform Load and Lock Secret command on the device.
IanBenzMaxim 74:23be10c32fa3 317 /// @note The secret should already be stored in the scratchpad on the device.
IanBenzMaxim 74:23be10c32fa3 318 /// @param lock Prevent further changes to the secret on the device after loading.
IanBenzMaxim 74:23be10c32fa3 319 CmdResult loadSecret(bool lock);
IanBenzMaxim 74:23be10c32fa3 320
IanBenzMaxim 74:23be10c32fa3 321 /// Perform Write Scratchpad operation on the device.
IanBenzMaxim 74:23be10c32fa3 322 /// @note 1-Wire ROM selection should have already occurred.
IanBenzMaxim 74:23be10c32fa3 323 /// @param[in] data Data to write to the scratchpad.
IanBenzMaxim 74:23be10c32fa3 324 CmdResult writeScratchpad(const Scratchpad & data) const;
IanBenzMaxim 74:23be10c32fa3 325
IanBenzMaxim 74:23be10c32fa3 326 /// Perform a Read Scratchpad operation on the device.
IanBenzMaxim 74:23be10c32fa3 327 /// @note 1-Wire ROM selection should have already occurred.
IanBenzMaxim 74:23be10c32fa3 328 /// @param[out] data Buffer to read data from the scratchpad into.
IanBenzMaxim 74:23be10c32fa3 329 CmdResult readScratchpad(Scratchpad & data) const;
IanBenzMaxim 74:23be10c32fa3 330
IanBenzMaxim 74:23be10c32fa3 331 /// Read memory segment using the Read Memory command on the device.
IanBenzMaxim 74:23be10c32fa3 332 /// @note 1-Wire ROM selection should have already occurred.
IanBenzMaxim 74:23be10c32fa3 333 /// @param pageNum Page number for read operation.
IanBenzMaxim 74:23be10c32fa3 334 /// @param segmentNum Segment number within page for read operation.
IanBenzMaxim 74:23be10c32fa3 335 /// @param[out] data Buffer to read data from the segment into.
IanBenzMaxim 74:23be10c32fa3 336 /// @param continuing True if continuing a previous Read Memory command.
IanBenzMaxim 74:23be10c32fa3 337 /// False to begin a new command.
IanBenzMaxim 74:23be10c32fa3 338 CmdResult readSegment(unsigned int pageNum, unsigned int segmentNum, Segment & data, bool continuing = false) const;
IanBenzMaxim 74:23be10c32fa3 339
IanBenzMaxim 74:23be10c32fa3 340 /// Write memory segment using the Write Memory command.
IanBenzMaxim 74:23be10c32fa3 341 /// @note 1-Wire ROM selection should have already occurred.
IanBenzMaxim 74:23be10c32fa3 342 /// @param pageNum Page number for write operation.
IanBenzMaxim 74:23be10c32fa3 343 /// @param segmentNum Segment number within page for write operation.
IanBenzMaxim 74:23be10c32fa3 344 /// @param[in] data Data to write to the memory segment.
IanBenzMaxim 74:23be10c32fa3 345 /// @param continuing True to continue writing with the next sequential segment.
IanBenzMaxim 74:23be10c32fa3 346 /// False to begin a new command.
IanBenzMaxim 74:23be10c32fa3 347 CmdResult writeSegment(unsigned int pageNum, unsigned int segmentNum, const Segment & data, bool continuing = false);
IanBenzMaxim 73:2cecc1372acc 348
IanBenzMaxim 74:23be10c32fa3 349 /// Read memory page using the Read Memory command on the device.
IanBenzMaxim 74:23be10c32fa3 350 /// @note 1-Wire ROM selection should have already occurred.
IanBenzMaxim 74:23be10c32fa3 351 /// @param pageNum Page number for write operation.
IanBenzMaxim 74:23be10c32fa3 352 /// @param[out] rdbuf Buffer to read data from the page into.
IanBenzMaxim 74:23be10c32fa3 353 /// @param continuing True if continuing a previous Read Memory command.
IanBenzMaxim 74:23be10c32fa3 354 /// False to begin a new command.
IanBenzMaxim 74:23be10c32fa3 355 CmdResult readPage(unsigned int pageNum, Page & rdbuf, bool continuing = false) const;
IanBenzMaxim 74:23be10c32fa3 356
IanBenzMaxim 74:23be10c32fa3 357 /// Perform a Compute and Lock Secret command on the device.
IanBenzMaxim 74:23be10c32fa3 358 /// @note 1-Wire ROM selection should have already occurred.
IanBenzMaxim 74:23be10c32fa3 359 /// @param pageNum Page number to use as the binding data.
IanBenzMaxim 74:23be10c32fa3 360 /// @param lock Prevent further changes to the secret on the device after computing.
IanBenzMaxim 74:23be10c32fa3 361 CmdResult computeSecret(unsigned int pageNum, bool lock);
IanBenzMaxim 74:23be10c32fa3 362
IanBenzMaxim 74:23be10c32fa3 363 /// Perform a Compute Page MAC command on the device.
IanBenzMaxim 74:23be10c32fa3 364 /// Read back the MAC and verify the CRC16.
IanBenzMaxim 74:23be10c32fa3 365 /// @note 1-Wire ROM selection should have already occurred.
IanBenzMaxim 74:23be10c32fa3 366 /// @param pageNum Page number to use for the computation.
IanBenzMaxim 74:23be10c32fa3 367 /// @param anon True to compute in anonymous mode where ROM ID is not used.
IanBenzMaxim 74:23be10c32fa3 368 /// @param[out] mac The device computed MAC.
IanBenzMaxim 74:23be10c32fa3 369 CmdResult computeReadPageMac(unsigned int pageNum, bool anon, Mac & mac) const;
IanBenzMaxim 74:23be10c32fa3 370
IanBenzMaxim 74:23be10c32fa3 371 /// Read the status of a memory protection block using the Read Status command.
IanBenzMaxim 74:23be10c32fa3 372 /// @note 1-Wire ROM selection should have already occurred.
IanBenzMaxim 74:23be10c32fa3 373 /// @param blockNum Block number to to read status of.
IanBenzMaxim 74:23be10c32fa3 374 /// @param[out] protection Receives protection status read from device.
IanBenzMaxim 74:23be10c32fa3 375 CmdResult readBlockProtection(unsigned int blockNum, BlockProtection & protection);
IanBenzMaxim 74:23be10c32fa3 376
IanBenzMaxim 74:23be10c32fa3 377 /// @{
IanBenzMaxim 74:23be10c32fa3 378 /// Read the status of all memory protection blocks using the Read Status command.
IanBenzMaxim 74:23be10c32fa3 379 /// @note 1-Wire ROM selection should have already occurred.
IanBenzMaxim 74:23be10c32fa3 380 /// @param[out] protection Receives protection statuses read from device.
IanBenzMaxim 75:8b627804927c 381 CmdResult readAllBlockProtection(BlockProtection(&protection)[DS28E15_Blocks]) const;
IanBenzMaxim 75:8b627804927c 382 CmdResult readAllBlockProtection(BlockProtection(&protection)[DS28E25_Blocks]) const;
IanBenzMaxim 74:23be10c32fa3 383 /// @}
IanBenzMaxim 74:23be10c32fa3 384
IanBenzMaxim 74:23be10c32fa3 385 /// Read the personality bytes using the Read Status command.
IanBenzMaxim 74:23be10c32fa3 386 /// @note 1-Wire ROM selection should have already occurred.
IanBenzMaxim 74:23be10c32fa3 387 /// @param[out] personality Receives personality read from device.
IanBenzMaxim 74:23be10c32fa3 388 CmdResult readPersonality(Personality & personality) const;
IanBenzMaxim 74:23be10c32fa3 389
IanBenzMaxim 74:23be10c32fa3 390 /// Update the status of a memory protection block using the Write Page Protection command.
IanBenzMaxim 74:23be10c32fa3 391 /// @note 1-Wire ROM selection should have already occurred.
IanBenzMaxim 74:23be10c32fa3 392 /// @param[in] Desired protection status for the block.
IanBenzMaxim 74:23be10c32fa3 393 /// It is not possible to disable existing protections.
IanBenzMaxim 74:23be10c32fa3 394 /// @param continuing True to continue a previous Write Page Protection command.
IanBenzMaxim 74:23be10c32fa3 395 /// False to begin a new command.
IanBenzMaxim 74:23be10c32fa3 396 CmdResult writeBlockProtection(const BlockProtection & protection);
IanBenzMaxim 74:23be10c32fa3 397
IanBenzMaxim 74:23be10c32fa3 398 /// Update the status of a memory protection block using the Authenticated Write Page Protection command.
IanBenzMaxim 74:23be10c32fa3 399 /// @note 1-Wire ROM selection should have already occurred.
IanBenzMaxim 74:23be10c32fa3 400 /// @param MacCoproc Coprocessor with Slave Secret to use for the operation.
IanBenzMaxim 74:23be10c32fa3 401 /// @param[in] newProtection New protection status to write.
IanBenzMaxim 74:23be10c32fa3 402 /// @param[in] oldProtection Existing protection status in device.
IanBenzMaxim 74:23be10c32fa3 403 /// @param continuing True to continue a previous Authenticated Write Page Protection command.
IanBenzMaxim 74:23be10c32fa3 404 /// False to begin a new command.
IanBenzMaxim 74:23be10c32fa3 405 CmdResult writeAuthBlockProtection(const ISha256MacCoproc & MacCoproc,
IanBenzMaxim 74:23be10c32fa3 406 const BlockProtection & newProtection,
IanBenzMaxim 74:23be10c32fa3 407 const BlockProtection & oldProtection);
IanBenzMaxim 74:23be10c32fa3 408
IanBenzMaxim 74:23be10c32fa3 409 /// Write memory segment with authentication using the Authenticated Write Memory command.
IanBenzMaxim 74:23be10c32fa3 410 /// @note 1-Wire ROM selection should have already occurred.
IanBenzMaxim 74:23be10c32fa3 411 /// @param MacCoproc Coprocessor to use for Write MAC computation.
IanBenzMaxim 74:23be10c32fa3 412 /// @param pageNum Page number for write operation.
IanBenzMaxim 74:23be10c32fa3 413 /// @param segmentNum Segment number within page for write operation.
IanBenzMaxim 74:23be10c32fa3 414 /// @param[in] newData New data to write to the segment.
IanBenzMaxim 74:23be10c32fa3 415 /// @param[in] oldData Existing data contained in the segment.
IanBenzMaxim 74:23be10c32fa3 416 /// @param continuing True to continue writing with the next sequential segment.
IanBenzMaxim 74:23be10c32fa3 417 /// False to begin a new command.
IanBenzMaxim 74:23be10c32fa3 418 CmdResult writeAuthSegment(const ISha256MacCoproc & MacCoproc,
IanBenzMaxim 74:23be10c32fa3 419 unsigned int pageNum,
IanBenzMaxim 74:23be10c32fa3 420 unsigned int segmentNum,
IanBenzMaxim 74:23be10c32fa3 421 const Segment & newData,
IanBenzMaxim 74:23be10c32fa3 422 const Segment & oldData,
IanBenzMaxim 74:23be10c32fa3 423 bool continuing = false);
IanBenzMaxim 74:23be10c32fa3 424
IanBenzMaxim 74:23be10c32fa3 425 /// Write memory segment with authentication using the Authenticated Write Memory command.
IanBenzMaxim 74:23be10c32fa3 426 /// @note 1-Wire ROM selection should have already occurred.
IanBenzMaxim 74:23be10c32fa3 427 /// @param pageNum Page number for write operation.
IanBenzMaxim 74:23be10c32fa3 428 /// @param segmentNum Segment number within page for write operation.
IanBenzMaxim 74:23be10c32fa3 429 /// @param[in] newData New data to write to the segment.
IanBenzMaxim 74:23be10c32fa3 430 /// @param[in] mac Write MAC computed for this operation.
IanBenzMaxim 74:23be10c32fa3 431 /// @param continuing True to continue writing with the next sequential segment.
IanBenzMaxim 74:23be10c32fa3 432 /// False to begin a new command.
IanBenzMaxim 74:23be10c32fa3 433 CmdResult writeAuthSegmentMac(unsigned int pageNum,
IanBenzMaxim 74:23be10c32fa3 434 unsigned int segmentNum,
IanBenzMaxim 74:23be10c32fa3 435 const Segment & newData,
IanBenzMaxim 74:23be10c32fa3 436 const Mac & mac,
IanBenzMaxim 74:23be10c32fa3 437 bool continuing = false);
IanBenzMaxim 74:23be10c32fa3 438
IanBenzMaxim 74:23be10c32fa3 439 private:
IanBenzMaxim 74:23be10c32fa3 440 static const unsigned int shaComputationDelayMs = 3;
IanBenzMaxim 74:23be10c32fa3 441 static const unsigned int eepromWriteDelayMs = 10;
IanBenzMaxim 74:23be10c32fa3 442 unsigned int secretEepromWriteDelayMs() const { return (lowVoltage ? 200 : 100); }
IanBenzMaxim 74:23be10c32fa3 443
IanBenzMaxim 74:23be10c32fa3 444 Masters::OneWireMaster & m_OW_master;
IanBenzMaxim 74:23be10c32fa3 445
IanBenzMaxim 74:23be10c32fa3 446 /// Read status bytes which are either personality or block protection.
IanBenzMaxim 74:23be10c32fa3 447 /// @note 1-Wire ROM selection should have already occurred.
IanBenzMaxim 74:23be10c32fa3 448 /// @param personality True to read personality or false to read block protection.
IanBenzMaxim 74:23be10c32fa3 449 /// @param allpages True to read all pages or false to read one page.
IanBenzMaxim 74:23be10c32fa3 450 /// @param pageNum Page number if reading block protection.
IanBenzMaxim 74:23be10c32fa3 451 /// @param rdbuf Buffer to receive data read from device.
IanBenzMaxim 74:23be10c32fa3 452 CmdResult readStatus(bool personality, bool allpages, unsigned int blockNum, uint8_t * rdbuf) const;
IanBenzMaxim 74:23be10c32fa3 453
IanBenzMaxim 74:23be10c32fa3 454 /// Extends readStatus() to use the BlockProtection wrapper.
IanBenzMaxim 74:23be10c32fa3 455 /// @see readAllBlockProtection(BlockProtection (&)[DS28E15_BLOCKS])
IanBenzMaxim 74:23be10c32fa3 456 template <ProtectionBlocks blocks> CmdResult readAllBlockProtection(BlockProtection(&protection)[blocks]) const;
IanBenzMaxim 74:23be10c32fa3 457 };
IanBenzMaxim 74:23be10c32fa3 458 }
IanBenzMaxim 73:2cecc1372acc 459 }
IanBenzMaxim 25:bdb1c5a53b58 460
IanBenzMaxim 49:36954b62f503 461 #endif