Implementation of 1-Wire with added Alarm Search Functionality

Dependents:   Max32630_One_Wire_Interface

OneWire_Masters/DS2465/DS2465.hpp

Committer:
IanBenzMaxim
Date:
2016-03-22
Revision:
24:8942d8478d68
Parent:
21:00c94aeb533e
Child:
26:a361e3f42ba5

File content as of revision 24:8942d8478d68:

#ifndef DS2465_H
#define DS2465_H

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

class DS2465 : public OneWireMaster, public ISha256MacCoprocessor
{
public:  
  enum Direction
  {
    DIRECTION_WRITE_ZERO = 0,
    DIRECTION_WRITE_ONE = 1
  };
  
  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
  };
  
  DS2465(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(void);
  OneWireMaster::CmdResult Reset(void);                                      // Resets DS2465 (NOTE: This is NOT a 1-Wire Reset)

  // DS2465 Memory Commands
  OneWireMaster::CmdResult ReadMemory(std::uint8_t addr, std::uint8_t * buf, size_t bufLen, bool skip_set_pointer = 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 dest_secret, 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 OWSearch(RomId & romId);
  virtual OneWireMaster::CmdResult OWReadByte(uint8_t & recvbyte);
  virtual OneWireMaster::CmdResult OWWriteByte(uint8_t sendbyte);
  virtual OneWireMaster::CmdResult OWTouchBit(uint8_t & sendrecvbit);
  virtual OneWireMaster::CmdResult OWSpeed(OW_SPEED new_speed);
  virtual OneWireMaster::CmdResult OWLevel(OW_LEVEL new_level);
  virtual OneWireMaster::CmdResult OWReadBlock(uint8_t *rx_buf, uint8_t rx_len);
  virtual OneWireMaster::CmdResult OWWriteBlock(const uint8_t *tran_buf, uint8_t tran_len);
  OneWireMaster::CmdResult OWWriteBlock(bool tx_mac, const uint8_t *tran_buf, uint8_t tran_len);
  OneWireMaster::CmdResult OWPowerDown(void);
  OneWireMaster::CmdResult OWPowerUp(void);
  OneWireMaster::CmdResult ConfigureAPU(bool apu_enable);
  OneWireMaster::CmdResult Triplet(Direction search_direction, uint8_t & status);
  
  virtual OneWireMaster::CmdResult OWWriteBytePower(uint8_t sendbyte);
  virtual OneWireMaster::CmdResult OWReadBytePower(uint8_t & recvbyte);
  virtual OneWireMaster::CmdResult OWReadBitPower(uint8_t applyPowerResponse);
  
  virtual OneWireMaster::CmdResult OWBlock(uint8_t *tran_buf, uint8_t tran_len);
  
  //DS2465 Coprocessor Commands
  OneWireMaster::CmdResult Compute_NextMasterSecret(bool swap, unsigned int pageNum, PageRegion region);
  OneWireMaster::CmdResult Compute_WriteMAC(bool regwrite, bool swap, unsigned int pageNum, unsigned int segmentNum) const;
  OneWireMaster::CmdResult Compute_SSecret(bool swap, unsigned int pageNum, PageRegion region);
  OneWireMaster::CmdResult Compute_AuthMAC(bool swap, unsigned int pageNum, PageRegion region) const;
  
  virtual ISha256MacCoprocessor::CmdResult setMasterSecret(const std::uint8_t (&secret)[ISha256MacCoprocessor::secret_len]);
  virtual ISha256MacCoprocessor::CmdResult ComputeAndRead_WriteMAC(const std::uint8_t (&WriteMAC_data)[WriteMAC_data_len], std::uint8_t (&mac)[mac_len]) const;
  virtual ISha256MacCoprocessor::CmdResult ComputeAndRead_AuthMAC(const std::uint8_t (&devicePage)[devicePage_len], const std::uint8_t (&challenge)[deviceScratchpad_len],
                                      const std::uint8_t (&AuthMAC_data)[AuthMAC_data_len], std::uint8_t (&mac)[mac_len]) const;
  virtual ISha256MacCoprocessor::CmdResult Compute_SSecret(const std::uint8_t (&devicePage)[devicePage_len],
                               const std::uint8_t (&deviceScratchpad)[deviceScratchpad_len], const std::uint8_t (&SSecret_data)[SSecret_data_len]);
  
private:
  struct Config
  {    
    bool c1WS, cSPU, cPDN, cAPU;
    
    std::uint8_t readByte() const;
    std::uint8_t writeByte() const;
    
    void reset();
    Config() { reset(); }
  };
  
  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 WriteConfig(const Config & config);
  OneWireMaster::CmdResult PollBusy(uint8_t * pStatus = NULL);
};

#endif