Implementation of 1-Wire with added Alarm Search Functionality

Dependents:   Max32630_One_Wire_Interface

Revision:
26:a361e3f42ba5
Parent:
23:e8e403d61359
Child:
27:d5aaefa252f1
diff -r bdb1c5a53b58 -r a361e3f42ba5 OneWire_Masters/DS248x/ds248x.cpp
--- a/OneWire_Masters/DS248x/ds248x.cpp	Tue Mar 22 15:18:00 2016 -0500
+++ b/OneWire_Masters/DS248x/ds248x.cpp	Wed Mar 23 15:25:40 2016 -0500
@@ -397,10 +397,14 @@
 
 
 //*********************************************************************
-OneWireMaster::CmdResult Ds248x::OWTouchBit(uint8_t & sendrecvbit)
+OneWireMaster::CmdResult Ds248x::OWTouchBit(uint8_t & sendrecvbit, OW_LEVEL after_level)
 {
     OneWireMaster::CmdResult result;
     
+    result = ConfigureSPU(after_level == LEVEL_STRONG);
+    if (result != OneWireMaster::Success)
+        return result;
+    
     uint8_t poll_count = 0;
     char status;
     char packet[] = {CMD_1WSB, sendrecvbit ? 0x80 : 0x00};
@@ -463,10 +467,14 @@
 
 
 //*********************************************************************
-OneWireMaster::CmdResult Ds248x::OWWriteByte(uint8_t sendbyte)
+OneWireMaster::CmdResult Ds248x::OWWriteByte(uint8_t sendbyte, OW_LEVEL after_level)
 {
     OneWireMaster::CmdResult result;
     
+    result = ConfigureSPU(after_level == LEVEL_STRONG);
+    if (result != OneWireMaster::Success)
+        return result;
+    
     uint8_t poll_count = 0;
     char status;
     char packet [] = {CMD_1WWB, sendbyte};
@@ -519,72 +527,89 @@
 
 
 //*********************************************************************
-OneWireMaster::CmdResult Ds248x::OWReadByte(uint8_t & recvbyte)
+OneWireMaster::CmdResult Ds248x::OWReadByte(uint8_t & recvbyte, OW_LEVEL after_level)
 {
     OneWireMaster::CmdResult result;
     
-    uint8_t poll_count = 0;
-    char data, status;
-    char packet[2] = {CMD_1WRB, 0};
-
-    // 1-Wire Read Bytes (Case C)
-    //   S AD,0 [A] 1WRB [A] Sr AD,1 [A] [Status] A [Status] A\
-    //                                   \--------/
-    //                     Repeat until 1WB bit has changed to 0
-    //   Sr AD,0 [A] SRP [A] E1 [A] Sr AD,1 [A] DD A\ P
-    //
-    //  [] indicates from slave
-    //  DD data read
-
-    if(_p_i2c_bus->write(_w_adrs, packet, 1) != I2C_WRITE_OK)
+    if (after_level == LEVEL_STRONG) // Enabling strong pull-up after a Read Byte command is not supported natively by the DS248x
     {
-        result = OneWireMaster::CommunicationWriteError;
+        uint8_t recvbit;
+        recvbyte = 0;
+        
+        for (unsigned int i = 1; i <= 8; i++)
+        {
+            // Set strong pull-up on last bit
+            result = OWReadBit(recvbit, (i == 8 ? LEVEL_STRONG : LEVEL_NORMAL));
+            if (result != Success)
+                break;
+            recvbyte = (recvbyte << 1) | recvbit;
+        }
     }
     else
     {
-        // loop checking 1WB bit for completion of 1-Wire operation
-        // abort if poll limit reached
-        //dummy write for loop 
-        result = OneWireMaster::Success; //so far
-        do 
+        uint8_t poll_count = 0;
+        char data, status;
+        char packet[2] = {CMD_1WRB, 0};
+
+        // 1-Wire Read Bytes (Case C)
+        //   S AD,0 [A] 1WRB [A] Sr AD,1 [A] [Status] A [Status] A\
+        //                                   \--------/
+        //                     Repeat until 1WB bit has changed to 0
+        //   Sr AD,0 [A] SRP [A] E1 [A] Sr AD,1 [A] DD A\ P
+        //
+        //  [] indicates from slave
+        //  DD data read
+
+        if(_p_i2c_bus->write(_w_adrs, packet, 1) != I2C_WRITE_OK)
         {
-            if(_p_i2c_bus->read(_r_adrs, &status, 1) != I2C_READ_OK)
-            {
-                result = OneWireMaster::CommunicationReadError;
-            }
-        } 
-        while ((status & STATUS_1WB) && (poll_count++ < POLL_LIMIT) && (result != OneWireMaster::CommunicationReadError));
-        
-        if((result == OneWireMaster::CommunicationReadError) || (poll_count >= POLL_LIMIT))
-        {
-            // check for failure due to poll limit reached
-            if (poll_count >= POLL_LIMIT) 
-            {
-                // handle error
-                // ...
-                reset();
-                result = OneWireMaster::TimeoutError;
-            }
+            result = OneWireMaster::CommunicationWriteError;
         }
         else
         {
-            packet[0] = CMD_SRP;
-            packet[1] = 0xE1;
-        
-            if(_p_i2c_bus->write(_w_adrs, packet, 2) != I2C_WRITE_OK)
+            // loop checking 1WB bit for completion of 1-Wire operation
+            // abort if poll limit reached
+            //dummy write for loop 
+            result = OneWireMaster::Success; //so far
+            do 
             {
-                result = OneWireMaster::CommunicationWriteError;
+                if(_p_i2c_bus->read(_r_adrs, &status, 1) != I2C_READ_OK)
+                {
+                    result = OneWireMaster::CommunicationReadError;
+                }
+            } 
+            while ((status & STATUS_1WB) && (poll_count++ < POLL_LIMIT) && (result != OneWireMaster::CommunicationReadError));
+            
+            if((result == OneWireMaster::CommunicationReadError) || (poll_count >= POLL_LIMIT))
+            {
+                // check for failure due to poll limit reached
+                if (poll_count >= POLL_LIMIT) 
+                {
+                    // handle error
+                    // ...
+                    reset();
+                    result = OneWireMaster::TimeoutError;
+                }
             }
             else
             {
-                if(_p_i2c_bus->read(_r_adrs, &data, 1) != I2C_READ_OK)
+                packet[0] = CMD_SRP;
+                packet[1] = 0xE1;
+            
+                if(_p_i2c_bus->write(_w_adrs, packet, 2) != I2C_WRITE_OK)
                 {
-                    result = OneWireMaster::CommunicationReadError;
+                    result = OneWireMaster::CommunicationWriteError;
                 }
                 else
                 {
-                    recvbyte = data;
-                    result = OneWireMaster::Success;
+                    if(_p_i2c_bus->read(_r_adrs, &data, 1) != I2C_READ_OK)
+                    {
+                        result = OneWireMaster::CommunicationReadError;
+                    }
+                    else
+                    {
+                        recvbyte = data;
+                        result = OneWireMaster::Success;
+                    }
                 }
             }
         }
@@ -601,7 +626,7 @@
     
     for(uint8_t idx = 0; idx < tran_len; idx++)
     {
-        result = OWWriteByte(tran_buf[idx]);
+        result = OneWireMaster::OWWriteByte(tran_buf[idx]);
         if(result != OneWireMaster::Success)
         {
             break;
@@ -620,7 +645,7 @@
     for(uint8_t idx = 0; idx < rx_len; idx++)
     {
         //OwReadByte() uses pass by reference
-        result = OWReadByte(rx_buf[idx]);
+        result = OneWireMaster::OWReadByte(rx_buf[idx]);
         if(result != OneWireMaster::Success)
         {
             break;
@@ -664,7 +689,7 @@
       }
 
       // issue the search command 
-      OWWriteByte(0xF0);  
+      OneWireMaster::OWWriteByte(0xF0);  
 
       // loop to do the search
       do
@@ -799,82 +824,19 @@
 }
 
 
-//*********************************************************************
-OneWireMaster::CmdResult Ds248x::OWWriteBytePower(uint8_t sendbyte)
-{
-    OneWireMaster::CmdResult result;
-    
-    // set strong pull-up enable
-    _cSPU = CONFIG_SPU;
-    
-    // write the new config
-    result = write_config(_c1WS | _cSPU | _cPDN | _cAPU);
-    if (result == OneWireMaster::Success)
-    {
-        // perform write byte
-        result = OWWriteByte(sendbyte);
-    }
-    
-    return result;
-}
-
-
-//*********************************************************************
-OneWireMaster::CmdResult Ds248x::OWReadBitPower(uint8_t applyPowerResponse)
+OneWireMaster::CmdResult Ds248x::ConfigureSPU(bool spu_enable)
 {
-    OneWireMaster::CmdResult result;
-    uint8_t rdbit;
-
-    // set strong pull-up enable
-    _cSPU = CONFIG_SPU;
-
-    // write the new config
-    result = write_config(_c1WS | _cSPU | _cPDN | _cAPU);
-    if (result == OneWireMaster::Success)
-    {
-        // perform read bit
-        result = OWReadBit(rdbit);
-        if(result == OneWireMaster::Success)
-        {
-            // check if response was correct, if not then turn off strong pull-up
-            if (rdbit != applyPowerResponse) 
-            {
-                OWLevel(LEVEL_NORMAL);
-                result = OneWireMaster::OperationFailure;
-            }
-        }
-    }
-
-    return result;
-}
-
-
-OneWireMaster::CmdResult Ds248x::OWReadBytePower(uint8_t & recvbyte)
-{
-    OneWireMaster::CmdResult result = Success;
-    uint8_t recvbit;
-    
-    recvbyte = 0;
-    for (unsigned int i = 1; i <= 8; i++)
-    {
-        // Set strong pull-up on last bit
-        if (i == 8)
-        {
-            // set strong pull-up enable
-            _cSPU = CONFIG_SPU;
-
-            // write the new config
-            result = write_config(_c1WS | _cSPU | _cPDN | _cAPU);
-            if (result != Success)
-                break;
-        }
-        result = OWReadBit(recvbit);
-        if (result != Success)
-            break;
-        recvbyte = (recvbyte << 1) | recvbit;
-    }
-    
-    return result;
+  OneWireMaster::CmdResult result;
+  if ((_cSPU == CONFIG_SPU) != spu_enable)
+  {
+    _cSPU = spu_enable;
+    result =  write_config(_c1WS | _cSPU | _cPDN | _cAPU);
+  }
+  else
+  {
+    result = OneWireMaster::Success;
+  }
+  return result;
 }