Restructure SW watchdog and fix bug for >3 channels
The feed function considered max. number of channels and not actual number of registered channels, which resulted in undefined behavior if not all channels were used. Also introducing more simple API to hide the software implementation.
This commit is contained in:
+51
-32
@@ -33,71 +33,90 @@
|
||||
#include <drivers/gpio.h>
|
||||
#include <drivers/watchdog.h>
|
||||
|
||||
#define MAX_SW_WDT_CHANNELS 3
|
||||
#include <logging/log.h>
|
||||
LOG_MODULE_REGISTER(hardware, CONFIG_LOG_DEFAULT_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 struct device *wdt;
|
||||
struct k_timer sw_wtchdg_timer;
|
||||
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) {
|
||||
printk("Cannot get WDT device\n");
|
||||
LOG_DBG("Cannot get IWDG 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);
|
||||
}
|
||||
|
||||
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();
|
||||
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 sw_watchdog(struct k_timer *timer_id)
|
||||
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 < MAX_SW_WDT_CHANNELS; i++) {
|
||||
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!\n", i);
|
||||
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()
|
||||
|
||||
+9
-28
@@ -15,11 +15,6 @@
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
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
|
||||
*/
|
||||
@@ -29,6 +24,8 @@ 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);
|
||||
|
||||
@@ -37,6 +34,13 @@ int watchdog_register(uint32_t timeout_ms);
|
||||
*/
|
||||
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)
|
||||
@@ -56,27 +60,4 @@ 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 */
|
||||
+2
-2
@@ -117,7 +117,7 @@ void leds_update_thread()
|
||||
bool flicker_state = true;
|
||||
struct device *led_devs[NUM_LED_PINS];
|
||||
|
||||
int wdt_channel = sw_watchdog_register(1000);
|
||||
int wdt_channel = watchdog_register(1000);
|
||||
|
||||
for (int pin = 0; pin < NUM_LED_PINS; pin++) {
|
||||
led_devs[pin] = device_get_binding(led_ports[pin]);
|
||||
@@ -127,7 +127,7 @@ void leds_update_thread()
|
||||
|
||||
while (true) {
|
||||
|
||||
sw_watchdog_feed(wdt_channel);
|
||||
watchdog_feed(wdt_channel);
|
||||
|
||||
// could be increased to value >NUM_LEDS to reduce on-time
|
||||
if (led_count >= NUM_LEDS) {
|
||||
|
||||
+4
-5
@@ -73,7 +73,6 @@ void main(void)
|
||||
// wait until all threads are spawned before activating the watchdog
|
||||
k_sleep(K_MSEC(2500));
|
||||
watchdog_start();
|
||||
sw_watchdog_start();
|
||||
|
||||
while (true) {
|
||||
charger.discharge_control(&bat_conf);
|
||||
@@ -96,13 +95,13 @@ void main(void)
|
||||
void control_thread()
|
||||
{
|
||||
uint32_t last_call = 0;
|
||||
int wdt_channel = sw_watchdog_register(200);
|
||||
int wdt_channel = watchdog_register(200);
|
||||
|
||||
while (true) {
|
||||
|
||||
bool charging = false;
|
||||
|
||||
sw_watchdog_feed(wdt_channel);
|
||||
watchdog_feed(wdt_channel);
|
||||
|
||||
// convert ADC readings to meaningful measurement values
|
||||
daq_update();
|
||||
@@ -175,14 +174,14 @@ void control_thread()
|
||||
void ext_mgr_thread()
|
||||
{
|
||||
uint32_t last_call = 0;
|
||||
int wdt_channel = sw_watchdog_register(3000);
|
||||
int wdt_channel = watchdog_register(3000);
|
||||
|
||||
// quite long watchdog timeout as we might be dealing with slow communication (e.g. modems
|
||||
// using AT commands via serial interface)
|
||||
|
||||
while (true) {
|
||||
|
||||
sw_watchdog_feed(wdt_channel);
|
||||
watchdog_feed(wdt_channel);
|
||||
|
||||
uint32_t now = k_uptime_get() / 1000;
|
||||
ext_mgr.process_asap(); // approx. every millisecond
|
||||
|
||||
Reference in New Issue
Block a user