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.

OneWire_Masters/DS2465/DS2465.hpp

Committer:
IanBenzMaxim
Date:
2016-04-01
Revision:
35:5d23395628f6
Parent:
34:11fffbe98ef9
Child:
47:307dc45952db

File content as of revision 35:5d23395628f6:

#ifndef DS2465_H
#define DS2465_H

#include "OneWire_Masters/OneWireMaster.h"
#include "OneWire_Masters/ISha256MacCoprocessor.hpp"

namespace mbed
{
  class I2C;
}

class DS2465 : public OneWireMaster, public ISha256MacCoprocessor
{
public:
  enum PageRegion
  {
    REGION_FULL_PAGE = 0x03,
    REGION_FIRST_HALF = 0x01,
    REGION_SECOND_HALF = 0x02
  };
  
  enum MemoryAddr
  {
    ADDR_SPAD = 0x00,
    ADDR_CMD_REG = 0x60,
    ADDR_STATUS_REG = 0x61,
    ADDR_DATA_REG = 0x62,
    ADDR_MAC_READ = 0x63,
    ADDR_SHA_SELECT_REG = 0x66,
    ADDR_WCFG_REG = 0x67,
    ADDR_TRSTL_REG = 0x68,
    ADDR_TMSP_REG = 0x69,
    ADDR_TW0L_REG = 0x6A,
    ADDR_TREC0_REG = 0x6B,
    ADDR_RWPU_REG = 0x6C,
    ADDR_TW1L_REG = 0x6D,
    ADDR_USER_MEM_PAGE_0 = 0x80,
    ADDR_USER_MEM_PAGE_1 = 0xA0
  };
  
  struct Config
  {    
    bool c1WS, cSPU, cPDN, cAPU;
    
    std::uint8_t readByte() const;
    std::uint8_t writeByte() const;
    
    void reset();
    Config() { reset(); }
  };
  
  DS2465(mbed::I2C & I2C_interface, std::uint8_t I2C_address);
  
  // Const member functions should not change the settings of the DS2465 or affect the state of the 1-Wire bus.
  // Read pointer, scratchpad, MAC output register, and command register on the DS2465 are considered mutable.

  //Misc. Functions
  OneWireMaster::CmdResult detect();
  OneWireMaster::CmdResult reset(void);                                      // Resets DS2465 (NOTE: This is NOT a 1-Wire Reset)
  OneWireMaster::CmdResult writeConfig(const Config & config, bool verify);
  Config currentConfig() const;

  // DS2465 Memory Commands
  OneWireMaster::CmdResult readMemory(std::uint8_t addr, std::uint8_t * buf, std::size_t bufLen, bool skipSetPointer = false) const;
  OneWireMaster::CmdResult writeMemory(std::uint8_t addr, const std::uint8_t * buf, std::size_t bufLen) { return cWriteMemory(addr, buf, bufLen); }
  OneWireMaster::CmdResult writeScratchpad(const std::uint8_t * buf, std::size_t bufLen) const { return cWriteMemory(ADDR_SPAD, buf, bufLen); }
  OneWireMaster::CmdResult copyScratchpad(bool destSecret, unsigned int pageNum, bool notFull, unsigned int segmentNum);
  
  // 1-Wire Master Commands
  virtual OneWireMaster::CmdResult OWInitMaster(void);
  virtual OneWireMaster::CmdResult OWReset(void);                                   // Issues a 1-Wire Reset Pulse
  virtual OneWireMaster::CmdResult OWTouchBit(std::uint8_t & sendrecvbit, OWLevel after_level);
  virtual OneWireMaster::CmdResult OWReadByte(std::uint8_t & recvbyte, OWLevel after_level);
  virtual OneWireMaster::CmdResult OWWriteByte(std::uint8_t sendbyte, OWLevel after_level);
  virtual OneWireMaster::CmdResult OWReadBlock(std::uint8_t *rx_buf, std::uint8_t rx_len);
  virtual OneWireMaster::CmdResult OWWriteBlock(const std::uint8_t *tran_buf, std::uint8_t tran_len);
  OneWireMaster::CmdResult OWWriteBlock(bool tx_mac, const std::uint8_t *tran_buf, std::uint8_t tran_len);
  virtual OneWireMaster::CmdResult OWSetSpeed(OWSpeed new_speed);
  virtual OneWireMaster::CmdResult OWSetLevel(OWLevel new_level);
  virtual OneWireMaster::CmdResult OWTriplet(SearchDirection & search_direction, std::uint8_t & sbr, std::uint8_t & tsb);
  
  //DS2465 Coprocessor Commands
  OneWireMaster::CmdResult computeNextMasterSecret(bool swap, unsigned int pageNum, PageRegion region);
  OneWireMaster::CmdResult computeWriteMac(bool regwrite, bool swap, unsigned int pageNum, unsigned int segmentNum) const;
  OneWireMaster::CmdResult computeSlaveSecret(bool swap, unsigned int pageNum, PageRegion region);
  OneWireMaster::CmdResult computeAuthMac(bool swap, unsigned int pageNum, PageRegion region) const;
  
  // ISha256MacCoprocessor Commands
  virtual ISha256MacCoprocessor::CmdResult setMasterSecret(const Secret & secret);
  virtual ISha256MacCoprocessor::CmdResult computeWriteMac(const WriteMacData & writeMacData, Mac & mac) const;
  virtual ISha256MacCoprocessor::CmdResult computeAuthMac(const DevicePage & devicePage, const DeviceScratchpad & challenge, const AuthMacData & authMacData, Mac & mac) const;
  virtual ISha256MacCoprocessor::CmdResult computeSlaveSecret(const DevicePage & devicePage, const DeviceScratchpad & deviceScratchpad, const SlaveSecretData & slaveSecretData);
  
private:
  static const unsigned int eepromWriteDelayMs = 10;
  static const unsigned int shaComputationDelayMs = 2;
  
  mbed::I2C & m_I2C_interface;
  std::uint8_t m_I2C_address;
  Config m_curConfig;

  OneWireMaster::CmdResult cWriteMemory(std::uint8_t addr, const std::uint8_t * buf, std::size_t bufLen) const;
  OneWireMaster::CmdResult pollBusy(std::uint8_t * pStatus = NULL);
  OneWireMaster::CmdResult configureLevel(OWLevel level);
};

#endif