]> git.earman.xyz Git - sensor-watch.git/commitdiff
automatically detect temperature sensor
authorJoey Castillo <joeycastillo@utexas.edu>
Sat, 17 May 2025 14:23:38 +0000 (10:23 -0400)
committerJoey Castillo <joeycastillo@utexas.edu>
Sat, 17 May 2025 14:23:38 +0000 (10:23 -0400)
Makefile
gossamer
movement.c
movement.h
watch-library/shared/driver/thermistor_driver.c
watch-library/shared/driver/thermistor_driver.h

index 7e8d2751879cf263d21e85a5572115c50eddade3..34fccb02f32ae4674c68507d2f711210f16bc885 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -12,9 +12,6 @@ GOSSAMER_PATH=gossamer
 # If you need to force a specific display, set this to the type you want, CLASSIC or CUSTOM
 FORCE_DISPLAY_TYPE=CLASSIC
 
-# Which sensor board?
-SENSOR=NONE
-
 # End of user configurable options.
 
 # Support USB features?
@@ -32,10 +29,6 @@ ifndef BOARD
 $(error Build failed: BOARD not defined. Use one of the four options below, depending on your hardware:$n$n    make BOARD=sensorwatch_red$n    make BOARD=sensorwatch_green$n    make BOARD=sensorwatch_blue$n    make BOARD=sensorwatch_pro$n$n)
 endif
 
-ifeq ($(SENSOR), TEMPERATURE)
-  DEFINES += -DTEMPERATURE_BOARD_INSTALLED
-endif
-
 ifdef FORCE_DISPLAY_TYPE
   ifeq ($(FORCE_DISPLAY_TYPE), CUSTOM)
     DEFINES += -DFORCE_CUSTOM_LCD_TYPE
index 6657eb097bcb83ac19b3509e0296ee151fb310d9..2d79f337c7a6d928830ded91f5d998582f8ea58a 160000 (submodule)
--- a/gossamer
+++ b/gossamer
@@ -1 +1 @@
-Subproject commit 6657eb097bcb83ac19b3509e0296ee151fb310d9
+Subproject commit 2d79f337c7a6d928830ded91f5d998582f8ea58a
index 2d2edae16165ce497971c79101d8843805dffcb1..c7c0bfc1924484ce0bcb68597f255de64cf1a791 100644 (file)
@@ -45,6 +45,7 @@
 #include "evsys.h"
 #include "delay.h"
 #include "movement_activity.h"
+#include "thermistor_driver.h"
 
 #include "movement_config.h"
 
@@ -564,6 +565,8 @@ void app_init(void) {
 
     filesystem_init();
 
+    movement_state.has_thermistor = thermistor_driver_init();
+
     // check if we are plugged into USB power.
     HAL_GPIO_VBUS_DET_in();
     HAL_GPIO_VBUS_DET_pulldown();
index b7d52eb570d27bec114dd731de1dc128aa9eafce..7571b9479513a434b6220a4905d5ea04eda82643 100644 (file)
@@ -287,6 +287,9 @@ typedef struct {
     // temporary alarm enabled boolean, until we implement this in advisories
     bool alarm_enabled;
 
+    // boolean set if thermistor is detected
+    bool has_thermistor;
+
     // boolean set if accelerometer is detected
     bool has_lis2dw;
 } movement_state_t;
index f8891bdd164941c3e2ab507e45efe6445ceedc8b..0b6839e4b9256c109a779171466063dc11489767 100644 (file)
 #include "watch.h"
 #include "watch_utility.h"
 
+// assume we have no thermistor until thermistor_driver_init is called.
+static bool has_thermistor = false;
+
+bool thermistor_driver_init(void) {
+    // once called, assume we have a thermistor unless proven otherwise
+    has_thermistor = true;
+    uint16_t value;
+#if THERMISTOR_ENABLE_VALUE == false
+    const uint16_t threshold_value = 60000;
+#else
+    const uint16_t threshold_value = 5000;
+#endif
+
+    watch_enable_adc();
+    // Enable analog circuitry on the sense pin, which is tied to the thermistor resistor divider.
+    HAL_GPIO_TEMPSENSE_in();
+    HAL_GPIO_TEMPSENSE_pmuxen(HAL_GPIO_PMUX_ADC);
+
+    // Enable digital output on the enable pin, which is the power to the thermistor circuit.
+    HAL_GPIO_TS_ENABLE_out();
+    // and make sure it's DISABLED.
+    HAL_GPIO_TS_ENABLE_write(!THERMISTOR_ENABLE_VALUE);
+
+    // If the temperature sensor is connected, pulling the TS_ENABLE line to its disabled value
+    // connects both sides of the voltage divider to the same potential. If enable value is false,
+    // this will be high, if enable value is true it will be low, and TEMPSENSE should read the same.
+    value = watch_get_analog_pin_level(HAL_GPIO_TEMPSENSE_pin());
+
+    // If setting TS_ENABLE has no effect, there is no thermistor circuit connected to TEMPSENSE.
+    if (value < threshold_value) has_thermistor = false;
+
+    // now flip it to enable the temperature sensor
+    HAL_GPIO_TS_ENABLE_write(THERMISTOR_ENABLE_VALUE);
+
+    // If the temperature sensor is connected, pulling the TS_ENABLE line to its ENABLED value
+    // means we should get a reasonable temperature at this point.
+    value = watch_get_analog_pin_level(HAL_GPIO_TEMPSENSE_pin());
+
+    // value should be >15000 and <55000 and (between -4° and 76° C)
+    if (value < 15000 || value > 55000) has_thermistor = false;
+
+    return has_thermistor;
+}
 
 void thermistor_driver_enable(void) {
-#ifdef HAS_TEMPERATURE_SENSOR
+    if (!has_thermistor) return;
+
     // Enable the ADC peripheral, which we'll use to read the thermistor value.
     watch_enable_adc();
     // Enable analog circuitry on the sense pin, which is tied to the thermistor resistor divider.
@@ -39,11 +83,11 @@ void thermistor_driver_enable(void) {
     HAL_GPIO_TS_ENABLE_out();
     // and make sure it's off.
     HAL_GPIO_TS_ENABLE_write(!THERMISTOR_ENABLE_VALUE);
-#endif
 }
 
 void thermistor_driver_disable(void) {
-#ifdef HAS_TEMPERATURE_SENSOR
+    if (!has_thermistor) return;
+
     // Disable the ADC peripheral.
     watch_disable_adc();
     // Disable analog circuitry on the sense pin to save power.
@@ -51,11 +95,11 @@ void thermistor_driver_disable(void) {
     HAL_GPIO_TEMPSENSE_off();
     // Disable the enable pin's output circuitry.
     HAL_GPIO_TS_ENABLE_off();
-#endif
 }
 
 float thermistor_driver_get_temperature(void) {
-#ifdef HAS_TEMPERATURE_SENSOR
+    if (!has_thermistor) return (float) 0xFFFFFFFF;
+
     // set the enable pin to the level that powers the thermistor circuit.
     HAL_GPIO_TS_ENABLE_write(THERMISTOR_ENABLE_VALUE);
     // get the sense pin level
@@ -64,7 +108,4 @@ float thermistor_driver_get_temperature(void) {
     HAL_GPIO_TS_ENABLE_write(!THERMISTOR_ENABLE_VALUE);
 
     return watch_utility_thermistor_temperature(value, THERMISTOR_HIGH_SIDE, THERMISTOR_B_COEFFICIENT, THERMISTOR_NOMINAL_TEMPERATURE, THERMISTOR_NOMINAL_RESISTANCE, THERMISTOR_SERIES_RESISTANCE);
-#else
-    return (float) 0xFFFFFFFF;
-#endif
 }
index 4abce94985286f408b8f35a5ac9be3b54a91f81d..44510619d71dd2fc968ef5f4bdbc030bcbeb6e8e 100644 (file)
@@ -35,6 +35,7 @@
 #define THERMISTOR_NOMINAL_RESISTANCE (10000.0)
 #define THERMISTOR_SERIES_RESISTANCE (10000.0)
 
+bool thermistor_driver_init(void);
 void thermistor_driver_enable(void);
 void thermistor_driver_disable(void);
 float thermistor_driver_get_temperature(void);