Implementation of 1-Wire with added Alarm Search Functionality

Dependents:   Max32630_One_Wire_Interface

OneWire_Masters/DS2465/DS2465.hpp

Committer:
IanBenzMaxim
Date:
2016-03-21
Revision:
21:00c94aeb533e
Child:
24:8942d8478d68

File content as of revision 21:00c94aeb533e:

#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 OWConfigAddr
  {
    ADDR_TRSTL_REG = 0x68,
    ADDR_TMSP_REG = 0x69,
    ADDR_TW0L_REG = 0x6A,
    ADDR_TREC0_REG = 0x6B,
    ADDR_RWPU_REG = 0x6C,
    ADDR_TW1L_REG = 0x6D
  };
  
  // DS2465 addresses
  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_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 WriteScratchpad(std::uint8_t addr, const std::uint8_t * buf, size_t bufLen) const;
  OneWireMaster::CmdResult CopyScratchpad(bool dest_secret, unsigned int pageNum, bool notFull, unsigned int segmentNum);

  // DS2465 Configuration Commands
  OneWireMaster::CmdResult ReadOneWireConfig(OWConfigAddr addr, std::uint8_t & config) const;
  OneWireMaster::CmdResult WriteOneWireConfig(OWConfigAddr addr, unsigned int ovr, unsigned int std);
  
  // 1-Wire Master Commands
  virtual OneWireMaster::CmdResult OWInitMaster(void);
  virtual OneWireMaster::CmdResult OWReset(void);                                   // Issues a 1-Wire Reset Pulse
  virtual OneWireMaster::CmdResult OWWriteByte(uint8_t sendbyte);
  virtual OneWireMaster::CmdResult OWReadByte(uint8_t & recvbyte);
  virtual OneWireMaster::CmdResult OWTouchByte(uint8_t & sendrecvbyte);
  virtual OneWireMaster::CmdResult OWTouchBit(uint8_t & sendrecvbit);
  virtual OneWireMaster::CmdResult OWWriteBit(uint8_t sendbit);
  virtual OneWireMaster::CmdResult OWReadBit(uint8_t & recvbit);
  virtual OneWireMaster::CmdResult OWBlock(uint8_t *tran_buf, uint8_t tran_len);
  virtual OneWireMaster::CmdResult OWFirst(RomId & romId);
  virtual OneWireMaster::CmdResult OWNext(RomId & romId);
  virtual OneWireMaster::CmdResult OWVerify(const RomId & romId);
  virtual void OWTargetSetup(RomId & romId);
  virtual void OWFamilySkipSetup(void);
  virtual OneWireMaster::CmdResult OWSearch(RomId & romId);
  virtual OneWireMaster::CmdResult OWReadROM(RomId & romId);
  virtual OneWireMaster::CmdResult OWSkipROM(void);
  virtual OneWireMaster::CmdResult OWMatchROM(const RomId & romId);
  virtual OneWireMaster::CmdResult OWOverdriveSkipROM(void);
  virtual OneWireMaster::CmdResult OWResume(void);
  virtual OneWireMaster::CmdResult OWOverdriveMatchROM(const RomId & romId);
  
  // Extended 1-Wire functions
  virtual OneWireMaster::CmdResult OWSpeed(OW_SPEED new_speed);
  virtual OneWireMaster::CmdResult OWLevel(OW_LEVEL new_level);
  virtual OneWireMaster::CmdResult OWWriteBytePower(uint8_t sendbyte);
  virtual OneWireMaster::CmdResult OWReadBytePower(uint8_t & recvbyte);
  virtual OneWireMaster::CmdResult OWReadBitPower(uint8_t applyPowerResponse);
  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);
  virtual OneWireMaster::CmdResult OWReadBlock(uint8_t *rx_buf, uint8_t rx_len);
  OneWireMaster::CmdResult OWPowerDown(void);
  OneWireMaster::CmdResult OWPowerUp(void);
  OneWireMaster::CmdResult ConfigureAPU(bool apu_enable);

  OneWireMaster::CmdResult Triplet(Direction search_direction, uint8_t & status);
  
  //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:
  static const int POLL_LIMIT = 200;
  
  I2C & m_I2C_interface;
  std::uint8_t m_I2C_address;

  std::uint8_t c1WS, cSPU, cPDN, cAPU;
  bool short_detected;

  // Search state
  std::uint8_t m_lastDiscrepancy;
  std::uint8_t m_lastFamilyDiscrepancy;
  std::uint8_t m_lastDeviceFlag;

  //DS2465 Configuration Commands
  OneWireMaster::CmdResult Write_Config(uint8_t config);
  OneWireMaster::CmdResult Write_Command_Reg(uint8_t cmd, uint8_t par, bool poll) const;
};

#endif