Dallas' 1-Wire bus protocol library

Dependents:   DS1825 DISCO-F746-Dessiccateur-V1 watersenor_and_temp_code DS1820 ... more

Revision:
8:87fbdaba5535
Parent:
7:acf3f0ee66d2
Child:
9:4af0015b0f47
--- a/OneWire.cpp	Sun Jan 20 19:52:50 2019 +0000
+++ b/OneWire.cpp	Mon Jan 21 11:37:43 2019 +0000
@@ -113,45 +113,64 @@
 // Branding Policy.
 //--------------------------------------------------------------------------
 */
-
 #include "OneWire.h"
 
-OneWire::OneWire(PinName pin):
+/**
+ * @brief
+ * @note
+ * @param
+ * @retval
+ */
+OneWire::OneWire(PinName pin) :
     wire(pin)
 {
-    wire.mode(PullUp);
     timer.stop();
     timer.reset();
+#if defined(TARGET_STM)
+    wire.mode(OpenDrain);
+#else
+    wire.mode(PullUp);
+#endif
 #if ONEWIRE_SEARCH
     reset_search();
 #endif
 }
 
-
 // Perform the onewire reset function.  We will wait up to 250uS for
 // the bus to come high, if it doesn't then it is broken or shorted
 // and we return a 0;
 //
 // Returns 1 if a device asserted a presence pulse, 0 otherwise.
+
 //
 uint8_t OneWire::reset(void)
 {
-   uint8_t r;
-   uint8_t retries = 125;
+    uint8_t r;
+    uint8_t retries = 125;
 
+#if defined(TARGET_STM)
+    wire = 1;
+#else
     wire.input();
+#endif
     // wait until the wire is high... just in case
+
     do {
         if (--retries == 0) {
-             return 0;
+            return 0;
         }
+
         wait_us(2);
     } while (wire.read() != 1);
 
     wire.output();
     wire = 0;
     wait_us(480);
+#if defined(TARGET_STM)
+    wire = 1;
+#else
     wire.input();
+#endif
     wait_us(70);
     r = !wire.read();
     wait_us(410);
@@ -161,16 +180,20 @@
 //
 // Write a bit. Port and bit is used to cut lookup time and provide
 // more certain timing.
+
 //
 void OneWire::write_bit(uint8_t v)
 {
+#if !defined(TARGET_STM)
     wire.output();
+#endif
     if (v & 1) {
         wire = 0;   // drive output low
         wait_us(10);
         wire = 1;   // drive output high
         wait_us(55);
-    } else {
+    }
+    else {
         wire = 0;   // drive output low
         wait_us(65);
         wire = 1;   // drive output high
@@ -181,19 +204,26 @@
 //
 // Read a bit. Port and bit is used to cut lookup time and provide
 // more certain timing.
+
 //
 uint8_t OneWire::read_bit(void)
 {
     uint8_t r;
     int     t;
 
+#if !defined(TARGET_STM)
     wire.output();
+#endif
     wire = 0;
     timer.start();
+#if defined(TARGET_STM)
+    wire = 1;
+#else
     wire.input();
+#endif
     t = timer.read_us();
-    if (t < 10)
-        wait_us(10 - t);
+    if (t < 7)
+        wait_us(7 - t);
     r = wire.read();
     timer.stop();
     timer.reset();
@@ -207,99 +237,145 @@
 // parasite power mode) then set 'power' to 1, otherwise the pin will
 // go tri-state at the end of the write to avoid heating in a short or
 // other mishap.
+
 //
-void OneWire::write(uint8_t v, uint8_t power /* = 0 */) {
+void OneWire::write(uint8_t v, uint8_t power /* = 0 */ )
+{
     uint8_t bitMask;
 
     for (bitMask = 0x01; bitMask; bitMask <<= 1) {
-    OneWire::write_bit( (bitMask & v)?1:0);
+        OneWire::write_bit((bitMask & v) ? 1 : 0);
     }
-    if ( !power) {
+
+    if (!power)
+    {
+#if defined(TARGET_STM)
+        wire = 1;
+#else
         wire.input();
+#endif
     }
 }
 
-void OneWire::write_bytes(const uint8_t *buf, uint16_t count, bool power /* = 0 */) {
-  for (uint16_t i = 0 ; i < count ; i++)
-    write(buf[i]);
-  if (!power) {
+/**
+ * @brief
+ * @note
+ * @param
+ * @retval
+ */
+void OneWire::write_bytes(const uint8_t* buf, uint16_t count, bool power /* = 0 */ )
+{
+    for (uint16_t i = 0; i < count; i++)
+        write(buf[i]);
+    if (!power)
+    {
+#if defined(TARGET_STM)
+        wire = 1;
+#else
         wire.input();
-  }
+#endif
+    }
 }
 
 //
 // Read a byte
+
 //
-uint8_t OneWire::read() {
+uint8_t OneWire::read()
+{
     uint8_t bitMask;
     uint8_t r = 0;
 
     for (bitMask = 0x01; bitMask; bitMask <<= 1) {
-    if ( OneWire::read_bit()) r |= bitMask;
+        if (OneWire::read_bit())
+            r |= bitMask;
     }
+
     return r;
 }
 
-void OneWire::read_bytes(uint8_t *buf, uint16_t count) {
-  for (uint16_t i = 0 ; i < count ; i++)
-    buf[i] = read();
+/**
+ * @brief
+ * @note
+ * @param
+ * @retval
+ */
+void OneWire::read_bytes(uint8_t* buf, uint16_t count)
+{
+    for (uint16_t i = 0; i < count; i++)
+        buf[i] = read();
 }
 
 //
 // Do a ROM select
+
 //
 void OneWire::select(const uint8_t rom[8])
 {
     uint8_t i;
 
-    write(0x55);           // Choose ROM
-
-    for (i = 0; i < 8; i++) write(rom[i]);
+    write(0x55);    // Choose ROM
+    for (i = 0; i < 8; i++)
+        write(rom[i]);
 }
 
 //
 // Do a ROM skip
+
 //
 void OneWire::skip()
 {
-    write(0xCC);           // Skip ROM
+    write(0xCC);    // Skip ROM
 }
 
+/**
+ * @brief
+ * @note
+ * @param
+ * @retval
+ */
 void OneWire::depower()
 {
+#if defined(TARGET_STM)
+    wire = 1;
+#else
     wire.input();
+#endif
 }
 
 #if ONEWIRE_SEARCH
+//
 
-//
 // You need to use this function to start a search again from the beginning.
 // You do not need to do it for the first search, though you could.
+
 //
 void OneWire::reset_search()
 {
-  // reset the search state
-  LastDiscrepancy = 0;
-  LastDeviceFlag = false;
-  LastFamilyDiscrepancy = 0;
-  for(int i = 7; ; i--) {
-    ROM_NO[i] = 0;
-    if ( i == 0) break;
-  }
+    // reset the search state
+    LastDiscrepancy = 0;
+    LastDeviceFlag = false;
+    LastFamilyDiscrepancy = 0;
+    for (int i = 7;; i--) {
+        ROM_NO[i] = 0;
+        if (i == 0)
+            break;
+    }
 }
 
 // Setup the search to find the device type 'family_code' on the next call
 // to search(*newAddr) if it is present.
+
 //
 void OneWire::target_search(uint8_t family_code)
 {
-   // set the search state to find SearchFamily type devices
-   ROM_NO[0] = family_code;
-   for (uint8_t i = 1; i < 8; i++)
-      ROM_NO[i] = 0;
-   LastDiscrepancy = 64;
-   LastFamilyDiscrepancy = 0;
-   LastDeviceFlag = false;
+    // set the search state to find SearchFamily type devices
+    ROM_NO[0] = family_code;
+    for (uint8_t i = 1; i < 8; i++)
+        ROM_NO[i] = 0;
+    LastDiscrepancy = 64;
+    LastFamilyDiscrepancy = 0;
+    LastDeviceFlag = false;
 }
 
 //
@@ -317,146 +393,140 @@
 // search state.
 // Return true  : device found, ROM number in ROM_NO buffer
 //        false : device not found, end of search
+
 //
-uint8_t OneWire::search(uint8_t *newAddr)
+uint8_t OneWire::search(uint8_t* newAddr)
 {
-   uint8_t id_bit_number;
-   uint8_t last_zero, rom_byte_number, search_result;
-   uint8_t id_bit, cmp_id_bit;
+    uint8_t         id_bit_number;
+    uint8_t         last_zero, rom_byte_number, search_result;
+    uint8_t         id_bit, cmp_id_bit;
 
-   unsigned char rom_byte_mask, search_direction;
+    unsigned char   rom_byte_mask, search_direction;
 
-   // initialize for search
-   id_bit_number = 1;
-   last_zero = 0;
-   rom_byte_number = 0;
-   rom_byte_mask = 1;
-   search_result = 0;
+    // initialize for search
+    id_bit_number = 1;
+    last_zero = 0;
+    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
-      if (!reset())
-      {
-         // reset the search
-         LastDiscrepancy = 0;
-         LastDeviceFlag = false;
-         LastFamilyDiscrepancy = 0;
-         return false;
-      }
+    // if the last call was not the last one
+    if (!LastDeviceFlag) {
+        // 1-Wire reset
+        if (!reset()) {
+            // reset the search
+            LastDiscrepancy = 0;
+            LastDeviceFlag = false;
+            LastFamilyDiscrepancy = 0;
+            return false;
+        }
 
-      // issue the search command
-      write(0xF0);
+        // issue the search command
+        write(0xF0);
 
-      // loop to do the search
-      do
-      {
-         // read a bit and its complement
-         id_bit = read_bit();
-         cmp_id_bit = read_bit();
+        // loop to do the search
+        do {
+            // read a bit and its complement
+            id_bit = read_bit();
+            cmp_id_bit = read_bit();
 
-         // check for no devices on 1-wire
-         if ((id_bit == 1) && (cmp_id_bit == 1))
-            break;
-         else
-         {
-            // all devices coupled have 0 or 1
-            if (id_bit != cmp_id_bit)
-               search_direction = id_bit;  // bit write value for search
-            else
-            {
-               // if this discrepancy if before the Last Discrepancy
-               // on a previous next then pick the same as last time
-               if (id_bit_number < LastDiscrepancy)
-                  search_direction = ((ROM_NO[rom_byte_number] & rom_byte_mask) > 0);
-               else
-                  // if equal to last pick 1, if not then pick 0
-                  search_direction = (id_bit_number == LastDiscrepancy);
+            // check for no devices on 1-wire
+            if ((id_bit == 1) && (cmp_id_bit == 1))
+                break;
+            else {
+                // all devices coupled have 0 or 1
+                if (id_bit != cmp_id_bit)
+                    search_direction = id_bit;  // bit write value for search
+                else {
+                    // if this discrepancy if before the Last Discrepancy
+                    // on a previous next then pick the same as last time
+                    if (id_bit_number < LastDiscrepancy)
+                        search_direction = ((ROM_NO[rom_byte_number] & rom_byte_mask) > 0);
+                    else
+                        // if equal to last pick 1, if not then pick 0
+                        search_direction = (id_bit_number == LastDiscrepancy);
 
-               // if 0 was picked then record its position in LastZero
-               if (search_direction == 0)
-               {
-                  last_zero = id_bit_number;
+                    // if 0 was picked then record its position in LastZero
+                    if (search_direction == 0) {
+                        last_zero = id_bit_number;
 
-                  // check for Last discrepancy in family
-                  if (last_zero < 9)
-                     LastFamilyDiscrepancy = last_zero;
-               }
-            }
+                        // check for Last discrepancy in family
+                        if (last_zero < 9)
+                            LastFamilyDiscrepancy = last_zero;
+                    }
+                }
 
-            // set or clear the bit in the ROM byte rom_byte_number
-            // with mask rom_byte_mask
-            if (search_direction == 1)
-              ROM_NO[rom_byte_number] |= rom_byte_mask;
-            else
-              ROM_NO[rom_byte_number] &= ~rom_byte_mask;
+                // set or clear the bit in the ROM byte rom_byte_number
+                // with mask rom_byte_mask
+                if (search_direction == 1)
+                    ROM_NO[rom_byte_number] |= rom_byte_mask;
+                else
+                    ROM_NO[rom_byte_number] &= ~rom_byte_mask;
 
-            // serial number search direction write bit
-            write_bit(search_direction);
+                // serial number search direction write bit
+                write_bit(search_direction);
 
-            // increment the byte counter id_bit_number
-            // and shift the mask rom_byte_mask
-            id_bit_number++;
-            rom_byte_mask <<= 1;
+                // 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)
-            {
-                rom_byte_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) {
+                    rom_byte_number++;
+                    rom_byte_mask = 1;
+                }
             }
-         }
-      }
-      while(rom_byte_number < 8);  // loop until through all ROM bytes 0-7
+        } while (rom_byte_number < 8);
+        // loop until through all ROM bytes 0-7
+        // if the search was successful then
+        if (!(id_bit_number < 65)) {
+            // search successful so set LastDiscrepancy,LastDeviceFlag,search_result
+            LastDiscrepancy = last_zero;
 
-      // if the search was successful then
-      if (!(id_bit_number < 65))
-      {
-         // search successful so set LastDiscrepancy,LastDeviceFlag,search_result
-         LastDiscrepancy = last_zero;
-
-         // check for last device
-         if (LastDiscrepancy == 0)
-            LastDeviceFlag = true;
+            // check for last device
+            if (LastDiscrepancy == 0)
+                LastDeviceFlag = true;
 
-         search_result = true;
-      }
-   }
+            search_result = true;
+        }
+    }
 
-   // if no device found then reset counters so next 'search' will be like a first
-   if (!search_result || !ROM_NO[0])
-   {
-      LastDiscrepancy = 0;
-      LastDeviceFlag = false;
-      LastFamilyDiscrepancy = 0;
-      search_result = false;
-   }
-   for (int i = 0; i < 8; i++) newAddr[i] = ROM_NO[i];
-   return search_result;
-  }
+    // if no device found then reset counters so next 'search' will be like a first
+    if (!search_result || !ROM_NO[0]) {
+        LastDiscrepancy = 0;
+        LastDeviceFlag = false;
+        LastFamilyDiscrepancy = 0;
+        search_result = false;
+    }
 
+    for (int i = 0; i < 8; i++)
+        newAddr[i] = ROM_NO[i];
+    return search_result;
+}
 #endif
-
 #if ONEWIRE_CRC
 // The 1-Wire CRC scheme is described in Maxim Application Note 27:
+
 // "Understanding and Using Cyclic Redundancy Checks with Maxim iButton Products"
 // Compute a Dallas Semiconductor 8 bit CRC directly.
+
 //
-uint8_t OneWire::crc8(const uint8_t *addr, uint8_t len)
+uint8_t OneWire::crc8(const uint8_t* addr, uint8_t len)
 {
     uint8_t crc = 0;
-    
+
     while (len--) {
         uint8_t inbyte = *addr++;
         for (uint8_t i = 8; i; i--) {
             uint8_t mix = (crc ^ inbyte) & 0x01;
             crc >>= 1;
-            if (mix) crc ^= 0x8C;
+            if (mix)
+                crc ^= 0x8C;
             inbyte >>= 1;
         }
     }
+
     return crc;
 }
 #endif
-