Library for reading temperature from DS1820, DS18B20 and DS1822

Dependencies:   LinkedList

Dependents:   heatmap BLE_Temperature BLE_Temperature_Exercise F334andDS18B20 ... more

Fork of DS1820 by David Pairman

HelloWorld: http://mbed.org/users/Sissors/code/DS1820_HelloWorld/

Library should currently work on all mbed targets, let me know if there is an issue. First however make sure you have latest version of mbed library and this library.

Revision:
14:c591209285e9
Parent:
12:196e9e54b033
Child:
15:236eb8f8e73a
--- a/DS1820.cpp	Mon Mar 09 20:41:08 2015 +0000
+++ b/DS1820.cpp	Sun Jan 08 17:26:21 2017 +0000
@@ -1,5 +1,56 @@
 #include "DS1820.h"
 
+#ifdef TARGET_STM
+//STM targets use opendrain mode since their GPIO code is too bad to be used like the others
+    #define ONEWIRE_INPUT(pin)  pin->write(1)
+    #define ONEWIRE_OUTPUT(pin) 
+    #define ONEWIRE_INIT(pin)   pin->output(); pin->mode(OpenDrain)
+    
+    // TEMP, remove once STM fixed their stuff
+// Enable GPIO clock and return GPIO base address 
+static uint32_t Set_GPIO_Clock(uint32_t port_idx) { 
+    uint32_t gpio_add = 0; 
+    switch (port_idx) { 
+        case PortA: 
+           gpio_add = GPIOA_BASE; 
+           __GPIOA_CLK_ENABLE(); 
+           break; 
+        case PortB: 
+            gpio_add = GPIOB_BASE; 
+            __GPIOB_CLK_ENABLE(); 
+            break; 
+#if defined(GPIOC_BASE) 
+        case PortC: 
+            gpio_add = GPIOC_BASE; 
+            __GPIOC_CLK_ENABLE(); 
+            break; 
+#endif 
+#if defined(GPIOD_BASE) 
+       case PortD: 
+           gpio_add = GPIOD_BASE; 
+            __GPIOD_CLK_ENABLE(); 
+            break; 
+#endif 
+#if defined(GPIOF_BASE) 
+        case PortF: 
+            gpio_add = GPIOF_BASE; 
+            __GPIOF_CLK_ENABLE(); 
+            break; 
+#endif 
+      default: 
+           error("Pinmap error: wrong port number."); 
+           break; 
+   } 
+   return gpio_add; 
+} 
+
+
+#else
+    #define ONEWIRE_INPUT(pin)  pin->input()
+    #define ONEWIRE_OUTPUT(pin) pin->output()
+    #define ONEWIRE_INIT(pin)
+#endif
+
 LinkedList<node> DS1820::probes;
  
  
@@ -12,6 +63,20 @@
     for(byte_counter=0;byte_counter<9;byte_counter++)
         RAM[byte_counter] = 0x00;
     
+    ONEWIRE_INIT((&_datapin));
+    // Temp code since the above doesn't actually do anything in mbed revisions up to 133
+    #ifdef TARGET_STM
+    
+    uint32_t port_index = STM_PORT(data_pin); 
+    uint32_t pin_index  = STM_PIN(data_pin); 
+    
+    // Enable GPIO clock 
+    uint32_t gpio_add = Set_GPIO_Clock(port_index); 
+    GPIO_TypeDef *gpio = (GPIO_TypeDef *)gpio_add; 
+
+    gpio->OTYPER |= (uint32_t)(1 << pin_index); 
+    #endif
+    
     if (!unassignedProbe(&_datapin, _ROM))
         error("No unassigned DS1820 found!\n");
     else {
@@ -35,10 +100,10 @@
 bool DS1820::onewire_reset(DigitalInOut *pin) {
 // This will return false if no devices are present on the data bus
     bool presence=false;
-    pin->output();
+    ONEWIRE_OUTPUT(pin);
     pin->write(0);          // bring low for 500 us
     wait_us(500);
-    pin->input();       // let the data line float high
+    ONEWIRE_INPUT(pin);       // let the data line float high
     wait_us(90);            // wait 90us
     if (pin->read()==0) // see if any devices are pulling the data line low
         presence=true;
@@ -47,7 +112,7 @@
 }
  
 void DS1820::onewire_bit_out (DigitalInOut *pin, bool bit_data) {
-    pin->output();
+    ONEWIRE_OUTPUT(pin);
     pin->write(0);
     wait_us(3);                 // DXP modified from 5
     if (bit_data) {
@@ -70,10 +135,10 @@
  
 bool DS1820::onewire_bit_in(DigitalInOut *pin) {
     bool answer;
-    pin->output();
+    ONEWIRE_OUTPUT(pin);
     pin->write(0);
     wait_us(3);                 // DXP modofied from 5
-    pin->input();
+    ONEWIRE_INPUT(pin);
     wait_us(10);                // DXP modified from 5
     answer = pin->read();
     wait_us(45);                // DXP modified from 50
@@ -93,6 +158,19 @@
 
 bool DS1820::unassignedProbe(PinName pin) {
     DigitalInOut _pin(pin);
+    ONEWIRE_INIT((&_pin));
+    // Temp code since the above doesn't actually do anything in mbed revisions up to 133
+    #ifdef TARGET_STM
+    
+    uint32_t port_index = STM_PORT(pin); 
+    uint32_t pin_index  = STM_PIN(pin); 
+    
+    // Enable GPIO clock 
+    uint32_t gpio_add = Set_GPIO_Clock(port_index); 
+    GPIO_TypeDef *gpio = (GPIO_TypeDef *)gpio_add; 
+
+    gpio->OTYPER |= (uint32_t)(1 << pin_index); 
+    #endif
     char ROM_address[8];
     return search_ROM_routine(&_pin, 0xF0, ROM_address);
 }