Implementation of 1-Wire with added Alarm Search Functionality

Dependents:   Max32630_One_Wire_Interface

Revision:
47:307dc45952db
Parent:
35:5d23395628f6
Child:
48:6f9208ae280e
--- a/OneWire_Masters/DS2465/DS2465.cpp	Wed Apr 06 10:06:06 2016 -0500
+++ b/OneWire_Masters/DS2465/DS2465.cpp	Thu Apr 07 10:26:26 2016 -0500
@@ -37,7 +37,6 @@
 
 static const int I2C_WRITE_OK = 0;
 
-
 std::uint8_t DS2465::Config::readByte() const
 {
   std::uint8_t config = 0;
@@ -64,100 +63,59 @@
   cAPU = true;
 }
 
-
-
-
 DS2465::DS2465(I2C & I2C_interface, std::uint8_t I2C_address)
   : m_I2C_interface(I2C_interface), m_I2C_address(I2C_address)
 {
   
 }
 
-
-
-
 OneWireMaster::CmdResult DS2465::OWInitMaster()
 {
-  return detect();
+  OneWireMaster::CmdResult result;
+  
+  // reset DS2465 
+  result = reset();
+  if (result != OneWireMaster::Success)
+    return result;
+
+  // write the default configuration setup
+  Config defaultConfig;
+  result = writeConfig(defaultConfig, true);
+  return result;
 }
 
-
-//--------------------------------------------------------------------------
-// Compute Next Master Secret DS2465 
-//
-// 'swap' - 1 if swapping a page into the computation
-// 'page' - page number to swap in
-// 'region' - (1) first 1/2 page, (2) second 1/2 page, (3) entire page 
-//
-// Returns: true write successful
-//          false failure to complete read
-//
 OneWireMaster::CmdResult DS2465::computeNextMasterSecret(bool swap, unsigned int pageNum, PageRegion region)
 {
    std::uint8_t command[2] = { CMD_CNMS, (swap ? (0xC8 | (pageNum << 4) | region) : 0xBF) };
    return writeMemory(ADDR_CMD_REG, command, 2);
 }
 
-//--------------------------------------------------------------------------
-// Compute Write MAC DS2465 
-//
-// 'regwrite' - true if writing to a register, false if regular memory
-// 'swap' - true if swapping a page into the computation
-// 'page' - page number to swap in
-// 'segment' - segment number if swaping 
-//
-// Returns: true write successful
-//          false failure to complete read
-//
 OneWireMaster::CmdResult DS2465::computeWriteMac(bool regwrite, bool swap, unsigned int pageNum, unsigned int segmentNum) const
 {
    std::uint8_t command[2] = { CMD_CSWM, ((regwrite << 7) | (swap << 6) | (pageNum << 4) | segmentNum) };
    return cWriteMemory(ADDR_CMD_REG, command, 2);
 }
 
-//--------------------------------------------------------------------------
-// Compute Slave Authentication MAC DS2465 
-//
-// 'swap' - true if swapping a page into the computation
-// 'page' - page number to swap in
-// 'region' - (1) first 1/2 page, (2) second 1/2 page, (3) entire page 
-//
-// Returns: true write successful
-//          false failure to complete read
-//
 OneWireMaster::CmdResult DS2465::computeAuthMac(bool swap, unsigned int pageNum, PageRegion region) const
 {
    std::uint8_t command[2] = { CMD_CSAM, (swap ? (0xC8 | (pageNum << 4) | region) : 0xBF) };
    return cWriteMemory(ADDR_CMD_REG, command, 2);
 }
 
-//--------------------------------------------------------------------------
-// Compute S-Secret on DS2465 
-//
-// 'swap' - true if swapping a page into the computation
-// 'page' - page number to swap in
-// 'region' - (1) first 1/2 page, (2) second 1/2 page, (3) entire page 
-//
-// Returns: true write successful
-//          false failure to complete read
-//
 OneWireMaster::CmdResult DS2465::computeSlaveSecret(bool swap, unsigned int pageNum, PageRegion region)
 {
    std::uint8_t command[2] = { CMD_CSS, (swap ? (0xC8 | (pageNum << 4) | region) : 0xBF) };
    return writeMemory(ADDR_CMD_REG, command, 2);
 }
 
-
-
-
 ISha256MacCoprocessor::CmdResult DS2465::setMasterSecret(const Secret & secret)
 {
   OneWireMaster::CmdResult result;
   result = writeMemory(ADDR_SPAD, secret, secret.length);
   if (result == OneWireMaster::Success)
-    result = copyScratchpad(1, 0, 1, 0);
+    result = copyScratchpadToSecret();
   if (result == OneWireMaster::Success)
-    wait_ms(8 * eepromWriteDelayMs);
+    wait_ms(eepromPageWriteDelayMs);
   return (result == OneWireMaster::Success ? ISha256MacCoprocessor::Success : ISha256MacCoprocessor::OperationFailure);
 }
 
@@ -168,7 +126,7 @@
   result = writeScratchpad(writeMacData, writeMacData.length);
   // Compute MAC
   if (result == OneWireMaster::Success)
-    result = computeWriteMac(false, false, 0, 0);
+    result = computeWriteMac(false);
   if (result == OneWireMaster::Success)
   {
     wait_ms(shaComputationDelayMs);
@@ -196,7 +154,7 @@
   }
   // Compute MAC
   if (result == OneWireMaster::Success)
-    result = computeAuthMac(false, 0, REGION_FULL_PAGE);
+    result = computeAuthMac();
   if (result == OneWireMaster::Success)
   {
     wait_ms(shaComputationDelayMs * 2);
@@ -224,32 +182,18 @@
   }
   // Compute secret
   if (result == OneWireMaster::Success)
-    result = computeSlaveSecret(false, 0, REGION_FULL_PAGE);
+    result = computeSlaveSecret();
   if (result == OneWireMaster::Success)
     wait_ms(shaComputationDelayMs * 2);
   return (result == OneWireMaster::Success ? ISha256MacCoprocessor::Success : ISha256MacCoprocessor::OperationFailure);
 }
 
-//--------------------------------------------------------------------------
-// Copy Scratchpad on DS2465 to either secret or memory page
-//
-// 'destSecret' - 1 if destination is secret, 0 if memory page
-// 'page' - page number if destSecret=0
-// 'notfull' - 0 if only 4 byte segment, 1 if writing to full page, 
-// 'seg' - Segment number if full=0.
-//
-// Returns: true write successful
-//          false failure to complete read
-//
 OneWireMaster::CmdResult DS2465::copyScratchpad(bool destSecret, unsigned int pageNum, bool notFull, unsigned int segmentNum)	
 {
   std::uint8_t command[2] = { CMD_CPS, (destSecret ? 0 : (0x80 | (pageNum << 4) | (notFull << 3) | segmentNum)) };
   return writeMemory(ADDR_CMD_REG, command, 2);
 }
 
-
-
-
 OneWireMaster::CmdResult DS2465::configureLevel(OWLevel level)
 {
   OneWireMaster::CmdResult result;
@@ -266,17 +210,6 @@
   return result;
 }
 
-//--------------------------------------------------------------------------
-// Set the 1-Wire Net line level pull-up to normal. The DS2465 does only
-// allows enabling strong pull-up on a bit or byte event. Consequently this
-// function only allows the MODE_STANDARD argument. To enable strong pull-up
-// use OWWriteBytePower or OWReadBitPower.  
-//
-// 'new_level' - new level defined as
-//                MODE_STANDARD     0x00
-//
-// Returns:  current 1-Wire Net level
-//
 OneWireMaster::CmdResult DS2465::OWSetLevel(OWLevel new_level)
 {
   if (new_level == LEVEL_STRONG)
@@ -285,15 +218,6 @@
   return configureLevel(new_level);
 }
 
-//--------------------------------------------------------------------------
-// Set the 1-Wire Net communication speed.
-//
-// 'new_speed' - new speed defined as
-//                MODE_STANDARD   0x00
-//                MODE_OVERDRIVE  0x01
-//
-// Returns:  current 1-Wire Net speed
-//
 OneWireMaster::CmdResult DS2465::OWSetSpeed(OWSpeed new_speed)
 {
   // Requested speed is already set
@@ -308,14 +232,6 @@
   return writeConfig(newConfig, true);
 }
 
-//--------------------------------------------------------------------------
-// Use the DS2465 help command '1-Wire triplet' to perform one bit of a 1-Wire
-// search. This command does two read bits and one write bit. The write bit
-// is either the default direction (all device have same bit) or in case of 
-// a discripancy, the 'search_direction' parameter is used. 
-//
-// Returns � The DS2465 status byte result from the triplet command
-//
 OneWireMaster::CmdResult DS2465::OWTriplet(SearchDirection & search_direction, std::uint8_t & sbr, std::uint8_t & tsb) 
 {
   // 1-Wire Triplet (Case B)
@@ -343,15 +259,6 @@
   return result;
 }
 
-//--------------------------------------------------------------------------
-// The 'OWReadBlock' receives a block of data from the
-// 1-Wire Net. The destination is the mac buffer (rx_mac=1) or 
-// the scratchpad (rx_mac=0). The result is buffer is returned. 
-//
-// 'rx_buf'   - pointer to a block to receive bytes
-//              of length 'rx_len' from 1-Wire Net
-// 'rx_len' - length in bytes to read. Only valid numbers are 8,16,20,32;
-//
 OneWireMaster::CmdResult DS2465::OWReadBlock(std::uint8_t *rx_buf, std::uint8_t rx_len)	
 {
   // 1-Wire Receive Block (Case A)
@@ -371,26 +278,16 @@
   return result;
 }
 
-
-
-
 OneWireMaster::CmdResult DS2465::OWWriteBlock(const std::uint8_t *tran_buf, std::uint8_t tran_len)
 {
   return OWWriteBlock(false, tran_buf, tran_len);
 }
 
-//--------------------------------------------------------------------------
-// The 'OWWriteBlock' transfers a block of data to the
-// 1-Wire Net. The mac buffer can be sent (tx_mac=1) or a
-// portion of the scratchpad can be sent. 
-//
-// 'tx_mac'   - flag to indicate if the MAC buffer is to be sent (1) or
-//              the data provided in teh tran_buf is to be sent (0)
-// 'tran_buf' - pointer to a block of bytes
-//              of length 'tran_len' that will be sent
-//              to the 1-Wire Net
-// 'tran_len' - length in bytes to transfer. Only valid numbers are 8,16,20,32;
-//
+OneWireMaster::CmdResult DS2465::OWWriteBlockMac()
+{
+  return OWWriteBlock(true, NULL, 0);
+}
+
 OneWireMaster::CmdResult DS2465::OWWriteBlock(bool tx_mac, const std::uint8_t *tran_buf, std::uint8_t tran_len)
 {
   OneWireMaster::CmdResult result;
@@ -417,12 +314,6 @@
   return result;
 }
 
-//--------------------------------------------------------------------------
-// Send 8 bits of read communication to the 1-Wire Net and return the
-// result 8 bits read from the 1-Wire Net.
-//
-// Returns:  8 bits read from 1-Wire Net
-//
 OneWireMaster::CmdResult DS2465::OWReadByte(std::uint8_t & recvbyte, OWLevel after_level)	
 {
   OneWireMaster::CmdResult result;
@@ -456,16 +347,6 @@
   return result;
 }
 
-//--------------------------------------------------------------------------
-// Send 8 bits of communication to the 1-Wire Net and verify that the
-// 8 bits read from the 1-Wire Net is the same (write operation).
-// The parameter 'sendbyte' least significant 8 bits are used.
-//
-// 'sendbyte' - 8 bits to send (least significant byte)
-//
-// Returns:  true: bytes written and echo was the same
-//           false: echo was not the same
-//
 OneWireMaster::CmdResult DS2465::OWWriteByte(std::uint8_t sendbyte, OWLevel after_level)	
 {    
   // 1-Wire Write Byte (Case B)
@@ -490,17 +371,6 @@
   return result;
 }
 
-//--------------------------------------------------------------------------
-// Send 1 bit of communication to the 1-Wire Net and return the
-// result 1 bit read from the 1-Wire Net.  The parameter 'sendbit'
-// least significant bit is used and the least significant bit
-// of the result is the return bit.
-//
-// 'sendbit' - the least significant bit is the bit to send
-//
-// Returns: 0:   0 bit read from sendbit
-//          1:   1 bit read from sendbit
-//
 OneWireMaster::CmdResult DS2465::OWTouchBit(std::uint8_t & sendrecvbit, OWLevel after_level)	
 {
   // 1-Wire bit (Case B)
@@ -530,16 +400,6 @@
   return result;
 }
 
-//--------------------------------------------------------------------------
-// Write to Scratchpad (SRAM) memory on the DS2465
-//
-// 'addr' - address to start writing (must be in SRAM)
-// 'buf' - buffer of data to write
-// 'len' - length to write
-//
-// Returns: true write successful
-//          false failure to complete write
-//
 OneWireMaster::CmdResult DS2465::cWriteMemory(std::uint8_t addr, const std::uint8_t * buf, std::size_t bufLen) const
 {
   int i;
@@ -574,71 +434,52 @@
   }
   m_I2C_interface.stop();
    
-   return OneWireMaster::Success;
+  return OneWireMaster::Success;
 }
 
-//--------------------------------------------------------------------------
-// Read memory from the DS2465
-//
-// 'addr' - address to start reading
-// 'buf' - buffer to hold memory read
-// 'len' - length to read
-// 'skipSetPointer' - flag to indicate to skip setting address pointer
-//
-// Returns: true read successful
-//          false failure to complete read
-//
 OneWireMaster::CmdResult DS2465::readMemory(std::uint8_t addr, std::uint8_t * buf, std::size_t bufLen, bool skipSetPointer) const
 {
-   int i;
+  int i;
 
-   // Read (Case A)
-   //   S AD,0 [A] MA [A] Sr AD,1 [A] [DD] A [DD] A\ P
-   //                                 \-----/
-   //                                   Repeat for each data byte, NAK last byte
-   //  [] indicates from slave
-   //  MA memory address
-   //  DD memory data read
+  // Read (Case A)
+  //   S AD,0 [A] MA [A] Sr AD,1 [A] [DD] A [DD] A\ P
+  //                                 \-----/
+  //                                   Repeat for each data byte, NAK last byte
+  //  [] indicates from slave
+  //  MA memory address
+  //  DD memory data read
 
-      m_I2C_interface.start();
-      if (!skipSetPointer)
-      {
-         if (m_I2C_interface.write((m_I2C_address | I2C_WRITE)) != I2C_WRITE_OK)
-         {
-            m_I2C_interface.stop();
-            return OneWireMaster::CommunicationWriteError;
-         }
-         if (m_I2C_interface.write(addr) != I2C_WRITE_OK)
-         {
-            m_I2C_interface.stop();
-            return OneWireMaster::CommunicationWriteError;
-         }
-         m_I2C_interface.start();
-      }
+  m_I2C_interface.start();
+  if (!skipSetPointer)
+  {
+     if (m_I2C_interface.write((m_I2C_address | I2C_WRITE)) != I2C_WRITE_OK)
+     {
+        m_I2C_interface.stop();
+        return OneWireMaster::CommunicationWriteError;
+     }
+     if (m_I2C_interface.write(addr) != I2C_WRITE_OK)
+     {
+        m_I2C_interface.stop();
+        return OneWireMaster::CommunicationWriteError;
+     }
+     m_I2C_interface.start();
+  }
 
-      if (m_I2C_interface.write((m_I2C_address | I2C_READ)) != I2C_WRITE_OK)
-      {
-         m_I2C_interface.stop();
-         return OneWireMaster::CommunicationWriteError;
-      }
-      // loop to read each byte, NAK last byte
-      for (i = 0; i < bufLen; i++)
-      {
-         buf[i] = m_I2C_interface.read((i == (bufLen - 1)) ? m_I2C_interface.NoACK : m_I2C_interface.ACK);
-      }
-      m_I2C_interface.stop();
+  if (m_I2C_interface.write((m_I2C_address | I2C_READ)) != I2C_WRITE_OK)
+  {
+     m_I2C_interface.stop();
+     return OneWireMaster::CommunicationWriteError;
+  }
+  // loop to read each byte, NAK last byte
+  for (i = 0; i < bufLen; i++)
+  {
+     buf[i] = m_I2C_interface.read((i == (bufLen - 1)) ? m_I2C_interface.NoACK : m_I2C_interface.ACK);
+  }
+  m_I2C_interface.stop();
   
-   return OneWireMaster::Success;
+  return OneWireMaster::Success;
 }
 
-//--------------------------------------------------------------------------
-// Write the configuration register in the DS2465. The configuration 
-// options are provided in the lower nibble of the provided config byte. 
-// The uppper nibble in bitwise inverted when written to the DS2465.
-//  
-// Returns:  true: config written and response correct
-//           false: response incorrect
-//
 OneWireMaster::CmdResult DS2465::writeConfig(const Config & config, bool verify)
 {
   std::uint8_t configBuf;
@@ -665,47 +506,33 @@
   return result;
 }
 
-
-
 DS2465::Config DS2465::currentConfig() const
 {
   return m_curConfig;
 }
 
-
-
-
 OneWireMaster::CmdResult DS2465::pollBusy(std::uint8_t * pStatus)
 {
-    const unsigned int pollLimit = 200;
-    
-    OneWireMaster::CmdResult result;
-    std::uint8_t status;
-    unsigned int pollCount = 0;
-    
-   // loop checking 1WB bit for completion of 1-Wire operation 
-   // abort if poll limit reached
+  const unsigned int pollLimit = 200;
+
+  OneWireMaster::CmdResult result;
+  std::uint8_t status;
+  unsigned int pollCount = 0;
    
-   do
-   {
-      result = readMemory(ADDR_STATUS_REG, &status, 1, true);
-      if (result != OneWireMaster::Success)
-        return result;
-      if (pStatus != NULL)
-          *pStatus = status;
-      if (pollCount++ >= pollLimit)
-        return OneWireMaster::TimeoutError;
-   } while (status & STATUS_1WB);
+  do
+  {
+    result = readMemory(ADDR_STATUS_REG, &status, 1, true);
+    if (result != OneWireMaster::Success)
+      return result;
+    if (pStatus != NULL)
+        *pStatus = status;
+    if (pollCount++ >= pollLimit)
+      return OneWireMaster::TimeoutError;
+  } while (status & STATUS_1WB);
 
-   return OneWireMaster::Success;
+  return OneWireMaster::Success;
 }
 
-//--------------------------------------------------------------------------
-// Reset all of the devices on the 1-Wire Net and return the result.
-//
-// Returns: true(1):  presense pulse(s) detected, device(s) reset
-//          false(0): no presense pulses detected
-//
 OneWireMaster::CmdResult DS2465::OWReset(void)
 {     
   // 1-Wire reset (Case B)
@@ -760,18 +587,3 @@
 
   return result;
 }
-
-OneWireMaster::CmdResult DS2465::detect()
-{
-  OneWireMaster::CmdResult result;
-  
-  // reset DS2465 
-  result = reset();
-  if (result != OneWireMaster::Success)
-    return result;
-
-  // write the default configuration setup
-  Config defaultConfig;
-  result = writeConfig(defaultConfig, true);
-  return result;
-}
\ No newline at end of file