Working versione for L-tek FF1705

Dependents:   DS1820

Files at this revision

API Documentation at this revision

Comitter:
hudakz
Date:
Sun Mar 10 13:17:38 2019 +0000
Parent:
11:bc8ed7280966
Child:
13:573a546ef5f9
Commit message:
Updated.

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
--- a/OneWire.cpp	Sun Jan 27 18:01:19 2019 +0000
+++ b/OneWire.cpp	Sun Mar 10 13:17:38 2019 +0000
@@ -115,6 +115,7 @@
 */
 #include "OneWire.h"
 
+
 /**
  * @brief   Constructs a OneWire object.
  * @note    GPIO is configured as output and an internal pull up resistor is connected.
@@ -126,7 +127,8 @@
 OneWire::OneWire(PinName pin) :
     DigitalInOut(pin)
 {
-    MODE(); // set mode either PullUp or OpenDrain for STM
+    MODE(); // set mode to either OpenDrain for STM or PullUp for others
+    INIT_WAIT;
 #if ONEWIRE_SEARCH
     reset_search();
 #endif
@@ -141,25 +143,17 @@
  */
 uint8_t OneWire::reset(void)
 {
-    uint8_t r;
-    uint8_t retries = 125;
-
-    INPUT();
-    // wait until the wire is high... just in case
-    do {
-        if (--retries == 0)
-            return 0;
-        wait_us(2);
-    } while (READ() == 0);
+    uint8_t present;
 
     OUTPUT();
-    WRITE(0);
-    wait_us(480);
-    INPUT();
-    wait_us(65);
-    r = !READ();
-    wait_us(420);
-    return r;
+    WRITE(0);           // pull down the 1-wire bus do create reset pulse
+    WAIT_US(500);       // wait at least 480 us
+    INPUT();            // release the 1-wire bus and go into receive mode
+    WAIT_US(90);        // DS1820 waits about 15 to 60 us and generates a 60 to 240 us presence pulse
+    present = !READ();  // read the presence pulse
+    WAIT_US(420);
+    
+    return present;
 }
 
 /**
@@ -173,15 +167,15 @@
     OUTPUT();
     if (v & 1) {
         WRITE(0);   // drive output low
-        wait_us(1);
+        WAIT_US(1);
         WRITE(1);   // drive output high
-        wait_us(60);
+        WAIT_US(60);
     }
     else {
         WRITE(0);   // drive output low
-        wait_us(60);
+        WAIT_US(60);
         WRITE(1);   // drive output high
-        wait_us(1);
+        WAIT_US(1);
     }
 }
 
@@ -193,20 +187,22 @@
  */
 uint8_t OneWire::read_bit(void)
 {
+    const int SAMPLE_POINT = 10;
     uint8_t r;
     int     t;
 
     OUTPUT();
+    WRITE(0);
     timer.start();
-    WRITE(0);
     INPUT();
     t = timer.read_us();
-    if (t < 7)
-        wait_us(7 - t);
+    if (t < SAMPLE_POINT)
+        WAIT_US(SAMPLE_POINT - t);
     r = READ();
     timer.stop();
     timer.reset();
-    wait_us(55);
+    WAIT_US(55);
+    //printf("t = %d\r\n", t);
     return r;
 }
 
@@ -384,7 +380,7 @@
     rom_byte_number = 0;
     rom_byte_mask = 1;
     search_result = 0;
-
+    
     // if the last call was not the last one
     if (!LastDeviceFlag) {
         // 1-Wire reset
--- a/OneWire.h	Sun Jan 27 18:01:19 2019 +0000
+++ b/OneWire.h	Sun Mar 10 13:17:38 2019 +0000
@@ -5,8 +5,8 @@
 #include <mbed.h>
 
 #if defined(TARGET_STM)
-    #define MODE()       output(); \
-                         mode(OpenDrain)
+    #define MODE()      output(); \
+                        mode(OpenDrain)
     #define INPUT()     (*gpio.reg_set = gpio.mask) // write 1 to open drain
     #define OUTPUT()    // configured as output in the constructor and stays like that forever
     #define READ()      ((*gpio.reg_in & gpio.mask) != 0)
@@ -19,6 +19,28 @@
     #define WRITE(x)    write(x)
 #endif
 
+#ifdef TARGET_NORDIC
+//NORDIC targets (NRF) use software delays since their ticker uses a 32kHz clock
+    static uint32_t loops_per_us = 0;
+    
+    #define INIT_WAIT   init_soft_delay()
+    #define WAIT_US(x)  for(int cnt = 0; cnt < (x * loops_per_us) >> 5; cnt++) {__NOP(); __NOP(); __NOP();}
+    
+void init_soft_delay( void ) {
+    if (loops_per_us == 0) {
+        loops_per_us = 1;
+        Timer timey; 
+        timey.start();
+        ONEWIRE_DELAY_US(320000);                     
+        timey.stop();
+        loops_per_us = (320000 + timey.read_us() / 2) / timey.read_us();  
+    }
+}
+#else
+    #define INIT_WAIT
+    #define WAIT_US(x)  wait_us(x)
+#endif
+
 // You can exclude certain features from OneWire.  In theory, this
 // might save some space.  In practice, the compiler automatically
 // removes unused code (technically, the linker, using -fdata-sections
@@ -155,3 +177,5 @@
 };
 
 #endif
+
+