From a352d6b98c5a747108194a454f4742338f4c1ddc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20J=C3=A4ger?= Date: Tue, 22 Dec 2020 19:52:52 +0100 Subject: [PATCH] Replace software watchdog with Zephyr task_wdt subsystem --- src/ext/can.cpp | 6 ++- src/ext/serial.cpp | 5 ++- src/hardware.cpp | 110 +++++---------------------------------------- src/hardware.h | 39 +++------------- src/leds.cpp | 5 ++- src/main.cpp | 8 ++-- zephyr/prj.conf | 2 + 7 files changed, 32 insertions(+), 143 deletions(-) diff --git a/src/ext/can.cpp b/src/ext/can.cpp index 287e76a..d6289ca 100644 --- a/src/ext/can.cpp +++ b/src/ext/can.cpp @@ -10,6 +10,7 @@ #include #include #include +#include #ifdef CONFIG_ISOTP #include @@ -122,7 +123,7 @@ void can_pub_isr(uint32_t err_flags, void *arg) void can_pub_thread() { - int wdt_channel = watchdog_register(1100); + int wdt_channel = task_wdt_add(1100, task_wdt_callback, (void *)k_current_get()); unsigned int can_id; uint8_t can_data[8]; @@ -136,7 +137,8 @@ void can_pub_thread() int64_t t_start = k_uptime_get(); while (true) { - watchdog_feed(wdt_channel); + + task_wdt_feed(wdt_channel); if (pub_can_enable) { int data_len = 0; diff --git a/src/ext/serial.cpp b/src/ext/serial.cpp index a333576..9137256 100644 --- a/src/ext/serial.cpp +++ b/src/ext/serial.cpp @@ -12,6 +12,7 @@ #include #include #include +#include #include "thingset.h" #include "hardware.h" @@ -114,13 +115,13 @@ void serial_thread() uint32_t last_call = 0; // long watchdog timeout needed for ThingSet conf calls which write to slow EEPROM - int wdt_channel = watchdog_register(500); + int wdt_channel = task_wdt_add(500, task_wdt_callback, (void *)k_current_get()); uart_irq_callback_user_data_set(uart_dev, process_input, NULL); uart_irq_rx_enable(uart_dev); while (true) { - watchdog_feed(wdt_channel); + task_wdt_feed(wdt_channel); uint32_t now = k_uptime_get() / 1000; diff --git a/src/hardware.cpp b/src/hardware.cpp index d27f5ce..6095119 100644 --- a/src/hardware.cpp +++ b/src/hardware.cpp @@ -12,17 +12,6 @@ #include "leds.h" #include "setup.h" -#if defined(CONFIG_SOC_SERIES_STM32F0X) -#define SYS_MEM_START 0x1FFFC800 -#define SRAM_END 0x20003FFF // 16k -#elif defined(CONFIG_SOC_SERIES_STM32L0X) -#define SYS_MEM_START 0x1FF00000 -#define SRAM_END 0X20004FFF // 20k -#define FLASH_LAST_PAGE 0x0802FF80 // start address of last page (192 kbyte cat 5 devices) -#endif - -#define MAGIC_CODE_ADDR (SRAM_END - 0xF) // where the magic code is stored - #ifndef UNIT_TEST #if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), boot0)) @@ -31,94 +20,10 @@ #include #include -#include #include LOG_MODULE_REGISTER(hardware, CONFIG_HW_LOG_LEVEL); -#define MAX_SW_WDT_CHANNELS 5 - -struct sw_wdt_channel { - int64_t check_in_time; // most recent check-in/feed-in time of a thread for sw_watchdog - uint32_t timeout; // timeout of corresponding thread for sw_watchdog -}; - -static const struct device *wdt; -struct k_timer sw_wdt_timer; -struct sw_wdt_channel sw_wdt_channels[MAX_SW_WDT_CHANNELS]; - -int sw_wdt_channel_count = 0; -int hw_wdt_channel; - -/* - * Main software watchdog function called by kernel timer - */ -static void sw_watchdog(struct k_timer *timer_id); - -void watchdog_init() -{ - wdt = device_get_binding(DT_LABEL(DT_NODELABEL(iwdg))); - if (!wdt) { - LOG_DBG("Cannot get IWDG device"); - return; - } -} - -int watchdog_register(uint32_t timeout_ms) -{ - if (sw_wdt_channel_count < MAX_SW_WDT_CHANNELS) { - sw_wdt_channels[sw_wdt_channel_count].timeout = timeout_ms; - return sw_wdt_channel_count++; - } - else { - return -1; - } -} - -void watchdog_start() -{ - struct wdt_timeout_cfg wdt_config; - wdt_config.flags = WDT_FLAG_RESET_SOC; - wdt_config.window.min = 0U; - wdt_config.window.max = 1000U; // initialize with long timeout - wdt_config.callback = NULL; // STM32 does not support callbacks - - // look for smallest timeout in software watchdog channels - for (int i = 0; i < sw_wdt_channel_count; i++) { - if (sw_wdt_channels[i].timeout < wdt_config.window.max) { - wdt_config.window.max = sw_wdt_channels[i].timeout; - } - } - - // start timer for software watchdog - k_timer_init(&sw_wdt_timer, sw_watchdog, NULL); - k_timer_start(&sw_wdt_timer, K_MSEC(10), K_MSEC(10)); - - // finally start hardware watchdog - wdt_setup(wdt, 0); - hw_wdt_channel = wdt_install_timeout(wdt, &wdt_config); -} - -void watchdog_feed(int thread_id) -{ - sw_wdt_channels[thread_id].check_in_time = k_uptime_get(); -} - -static void sw_watchdog(struct k_timer *timer_id) -{ - // feed also hardware watchdog - wdt_feed(wdt, hw_wdt_channel); - - int64_t current_time = k_uptime_get(); - - for (int i = 0; i < sw_wdt_channel_count; i++) { - if ((current_time - sw_wdt_channels[i].check_in_time) > sw_wdt_channels[i].timeout) { - LOG_ERR("Watchdog channel %d triggered!", i); - reset_device(); - } - } -} - void start_stm32_bootloader() { #if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), boot0)) @@ -130,10 +35,6 @@ void start_stm32_bootloader() k_sleep(K_MSEC(100)); // wait for capacitor at BOOT0 pin to charge up reset_device(); -#elif defined(CONFIG_SOC_SERIES_STM32F0X) - // place magic code at end of RAM and initiate reset - *((uint32_t *)(MAGIC_CODE_ADDR)) = 0xDEADBEEF; - reset_device(); #endif } @@ -142,10 +43,21 @@ void reset_device() sys_reboot(SYS_REBOOT_COLD); } +void task_wdt_callback(int channel_id, void *user_data) +{ + printk("Task watchdog callback (channel: %d, thread: %s)\n", + channel_id, k_thread_name_get((k_tid_t)user_data)); + + printk("Resetting device...\n"); + + sys_reboot(SYS_REBOOT_COLD); +} + #else // dummy functions for unit tests void start_stm32_bootloader() {} void reset_device() {} +void task_wdt_callback(int channel_id, void *user_data) {} #endif /* UNIT_TEST */ diff --git a/src/hardware.h b/src/hardware.h index acd99ed..6424d9c 100644 --- a/src/hardware.h +++ b/src/hardware.h @@ -13,40 +13,6 @@ * Hardware-specific functions like timers, watchdog, bootloader */ -#include - -/** - * Initialization of IWDG - */ -void watchdog_init(); - -/** - * Register watchdog for this thread - * - * @param timeout_ms Timeout in milliseconds - * - * @returns Assigned channel number or -1 in case of error - */ -int watchdog_register(uint32_t timeout_ms); - -/** - * Must be called after all channels have been registered - */ -void watchdog_start(); - -/** - * Feed/Check-in watchdog for given channel - * - * @param channel Channel number assigned by watchdog_register function - */ -void watchdog_feed(int channel); - -/** Timer for system control (main DC/DC or PWM control loop) is started - * - * @param freq_Hz Frequency in Hz (10 Hz suggested) - */ -void control_timer_start(int freq_Hz); - /** DC/DC or PWM control loop (implemented in main.cpp) */ void system_control(); @@ -60,4 +26,9 @@ void start_stm32_bootloader(); */ void reset_device(); +/** + * Callback for task watchdogs used in multiple threads + */ +void task_wdt_callback(int channel_id, void *user_data); + #endif /* HARDWARE_H */ \ No newline at end of file diff --git a/src/leds.cpp b/src/leds.cpp index f909599..657aeb4 100644 --- a/src/leds.cpp +++ b/src/leds.cpp @@ -9,6 +9,7 @@ #include #ifndef UNIT_TEST #include +#include #endif #include "hardware.h" @@ -136,7 +137,7 @@ void leds_update_thread() bool flicker_state = true; const struct device *led_devs[NUM_LED_PINS]; - int wdt_channel = watchdog_register(1000); + int wdt_channel = task_wdt_add(1000, task_wdt_callback, (void *)k_current_get()); for (int pin = 0; pin < NUM_LED_PINS; pin++) { led_devs[pin] = device_get_binding(led_ports[pin]); @@ -146,7 +147,7 @@ void leds_update_thread() while (true) { - watchdog_feed(wdt_channel); + task_wdt_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 fdd7fce..71521c1 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -10,6 +10,7 @@ #include #include #include +#include #include "thingset.h" // handles access to internal data via communication interfaces #include "setup.h" @@ -31,7 +32,7 @@ void main(void) { printf("Libre Solar Charge Controller: %s\n", CONFIG_BOARD); - watchdog_init(); + task_wdt_init(device_get_binding(DT_LABEL(DT_NODELABEL(iwdg)))); setup(); @@ -71,7 +72,6 @@ void main(void) // wait until all threads are spawned before activating the watchdog k_sleep(K_MSEC(2500)); - watchdog_start(); int64_t t_start = k_uptime_get(); @@ -129,14 +129,14 @@ void main(void) void control_thread() { - int wdt_channel = watchdog_register(200); + int wdt_channel = task_wdt_add(200, task_wdt_callback, (void *)k_current_get()); while (true) { // control loop runs at approx. 10 Hz bool charging = false; - watchdog_feed(wdt_channel); + task_wdt_feed(wdt_channel); // convert ADC readings to meaningful measurement values daq_update(); diff --git a/zephyr/prj.conf b/zephyr/prj.conf index 573637a..ac8efdf 100644 --- a/zephyr/prj.conf +++ b/zephyr/prj.conf @@ -12,6 +12,8 @@ CONFIG_BUILD_OUTPUT_BIN=y CONFIG_WATCHDOG=y CONFIG_WDT_DISABLE_AT_BOOT=y +CONFIG_TASK_WDT=y +CONFIG_THREAD_NAME=y CONFIG_REBOOT=y