Library to communicate with Maxim OneWire protocol devices Modified timings and IRQ overrides

Dependents:   RdGasUseMonitor

Fork of Onewire by Simon Barker

Files at this revision

API Documentation at this revision

Comitter:
Bobty
Date:
Fri Oct 16 06:25:11 2015 +0000
Parent:
7:0a87f8c2d9e6
Commit message:
Changed timings to be consistent with Arduino onewire library https://github.com/PaulStoffregen/OneWire

Changed in this revision

Onewire.cpp Show annotated file Show diff for this revision Revisions of this file
Onewire.h Show annotated file Show diff for this revision Revisions of this file
diff -r 0a87f8c2d9e6 -r 5d0bd95b586f Onewire.cpp
--- a/Onewire.cpp	Thu Oct 15 21:39:21 2015 +0000
+++ b/Onewire.cpp	Fri Oct 16 06:25:11 2015 +0000
@@ -17,20 +17,24 @@
     if (bit) 
     {
         // Write '1' bit
+        __disable_irq();
         oneBus_.output();
         oneBus_ = 0;
-        wait_us(12);
-        oneBus_.input();
-        wait_us(60);
+        wait_us(10);
+        oneBus_ = 1;
+        __enable_irq();        
+        wait_us(55);
     }
     else
     {
         // Write '0' bit
+        __disable_irq();
         oneBus_.output();
         oneBus_ = 0;
-        wait_us(60);
-        oneBus_.input();
-        wait_us(12);
+        wait_us(65);
+        oneBus_ = 1;
+        __enable_irq();        
+        wait_us(5);
     }
     __enable_irq();
 }
@@ -42,31 +46,45 @@
     __disable_irq();
     oneBus_.output();
     oneBus_ = 0;
-    wait_us(5);
+    wait_us(3);
     oneBus_.input();
-    wait_us(7);
+    wait_us(10);
     result = oneBus_.read();
-    wait_us(60);
     __enable_irq();
+    wait_us(53);
     return result;
 }
 
 int Onewire::init()
 {
+    // Ensure bus is high to start
+    oneBus_.input();
+    int MAX_RETRY_TEST_BUS_HIGH = 125;
+    for (int i = 0; i < MAX_RETRY_TEST_BUS_HIGH; i++)
+    {
+        if (oneBus_.read() == 1)
+            break;
+        wait_us(2);
+    }
+    if (oneBus_.read() != 1)
+        return ONEWIRE_FAIL_STUCK_LOW;
+    
+    // Pull LOW for 480us
+    __disable_irq();
     oneBus_.output();
     oneBus_ = 0;
+    __enable_irq();
     wait_us(480);
+    
+    // Allow to float and test to ensure presence of devices
+    // (which pull the bus low)
     __disable_irq();
     oneBus_.input();
-    wait_us(60);
-    if (oneBus_.read() == 0) 
-    {
-        __enable_irq();
-        wait_us(100);
-        return ONEWIRE_OK;
-    }
+    wait_us(70);
+    int readVal = oneBus_.read();
     __enable_irq();
-    return ONEWIRE_SEARCH_INIT_FAIL;
+    wait_us(410);
+    return (readVal == 0) ? ONEWIRE_OK : ONEWIRE_SEARCH_INIT_FAIL;
 }
 
 int Onewire::readByte() 
@@ -149,7 +167,6 @@
         
     // 1-Wire reset
     int initRslt = init();
-    wait_us(410);
     if (initRslt != ONEWIRE_OK)
     {
         // reset the search
@@ -236,10 +253,10 @@
     switch(errCode)
     {
         case ONEWIRE_OK: return "OK";
+        case ONEWIRE_FAIL_STUCK_LOW: return "Stuck Low";
         case ONEWIRE_SEARCH_ALL_DONE: return "All Done";
         case ONEWIRE_SEARCH_INIT_FAIL: return "Init Fail";
         case ONEWIRE_SEARCH_NOT_FOUND: return "Not Found";
-        case ONEWIRE_SEARCH_STUCK_HIGH: return "Stuck High";
         case ONEWIRE_SEARCH_COMP_BIT_ERR: return "Comp Bit Err";
     }
     return "Unknown Err";
diff -r 0a87f8c2d9e6 -r 5d0bd95b586f Onewire.h
--- a/Onewire.h	Thu Oct 15 21:39:21 2015 +0000
+++ b/Onewire.h	Fri Oct 16 06:25:11 2015 +0000
@@ -6,10 +6,10 @@
 #define ONEWIRE_ADDR_BYTES 8
 
 const int ONEWIRE_OK = 0;
-const int ONEWIRE_SEARCH_ALL_DONE = 1;
-const int ONEWIRE_SEARCH_INIT_FAIL = 2;
-const int ONEWIRE_SEARCH_NOT_FOUND = 3;
-const int ONEWIRE_SEARCH_STUCK_HIGH = 4;
+const int ONEWIRE_FAIL_STUCK_LOW = 1;
+const int ONEWIRE_SEARCH_ALL_DONE = 2;
+const int ONEWIRE_SEARCH_INIT_FAIL = 3;
+const int ONEWIRE_SEARCH_NOT_FOUND = 4;
 const int ONEWIRE_SEARCH_COMP_BIT_ERR = 5;
 
 class Onewire