Browse Source

Onewire module maintenance.

Vladimir N. Shilov 1 year ago
parent
commit
2d1bfbf9ee
5 changed files with 190 additions and 179 deletions
  1. 81 0
      lib/main.h
  2. 0 27
      lib/onewire/boarddef.h
  3. 37 117
      lib/onewire/onewire.c
  4. 21 4
      lib/onewire/onewire.h
  5. 51 31
      main.c

+ 81 - 0
lib/main.h

@@ -25,6 +25,22 @@
 #define ADC_CHANNEL_NUM         ADC_CHANNEL_IN9
 #define ADC_CHANNEL_NUM         ADC_CHANNEL_IN9
 #define ADC_REF_VOLTAGE         3300
 #define ADC_REF_VOLTAGE         3300
 
 
+#define ONEWIRE1_PORT           GPIOB
+#define ONEWIRE1_PIN            9
+#define ONEWIRE1_PWM_DRIVER     PWMD4
+#define ONEWIRE1_MASTER_CHANNEL 4
+#define ONEWIRE1_SAMPLE_CHANNEL 3
+
+#define ONEWIRE2_PORT           GPIOB
+#define ONEWIRE2_PIN            8
+#define ONEWIRE2_PWM_DRIVER     PWMD4
+#define ONEWIRE2_MASTER_CHANNEL 3
+#define ONEWIRE2_SAMPLE_CHANNEL 4
+
+#define ONWIRE_CHANNELS_NUM     2
+#define ONEWIRE_PAD_MODE_IDLE   PAL_MODE_INPUT
+#define ONEWIRE_PAD_MODE_ACTIVE PAL_MODE_STM32_ALTERNATE_OPENDRAIN
+
 /* Private variables */
 /* Private variables */
 
 
 /*
 /*
@@ -84,4 +100,69 @@ static const GPTConfig gptcfg1 = {
   .dier         =  0U
   .dier         =  0U
 };
 };
 
 
+/*
+ * Config for underlying PWM driver.
+ * Note! It is NOT constant because 1-wire driver needs to change them
+ * during functioning.
+ */
+static PWMConfig pwm_cfg = {
+    0,
+    0,
+    NULL,
+    {
+     {PWM_OUTPUT_DISABLED, NULL},
+     {PWM_OUTPUT_DISABLED, NULL},
+     {PWM_OUTPUT_DISABLED, NULL},
+     {PWM_OUTPUT_DISABLED, NULL}
+    },
+    0,
+#if STM32_PWM_USE_ADVANCED
+    0,
+#endif
+    0,
+    0
+};
+
+/*
+ * OneWire channel 1
+ */
+static const onewireConfig ow_cfg1 = {
+    &ONEWIRE1_PWM_DRIVER,
+    &pwm_cfg,
+    PWM_OUTPUT_ACTIVE_LOW,
+    ONEWIRE1_MASTER_CHANNEL,
+    ONEWIRE1_SAMPLE_CHANNEL,
+    ONEWIRE1_PORT,
+    ONEWIRE1_PIN,
+#if defined(STM32F1XX)
+    ONEWIRE_PAD_MODE_IDLE,
+#endif
+    ONEWIRE_PAD_MODE_ACTIVE,
+#if ONEWIRE_USE_STRONG_PULLUP
+    strong_pullup_assert,
+    strong_pullup_release
+#endif
+};
+
+/*
+ * OneWire channel 2
+ */
+static const onewireConfig ow_cfg2 = {
+    &ONEWIRE2_PWM_DRIVER,
+    &pwm_cfg,
+    PWM_OUTPUT_ACTIVE_LOW,
+    ONEWIRE2_MASTER_CHANNEL,
+    ONEWIRE2_SAMPLE_CHANNEL,
+    ONEWIRE2_PORT,
+    ONEWIRE2_PIN,
+#if defined(STM32F1XX)
+    ONEWIRE_PAD_MODE_IDLE,
+#endif
+    ONEWIRE_PAD_MODE_ACTIVE,
+#if ONEWIRE_USE_STRONG_PULLUP
+    strong_pullup_assert,
+    strong_pullup_release
+#endif
+};
+
 #endif /* __MAIN_H__ */
 #endif /* __MAIN_H__ */

+ 0 - 27
lib/onewire/boarddef.h

@@ -1,27 +0,0 @@
-/*
-    ChibiOS/RT - Copyright (C) 2016 Uladzimir Pylinsky aka barthess
-
-    Licensed under the Apache License, Version 2.0 (the "License");
-    you may not use this file except in compliance with the License.
-    You may obtain a copy of the License at
-
-        http://www.apache.org/licenses/LICENSE-2.0
-
-    Unless required by applicable law or agreed to in writing, software
-    distributed under the License is distributed on an "AS IS" BASIS,
-    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-    See the License for the specific language governing permissions and
-    limitations under the License.
-*/
-
-#ifndef BOARDDEF_H_
-#define BOARDDEF_H_
-
-#define ONEWIRE_PORT                  GPIOB
-#define ONEWIRE_PIN                   0
-#define ONEWIRE_PAD_MODE_IDLE         PAL_MODE_INPUT
-#define ONEWIRE_PAD_MODE_ACTIVE       PAL_MODE_STM32_ALTERNATE_OPENDRAIN
-#define ONEWIRE_MASTER_CHANNEL        2
-#define ONEWIRE_SAMPLE_CHANNEL        3
-
-#endif /* BOARDDEF_H_ */

+ 37 - 117
lib/onewire/onewire.c

@@ -17,26 +17,8 @@
 #include <string.h>
 #include <string.h>
 
 
 #include "hal.h"
 #include "hal.h"
-#include "boarddef.h"
 #include "onewire.h"
 #include "onewire.h"
 
 
-/*
- ******************************************************************************
- * DEFINES
- ******************************************************************************
- */
-
-/*
- ******************************************************************************
- * EXTERNS
- ******************************************************************************
- */
-
-/*
- ******************************************************************************
- * PROTOTYPES
- ******************************************************************************
- */
 /*
 /*
  * Forward declarations
  * Forward declarations
  */
  */
@@ -46,62 +28,11 @@ static void strong_pullup_release(void);
 #endif
 #endif
 
 
 /*
 /*
- ******************************************************************************
- * GLOBAL VARIABLES
- ******************************************************************************
+ * VARIABLES
  */
  */
 
 
 static uint8_t testbuf[12];
 static uint8_t testbuf[12];
 
 
-/* stores 3 temperature values in millicelsius */
-static int32_t temperature[3];
-static size_t devices_on_bus = 0;
-static onewire_error_t error_code;
-
-/*
- * Config for underlying PWM driver.
- * Note! It is NOT constant because 1-wire driver needs to change them
- * during functioning.
- */
-static PWMConfig pwm_cfg = {
-    0,
-    0,
-    NULL,
-    {
-     {PWM_OUTPUT_DISABLED, NULL},
-     {PWM_OUTPUT_DISABLED, NULL},
-     {PWM_OUTPUT_DISABLED, NULL},
-     {PWM_OUTPUT_DISABLED, NULL}
-    },
-    0,
-#if STM32_PWM_USE_ADVANCED
-    0,
-#endif
-    0,
-    0
-};
-
-/*
- *
- */
-static const onewireConfig ow_cfg = {
-    &PWMD4,
-    &pwm_cfg,
-    PWM_OUTPUT_ACTIVE_LOW,
-    ONEWIRE_MASTER_CHANNEL,
-    ONEWIRE_SAMPLE_CHANNEL,
-    ONEWIRE_PORT,
-    ONEWIRE_PIN,
-#if defined(STM32F1XX)
-    ONEWIRE_PAD_MODE_IDLE,
-#endif
-    ONEWIRE_PAD_MODE_ACTIVE,
-#if ONEWIRE_USE_STRONG_PULLUP
-    strong_pullup_assert,
-    strong_pullup_release
-#endif
-};
-
 /*
 /*
  ******************************************************************************
  ******************************************************************************
  ******************************************************************************
  ******************************************************************************
@@ -126,57 +57,48 @@ static void strong_pullup_release(void) {
 }
 }
 #endif /* ONEWIRE_USE_STRONG_PULLUP */
 #endif /* ONEWIRE_USE_STRONG_PULLUP */
 
 
-/*
- ******************************************************************************
- * EXPORTED FUNCTIONS
- ******************************************************************************
- */
-
 /*
 /*
  *
  *
  */
  */
-void onewireMeasure(void) {
-  error_code = onewire_OK;
+void onewireMeasure(onewired_t * ow) {
   int16_t tmp;
   int16_t tmp;
   uint8_t rombuf[24];
   uint8_t rombuf[24];
-  size_t i = 0;
   bool presence;
   bool presence;
 
 
   onewireObjectInit(&OWD1);
   onewireObjectInit(&OWD1);
-  onewireStart(&OWD1, &ow_cfg);
+  onewireStart(&OWD1, ow->ow_cfg);
 
 
 #if ONEWIRE_SYNTH_SEARCH_TEST
 #if ONEWIRE_SYNTH_SEARCH_TEST
   synthSearchRomTest(&OWD1);
   synthSearchRomTest(&OWD1);
 #endif
 #endif
 
 
-  for (i=0; i<3; i++)
-    temperature[i] = -666;
+  ow->temperature = -666000;
 
 
   if (true == onewireReset(&OWD1)){
   if (true == onewireReset(&OWD1)){
 
 
     memset(rombuf, 0x55, sizeof(rombuf));
     memset(rombuf, 0x55, sizeof(rombuf));
-    devices_on_bus = onewireSearchRom(&OWD1, rombuf, 3);
-    if (devices_on_bus == 0) {
-      error_code = onewire_No_Dev;
+    ow->dev_on_bus = onewireSearchRom(&OWD1, rombuf, 3);
+    if (ow->dev_on_bus == 0) {
+      ow->err_code = onewire_No_Dev;
       return;
       return;
     }
     }
 
 
-    if (1 == devices_on_bus){
+    if (1 == ow->dev_on_bus){
       /* test read rom command */
       /* test read rom command */
       presence = onewireReset(&OWD1);
       presence = onewireReset(&OWD1);
       if (true != presence) {
       if (true != presence) {
-        error_code = onewire_Dev_Lost;
+        ow->err_code = onewire_Dev_Lost;
         return;
         return;
       }
       }
       testbuf[0] = ONEWIRE_CMD_READ_ROM;
       testbuf[0] = ONEWIRE_CMD_READ_ROM;
       onewireWrite(&OWD1, testbuf, 1, 0);
       onewireWrite(&OWD1, testbuf, 1, 0);
       onewireRead(&OWD1, testbuf, 8);
       onewireRead(&OWD1, testbuf, 8);
       if (testbuf[7] != onewireCRC(testbuf, 7)) {
       if (testbuf[7] != onewireCRC(testbuf, 7)) {
-        error_code = onewire_CRC_Err;
+        ow->err_code = onewire_CRC_Err;
         return;
         return;
       }
       }
       if (0 != memcmp(rombuf, testbuf, 8)) {
       if (0 != memcmp(rombuf, testbuf, 8)) {
-        error_code = onewire_MemCmp_Err;
+        ow->err_code = onewire_MemCmp_Err;
         return;
         return;
       }
       }
     }
     }
@@ -184,7 +106,7 @@ void onewireMeasure(void) {
     /* start temperature measurement on all connected devices at once */
     /* start temperature measurement on all connected devices at once */
     presence = onewireReset(&OWD1);
     presence = onewireReset(&OWD1);
     if (true != presence) {
     if (true != presence) {
-      error_code = onewire_Dev_Lost;
+      ow->err_code = onewire_Dev_Lost;
       return;
       return;
     }
     }
     testbuf[0] = ONEWIRE_CMD_SKIP_ROM;
     testbuf[0] = ONEWIRE_CMD_SKIP_ROM;
@@ -202,48 +124,46 @@ void onewireMeasure(void) {
     }
     }
 #endif
 #endif
 
 
-    for (i=0; i<devices_on_bus; i++) {
-      /* read temperature device by device from their scratchpads */
-      presence = onewireReset(&OWD1);
-      if (true != presence) {
-        error_code = onewire_Dev_Lost;
-        return;
-      }
+    /* read temperature device by device from their scratchpads */
+    presence = onewireReset(&OWD1);
+    if (true != presence) {
+      ow->err_code = onewire_Dev_Lost;
+      return;
+    }
 
 
-      testbuf[0] = ONEWIRE_CMD_MATCH_ROM;
-      memcpy(&testbuf[1], &rombuf[i*8], 8);
-      testbuf[9] = ONEWIRE_CMD_READ_SCRATCHPAD;
-      onewireWrite(&OWD1, testbuf, 10, 0);
+    testbuf[0] = ONEWIRE_CMD_MATCH_ROM;
+    memcpy(&testbuf[1], &rombuf[8], 8);
+    testbuf[9] = ONEWIRE_CMD_READ_SCRATCHPAD;
+    onewireWrite(&OWD1, testbuf, 10, 0);
 
 
-      onewireRead(&OWD1, testbuf, 9);
-      if (testbuf[8] != onewireCRC(testbuf, 8)) {
-        error_code = onewire_CRC_Err;
-        return;
-      }
-      memcpy(&tmp, &testbuf, 2);
-      temperature[i] = ((int32_t)tmp * 625) / 10;
+    onewireRead(&OWD1, testbuf, 9);
+    if (testbuf[8] != onewireCRC(testbuf, 8)) {
+      ow->err_code = onewire_CRC_Err;
+      return;
     }
     }
+    memcpy(&tmp, &testbuf, 2);
+    ow->temperature = ((int32_t)tmp * 625) / 10;
   }
   }
   else {
   else {
-    devices_on_bus = 0;
+    ow->dev_on_bus = 0;
   }
   }
   osalThreadSleep(1); /* enforce ChibiOS's stack overflow check */
   osalThreadSleep(1); /* enforce ChibiOS's stack overflow check */
   
   
   onewireStop(&OWD1);
   onewireStop(&OWD1);
 }
 }
 
 
-onewire_error_t onewireGetErrorCode(void) {
-  return error_code;
+onewire_error_t onewireGetErrorCode(onewired_t * ow) {
+  return ow->err_code;
 }
 }
 
 
-uint8_t onewireGetDevicesNum(void) {
-  return (uint8_t)devices_on_bus;
+uint8_t onewireGetDevicesNum(onewired_t * ow) {
+  return (uint8_t)ow->dev_on_bus;
 }
 }
 
 
-int32_t onewireGetTemperature(const size_t num) {
-  if ((num+1) > devices_on_bus || (num+1) < devices_on_bus) {
-    return -999;
+int32_t onewireGetTemperature(onewired_t * ow) {
+  if (ow->err_code == onewire_OK) {
+    return ow->temperature;
   } else {
   } else {
-    return temperature[num];
+    return -333000;
   }
   }
 }
 }

+ 21 - 4
lib/onewire/onewire.h

@@ -17,6 +17,8 @@
 #ifndef _ONEWIRE_H_
 #ifndef _ONEWIRE_H_
 #define _ONEWIRE_H_
 #define _ONEWIRE_H_
 
 
+#include "hal.h"
+
 typedef enum {
 typedef enum {
   onewire_OK = 0,
   onewire_OK = 0,
   onewire_No_Dev,
   onewire_No_Dev,
@@ -25,13 +27,28 @@ typedef enum {
   onewire_MemCmp_Err
   onewire_MemCmp_Err
 } onewire_error_t;
 } onewire_error_t;
 
 
+typedef enum {
+  ow1 = 0,
+  ow2 = 1
+} onewire_dev_t;
+
+typedef struct onewire {
+  int32_t         temperature; /* temperature values in millicelsius */
+  size_t          dev_on_bus;
+  onewire_error_t err_code;
+  const onewireConfig * ow_cfg;
+} onewired_t;
+
+/*
+ * PROTOTYPES
+ */
 #ifdef __cplusplus
 #ifdef __cplusplus
 extern "C" {
 extern "C" {
 #endif
 #endif
-  void onewireMeasure(void);
-  onewire_error_t onewireGetErrorCode(void);
-  uint8_t onewireGetDevicesNum(void);
-  int32_t onewireGetTemperature(size_t num);
+  void onewireMeasure(onewired_t * ow);
+  onewire_error_t onewireGetErrorCode(onewired_t * ow);
+  uint8_t onewireGetDevicesNum(onewired_t * ow);
+  int32_t onewireGetTemperature(onewired_t * ow);
 #ifdef __cplusplus
 #ifdef __cplusplus
 }
 }
 #endif
 #endif

+ 51 - 31
main.c

@@ -24,11 +24,19 @@
 
 
 #include "main.h"
 #include "main.h"
 #include "st7735.h"
 #include "st7735.h"
-//#include "onewire.h"
+#include "onewire.h"
 
 
-/*===========================================================================*/
-/* Generic code.                                                             */
-/*===========================================================================*/
+/*
+ * Provate variables.
+ */
+onewired_t owdc[ONWIRE_CHANNELS_NUM] = {
+  {0, 0, onewire_OK, &ow_cfg1},
+  {0, 0, onewire_OK, &ow_cfg2}
+};
+
+/*
+ * Generic code.
+ */
 
 
 /*
 /*
  * Blinker thread, times are in milliseconds.
  * Blinker thread, times are in milliseconds.
@@ -97,39 +105,51 @@ int main(void) {
   /*
   /*
    * Normal main() thread activity.
    * Normal main() thread activity.
    */
    */
-  /*
-  ST7735_WriteString(0, LCD_LINE_1, "OneWire search...", LiberM_7x10, Blue, Black);
-  onewireMeasure();
   int32_t t;
   int32_t t;
-  int n = onewireGetDevicesNum();
+  int a, b, n;
+
+  ST7735_WriteString(0, LCD_LINE_1, "OneWire1 search...", LiberM_7x10, Blue, Black);
+  onewireMeasure(&owdc[ow1]);
+  n = onewireGetDevicesNum(&owdc[ow1]);
   chsnprintf(buf, 22, "Found %d device[s]", n);
   chsnprintf(buf, 22, "Found %d device[s]", n);
   ST7735_WriteString(0, LCD_LINE_1, buf, LiberM_7x10, Cyan, Black);
   ST7735_WriteString(0, LCD_LINE_1, buf, LiberM_7x10, Cyan, Black);
-  chThdSleepMilliseconds(2000);
-  if (n == 0) {
-    ST7735_WriteString(0, LCD_LINE_1, "DS18B20 not found.", LiberM_7x10, Red, Black);
-  }
-*/
-  int a, b;
+
+  ST7735_WriteString(0, LCD_LINE_2, "OneWire2 search...", LiberM_7x10, Blue, Black);
+  onewireMeasure(&owdc[ow2]);
+  n = onewireGetDevicesNum(&owdc[ow2]);
+  chsnprintf(buf, 22, "Found %d device[s]", n);
+  ST7735_WriteString(0, LCD_LINE_1, buf, LiberM_7x10, Cyan, Black);
+
+  chThdSleepMilliseconds(1000);
   while (true) {
   while (true) {
-/*
-    if (n != 0) {
-      onewireMeasure();
-      onewire_error_t err = onewireGetErrorCode();
-      if (err == onewire_OK) {
-        t = onewireGetTemperature(0);
-        t += 50;
-        t /= 100;
-        a = t / 10; b = t % 10;
-        chsnprintf(buf, 22, "T = %d.%u °C     ", a, b);
-        ST7735_WriteString(0, LCD_LINE_1, buf, LiberM_7x10, Yellow, Black);
-      } else {
-        chsnprintf(buf, 22, "Onewire error: %d", err);
-        ST7735_WriteString(0, LCD_LINE_1, buf, LiberM_7x10, Red, Black);
-      }
+    onewireMeasure(&owdc[ow1]);
+    if (owdc[ow1].err_code == onewire_OK) {
+      t = onewireGetTemperature(&owdc[ow1]);
+      t += 50;
+      t /= 100;
+      a = t / 10; b = t % 10;
+      chsnprintf(buf, 22, "T1 = %d.%u °C     ", a, b);
+      ST7735_WriteString(0, LCD_LINE_1, buf, LiberM_7x10, Yellow, Black);
+    } else {
+      chsnprintf(buf, 22, "Onewire1 error: %d", owdc[ow1].err_code);
+      ST7735_WriteString(0, LCD_LINE_1, buf, LiberM_7x10, Red, Black);
     }
     }
-*/
+
+    onewireMeasure(&owdc[ow1]);
+    if (owdc[ow2].err_code == onewire_OK) {
+      t = onewireGetTemperature(&owdc[ow2]);
+      t += 50;
+      t /= 100;
+      a = t / 10; b = t % 10;
+      chsnprintf(buf, 22, "T2 = %d.%u °C     ", a, b);
+      ST7735_WriteString(0, LCD_LINE_2, buf, LiberM_7x10, Yellow, Black);
+    } else {
+      chsnprintf(buf, 22, "Onewire2 error: %d", owdc[ow2].err_code);
+      ST7735_WriteString(0, LCD_LINE_2, buf, LiberM_7x10, Red, Black);
+    }
+
     chsnprintf(buf, 22, "U = %4u mV", Voltage);
     chsnprintf(buf, 22, "U = %4u mV", Voltage);
-    ST7735_WriteString(0, LCD_LINE_3, buf, LiberM_7x10, Orange, Black);
+    ST7735_WriteString(0, LCD_LINE_4, buf, LiberM_7x10, Orange, Black);
     //adcStartConversion(&ADCD1, &adcgrpcfg1, ADC_Data, ADC_GRP1_BUF_DEPTH);
     //adcStartConversion(&ADCD1, &adcgrpcfg1, ADC_Data, ADC_GRP1_BUF_DEPTH);
     chThdSleepMilliseconds(500);
     chThdSleepMilliseconds(500);
   }
   }