Replace software watchdog with Zephyr task_wdt subsystem

This commit is contained in:
Martin Jäger
2021-02-04 17:28:52 +01:00
parent 941545ea7e
commit a352d6b98c
7 changed files with 32 additions and 143 deletions
+4 -2
View File
@@ -10,6 +10,7 @@
#include <device.h>
#include <drivers/gpio.h>
#include <drivers/can.h>
#include <task_wdt/task_wdt.h>
#ifdef CONFIG_ISOTP
#include <canbus/isotp.h>
@@ -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;
+3 -2
View File
@@ -12,6 +12,7 @@
#include <zephyr.h>
#include <sys/printk.h>
#include <drivers/uart.h>
#include <task_wdt/task_wdt.h>
#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;
+11 -99
View File
@@ -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 <power/reboot.h>
#include <drivers/gpio.h>
#include <drivers/watchdog.h>
#include <logging/log.h>
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 */
+5 -34
View File
@@ -13,40 +13,6 @@
* Hardware-specific functions like timers, watchdog, bootloader
*/
#include <stdint.h>
/**
* 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 */
+3 -2
View File
@@ -9,6 +9,7 @@
#include <zephyr.h>
#ifndef UNIT_TEST
#include <drivers/gpio.h>
#include <task_wdt/task_wdt.h>
#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) {
+4 -4
View File
@@ -10,6 +10,7 @@
#include <stdio.h>
#include <device.h>
#include <drivers/gpio.h>
#include <task_wdt/task_wdt.h>
#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();
+2
View File
@@ -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