Add watchdog for each thread using Zephyr API

This commit is contained in:
Martin Jäger
2020-05-06 12:53:26 +02:00
parent 1252a1ffa1
commit 2d3594d254
5 changed files with 77 additions and 9 deletions
+34
View File
@@ -28,6 +28,40 @@
#include <power/reboot.h>
#include <drivers/gpio.h>
#include <drivers/watchdog.h>
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()
{
+21 -7
View File
@@ -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 <stdint.h>
/** 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
*
+3
View File
@@ -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) {
+17 -2
View File
@@ -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);
+2
View File
@@ -34,3 +34,5 @@ CONFIG_EXT_THINGSET_SERIAL=y
#CONFIG_EXT_OLED_DISPLAY=y
CONFIG_REBOOT=y
CONFIG_WATCHDOG=y