Implementation of 1-Wire with added Alarm Search Functionality

Dependents:   Max32630_One_Wire_Interface

Revision:
90:c233d1c265ff
Parent:
86:2ce08ca58b9e
Child:
104:3f48daed532b
diff -r 3a0e12c9b898 -r c233d1c265ff Masters/OneWireMaster.cpp
--- a/Masters/OneWireMaster.cpp	Thu Jun 16 10:45:24 2016 -0500
+++ b/Masters/OneWireMaster.cpp	Fri Jun 17 15:24:37 2016 -0500
@@ -33,399 +33,64 @@
 #include "OneWireMaster.h"
 #include "Utilities/crc.h"
 
-using OneWire::OneWireMaster;
 using namespace OneWire::crc;
 
-enum OwRomCmd
-{
-    ReadRomCmd = 0x33,
-    MatchRomCmd = 0x55,
-    SearchRomCmd = 0xF0,
-    SkipRomCmd = 0xCC,
-    ResumeCmd = 0xA5,
-    OverdriveSkipRomCmd = 0x3C,
-    OverdriveMatchRomCmd = 0x69
-};
-
-OneWireMaster::CmdResult OneWireMaster::OWWriteBlock(const uint8_t *sendBuf, uint8_t sendLen)
-{
-    CmdResult result;
-
-    for (uint8_t idx = 0; idx < sendLen; idx++)
-    {
-        result = OWWriteByte(sendBuf[idx]);
-        if (result != Success)
-        {
-            break;
-        }
-    }
-
-    return result;
-}
-
-OneWireMaster::CmdResult OneWireMaster::OWReadBlock(uint8_t *recvBuf, uint8_t recvLen)
-{
-    CmdResult result;
-
-    for (uint8_t idx = 0; idx < recvLen; idx++)
-    {
-        result = OWReadByte(recvBuf[idx]);
-        if (result != Success)
-        {
-            break;
-        }
-    }
-
-    return result;
-}
-
-OneWireMaster::CmdResult OneWireMaster::OWTriplet(SearchDirection & searchDirection, uint8_t & sbr, uint8_t & tsb)
-{
-    CmdResult result;
-    result = OWReadBit(sbr);
-    if (result == Success)
-    {
-        result = OWReadBit(tsb);
-    }
-    if (result == Success)
-    {
-        if (sbr)
-        {
-            searchDirection = WriteOne;
-        }
-        else if (tsb)
-        {
-            searchDirection = WriteZero;
-        }
-        // else: use search_direction parameter
-
-        result = OWWriteBit((searchDirection == WriteOne) ? 1 : 0);
-    }
-    return result;
-}
-
-void OneWireMaster::SearchState::reset()
+namespace OneWire
 {
-    last_discrepancy = 0;
-    last_device_flag = false;
-    last_family_discrepancy = 0;
-    romId.reset();
-}
-
-void OneWireMaster::SearchState::findFamily(uint8_t familyCode)
-{
-    reset();
-    romId.setFamilyCode(familyCode);
-    last_discrepancy = 64;
-}
-
-void OneWireMaster::SearchState::skipCurrentFamily()
-{
-    // set the Last discrepancy to last family discrepancy
-    last_discrepancy = last_family_discrepancy;
-
-    // clear the last family discrpepancy
-    last_family_discrepancy = 0;
-
-    // check for end of list
-    if (last_discrepancy == 0)
+    OneWireMaster::CmdResult OneWireMaster::OWWriteBlock(const uint8_t *sendBuf, uint8_t sendLen)
     {
-        last_device_flag = true;
-    }
-}
-
-OneWireMaster::CmdResult OneWireMaster::OWFirst(SearchState & searchState)
-{
-    // Reset and begin a new search
-    searchState.reset();
-    return OWSearch(searchState);
-}
+        CmdResult result;
 
-OneWireMaster::CmdResult OneWireMaster::OWNext(SearchState & searchState)
-{
-    // Continue the previous search
-    return OWSearch(searchState);
-}
-
-OneWireMaster::CmdResult OneWireMaster::OWVerify(const RomId & romId)
-{
-    OneWireMaster::CmdResult result;
-    SearchState searchState;
-
-    searchState.romId = romId;
-
-    // set search to find the same device
-    searchState.last_discrepancy = 64;
-    searchState.last_device_flag = false;
-
-    result = OWSearch(searchState);
-    if (result == OneWireMaster::Success)
-    {
-        // check if same device found
-        if (romId != searchState.romId)
+        for (uint8_t idx = 0; idx < sendLen; idx++)
         {
-            result = OneWireMaster::OperationFailure;
+            result = OWWriteByte(sendBuf[idx]);
+            if (result != Success)
+            {
+                break;
+            }
         }
-    }
-
-    return result;
-}
 
-OneWireMaster::CmdResult OneWireMaster::OWReadRom(RomId & romId)
-{
-    OneWireMaster::CmdResult result;
-    RomId readId;
-
-    result = OWReset();
-    if (result == OneWireMaster::Success)
-    {
-        result = OWWriteByte(ReadRomCmd);
-    }
-
-    // read the ROM
-    if (result == OneWireMaster::Success)
-    {
-        result = OWReadBlock(readId, RomId::byteLen);
+        return result;
     }
 
-    // verify CRC8
-    if (result == OneWireMaster::Success)
-    {
-        if (readId.crc8Valid())
-        {
-            romId = readId;
-        }
-        else
-        {
-            result = OneWireMaster::OperationFailure;
-        }
-    }
-
-    return result;
-}
-  
-OneWireMaster::CmdResult OneWireMaster::OWSkipRom()
-{
-    OneWireMaster::CmdResult result;
-
-    result = OWReset();
-    if (result == OneWireMaster::Success)
-    {
-        result = OWWriteByte(SkipRomCmd);
-    }
-
-    return result;
-}
-  
-OneWireMaster::CmdResult OneWireMaster::OWMatchRom(const RomId & romId)
-{
-    OneWireMaster::CmdResult result;
-
-    uint8_t buf[1 + RomId::byteLen];
-
-    // use MatchROM
-    result = OWReset();
-    if (result == OneWireMaster::Success)
+    OneWireMaster::CmdResult OneWireMaster::OWReadBlock(uint8_t *recvBuf, uint8_t recvLen)
     {
-        buf[0] = MatchRomCmd;
-        std::memcpy(&buf[1], romId, RomId::byteLen);
-        // send command and rom
-        result = OWWriteBlock(buf, 1 + RomId::byteLen);
-    }
-
-    return result;
-}
-  
-OneWireMaster::CmdResult OneWireMaster::OWOverdriveSkipRom()
-{
-    OneWireMaster::CmdResult result = OWSetSpeed(OneWireMaster::StandardSpeed);
-
-    if (result == OneWireMaster::Success)
-    {
-        result = OWReset();
-    }
+        CmdResult result;
 
-    if (result == OneWireMaster::Success)
-    {
-        result = OWWriteByte(OverdriveSkipRomCmd);
-    }
-
-    if (result == OneWireMaster::Success)
-    {
-        result = OWSetSpeed(OneWireMaster::OverdriveSpeed);
-    }
-
-    return result;
-}
-  
-OneWireMaster::CmdResult OneWireMaster::OWOverdriveMatchRom(const RomId & romId)
-{
-    OneWireMaster::CmdResult result;
-
-    // use overdrive MatchROM
-    OWSetSpeed(OneWireMaster::StandardSpeed);
-
-    result = OWReset();
-    if (result == OneWireMaster::Success)
-    {
-        result = OWWriteByte(OverdriveMatchRomCmd);
-        if (result == OneWireMaster::Success)
+        for (uint8_t idx = 0; idx < recvLen; idx++)
         {
-            OWSetSpeed(OneWireMaster::OverdriveSpeed);
-            // send ROM
-            result = OWWriteBlock(romId, RomId::byteLen);
-        }
-    }
-    return result;
-}
-   
-OneWireMaster::CmdResult OneWireMaster::OWResume()
-{
-    OneWireMaster::CmdResult result;
-
-    result = OWReset();
-    if (result == OneWireMaster::Success)
-    {
-        result = OWWriteByte(ResumeCmd);
-    }
-
-    return result;
-}
-
-OneWireMaster::CmdResult OneWireMaster::OWSearch(SearchState & searchState)
-{
-    uint8_t id_bit_number;
-    uint8_t last_zero, rom_byte_number;
-    uint8_t id_bit, cmp_id_bit;
-    uint8_t rom_byte_mask;
-    bool search_result;
-    uint8_t crc8 = 0;
-    OneWireMaster::SearchDirection search_direction;
-
-    // initialize for search
-    id_bit_number = 1;
-    last_zero = 0;
-    rom_byte_number = 0;
-    rom_byte_mask = 1;
-    search_result = false;
-
-    // if the last call was not the last one
-    if (!searchState.last_device_flag)
-    {
-        // 1-Wire reset
-        OneWireMaster::CmdResult result = OWReset();
-        if (result != OneWireMaster::Success)
-        {
-            // reset the search
-            searchState.reset();
-            return result;
-        }
-
-        // issue the search command 
-        OWWriteByte(SearchRomCmd);
-
-        // loop to do the search
-        do
-        {
-            // if this discrepancy if before the Last Discrepancy
-            // on a previous next then pick the same as last time
-            if (id_bit_number < searchState.last_discrepancy)
-            {
-                if ((searchState.romId[rom_byte_number] & rom_byte_mask) > 0)
-                {
-                    search_direction = OneWireMaster::WriteOne;
-                }
-                else
-                {
-                    search_direction = OneWireMaster::WriteZero;
-                }
-            }
-            else
-            {
-                // if equal to last pick 1, if not then pick 0
-                if (id_bit_number == searchState.last_discrepancy)
-                {
-                    search_direction = OneWireMaster::WriteOne;
-                }
-                else
-                {
-                    search_direction = OneWireMaster::WriteZero;
-                }
-            }
-
-            // Peform a triple operation on the DS2465 which will perform 2 read bits and 1 write bit
-            result = OWTriplet(search_direction, id_bit, cmp_id_bit);
-            if (result != OneWireMaster::Success)
-            {
-                return result;
-            }
-
-            // check for no devices on 1-wire
-            if (id_bit && cmp_id_bit)
+            result = OWReadByte(recvBuf[idx]);
+            if (result != Success)
             {
                 break;
             }
-            else
-            {
-                if (!id_bit && !cmp_id_bit && (search_direction == OneWireMaster::WriteZero))
-                {
-                    last_zero = id_bit_number;
-
-                    // check for Last discrepancy in family
-                    if (last_zero < 9)
-                    {
-                        searchState.last_family_discrepancy = last_zero;
-                    }
-                }
-
-                // set or clear the bit in the ROM byte rom_byte_number
-                // with mask rom_byte_mask
-                if (search_direction == OneWireMaster::WriteOne)
-                {
-                    searchState.romId[rom_byte_number] |= rom_byte_mask;
-                }
-                else
-                {
-                    searchState.romId[rom_byte_number] &= (uint8_t)~rom_byte_mask;
-                }
+        }
 
-                // increment the byte counter id_bit_number
-                // and shift the mask rom_byte_mask
-                id_bit_number++;
-                rom_byte_mask <<= 1;
-
-                // if the mask is 0 then go to new SerialNum byte rom_byte_number and reset mask
-                if (rom_byte_mask == 0)
-                {
-                    crc8 = calculateCrc8(crc8, searchState.romId[rom_byte_number]);  // accumulate the CRC
-                    rom_byte_number++;
-                    rom_byte_mask = 1;
-                }
-            }
-        } while (rom_byte_number < RomId::byteLen);  // loop until through all ROM bytes 0-7
-
-        // if the search was successful then
-        if (!((id_bit_number <= (RomId::byteLen * 8)) || (crc8 != 0)))
-        {
-            // search successful so set m_last_discrepancy,m_last_device_flag,search_result
-            searchState.last_discrepancy = last_zero;
-
-            // check for last device
-            if (searchState.last_discrepancy == 0)
-            {
-                searchState.last_device_flag = true;
-            }
-
-            search_result = true;
-        }
+        return result;
     }
 
-    // if no device found then reset counters so next 'search' will be like a first
-    if (!search_result || (searchState.romId.familyCode() == 0))
+    OneWireMaster::CmdResult OneWireMaster::OWTriplet(SearchDirection & searchDirection, uint8_t & sbr, uint8_t & tsb)
     {
-        searchState.reset();
-        search_result = false;
+        CmdResult result;
+        result = OWReadBit(sbr);
+        if (result == Success)
+        {
+            result = OWReadBit(tsb);
+        }
+        if (result == Success)
+        {
+            if (sbr)
+            {
+                searchDirection = WriteOne;
+            }
+            else if (tsb)
+            {
+                searchDirection = WriteZero;
+            }
+            // else: use search_direction parameter
+
+            result = OWWriteBit((searchDirection == WriteOne) ? 1 : 0);
+        }
+        return result;
     }
-
-    return (search_result ? OneWireMaster::Success : OneWireMaster::OperationFailure);
 }