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