diff --git a/src/hardware.cpp b/src/hardware.cpp index ad706ca..3cf617b 100644 --- a/src/hardware.cpp +++ b/src/hardware.cpp @@ -30,7 +30,14 @@ #include #include +#define MAX_SW_WDT_CHANNELS 3 + static struct device *wdt; +struct k_timer sw_wtchdg_timer; +struct sw_wdt_channel sw_wdt_channels[MAX_SW_WDT_CHANNELS]; + +int sw_wdt_channel_count = 0; +int hw_wdt_channel; void watchdog_init() { @@ -53,14 +60,41 @@ int watchdog_register(uint32_t timeout_ms) return wdt_install_timeout(wdt, &wdt_config); } +int sw_watchdog_register(uint32_t timeout_ms) +{ + sw_wdt_channels[sw_wdt_channel_count].timeout = timeout_ms; + return sw_wdt_channel_count++; +} + +void sw_watchdog_feed(int thread_id) +{ + sw_wdt_channels[thread_id].check_in_time = k_uptime_get(); +} + void watchdog_start() { wdt_setup(wdt, 0); } -void watchdog_feed(int channel_id) +void sw_watchdog(struct k_timer *timer_id) { - wdt_feed(wdt, channel_id); + wdt_feed(wdt, hw_wdt_channel); + + int64_t current_time = k_uptime_get(); + + for (int i = 0; i < MAX_SW_WDT_CHANNELS; i++) { + if ((current_time - sw_wdt_channels[i].check_in_time) > sw_wdt_channels[i].timeout) { + reset_device(); + } + } + +} + +void sw_watchdog_start() +{ + k_timer_init(&sw_wtchdg_timer, sw_watchdog, NULL); + k_timer_start(&sw_wtchdg_timer, K_MSEC(10), K_MSEC(10)); + hw_wdt_channel = watchdog_register(100); } void start_stm32_bootloader() diff --git a/src/hardware.h b/src/hardware.h index 5d41be3..a94af8e 100644 --- a/src/hardware.h +++ b/src/hardware.h @@ -15,6 +15,11 @@ #include +struct sw_wdt_channel { + int64_t check_in_time; ///< most recent check-in/feed-in time of a thread for sw_watchdog + int32_t timeout; ///< timeout of corresponding thread for sw_watchdog +}; + /** * Initialization of IWDG */ @@ -32,11 +37,6 @@ int watchdog_register(uint32_t timeout_ms); */ void watchdog_start(); -/** - * Feed/kick the watchdog for given channel - */ -void watchdog_feed(int channel_id); - /** Timer for system control (main DC/DC or PWM control loop) is started * * @param freq_Hz Frequency in Hz (10 Hz suggested) @@ -56,4 +56,27 @@ void start_stm32_bootloader(); */ void reset_device(); +/** + * Implements watchdog for multiple threads in software + */ +void sw_watchdog(struct k_timer *timer_id); + +/** + * Configure timer for software watchdog and + * configure actual watchdog + */ +void sw_watchdog_start(); + +/** + * Register sw_watchdog + * + * @param timeout_ms Timeout in milliseconds + */ +int sw_watchdog_register(uint32_t timeout_ms); + +/** + * Feed/Check-in sw_watchdog for given thread + */ +void sw_watchdog_feed(int thread_id); + #endif /* HARDWARE_H */ \ No newline at end of file diff --git a/src/leds.cpp b/src/leds.cpp index 005b4e2..62c0d2a 100644 --- a/src/leds.cpp +++ b/src/leds.cpp @@ -5,8 +5,6 @@ */ #include "leds.h" - -#include "hardware.h" #include "board.h" static int led_states[NUM_LEDS]; // must be one of enum LedState @@ -20,6 +18,7 @@ static bool charging = false; #include #include +#include "hardware.h" #define SLEEP_TIME_MS (1000/60/NUM_LEDS) // 60 Hz @@ -29,7 +28,8 @@ void leds_update_thread() int flicker_count = 0; bool flicker_state = 1; struct device *led_devs[NUM_LED_PINS]; - int wdt_channel = watchdog_register(1000); + + int wdt_channel = sw_watchdog_register(1000); for (int pin = 0; pin < NUM_LED_PINS; pin++) { led_devs[pin] = device_get_binding(led_ports[pin]); @@ -38,7 +38,8 @@ void leds_update_thread() leds_init(); while (1) { - watchdog_feed(wdt_channel); + + sw_watchdog_feed(wdt_channel); // could be increased to value >NUM_LEDS to reduce on-time if (led_count >= NUM_LEDS) { diff --git a/src/main.cpp b/src/main.cpp index 1e7d2fe..0818641 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -74,6 +74,7 @@ void main(void) // wait until all threads are spawned before activating the watchdog k_sleep(K_MSEC(2500)); watchdog_start(); + sw_watchdog_start(); while (1) { charger.discharge_control(&bat_conf); @@ -96,12 +97,13 @@ void main(void) void control_thread() { uint32_t last_call = 0; - int wdt_channel = watchdog_register(200); + int wdt_channel = sw_watchdog_register(200); while (1) { + bool charging = false; - watchdog_feed(wdt_channel); + sw_watchdog_feed(wdt_channel); // convert ADC readings to meaningful measurement values daq_update(); @@ -174,13 +176,14 @@ void control_thread() void ext_mgr_thread() { uint32_t last_call = 0; + int wdt_channel = sw_watchdog_register(3000); // quite long watchdog timeout as we might be dealing with slow communication (e.g. modems // using AT commands via serial interface) - int wdt_channel = watchdog_register(3000); while (1) { - watchdog_feed(wdt_channel); + + sw_watchdog_feed(wdt_channel); uint32_t now = k_uptime_get() / 1000; ext_mgr.process_asap(); // approx. every millisecond diff --git a/zephyr/prj.conf b/zephyr/prj.conf index d09157f..64ce61f 100644 --- a/zephyr/prj.conf +++ b/zephyr/prj.conf @@ -8,6 +8,8 @@ CONFIG_GPIO=y CONFIG_SERIAL=y CONFIG_I2C=y +CONFIG_WDT_DISABLE_AT_BOOT=y + # disable log by default to save memory for STM32F0 with little RAM CONFIG_LOG=y CONFIG_LOG_MINIMAL=y