]> git.earman.xyz Git - sensor-watch.git/commitdiff
watch_adc: enable ADC when checking battery; let's get rid of this footgun once and...
authorJoey Castillo <joeycastillo@utexas.edu>
Wed, 21 May 2025 12:51:39 +0000 (08:51 -0400)
committerJoey Castillo <joeycastillo@utexas.edu>
Wed, 21 May 2025 12:51:39 +0000 (08:51 -0400)
legacy/watch_faces/clock/close_enough_clock_face.c
legacy/watch_faces/clock/minute_repeater_decimal_face.c
legacy/watch_faces/clock/repetition_minute_face.c
legacy/watch_faces/clock/simple_clock_bin_led_face.c
legacy/watch_faces/clock/weeknumber_clock_face.c
watch-faces/clock/clock_face.c
watch-faces/sensor/voltage_face.c
watch-library/hardware/watch/watch_adc.c
watch-library/hardware/watch/watch_private.c
watch-library/shared/watch/watch_adc.h

index 98a8e7032ce3d39ad2470f111f1d6a9a5dfe3771..99dec5c4bf428bdc7b83ccc6b09f91d7aceb5354 100644 (file)
@@ -109,9 +109,7 @@ bool close_enough_clock_face_loop(movement_event_t event, void *context) {
             // check the battery voltage once a day...
             if (date_time.unit.day != state->last_battery_check) {
                 state->last_battery_check = date_time.unit.day;
-                watch_enable_adc();
                 uint16_t voltage = watch_get_vcc_voltage();
-                watch_disable_adc();
                 // 2.2 volts will happen when the battery has maybe 5-10% remaining?
                 // we can refine this later.
                 state->battery_low = (voltage < 2200);
index 9f5eb43c7df9170f6146798785480eb23c4df884..f971d560b4d2f9e43103a4bcd60b210ebf8ef006 100644 (file)
@@ -115,9 +115,7 @@ bool minute_repeater_decimal_face_loop(movement_event_t event, void *context) {
             // check the battery voltage once a day...
             if (date_time.unit.day != state->last_battery_check) {
                 state->last_battery_check = date_time.unit.day;
-                watch_enable_adc();
                 uint16_t voltage = watch_get_vcc_voltage();
-                watch_disable_adc();
                 // 2.2 volts will happen when the battery has maybe 5-10% remaining?
                 // we can refine this later.
                 state->battery_low = (voltage < 2200);
index b475b45ee3c4b336592a40117f00e9759b1ad5cf..0b1daf21136a96ca2ef37b6c669170158100fded 100644 (file)
@@ -100,9 +100,7 @@ bool repetition_minute_face_loop(movement_event_t event, void *context) {
             // check the battery voltage once a day...
             if (date_time.unit.day != state->last_battery_check) {
                 state->last_battery_check = date_time.unit.day;
-                watch_enable_adc();
                 uint16_t voltage = watch_get_vcc_voltage();
-                watch_disable_adc();
                 // 2.2 volts will happen when the battery has maybe 5-10% remaining?
                 // we can refine this later.
                 state->battery_low = (voltage < 2200);
index 527b1cd14e5802ac3b03026dd0caee4498a2d72a..a3f512425ef748e0ff60b48e88301e2d46527e5d 100644 (file)
@@ -126,9 +126,7 @@ bool simple_clock_bin_led_face_loop(movement_event_t event, void *context) {
                 // check the battery voltage once a day...
                 if (date_time.unit.day != state->last_battery_check) {
                     state->last_battery_check = date_time.unit.day;
-                    watch_enable_adc();
                     uint16_t voltage = watch_get_vcc_voltage();
-                    watch_disable_adc();
                     // 2.2 volts will happen when the battery has maybe 5-10% remaining?
                     // we can refine this later.
                     state->battery_low = (voltage < 2200);
index 3a153e185072a4ce7ad10b4401f98b463ec3ee04..5bfab84741647be4f2422e9e7d5c7958a21c5953 100644 (file)
@@ -82,9 +82,7 @@ bool weeknumber_clock_face_loop(movement_event_t event, void *context) {
             // check the battery voltage once a day...
             if (date_time.unit.day != state->last_battery_check) {
                 state->last_battery_check = date_time.unit.day;
-                watch_enable_adc();
                 uint16_t voltage = watch_get_vcc_voltage();
-                watch_disable_adc();
                 // 2.2 volts will happen when the battery has maybe 5-10% remaining?
                 // we can refine this later.
                 state->battery_low = (voltage < 2200);
index 78d9d7008d9b1e8ba60c0aef1756c515fe2e454e..73e1ecfae04893baad0ea8faf36b83544314d34e 100644 (file)
@@ -98,9 +98,7 @@ static void clock_check_battery_periodically(clock_state_t *state, watch_date_ti
 
     state->last_battery_check = date_time.unit.day;
 
-    watch_enable_adc();
     uint16_t voltage = watch_get_vcc_voltage();
-    watch_disable_adc();
 
     state->battery_low = voltage < CLOCK_FACE_LOW_BATTERY_VOLTAGE_THRESHOLD;
 
index bb0461bd8a887543aae9dfbbc3014afc0c261c40..320f30f93f82eacff181516999a4334fa9e7a43f 100644 (file)
@@ -28,9 +28,7 @@
 #include "watch.h"
 
 static void _voltage_face_update_display(void) {
-    watch_enable_adc();
     float voltage = (float)watch_get_vcc_voltage() / 1000.0;
-    watch_disable_adc();
 
     watch_display_text_with_fallback(WATCH_POSITION_TOP_LEFT, "BAT", "BA");
     watch_display_float_with_best_effort(voltage, " V");
index 11cc318cdc3bf2451fe283ee6b570da841128f08..f85e081bf0960ee8a8319ce9f02077b7a34e1637 100644 (file)
@@ -67,6 +67,11 @@ void _watch_set_analog_reference_voltage(uint8_t reference) {
 uint16_t watch_get_vcc_voltage(void) {
     // stash the previous reference so we can restore it when we're done.
     uint8_t oldref = ADC->REFCTRL.bit.REFSEL;
+    // same with the previous state of the ADC
+    bool adc_was_disabled = !adc_is_enabled();
+
+    // enable the ADC if needed
+    if (adc_was_disabled) watch_enable_adc();
 
     // if we weren't already using the internal reference voltage, select it now.
     if (oldref != ADC_REFCTRL_REFSEL_INTREF_Val) _watch_set_analog_reference_voltage(ADC_REFCTRL_REFSEL_INTREF_Val);
@@ -77,6 +82,9 @@ uint16_t watch_get_vcc_voltage(void) {
     // restore the old reference, if needed.
     if (oldref != ADC_REFCTRL_REFSEL_INTREF_Val) _watch_set_analog_reference_voltage(oldref);
 
+    // and restore the ADC to its previous state
+    if (adc_was_disabled) watch_disable_adc();
+
     return (uint16_t)((raw_val * 1000) / (1024 * 1 << ADC->AVGCTRL.bit.SAMPLENUM));
 }
 
index 0eb0cb45d108a90f529847941a66cfe79a1e38ce..03091c09894662f96330b5fb5b89bb7a8c348762 100644 (file)
@@ -51,9 +51,7 @@ void _watch_init(void) {
     while(!SUPC->STATUS.bit.VREGRDY); // wait for voltage regulator to become ready
 
     // check the battery voltage...
-    watch_enable_adc();
     uint16_t battery_voltage = watch_get_vcc_voltage();
-    watch_disable_adc();
     // ...because we can enable the more efficient low power regulator if the system voltage is > 2.5V
     // still, enable LPEFF only if the battery voltage is comfortably above this threshold.
     if (battery_voltage >= 2700) {
index b2685da9ef342fd5bd3289a2aff0e1a6fbaa3e90..c4b4bdb091cf19fef3261aa756f397aa2e46d515 100644 (file)
@@ -53,7 +53,8 @@ void watch_enable_analog_input(const uint16_t pin);
 uint16_t watch_get_analog_pin_level(const uint16_t pin);
 
 /** @brief Returns the voltage of the VCC supply in millivolts (i.e. 3000 mV == 3.0 V). If running on
-  *        a coin cell, this will be the battery voltage.
+  *        a coin cell, this will be the battery voltage. If the ADC is not running when this function
+  *        is called, it enabled the ADC briefly, and returns it to the off state.
   * @details Unlike other ADC functions, this function does not return a raw value from the ADC, but
   *          rather scales it to an actual number of millivolts. This is because the ADC doesn't let
   *          us measure VCC per se; it instead lets us measure VCC / 4, and we choose to measure it