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:
Thu May 12 14:38:16 2016 -0500
Revision:
73:2cecc1372acc
Parent:
OneWire_Masters/DS2465/DS2465.hpp@72:6892702709ee
Child:
74:23be10c32fa3
Added namespaces. Renamed files and directories for consistency. Use <stdint.h> instead of <cstdint> since it is not supported by C++98.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
IanBenzMaxim 73:2cecc1372acc 1 #ifndef OneWire_Masters_DS2465
IanBenzMaxim 73:2cecc1372acc 2 #define OneWire_Masters_DS2465
IanBenzMaxim 21:00c94aeb533e 3
IanBenzMaxim 73:2cecc1372acc 4 #include "Masters/OneWireMaster.h"
IanBenzMaxim 73:2cecc1372acc 5 #include "Authenticators/ISha256MacCoproc.h"
IanBenzMaxim 21:00c94aeb533e 6
IanBenzMaxim 47:307dc45952db 7 namespace mbed { class I2C; }
IanBenzMaxim 27:d5aaefa252f1 8
IanBenzMaxim 73:2cecc1372acc 9 namespace OneWire
IanBenzMaxim 21:00c94aeb533e 10 {
IanBenzMaxim 73:2cecc1372acc 11 namespace Masters
IanBenzMaxim 69:f915c4c59a69 12 {
IanBenzMaxim 73:2cecc1372acc 13 /// Interface to the DS2465 1-Wire master and SHA-256 coprocessor.
IanBenzMaxim 73:2cecc1372acc 14 class DS2465 : public OneWireMaster, public Authenticators::ISha256MacCoproc
IanBenzMaxim 73:2cecc1372acc 15 {
IanBenzMaxim 73:2cecc1372acc 16 public:
IanBenzMaxim 73:2cecc1372acc 17 /// Delay required after writing an EEPROM segment.
IanBenzMaxim 73:2cecc1372acc 18 static const unsigned int eepromSegmentWriteDelayMs = 10;
IanBenzMaxim 73:2cecc1372acc 19 /// Delay required after writing an EEPROM page such as the secret memory.
IanBenzMaxim 73:2cecc1372acc 20 static const unsigned int eepromPageWriteDelayMs = 8 * eepromSegmentWriteDelayMs;
IanBenzMaxim 73:2cecc1372acc 21 /// Delay required for a SHA computation to complete.
IanBenzMaxim 73:2cecc1372acc 22 static const unsigned int shaComputationDelayMs = 2;
IanBenzMaxim 21:00c94aeb533e 23
IanBenzMaxim 73:2cecc1372acc 24 /// Page region to use for swapping.
IanBenzMaxim 73:2cecc1372acc 25 enum PageRegion
IanBenzMaxim 73:2cecc1372acc 26 {
IanBenzMaxim 73:2cecc1372acc 27 REGION_FULL_PAGE = 0x03,
IanBenzMaxim 73:2cecc1372acc 28 REGION_FIRST_HALF = 0x01,
IanBenzMaxim 73:2cecc1372acc 29 REGION_SECOND_HALF = 0x02
IanBenzMaxim 73:2cecc1372acc 30 };
IanBenzMaxim 73:2cecc1372acc 31
IanBenzMaxim 73:2cecc1372acc 32 /// Starting memory addresses.
IanBenzMaxim 73:2cecc1372acc 33 enum MemoryAddr
IanBenzMaxim 73:2cecc1372acc 34 {
IanBenzMaxim 73:2cecc1372acc 35 ADDR_SPAD = 0x00,
IanBenzMaxim 73:2cecc1372acc 36 ADDR_CMD_REG = 0x60,
IanBenzMaxim 73:2cecc1372acc 37 ADDR_STATUS_REG = 0x61,
IanBenzMaxim 73:2cecc1372acc 38 ADDR_DATA_REG = 0x62,
IanBenzMaxim 73:2cecc1372acc 39 ADDR_MAC_READ = 0x63,
IanBenzMaxim 73:2cecc1372acc 40 ADDR_SHA_SELECT_REG = 0x66,
IanBenzMaxim 73:2cecc1372acc 41 ADDR_WCFG_REG = 0x67,
IanBenzMaxim 73:2cecc1372acc 42 ADDR_TRSTL_REG = 0x68,
IanBenzMaxim 73:2cecc1372acc 43 ADDR_TMSP_REG = 0x69,
IanBenzMaxim 73:2cecc1372acc 44 ADDR_TW0L_REG = 0x6A,
IanBenzMaxim 73:2cecc1372acc 45 ADDR_TREC0_REG = 0x6B,
IanBenzMaxim 73:2cecc1372acc 46 ADDR_RWPU_REG = 0x6C,
IanBenzMaxim 73:2cecc1372acc 47 ADDR_TW1L_REG = 0x6D,
IanBenzMaxim 73:2cecc1372acc 48 ADDR_USER_MEM_PAGE_0 = 0x80,
IanBenzMaxim 73:2cecc1372acc 49 ADDR_USER_MEM_PAGE_1 = 0xA0
IanBenzMaxim 73:2cecc1372acc 50 };
IanBenzMaxim 73:2cecc1372acc 51
IanBenzMaxim 73:2cecc1372acc 52 /// Represents a DS2465 configuration.
IanBenzMaxim 73:2cecc1372acc 53 class Config
IanBenzMaxim 73:2cecc1372acc 54 {
IanBenzMaxim 73:2cecc1372acc 55 public:
IanBenzMaxim 73:2cecc1372acc 56 /// @{
IanBenzMaxim 73:2cecc1372acc 57 /// 1-Wire Speed
IanBenzMaxim 73:2cecc1372acc 58 bool get1WS() const { return m_1WS; }
IanBenzMaxim 73:2cecc1372acc 59 void set1WS(bool new1WS) { m_1WS = new1WS; }
IanBenzMaxim 73:2cecc1372acc 60 /// @}
IanBenzMaxim 73:2cecc1372acc 61
IanBenzMaxim 73:2cecc1372acc 62 /// @{
IanBenzMaxim 73:2cecc1372acc 63 /// Strong Pullup
IanBenzMaxim 73:2cecc1372acc 64 bool getSPU() const { return m_SPU; }
IanBenzMaxim 73:2cecc1372acc 65 void setSPU(bool newSPU) { m_SPU = newSPU; }
IanBenzMaxim 73:2cecc1372acc 66 /// @}
IanBenzMaxim 73:2cecc1372acc 67
IanBenzMaxim 73:2cecc1372acc 68 /// @{
IanBenzMaxim 73:2cecc1372acc 69 /// 1-Wire Power Down
IanBenzMaxim 73:2cecc1372acc 70 bool getPDN() const { return m_PDN; }
IanBenzMaxim 73:2cecc1372acc 71 void setPDN(bool newPDN) { m_PDN = newPDN; }
IanBenzMaxim 73:2cecc1372acc 72 /// @}
IanBenzMaxim 73:2cecc1372acc 73
IanBenzMaxim 73:2cecc1372acc 74 /// @{
IanBenzMaxim 73:2cecc1372acc 75 /// Active Pullup
IanBenzMaxim 73:2cecc1372acc 76 bool getAPU() const { return m_APU; }
IanBenzMaxim 73:2cecc1372acc 77 void setAPU(bool newAPU) { m_APU = newAPU; }
IanBenzMaxim 73:2cecc1372acc 78 /// @}
IanBenzMaxim 73:2cecc1372acc 79
IanBenzMaxim 73:2cecc1372acc 80 /// Byte representation that is read from the DS2465.
IanBenzMaxim 73:2cecc1372acc 81 uint8_t readByte() const;
IanBenzMaxim 73:2cecc1372acc 82 /// Byte respresentation that is written to the DS2465.
IanBenzMaxim 73:2cecc1372acc 83 uint8_t writeByte() const;
IanBenzMaxim 73:2cecc1372acc 84
IanBenzMaxim 73:2cecc1372acc 85 /// Reset to the power-on default config.
IanBenzMaxim 73:2cecc1372acc 86 void reset();
IanBenzMaxim 73:2cecc1372acc 87 Config() { reset(); }
IanBenzMaxim 73:2cecc1372acc 88
IanBenzMaxim 73:2cecc1372acc 89 private:
IanBenzMaxim 73:2cecc1372acc 90 bool m_1WS, m_SPU, m_PDN, m_APU;
IanBenzMaxim 73:2cecc1372acc 91 };
IanBenzMaxim 73:2cecc1372acc 92
IanBenzMaxim 73:2cecc1372acc 93 /// @param I2C_interface Configured I2C communication interface for DS2465.
IanBenzMaxim 73:2cecc1372acc 94 /// @param I2C_address I2C bus address of the DS2465 in mbed format.
IanBenzMaxim 73:2cecc1372acc 95 DS2465(mbed::I2C & I2C_interface, uint8_t I2C_address);
IanBenzMaxim 73:2cecc1372acc 96
IanBenzMaxim 73:2cecc1372acc 97 // Const member functions should not change the settings of the DS2465 or affect the state of the 1-Wire bus.
IanBenzMaxim 73:2cecc1372acc 98 // Read pointer, scratchpad, MAC output register, and command register on the DS2465 are considered mutable.
IanBenzMaxim 73:2cecc1372acc 99
IanBenzMaxim 73:2cecc1372acc 100 /// Performs a soft reset on the DS2465. This is NOT a 1-Wire Reset.
IanBenzMaxim 73:2cecc1372acc 101 OneWireMaster::CmdResult reset(void);
IanBenzMaxim 73:2cecc1372acc 102
IanBenzMaxim 73:2cecc1372acc 103 /// Write a new configuration to the DS2465.
IanBenzMaxim 73:2cecc1372acc 104 /// @param[in] config New configuration to write.
IanBenzMaxim 73:2cecc1372acc 105 /// @param verify Verify that the configuration was written successfully.
IanBenzMaxim 73:2cecc1372acc 106 OneWireMaster::CmdResult writeConfig(const Config & config, bool verify);
IanBenzMaxim 73:2cecc1372acc 107
IanBenzMaxim 73:2cecc1372acc 108 /// Read the current DS2465 configuration.
IanBenzMaxim 73:2cecc1372acc 109 /// @returns The cached current configuration.
IanBenzMaxim 73:2cecc1372acc 110 Config currentConfig() const { return m_curConfig; }
IanBenzMaxim 21:00c94aeb533e 111
IanBenzMaxim 73:2cecc1372acc 112 // DS2465 Memory Commands
IanBenzMaxim 73:2cecc1372acc 113
IanBenzMaxim 73:2cecc1372acc 114 /// Read memory from the DS2465
IanBenzMaxim 73:2cecc1372acc 115 /// @param addr Address to begin reading from.
IanBenzMaxim 73:2cecc1372acc 116 /// @param[out] buf Buffer to hold read data.
IanBenzMaxim 73:2cecc1372acc 117 /// @param bufLen Length of buffer, buf, and number of bytes to read.
IanBenzMaxim 73:2cecc1372acc 118 /// @param skipSetPointer Assume that the read pointer is already set to the correct address.
IanBenzMaxim 73:2cecc1372acc 119 OneWireMaster::CmdResult readMemory(uint8_t addr, uint8_t * buf, size_t bufLen, bool skipSetPointer = false) const;
IanBenzMaxim 73:2cecc1372acc 120
IanBenzMaxim 73:2cecc1372acc 121 /// Write to SRAM memory on the DS2465
IanBenzMaxim 73:2cecc1372acc 122 /// @param addr Address to begin writing to.
IanBenzMaxim 73:2cecc1372acc 123 /// @param[in] buf Buffer containing the data to write.
IanBenzMaxim 73:2cecc1372acc 124 /// @param bufLen Length of buffer, buf, and number of bytes to write.
IanBenzMaxim 73:2cecc1372acc 125 OneWireMaster::CmdResult writeMemory(uint8_t addr, const uint8_t * buf, size_t bufLen) { return cWriteMemory(addr, buf, bufLen); }
IanBenzMaxim 73:2cecc1372acc 126
IanBenzMaxim 73:2cecc1372acc 127 /// Write data to the scratchpad area of the DS2465
IanBenzMaxim 73:2cecc1372acc 128 /// @param[in] buf Buffer containing the data to write.
IanBenzMaxim 73:2cecc1372acc 129 /// @param bufLen Length of buffer, buf, and the number of bytes to write.
IanBenzMaxim 73:2cecc1372acc 130 OneWireMaster::CmdResult writeScratchpad(const uint8_t * buf, size_t bufLen) const { return cWriteMemory(ADDR_SPAD, buf, bufLen); }
IanBenzMaxim 73:2cecc1372acc 131
IanBenzMaxim 73:2cecc1372acc 132 /// Copy the scratchpad contents to an EEPROM memory page.
IanBenzMaxim 73:2cecc1372acc 133 /// @param pageNum Page number to copy to.
IanBenzMaxim 73:2cecc1372acc 134 OneWireMaster::CmdResult copyScratchpadToPage(unsigned int pageNum) { return copyScratchpad(false, pageNum, false, 0); }
IanBenzMaxim 73:2cecc1372acc 135
IanBenzMaxim 73:2cecc1372acc 136 /// Copy the scratchpad contents to an EEPROM memory segment.
IanBenzMaxim 73:2cecc1372acc 137 /// @param pageNum Page number to copy to.
IanBenzMaxim 73:2cecc1372acc 138 /// @param segmentNum Segment number to copy to.
IanBenzMaxim 73:2cecc1372acc 139 OneWireMaster::CmdResult copyScratchpadToSegment(unsigned int pageNum, unsigned int segmentNum) { return copyScratchpad(false, pageNum, true, segmentNum); }
IanBenzMaxim 73:2cecc1372acc 140
IanBenzMaxim 73:2cecc1372acc 141 /// Copy the scratchpad contents to the secret EEPROM memory page.
IanBenzMaxim 73:2cecc1372acc 142 OneWireMaster::CmdResult copyScratchpadToSecret() { return copyScratchpad(true, 0, false, 0); }
IanBenzMaxim 73:2cecc1372acc 143
IanBenzMaxim 73:2cecc1372acc 144 // 1-Wire Master Commands
IanBenzMaxim 73:2cecc1372acc 145 virtual OneWireMaster::CmdResult OWInitMaster(void);
IanBenzMaxim 73:2cecc1372acc 146 virtual OneWireMaster::CmdResult OWReset(void);
IanBenzMaxim 73:2cecc1372acc 147 virtual OneWireMaster::CmdResult OWTouchBitSetLevel(uint8_t & sendrecvbit, OWLevel after_level);
IanBenzMaxim 73:2cecc1372acc 148 virtual OneWireMaster::CmdResult OWReadByteSetLevel(uint8_t & recvbyte, OWLevel after_level);
IanBenzMaxim 73:2cecc1372acc 149 virtual OneWireMaster::CmdResult OWWriteByteSetLevel(uint8_t sendbyte, OWLevel after_level);
IanBenzMaxim 73:2cecc1372acc 150 virtual OneWireMaster::CmdResult OWReadBlock(uint8_t *rx_buf, uint8_t rx_len);
IanBenzMaxim 73:2cecc1372acc 151 virtual OneWireMaster::CmdResult OWWriteBlock(const uint8_t *tran_buf, uint8_t tran_len);
IanBenzMaxim 73:2cecc1372acc 152 virtual OneWireMaster::CmdResult OWSetSpeed(OWSpeed new_speed);
IanBenzMaxim 73:2cecc1372acc 153 /// @note The DS2465 only supports enabling strong pullup following a 1-Wire read or write operation.
IanBenzMaxim 73:2cecc1372acc 154 virtual OneWireMaster::CmdResult OWSetLevel(OWLevel new_level);
IanBenzMaxim 73:2cecc1372acc 155 virtual OneWireMaster::CmdResult OWTriplet(SearchDirection & search_direction, uint8_t & sbr, uint8_t & tsb);
IanBenzMaxim 73:2cecc1372acc 156
IanBenzMaxim 73:2cecc1372acc 157 /// Write the last computed MAC to the 1-Wire bus
IanBenzMaxim 73:2cecc1372acc 158 OneWireMaster::CmdResult OWWriteBlockMac();
IanBenzMaxim 73:2cecc1372acc 159
IanBenzMaxim 73:2cecc1372acc 160 // DS2465 Coprocessor Commands
IanBenzMaxim 73:2cecc1372acc 161
IanBenzMaxim 73:2cecc1372acc 162 /// Compute Next Master Secret with scratchpad data.
IanBenzMaxim 73:2cecc1372acc 163 OneWireMaster::CmdResult computeNextMasterSecret() { return computeNextMasterSecret(false, 0, REGION_FULL_PAGE); }
IanBenzMaxim 73:2cecc1372acc 164
IanBenzMaxim 73:2cecc1372acc 165 /// Compute Next Master Secret with page swapping.
IanBenzMaxim 73:2cecc1372acc 166 /// @param pageNum Page number to swap in.
IanBenzMaxim 73:2cecc1372acc 167 /// @param region Region of the page to swap in.
IanBenzMaxim 73:2cecc1372acc 168 OneWireMaster::CmdResult computeNextMasterSecretSwap(unsigned int pageNum, PageRegion region) { return computeNextMasterSecret(true, pageNum, region); }
IanBenzMaxim 73:2cecc1372acc 169
IanBenzMaxim 73:2cecc1372acc 170 /// Compute Write MAC with scratchpad data.
IanBenzMaxim 73:2cecc1372acc 171 /// @param regwrite True if writing to a register or false if regular memory.
IanBenzMaxim 73:2cecc1372acc 172 OneWireMaster::CmdResult computeWriteMac(bool regwrite) const { return computeWriteMac(regwrite, false, 0, 0); }
IanBenzMaxim 73:2cecc1372acc 173
IanBenzMaxim 73:2cecc1372acc 174 /// Compute Write MAC with page swapping.
IanBenzMaxim 73:2cecc1372acc 175 /// @param regwrite True if writing to a register or false if regular memory.
IanBenzMaxim 73:2cecc1372acc 176 /// @param pageNum Page number to swap in.
IanBenzMaxim 73:2cecc1372acc 177 /// @param segmentNum Segment number to swap in.
IanBenzMaxim 73:2cecc1372acc 178 OneWireMaster::CmdResult computeWriteMacSwap(bool regwrite, unsigned int pageNum, unsigned int segmentNum) const { return computeWriteMac(regwrite, true, pageNum, segmentNum); }
IanBenzMaxim 73:2cecc1372acc 179
IanBenzMaxim 73:2cecc1372acc 180 /// Compute Slave Secret (S-Secret) with scratchpad data.
IanBenzMaxim 73:2cecc1372acc 181 OneWireMaster::CmdResult computeSlaveSecret() { return computeSlaveSecret(false, 0, REGION_FULL_PAGE); }
IanBenzMaxim 73:2cecc1372acc 182
IanBenzMaxim 73:2cecc1372acc 183 /// Compute Slave Secret (S-Secret) with page swapping.
IanBenzMaxim 73:2cecc1372acc 184 /// @param pageNum Page number to swap in.
IanBenzMaxim 73:2cecc1372acc 185 /// @param region Region of the page to swap in.
IanBenzMaxim 73:2cecc1372acc 186 OneWireMaster::CmdResult computeSlaveSecretSwap(unsigned int pageNum, PageRegion region) { return computeSlaveSecret(true, pageNum, region); }
IanBenzMaxim 73:2cecc1372acc 187
IanBenzMaxim 73:2cecc1372acc 188 /// Compute Authentication MAC with scratchpad data.
IanBenzMaxim 73:2cecc1372acc 189 OneWireMaster::CmdResult computeAuthMac() const { return computeAuthMac(false, 0, REGION_FULL_PAGE); }
IanBenzMaxim 73:2cecc1372acc 190
IanBenzMaxim 73:2cecc1372acc 191 /// Compute Authentication MAC with page swapping.
IanBenzMaxim 73:2cecc1372acc 192 /// @param pageNum Page number to swap in.
IanBenzMaxim 73:2cecc1372acc 193 /// @param region Region of the page to swap in.
IanBenzMaxim 73:2cecc1372acc 194 OneWireMaster::CmdResult computeAuthMacSwap(unsigned int pageNum, PageRegion region) const { return computeAuthMac(true, pageNum, region); }
IanBenzMaxim 73:2cecc1372acc 195
IanBenzMaxim 73:2cecc1372acc 196 // ISha256MacCoproc Commands
IanBenzMaxim 73:2cecc1372acc 197 virtual ISha256MacCoproc::CmdResult setMasterSecret(const Secret & masterSecret);
IanBenzMaxim 73:2cecc1372acc 198 virtual ISha256MacCoproc::CmdResult computeSlaveSecret(const DevicePage & devicePage, const DeviceScratchpad & deviceScratchpad, const SlaveSecretData & slaveSecretData);
IanBenzMaxim 73:2cecc1372acc 199 virtual ISha256MacCoproc::CmdResult computeWriteMac(const WriteMacData & writeMacData, Mac & mac) const;
IanBenzMaxim 73:2cecc1372acc 200 virtual ISha256MacCoproc::CmdResult computeAuthMac(const DevicePage & devicePage, const DeviceScratchpad & challenge, const AuthMacData & authMacData, Mac & mac) const;
IanBenzMaxim 73:2cecc1372acc 201
IanBenzMaxim 73:2cecc1372acc 202 private:
IanBenzMaxim 73:2cecc1372acc 203 mbed::I2C & m_I2C_interface;
IanBenzMaxim 73:2cecc1372acc 204 uint8_t m_I2C_address;
IanBenzMaxim 73:2cecc1372acc 205 Config m_curConfig;
IanBenzMaxim 21:00c94aeb533e 206
IanBenzMaxim 73:2cecc1372acc 207 /// Polls the DS2465 status waiting for the 1-Wire Busy bit (1WB) to be cleared.
IanBenzMaxim 73:2cecc1372acc 208 /// @param[out] pStatus Optionally retrive the status byte when 1WB cleared.
IanBenzMaxim 73:2cecc1372acc 209 /// @returns Success or TimeoutError if poll limit reached.
IanBenzMaxim 73:2cecc1372acc 210 OneWireMaster::CmdResult pollBusy(uint8_t * pStatus = NULL);
IanBenzMaxim 73:2cecc1372acc 211
IanBenzMaxim 73:2cecc1372acc 212 /// Ensure that the desired 1-Wire level is set in the configuration.
IanBenzMaxim 73:2cecc1372acc 213 /// @param level Desired 1-Wire level.
IanBenzMaxim 73:2cecc1372acc 214 OneWireMaster::CmdResult configureLevel(OWLevel level);
IanBenzMaxim 73:2cecc1372acc 215
IanBenzMaxim 73:2cecc1372acc 216 /// Const version of writeMemory() for internal use.
IanBenzMaxim 73:2cecc1372acc 217 OneWireMaster::CmdResult cWriteMemory(uint8_t addr, const uint8_t * buf, size_t bufLen) const;
IanBenzMaxim 73:2cecc1372acc 218
IanBenzMaxim 73:2cecc1372acc 219 // Legacy implementations
IanBenzMaxim 73:2cecc1372acc 220 OneWireMaster::CmdResult OWWriteBlock(bool tx_mac, const uint8_t *tran_buf, uint8_t tran_len);
IanBenzMaxim 73:2cecc1372acc 221 OneWireMaster::CmdResult copyScratchpad(bool destSecret, unsigned int pageNum, bool notFull, unsigned int segmentNum);
IanBenzMaxim 73:2cecc1372acc 222 OneWireMaster::CmdResult computeNextMasterSecret(bool swap, unsigned int pageNum, PageRegion region);
IanBenzMaxim 73:2cecc1372acc 223 OneWireMaster::CmdResult computeWriteMac(bool regwrite, bool swap, unsigned int pageNum, unsigned int segmentNum) const;
IanBenzMaxim 73:2cecc1372acc 224 OneWireMaster::CmdResult computeSlaveSecret(bool swap, unsigned int pageNum, PageRegion region);
IanBenzMaxim 73:2cecc1372acc 225 OneWireMaster::CmdResult computeAuthMac(bool swap, unsigned int pageNum, PageRegion region) const;
IanBenzMaxim 73:2cecc1372acc 226 };
IanBenzMaxim 73:2cecc1372acc 227 }
IanBenzMaxim 73:2cecc1372acc 228 }
IanBenzMaxim 21:00c94aeb533e 229
IanBenzMaxim 21:00c94aeb533e 230 #endif