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 Watchdog & | get_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);
}
}