Implementation of 1-Wire with added Alarm Search Functionality

Dependents:   Max32630_One_Wire_Interface

Revision:
17:b646b1e3970b
Parent:
13:d1bdb03703de
Child:
22:686273e55cdc
--- a/OneWire_Bridge/DS28E17/ds28e17.cpp	Wed Mar 16 19:10:19 2016 +0000
+++ b/OneWire_Bridge/DS28E17/ds28e17.cpp	Fri Mar 18 20:21:05 2016 +0000
@@ -1,16 +1,4 @@
 /******************************************************************//**
-* @file ds28e17.cpp
-*
-* @author Justin Jordan
-*
-* @version 0.0.0
-*
-* Started: 31JAN16
-*
-* Updated: 
-*
-* @brief Source file for DS28E17 1-wire to I2C bridge
-***********************************************************************
 * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
@@ -50,25 +38,39 @@
 
 //*********************************************************************
 Ds28e17::Ds28e17(OneWireInterface &owm)
-:_p_owm(owm)
+:_owm(owm)
 {
     
 }
 
 
 //*********************************************************************
-bool Ds28e17::I2C_WriteDataWithStop(uint8_t I2C_addr, uint8_t length, 
+void Ds28e17::set_rom_id(const RomId &romId)
+{
+    _romId = romId;
+}
+
+
+//*********************************************************************
+RomId Ds28e17::get_rom_id(void)
+{
+    return _romId;
+}
+
+
+//*********************************************************************
+Ds28e17::CmdResult Ds28e17::I2C_WriteDataWithStop(uint8_t I2C_addr, uint8_t length, 
                                     uint8_t *data, uint8_t &status, 
                                     uint8_t &wr_status)
 {
-    bool rtn_val = false;
+    Ds28e17::CmdResult bridge_result = Ds28e17::OperationFailure;
     
     size_t send_cnt = 0;
-    size_t idx = 0;
-    uint32_t poll_count = 0;
     uint8_t send_block[0xff];
     
-    if(_p_owm.OWMatchROM())
+    OneWireInterface::CmdResult ow_result = _owm.OWMatchROM(_romId);
+    
+    if(ow_result == OneWireInterface::Success)
     {
         //seed the crc
         _crc16 = 0;
@@ -88,7 +90,7 @@
         docrc16(send_block[send_cnt++]);
         
         // Form the write data  
-        for (idx = 0; idx < length; idx++) 
+        for (size_t idx = 0; idx < length; idx++) 
         {
             send_block[send_cnt] = data[idx];
             docrc16(send_block[send_cnt++]);
@@ -100,49 +102,26 @@
         send_block[send_cnt++] = ((uint8_t)((_crc16 >> 8) & 0xFF));
         
         // Send Packet
-        _p_owm.OWWriteBlock(send_block, send_cnt);
-        
-        // Poll for Zero 1-Wire bit and return if an error occurs
-        while(_p_owm.OWReadBit() && (poll_count++ < POLL_LIMIT));
-        
-        if(poll_count < POLL_LIMIT)
-        {
-            //Read Status
-            status = _p_owm.OWReadByte();
-            
-            //Read Write Status
-            wr_status = _p_owm.OWReadByte();
-            
-            rtn_val = true;
-        }
-        else
-        {
-            rtn_val = false;
-        }
-        
-    }
-    else
-    {
-        rtn_val = false;
+        bridge_result = send_packet(send_block, send_cnt, status, wr_status);
     }
     
-    return(rtn_val);
+    return bridge_result;
 }
 
 
 //*********************************************************************
-bool Ds28e17::I2C_WriteDataNoStop(uint8_t I2C_addr, uint8_t length, 
+Ds28e17::CmdResult Ds28e17::I2C_WriteDataNoStop(uint8_t I2C_addr, uint8_t length, 
                                   uint8_t *data, uint8_t &status, 
                                   uint8_t &wr_status)
 {
-    bool rtn_val = false;
+    Ds28e17::CmdResult bridge_result = Ds28e17::OperationFailure;
     
     size_t send_cnt = 0;
-    size_t idx = 0;
-    uint32_t poll_count = 0;
     uint8_t send_block[0xff];
     
-    if(_p_owm.OWMatchROM())
+    OneWireInterface::CmdResult ow_result = _owm.OWMatchROM(_romId);
+    
+    if(ow_result == OneWireInterface::Success)
     {
         // seed the crc
         _crc16 = 0;
@@ -160,7 +139,7 @@
         docrc16(send_block[send_cnt++]);
         
         // Form the write data
-        for(idx = 0; idx < length; idx++)
+        for(size_t idx = 0; idx < length; idx++)
         {
             send_block[send_cnt] = data[idx];
             docrc16(send_block[send_cnt++]);
@@ -172,47 +151,25 @@
         send_block[send_cnt++] = ((uint8_t)((_crc16 >> 8) & 0xFF));
         
         // Send Packet
-        _p_owm.OWBlock(send_block, send_cnt);
-        
-        // Poll for Zero 1-Wire bit and return if an error occurs
-        while(_p_owm.OWReadBit() && (poll_count++ < POLL_LIMIT)); 
-        
-        if(poll_count < POLL_LIMIT)
-        {
-            //Read Status
-            status = _p_owm.OWReadByte();
-            
-            //Read Write Status
-            wr_status = _p_owm.OWReadByte();
-            
-            rtn_val = true;
-        }
-        else
-        {
-            rtn_val = false;
-        }
-    }
-    else
-    {
-        rtn_val = false;
+        bridge_result = send_packet(send_block, send_cnt, status, wr_status);
     }
     
-    return(rtn_val);
+    return bridge_result;
 }
 
 
 //*********************************************************************
-bool Ds28e17::I2C_WriteDataOnly(uint8_t length, uint8_t *data, 
+Ds28e17::CmdResult Ds28e17::I2C_WriteDataOnly(uint8_t length, uint8_t *data, 
                                 uint8_t &status, uint8_t &wr_status)
 {
-    bool rtn_val = false;
+    Ds28e17::CmdResult bridge_result = Ds28e17::OperationFailure;
     
     size_t send_cnt = 0;
-    size_t idx = 0;
-    uint32_t poll_count = 0;
     uint8_t send_block[0xff];
     
-    if(_p_owm.OWMatchROM())
+    OneWireInterface::CmdResult ow_result = _owm.OWMatchROM(_romId);
+    
+    if(ow_result == OneWireInterface::Success)
     {
         // seed the crc
         _crc16 = 0;
@@ -228,7 +185,7 @@
         docrc16(send_block[send_cnt++]);
         
         // Form the write data
-        for (idx = 0; idx < length; idx++) 
+        for (size_t idx = 0; idx < length; idx++) 
         {
             send_block[send_cnt] = data[idx];
             docrc16(send_block[send_cnt++]);
@@ -240,49 +197,25 @@
         send_block[send_cnt++] = ((_crc16 >> 8) & 0xFF);
         
         // Send Packet
-        _p_owm.OWBlock(send_block, send_cnt);
-        
-        // Poll for Zero 1-Wire bit and return if an error occurs
-        while(_p_owm.OWReadBit() && (poll_count++ < POLL_LIMIT));
-        
-        
-        if(poll_count < POLL_LIMIT)
-        {
-            // Read Status
-            status = _p_owm.OWReadByte();
-            
-            // Read Write Status
-            wr_status = _p_owm.OWReadByte();
-            
-            rtn_val = true; 
-        }
-        else
-        {
-            rtn_val = false;
-        }
-    }
-    else
-    {
-        rtn_val = false;
+        bridge_result = send_packet(send_block, send_cnt, status, wr_status);
     }
     
-    
-    return(rtn_val);
+    return bridge_result;
 }
 
 
 //*********************************************************************
-bool Ds28e17::I2C_WriteDataOnlyWithStop(uint8_t length, uint8_t *data, 
+Ds28e17::CmdResult Ds28e17::I2C_WriteDataOnlyWithStop(uint8_t length, uint8_t *data, 
                                         uint8_t &status, uint8_t &wr_status)
 {
-    bool rtn_val = false;
+    Ds28e17::CmdResult bridge_result = Ds28e17::OperationFailure;
     
     size_t send_cnt = 0;
-    size_t idx = 0;
-    uint32_t poll_count = 0;
     uint8_t send_block[0xff];
     
-    if(_p_owm.OWMatchROM())
+    OneWireInterface::CmdResult ow_result = _owm.OWMatchROM(_romId);
+    
+    if(ow_result == OneWireInterface::Success)
     {
         //seed the crc
         _crc16 = 0;
@@ -298,7 +231,7 @@
         docrc16(send_block[send_cnt++]);
     
         // Form the write data
-        for (idx = 0; idx < length; idx++) 
+        for (size_t idx = 0; idx < length; idx++) 
         {
             send_block[send_cnt] = data[idx];
             docrc16(send_block[send_cnt++]);
@@ -310,49 +243,28 @@
         send_block[send_cnt++] = ((_crc16 >> 8) & 0xFF);
     
         // Send Packet
-        _p_owm.OWBlock(send_block, send_cnt);
-    
-        // Poll for Zero 1-Wire bit and return if an error occurs
-        while(_p_owm.OWReadBit() && (poll_count++ < POLL_LIMIT));
+        bridge_result = send_packet(send_block, send_cnt, status, wr_status);
+    } 
     
-        if(poll_count < POLL_LIMIT)
-        {
-            // Read Status
-            status = _p_owm.OWReadByte();
-            
-            // Read Write Status
-            wr_status = _p_owm.OWReadByte();
-            
-            rtn_val = true; 
-        }
-        else
-        {
-            rtn_val = false;
-        }
-    }
-    else
-    {
-        rtn_val = false;
-    }
-    
-    return(rtn_val);
+    return bridge_result;
 }
 
 
 //*********************************************************************
-bool Ds28e17::I2C_WriteReadDataWithStop(uint8_t I2C_addr, uint8_t length, 
+Ds28e17::CmdResult Ds28e17::I2C_WriteReadDataWithStop(uint8_t I2C_addr, uint8_t length, 
                                         uint8_t *data, uint8_t nu_bytes_read, 
                                         uint8_t &status, uint8_t &wr_status, 
                                         uint8_t *read_data)
 {
-    bool rtn_val = false;
+    Ds28e17::CmdResult bridge_result = Ds28e17::OperationFailure;
     
     size_t send_cnt = 0;
     size_t idx = 0;
-    uint32_t poll_count = 0;
     uint8_t send_block[0xff];
     
-    if(_p_owm.OWMatchROM())
+    OneWireInterface::CmdResult ow_result = _owm.OWMatchROM(_romId);
+    
+    if(ow_result == OneWireInterface::Success)
     {
         //seed the crc
         _crc16 = 0;
@@ -388,69 +300,37 @@
         send_block[send_cnt++] = ((_crc16 >> 8) & 0xFF);
     
         // Send Packet
-        _p_owm.OWBlock(send_block, send_cnt);
-    
-        // Poll for Zero 1-Wire bit and return if an error occurs
-        while(_p_owm.OWReadBit() && (poll_count++ < POLL_LIMIT));
-        
-        if(poll_count < POLL_LIMIT)
+        bridge_result = send_packet(send_block, send_cnt, status, wr_status);
+        if(bridge_result == Ds28e17::Success)
         {
-            // Read Status
-            status = _p_owm.OWReadByte();
-            
-            // Read Write Status
-            wr_status = _p_owm.OWReadByte();
-            
-            //seed the crc for read bytes
-            _crc16 = 0;
-        
-            // Read Data
-            for(idx = 0; idx < nu_bytes_read; idx++)
+            ow_result = _owm.OWReadBlock(read_data, nu_bytes_read);
+            if(ow_result == OneWireInterface::Success)
             {
-                read_data[idx] = _p_owm.OWReadByte();
-                docrc16(read_data[idx]);
+                bridge_result = Ds28e17::Success;
             }
-            
-            /********** TODO **********/
-            // need to check datasheet, below was left commented for a reason.  
-            // need to see why
-        
-            // Read the CRC16
-            //docrc16(OWReadByte());  //Receive first crc16 byte
-            //docrc16(OWReadByte());    //Receive second crc16 byte
-        
-            // check CRC16
-            //if (CRC16 != 0xB001)
-            //  return false;
-            
-            rtn_val = true; 
-        }
-        else
-        {
-            rtn_val = false;
+            else
+            {
+                bridge_result = Ds28e17::CommsReadBlockError;
+            }
         }
     }
-    else
-    {
-        rtn_val = false;
-    }
     
-    return(rtn_val);
+    return bridge_result;
 }
 
 
 //*********************************************************************
-bool Ds28e17::I2C_ReadDataWithStop(uint8_t I2C_addr, uint8_t nu_bytes_read, 
+Ds28e17::CmdResult Ds28e17::I2C_ReadDataWithStop(uint8_t I2C_addr, uint8_t nu_bytes_read, 
                                    uint8_t &status, uint8_t *read_data)
 {
-    bool rtn_val = false;
+    Ds28e17::CmdResult  bridge_result = Ds28e17::OperationFailure;
     
     size_t send_cnt = 0;
-    size_t idx = 0;
-    uint32_t poll_count = 0;
     uint8_t send_block[0xff];
     
-    if(_p_owm.OWMatchROM())
+    OneWireInterface::CmdResult ow_result = _owm.OWMatchROM(_romId);
+    
+    if(ow_result == OneWireInterface::Success)
     {
         //seed the crc for transmit bytes
         _crc16 = 0;
@@ -475,153 +355,166 @@
         send_block[send_cnt++] = ((_crc16 >> 8) & 0xFF);
     
         // Send Packet
-        _p_owm.OWBlock(send_block, send_cnt);
-    
-        // Poll for Zero 1-Wire bit and return if an error occurs
-        while(_p_owm.OWReadBit() && (poll_count++ < POLL_LIMIT));
-        
-        if(poll_count < POLL_LIMIT)
+        bridge_result = send_packet(send_block, send_cnt, status);
+        if(bridge_result == Ds28e17::Success)
         {
-            // Read Status
-            status = _p_owm.OWReadByte();
-            
-            //seed the crc for read bytes
-            _crc16 = 0;
-        
-            // Read Data
-            for(idx = 0; idx < nu_bytes_read; idx++)
+            ow_result = _owm.OWReadBlock(read_data, nu_bytes_read);
+            if(ow_result == OneWireInterface::Success)
             {
-                read_data[idx] = _p_owm.OWReadByte();
-                docrc16(read_data[idx]);
+                bridge_result = Ds28e17::Success;
             }
-            
-            /********** TODO **********/
-            // need to check datasheet, below was left commented for a reason.  
-            // need to see why
-        
-            // Read the CRC16
-            //docrc16(OWReadByte());  //Receive first crc16 byte
-            //docrc16(OWReadByte());    //Receive second crc16 byte
-        
-            // check CRC16
-            //if (CRC16 != 0xB001)
-            //  return false;
-            
-            rtn_val = true; 
-        }
-        else
-        {
-            rtn_val = false;
+            else
+            {
+                bridge_result = Ds28e17::CommsReadBlockError;
+            }
         }
     }
-    else
-    {
-        rtn_val = false;
-    }
     
-    return(rtn_val);
+    return bridge_result;
 }
 
 
 //*********************************************************************
-bool Ds28e17::WriteConfigReg(uint8_t data)
+Ds28e17::CmdResult Ds28e17::WriteConfigReg(uint8_t data)
 {
-    bool rtn_val = false;
+    Ds28e17::CmdResult bridge_result = Ds28e17::OperationFailure;
     
-    if(_p_owm.OWMatchROM())
+    OneWireInterface::CmdResult ow_result = _owm.OWMatchROM(_romId);
+    
+    if(ow_result == OneWireInterface::Success)
     {
         // Send CMD and Data
-        _p_owm.OWWriteByte(CMD_WRITE_CONFIG_REG);
-        _p_owm.OWWriteByte(data);
-        _i2c_speed = data & 0x03; // Save off _i2c_speed setting to be used by other functions
-        rtn_val = true;
-    }
-    else
-    {
-        rtn_val = false;
+        uint8_t send_block[] = {CMD_WRITE_CONFIG_REG, data};
+        
+        ow_result = _owm.OWWriteBlock(send_block, 2);
+        if(ow_result == OneWireInterface::Success)
+        {
+            _i2c_speed = data & 0x03; // Save off _i2c_speed setting to be used by other functions
+            bridge_result = Ds28e17::Success;
+        }
+        else
+        {
+            bridge_result = Ds28e17::CommsWriteBlockError;
+        }
     }
     
-    return(rtn_val);
+    return bridge_result;
 }
 
 
 //*********************************************************************
-uint8_t Ds28e17::ReadConfigReg(void)
+Ds28e17::CmdResult Ds28e17::ReadConfigReg(uint8_t & config)
 {
-    uint8_t rtn_val = false;
+    Ds28e17::CmdResult bridge_result = Ds28e17::OperationFailure;
     
-    if(_p_owm.OWMatchROM())
+    OneWireInterface::CmdResult ow_result = _owm.OWMatchROM(_romId);
+    
+    if(ow_result == OneWireInterface::Success)
     {
         // Send CMD and receive Data
-        _p_owm.OWWriteByte(CMD_READ_CONFIG_REG);
-        rtn_val = _p_owm.OWReadByte();
-    }
-    else
-    {
-        rtn_val = false;
+        ow_result = _owm.OWWriteByte(CMD_READ_CONFIG_REG);
+        if(ow_result == OneWireInterface::Success)
+        {
+            ow_result = _owm.OWReadByte(config);
+            if(ow_result == OneWireInterface::Success)
+            {
+                bridge_result = Ds28e17::Success;
+            }
+            else
+            {
+                bridge_result = Ds28e17::CommsReadByteError;
+            }
+        }
+        else
+        {
+            bridge_result = Ds28e17::CommsWriteByteError;
+        }
     }
     
-    return(rtn_val);
+    return bridge_result;
 }
 
 
 //*********************************************************************
-bool Ds28e17::DisableOWMode()
+Ds28e17::CmdResult Ds28e17::DisableOWMode()
 {
-    bool rtn_val = false;
+    Ds28e17::CmdResult bridge_result = Ds28e17::OperationFailure;
     
-    if(_p_owm.OWMatchROM())
+    OneWireInterface::CmdResult ow_result = _owm.OWMatchROM(_romId);
+    
+    if(ow_result == OneWireInterface::Success)
     {
         // Send CMD
-        _p_owm.OWWriteByte(CMD_DISABLE_OW_MODE);
-        rtn_val = true;
-    }
-    else
-    {
-        rtn_val = false;
+        ow_result = _owm.OWWriteByte(CMD_DISABLE_OW_MODE);
+        if(ow_result == OneWireInterface::Success)
+        {
+            bridge_result = Ds28e17::Success;
+        }
+        else
+        {
+            bridge_result = Ds28e17::CommsWriteByteError;
+        }
     }
     
-    return(rtn_val);
+    return bridge_result;
 }
 
 
 //*********************************************************************
-bool Ds28e17::EnableSleepMode()
+Ds28e17::CmdResult Ds28e17::EnableSleepMode()
 {
-    bool rtn_val = false;
+    Ds28e17::CmdResult bridge_result = Ds28e17::OperationFailure;
     
-    if(_p_owm.OWMatchROM())
+    OneWireInterface::CmdResult ow_result = _owm.OWMatchROM(_romId);
+    
+    if(ow_result == OneWireInterface::Success)
     {
         // Send CMD
-        _p_owm.OWWriteByte(CMD_ENABLE_SLEEP_MODE);
-        rtn_val = true;
-    }
-    else
-    {
-        rtn_val = false;
+        ow_result = _owm.OWWriteByte(CMD_ENABLE_SLEEP_MODE);
+        if(ow_result == OneWireInterface::Success)
+        {
+            bridge_result = Ds28e17::Success;
+        }
+        else
+        {
+            bridge_result = Ds28e17::CommsWriteByteError;
+        }
     }
     
-    return(rtn_val);
+    return bridge_result;
 }
 
 
 //*********************************************************************
-uint8_t Ds28e17::ReadDeviceRevision(void)
+Ds28e17::CmdResult Ds28e17::ReadDeviceRevision(uint8_t & rev)
 {
-    uint8_t rtn_val;
+    Ds28e17::CmdResult bridge_result = Ds28e17::OperationFailure;
     
-    if(_p_owm.OWMatchROM())
+    OneWireInterface::CmdResult ow_result = _owm.OWMatchROM(_romId);
+    
+    if(ow_result == OneWireInterface::Success)
     {
         // Send CMD and receive Data
-        _p_owm.OWWriteByte(CMD_READ_DEVICE_REV);
-        rtn_val = _p_owm.OWReadByte();
-    }
-    else
-    {
-        rtn_val = false;
+        ow_result = _owm.OWWriteByte(CMD_READ_DEVICE_REV);
+        if(ow_result == OneWireInterface::Success)
+        {
+            ow_result = _owm.OWReadByte(rev);
+            if(ow_result == OneWireInterface::Success)
+            {
+                bridge_result = Ds28e17::Success;
+            }
+            else
+            {
+                bridge_result = Ds28e17::CommsReadByteError;
+            }
+        }
+        else
+        {
+            bridge_result = Ds28e17::CommsWriteByteError;
+        }
     }
     
-    return(rtn_val);
+    return bridge_result;
 }
 
 
@@ -645,3 +538,105 @@
 }
 
 
+//*********************************************************************
+Ds28e17::CmdResult Ds28e17::send_packet(const uint8_t * data, uint8_t data_length,
+                                        uint8_t & status, uint8_t & wr_status)
+{
+    Ds28e17::CmdResult bridge_result = Ds28e17::CommsWriteBlockError;
+    uint32_t poll_count = 0;
+    
+    OneWireInterface::CmdResult ow_result = _owm.OWWriteBlock(data, data_length);
+    
+    if(ow_result == OneWireInterface::Success) 
+    {
+        // Poll for Zero 1-Wire bit and return if an error occurs
+        uint8_t recvbit = 0x01;
+        do 
+        {
+            ow_result = _owm.OWReadBit(recvbit);
+        } 
+        while(recvbit && (poll_count++ < POLL_LIMIT) && (ow_result == OneWireInterface::Success));
+
+        if(ow_result == OneWireInterface::Success) 
+        {
+            if(poll_count < POLL_LIMIT) 
+            {
+                //Read Status and write status
+                uint8_t read_block[2];
+                
+                ow_result = _owm.OWReadBlock(read_block, 2);
+                
+                if(ow_result == OneWireInterface::Success) 
+                {
+                    status = read_block[0];
+                    wr_status = read_block[1];
+                    bridge_result = Ds28e17::Success;
+                } 
+                else
+                {
+                   bridge_result = Ds28e17::CommsReadBlockError;
+                }
+            } 
+            else 
+            {
+                bridge_result = Ds28e17::TimeoutError;
+            }
+        } 
+        else 
+        {
+            bridge_result = Ds28e17::CommsReadBitError;
+        }
+    } 
+    
+    return bridge_result;
+}
+
+
+//*********************************************************************
+Ds28e17::CmdResult Ds28e17::send_packet(const uint8_t * data, uint8_t data_length, 
+                                        uint8_t & status)
+{
+    Ds28e17::CmdResult bridge_result = Ds28e17::CommsWriteBlockError;
+    uint32_t poll_count = 0;
+    
+    OneWireInterface::CmdResult ow_result = _owm.OWWriteBlock(data, data_length);
+    
+    if(ow_result == OneWireInterface::Success) 
+    {
+        // Poll for Zero 1-Wire bit and return if an error occurs
+        uint8_t recvbit = 0x01;
+        do 
+        {
+            ow_result = _owm.OWReadBit(recvbit);
+        } 
+        while(recvbit && (poll_count++ < POLL_LIMIT) && (ow_result == OneWireInterface::Success));
+
+        if(ow_result == OneWireInterface::Success) 
+        {
+            if(poll_count < POLL_LIMIT) 
+            {
+                //Read Status 
+                ow_result = _owm.OWReadByte(status);
+                if(ow_result == OneWireInterface::Success) 
+                {
+                    bridge_result = Ds28e17::Success;
+                } 
+                else
+                {
+                    bridge_result = Ds28e17::CommsReadByteError;
+                }
+            } 
+            else 
+            {
+                bridge_result = Ds28e17::TimeoutError;
+            }
+        } 
+        else 
+        {
+            bridge_result = Ds28e17::CommsReadBitError;
+        }
+    } 
+    
+    return bridge_result;
+}
+