<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://devzone.nordicsemi.com/cfs-file/__key/system/syndication/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>when performing FOTA, should we disable the watchdog timer?</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/116258/when-performing-fota-should-we-disable-the-watchdog-timer</link><description>Hi, 
 I used sdk 2.6.1, 
 when performing FOTA, should we disable the watchdog timer? If so, how can we disable it?</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Mon, 11 Nov 2024 07:58:45 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/116258/when-performing-fota-should-we-disable-the-watchdog-timer" /><item><title>RE: when performing FOTA, should we disable the watchdog timer?</title><link>https://devzone.nordicsemi.com/thread/509804?ContentTypeID=1</link><pubDate>Mon, 11 Nov 2024 07:58:45 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:4efba430-8192-458f-8e4a-d8e4e551c68d</guid><dc:creator>Susheel Nuguru</dc:creator><description>&lt;p&gt;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.&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;#include &amp;lt;zephyr.h&amp;gt;
#include &amp;lt;drivers/watchdog.h&amp;gt;

#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(&amp;quot;Error: Cannot find watchdog device\n&amp;quot;);
        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, &amp;amp;wdt_config);
    if (wdt_channel_id &amp;lt; 0) {
        printk(&amp;quot;Error: Failed to install watchdog timeout\n&amp;quot;);
        return;
    }

    // Start the watchdog timer
    int err = wdt_setup(wdt_dev, 0);
    if (err) {
        printk(&amp;quot;Error: Failed to set up watchdog\n&amp;quot;);
        return;
    }

    printk(&amp;quot;Watchdog initialized with %d ms timeout\n&amp;quot;, WDT_TIMEOUT_MS);
}

// Function to enable the watchdog, which starts the countdown
void watchdog_enable(void) {
    if (wdt_channel_id &amp;gt;= 0) {
        wdt_feed(wdt_dev, wdt_channel_id);  // Feed the watchdog to start it
        printk(&amp;quot;Watchdog enabled\n&amp;quot;);
    } else {
        printk(&amp;quot;Error: Watchdog channel not initialized\n&amp;quot;);
    }
}

// Function to disable the watchdog, typically before long operations like FOTA
void watchdog_disable(void) {
    if (wdt_channel_id &amp;gt;= 0) {
        int err = wdt_disable(wdt_dev);  // Disables the watchdog, if supported by your SDK
        if (err == 0) {
            printk(&amp;quot;Watchdog disabled\n&amp;quot;);
        } else {
            printk(&amp;quot;Error: Unable to disable watchdog\n&amp;quot;);
        }
    } else {
        printk(&amp;quot;Error: Watchdog channel not initialized\n&amp;quot;);
    }
}

// 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 &amp;gt;= 0) {
        wdt_feed(wdt_dev, wdt_channel_id);  // Reset the watchdog timer countdown
    }
}
&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>