Mistake on this page?
Report an issue in GitHub or email us

Watchdog

Watchdog class hierarchy

You can use the Watchdog interface to set up a hardware watchdog timer that resets the system in the case of system failures or malfunctions.

Note: There is only one instance in the system. Use Watchdog::get_instance() to obtain a reference.

If you fail to refresh the watchdog periodically, it resets the system after a set period of time.

Note: The maximum amount of time you can set as the Watchdog timeout varies depending on the target hardware. You can check the maximum value by calling Watchdog::get_instance().get_max_timeout().

Watchdog class reference

Public Member Functions
bool start ()
 Start the Watchdog timer with the maximum timeout value available for the target. More...
bool start (uint32_t timeout)
 Start the Watchdog timer. More...
bool stop ()
 Stop the Watchdog timer. More...
uint32_t get_timeout () const
 Get the Watchdog timer refresh value. More...
uint32_t get_max_timeout () const
 Get the maximum Watchdog refresh value for this platform. More...
bool is_running () const
 Check if the Watchdog timer is already running. More...
void kick ()
 Refresh the Watchdog timer. More...
Static Public Member Functions
static Watchdogget_instance ()
 Get a reference to the single Watchdog instance in the system. More...

Watchdog example

This example creates a watchdog timer that expires after five seconds and that you can refresh by pushing BUTTON1 on the target board:

/*
 * Copyright (c) 2020 Arm Limited and affiliates.
 * SPDX-License-Identifier: Apache-2.0
 */

#include "mbed.h"

const uint32_t TIMEOUT_MS = 5000;
InterruptIn button(BUTTON1);
volatile int countdown = 9;

void trigger()
{
    Watchdog::get_instance().kick();
    countdown = 9;
}

int main()
{
    printf("\r\nTarget started.\r\n");

    Watchdog &watchdog = Watchdog::get_instance();
    watchdog.start(TIMEOUT_MS);
    button.rise(&trigger);

    uint32_t watchdog_timeout = watchdog.get_timeout();
    printf("Watchdog initialized to %lu ms.\r\n", watchdog_timeout);
    printf("Press BUTTON1 at least once every %lu ms to kick the "
           "watchdog and prevent system reset.\r\n", watchdog_timeout);

    while (1) {
        printf("\r%3i", countdown--);
        fflush(stdout);
        ThisThread::sleep_for(TIMEOUT_MS / 10);
    }
}

Important Information for this Arm website

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies. If you are not happy with the use of these cookies, please review our Cookie Policy to learn how they can be disabled. By disabling cookies, some features of the site will not work.