Hi,
I used sdk 2.6.1,
when performing FOTA, should we disable the watchdog timer? If so, how can we disable it?
Hi,
I used sdk 2.6.1,
when performing FOTA, should we disable the watchdog timer? If so, how can we disable it?
Some FOTA applications do disable watchdog, but it is an architectural choice that you can make. A code snippet on how to do it can be as below. Please use this as a reference only as I have not tested this to work.
#include <zephyr.h>
#include <drivers/watchdog.h>
#define WDT_TIMEOUT_MS 10000 // Watchdog timeout in milliseconds (adjust as needed)
#define WDT_DEVICE_NAME DT_LABEL(DT_NODELABEL(wdt0))
static const struct device *wdt_dev;
static int wdt_channel_id = -1;
// Set up the watchdog timer to ensure system stability by resetting on timeouts
void watchdog_init(void) {
// Get the watchdog device
wdt_dev = device_get_binding(WDT_DEVICE_NAME);
if (!wdt_dev) {
printk("Error: Cannot find watchdog device\n");
return;
}
// Configure the watchdog timer with the specified timeout and reset behavior
struct wdt_timeout_cfg wdt_config = {
.window = { .min = 0, .max = K_MSEC(WDT_TIMEOUT_MS) },
.callback = NULL, // We’re not using a callback for simplicity
.flags = WDT_FLAG_RESET_SOC // Trigger a full system reset on timeout
};
// Install the watchdog timeout with the configuration above
wdt_channel_id = wdt_install_timeout(wdt_dev, &wdt_config);
if (wdt_channel_id < 0) {
printk("Error: Failed to install watchdog timeout\n");
return;
}
// Start the watchdog timer
int err = wdt_setup(wdt_dev, 0);
if (err) {
printk("Error: Failed to set up watchdog\n");
return;
}
printk("Watchdog initialized with %d ms timeout\n", WDT_TIMEOUT_MS);
}
// Function to enable the watchdog, which starts the countdown
void watchdog_enable(void) {
if (wdt_channel_id >= 0) {
wdt_feed(wdt_dev, wdt_channel_id); // Feed the watchdog to start it
printk("Watchdog enabled\n");
} else {
printk("Error: Watchdog channel not initialized\n");
}
}
// Function to disable the watchdog, typically before long operations like FOTA
void watchdog_disable(void) {
if (wdt_channel_id >= 0) {
int err = wdt_disable(wdt_dev); // Disables the watchdog, if supported by your SDK
if (err == 0) {
printk("Watchdog disabled\n");
} else {
printk("Error: Unable to disable watchdog\n");
}
} else {
printk("Error: Watchdog channel not initialized\n");
}
}
// Call this function right before starting a FOTA update to avoid unexpected resets
void prepare_for_fota(void) {
watchdog_disable(); // Temporarily turn off watchdog for the update
// Any other FOTA preparation steps can go here
}
// Call this function after FOTA completes to re-enable the watchdog
void post_fota_cleanup(void) {
// Any necessary cleanup steps after FOTA can go here
watchdog_enable(); // Re-enable the watchdog to restore normal operation
}
// Function to regularly feed the watchdog to prevent unwanted resets
void watchdog_feed_periodic(void) {
if (wdt_channel_id >= 0) {
wdt_feed(wdt_dev, wdt_channel_id); // Reset the watchdog timer countdown
}
}