From 2d3594d25418191c0a60e5bbd98fcc99447e4bf0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20J=C3=A4ger?= Date: Sun, 5 Apr 2020 19:24:51 +0200 Subject: [PATCH] Add watchdog for each thread using Zephyr API --- src/hardware.cpp | 34 ++++++++++++++++++++++++++++++++++ src/hardware.h | 28 +++++++++++++++++++++------- src/leds.cpp | 3 +++ src/main.cpp | 19 +++++++++++++++++-- zephyr/prj.conf | 2 ++ 5 files changed, 77 insertions(+), 9 deletions(-) diff --git a/src/hardware.cpp b/src/hardware.cpp index 12b4051..72a6ef3 100644 --- a/src/hardware.cpp +++ b/src/hardware.cpp @@ -28,6 +28,40 @@ #include #include +#include + +static struct device *wdt; + +void watchdog_init() +{ + wdt = device_get_binding(DT_INST_0_ST_STM32_WATCHDOG_LABEL); + if (!wdt) { + printk("Cannot get WDT device\n"); + return; + } +} + +int watchdog_register(uint32_t timeout_ms) +{ + struct wdt_timeout_cfg wdt_config; + + wdt_config.flags = WDT_FLAG_RESET_SOC; + wdt_config.window.min = 0U; + wdt_config.window.max = timeout_ms; + wdt_config.callback = NULL; // STM32 does not support callbacks + + return wdt_install_timeout(wdt, &wdt_config); +} + +void watchdog_start() +{ + wdt_setup(wdt, 0); +} + +void watchdog_feed(int channel_id) +{ + wdt_feed(wdt, channel_id); +} void start_stm32_bootloader() { diff --git a/src/hardware.h b/src/hardware.h index d6db2fc..5d41be3 100644 --- a/src/hardware.h +++ b/src/hardware.h @@ -13,15 +13,29 @@ * Hardware-specific functions like timers, watchdog, bootloader */ -/** Initialization of IWDG - * - * @param timeout Timeout in seconds - */ -void init_watchdog(float timeout); +#include -/** Feeds/kicks the watchdog +/** + * Initialization of IWDG */ -void feed_the_dog(); +void watchdog_init(); + +/** + * Register watchdog for this thread + * + * @param timeout_ms Timeout in milliseconds + */ +int watchdog_register(uint32_t timeout_ms); + +/** + * Must be called after all channels have been registered + */ +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 * diff --git a/src/leds.cpp b/src/leds.cpp index 6d287ee..3225658 100644 --- a/src/leds.cpp +++ b/src/leds.cpp @@ -6,6 +6,7 @@ #include "leds.h" +#include "hardware.h" #include "board.h" static int led_states[NUM_LEDS]; // must be one of enum LedState @@ -28,6 +29,7 @@ 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); for (int pin = 0; pin < NUM_LED_PINS; pin++) { led_devs[pin] = device_get_binding(led_ports[pin]); @@ -36,6 +38,7 @@ void leds_update_thread() leds_init(); while (1) { + 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 89a2c10..4ebf691 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -33,6 +33,8 @@ void main(void) { printf("Libre Solar Charge Controller: %s\n", CONFIG_BOARD); + watchdog_init(); + setup(); battery_conf_init(&bat_conf, CONFIG_BAT_DEFAULT_TYPE, @@ -71,7 +73,9 @@ void main(void) bat_conf.voltage_load_reconnect, bat_conf.voltage_absolute_max); #endif - k_sleep(2000); // safety feature: be able to re-flash before starting + // wait until all threads are spawned before activating the watchdog + k_sleep(2500); + watchdog_start(); while (1) { charger.discharge_control(&bat_conf); @@ -89,7 +93,10 @@ void main(void) void control_thread() { uint32_t last_call = 0; + int wdt_channel = watchdog_register(200); + while (1) { + watchdog_feed(wdt_channel); // convert ADC readings to meaningful measurement values daq_update(); @@ -150,7 +157,14 @@ void control_thread() void ext_mgr_thread() { uint32_t last_call = 0; + + // 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); + uint32_t now = k_uptime_get() / 1000; ext_mgr.process_asap(); // approx. every millisecond if (now >= last_call + 1) { @@ -161,9 +175,10 @@ void ext_mgr_thread() } } +// 2s delay for control thread as a safety feature: be able to re-flash before starting K_THREAD_DEFINE(control_thread_id, 1024, control_thread, NULL, NULL, NULL, 2, 0, 2000); -K_THREAD_DEFINE(leds_thread, 256, leds_update_thread, NULL, NULL, NULL, 4, 0, K_NO_WAIT); +K_THREAD_DEFINE(leds_thread, 256, leds_update_thread, NULL, NULL, NULL, 4, 0, 100); K_THREAD_DEFINE(ext_thread, 1024, ext_mgr_thread, NULL, NULL, NULL, 6, 0, 1000); diff --git a/zephyr/prj.conf b/zephyr/prj.conf index 16d7342..a308846 100644 --- a/zephyr/prj.conf +++ b/zephyr/prj.conf @@ -34,3 +34,5 @@ CONFIG_EXT_THINGSET_SERIAL=y #CONFIG_EXT_OLED_DISPLAY=y CONFIG_REBOOT=y + +CONFIG_WATCHDOG=y