From 53e95a3e5ceb51abfbb969747c689a45aedf7c53 Mon Sep 17 00:00:00 2001 From: Syler Clayton <2224238+Relys@users.noreply.github.com> Date: Fri, 13 Sep 2024 19:40:26 -0700 Subject: [PATCH] "Fix critical shutdown bug in spintend ubox" --- hwconf/Ubox/100v/hw_ubox_100_core.c | 48 +++++++++++++++++++---------- hwconf/Ubox/100v/hw_ubox_100_core.h | 2 ++ hwconf/shutdown.c | 4 ++- 3 files changed, 37 insertions(+), 17 deletions(-) mode change 100644 => 100755 hwconf/Ubox/100v/hw_ubox_100_core.c mode change 100644 => 100755 hwconf/Ubox/100v/hw_ubox_100_core.h diff --git a/hwconf/Ubox/100v/hw_ubox_100_core.c b/hwconf/Ubox/100v/hw_ubox_100_core.c old mode 100644 new mode 100755 index a5a27702c..06bc5bd05 --- a/hwconf/Ubox/100v/hw_ubox_100_core.c +++ b/hwconf/Ubox/100v/hw_ubox_100_core.c @@ -42,7 +42,7 @@ #define UBOX_QUERY_POWER_KEY_IO() (palReadPad(GPIOC, 13)) -void shutdown_ubox_init(void); +void shutdown_init(void); // Variables static volatile bool i2c_running = false; @@ -131,8 +131,8 @@ void hw_init_gpio(void) { palSetPadMode(GPIOC, 13, PAL_MODE_OUTPUT_OPENDRAIN | PAL_MODE_INPUT_PULLUP); UBOX_POWER_KEY_IO_RELEASE(); - - shutdown_ubox_init(); + + shutdown_init(); } void hw_setup_adc_channels(void) { @@ -287,21 +287,37 @@ float hw100_250_get_temp(void) { // Private variables static bool volatile m_button_pressed = false; static volatile float m_inactivity_time = 0.0; -static THD_WORKING_AREA(shutdown_ubox_thread_wa, 256); +static THD_WORKING_AREA(shutdown_thread_wa, 256); static mutex_t m_sample_mutex; static volatile bool m_init_done = false; static volatile bool m_sampling_disabled = false; // Private functions -static THD_FUNCTION(shutdown_ubox_thread, arg); +static THD_FUNCTION(shutdown_thread, arg); -void shutdown_ubox_init(void) { - chMtxObjectInit(&m_sample_mutex); - chThdCreateStatic(shutdown_ubox_thread_wa, sizeof(shutdown_ubox_thread_wa), NORMALPRIO, shutdown_ubox_thread, NULL); - m_init_done = true; +void shutdown_init(void) { + if(!m_init_done){ + chMtxObjectInit(&m_sample_mutex); + chThdCreateStatic(shutdown_thread_wa, sizeof(shutdown_thread_wa), NORMALPRIO, shutdown_thread, NULL); + m_init_done = true; + } } -static bool do_shutdown_ubox(void) { +void shutdown_reset_timer(void) { + m_inactivity_time = 0.0; +} + +float shutdown_get_inactivity_time(void) { + return m_inactivity_time; +} + +void shutdown_hold(bool hold) { + (void)hold; +} + +// TODO: Doesn't use resample. Maybe in future allow resampling if enPOWER_KEY_TYPE is momentary? +bool do_shutdown(bool resample) { + (void)resample; conf_general_store_backup_data(); chThdSleepMilliseconds(100); @@ -310,7 +326,6 @@ static bool do_shutdown_ubox(void) { return true; } - typedef enum { power_key_type_undecided = 0, power_key_type_momentary, @@ -321,7 +336,7 @@ static enPOWER_KEY_TYPE power_key_type = power_key_type_undecided; static uint32_t power_key_pressed_ms = 0; static bool power_key_pressed_when_power_on = false; -static THD_FUNCTION(shutdown_ubox_thread, arg) { +static THD_FUNCTION(shutdown_thread, arg) { (void)arg; chRegSetThreadName("Shutdown_ubox"); @@ -352,6 +367,7 @@ static THD_FUNCTION(shutdown_ubox_thread, arg) { } } else { if((power_key_pressed_ms > 50) && (power_key_pressed_ms < 500)) { + // TODO: Could toggle different led types here?? power_key_click = 1; } power_key_pressed_ms = 0; @@ -394,7 +410,7 @@ static THD_FUNCTION(shutdown_ubox_thread, arg) { //Inactive after 10 seconds, MCU cancels the enable signal, regulator shuts down if button released. m_inactivity_time += dt; if (m_inactivity_time >= 10.0f) { - do_shutdown_ubox(); + do_shutdown(false); } break; case SHUTDOWN_MODE_ALWAYS_ON: @@ -414,7 +430,7 @@ static THD_FUNCTION(shutdown_ubox_thread, arg) { break; case SHUTDOWN_MODE_TOGGLE_BUTTON_ONLY: if(clicked) { - do_shutdown_ubox(); + do_shutdown(true); } break; default: break; @@ -434,7 +450,7 @@ static THD_FUNCTION(shutdown_ubox_thread, arg) { default: break; } if (m_inactivity_time >= shutdown_timeout) { - do_shutdown_ubox(); + do_shutdown(false); } } else { //Because SHUTDOWN_MODE_ALWAYS_OFF's implementation will check m_inactivity_time. @@ -444,7 +460,7 @@ static THD_FUNCTION(shutdown_ubox_thread, arg) { } if(power_key_pressed_ms > 2000) { - do_shutdown_ubox(); + do_shutdown(true); } } else { m_inactivity_time += dt; diff --git a/hwconf/Ubox/100v/hw_ubox_100_core.h b/hwconf/Ubox/100v/hw_ubox_100_core.h old mode 100644 new mode 100755 index 1257c0af5..9cd288655 --- a/hwconf/Ubox/100v/hw_ubox_100_core.h +++ b/hwconf/Ubox/100v/hw_ubox_100_core.h @@ -22,6 +22,8 @@ #ifndef HW_UBOX_V2_100_CORE_H_ #define HW_UBOX_V2_100_CORE_H_ +#define HW_SHUTDOWN_CUSTOM + #ifdef HW_UBOX_V2_100 #define HW_NAME "UBOX_V2_100" #elif defined (HW_UBOX_SINGLE_100) diff --git a/hwconf/shutdown.c b/hwconf/shutdown.c index 73e348575..07ef16764 100644 --- a/hwconf/shutdown.c +++ b/hwconf/shutdown.c @@ -26,7 +26,9 @@ #include "lispif.h" #endif -#ifdef HW_SHUTDOWN_HOLD_ON +#ifdef HW_SHUTDOWN_CUSTOM +// Do nothing. All shutdown functionality is handled in the hardware file. +#elif defined(HW_SHUTDOWN_HOLD_ON) // Private variables bool volatile m_button_pressed = false;