Class WDT
Description
A class used for initializing, starting, stopping watchdog timer.
Syntax
class WDT
Members
Public Constructors |
|
---|---|
A public constructor should not be used as this class is intended to be a singleton class. Access member functions using the object instance named WDT. |
Public Methods |
|
---|---|
WDT:: InitWatchdog |
Initializes the watchdog, include time setting, and mode register |
WDT:: StartWatchdog |
Start the watchdog counting |
WDT:: StopWatchdog |
Stop the watchdog counting |
WDT:: RefreshWatchdog |
Refresh the watchdog counting to prevent WDT timeout |
WDT:: InitWatchdogIRQ |
Switch the watchdog timer to interrupt mode and register a watchdog timer timeout interrupt handler |
- WDT:: InitWatchdog
Description
Initializes the watchdog, include time setting, and mode register.
Syntax
void InitWatchdog(uint32_t timeout_ms);
Parameters
timeout_ms
: the watch-dog timer timeout value in millisecond (ms). The default action after watchdog timer timeout is to reset the whole system.
Returns
The function returns nothing.
Example Code
Example: WatchdogTimer
1 /**
2 * This example describes how to use watchdog api.
3 * In this example, watchdog is setup to 5s timeout.
4 * Watchdog won't bark if we refresh it before timeout in smallTask.
5 * The timer is also reloaded after refresh.
6 * Otherwise, while running bigTask, watchdog will restart system in default or call callback function if registered.
7*/
8#include "wdt.h"
9
10#define RUN_CALLBACK_IF_WATCHDOG_BARKS (0)
11WDT wdt;
12
13void setup() {
14 Serial.begin(115200);
15 wdt.InitWatchdog(5000); // setup 5s watchdog
16
17#if RUN_CALLBACK_IF_WATCHDOG_BARKS
18 wdt.InitWatchdogIRQ(my_watchdog_irq_handler, 0);
19#else
20 // system would restart in default when watchdog barks
21#endif
22
23 wdt.StartWatchdog(); // enable watchdog timer
24 Small_Task();
25 Big_Task();
26 while(1);
27}
28
29void loop() {
30 delay(1000);
31}
32
33void Small_Task (void) {
34 Serial.println("......doing small task......");
35 for (int i = 0; i < 50000000; i++) { // dummy task
36 asm(" nop");
37 }
38 Serial.println("Small_Task finished refresh watchdog.");
39 wdt.RefreshWatchdog();
40}
41
42/* If Big_Task unable to reach #10, watchdog barks. */
43void Big_Task (void) {
44 Serial.println("......doing big task, up to 10......");
45 for (int i = 0; i < 10; i++) {
46 Serial.print("doing dummy task #");
47 Serial.println(i, DEC);
48
49 for (int j = 0; j < 50000000; j++) // dummy task
50 asm(" nop");
51 }
52 Serial.println("Big_Task finished refresh watchdog.");
53 wdt.RefreshWatchdog();
54}
55
56void my_watchdog_irq_handler(uint32_t id) {
57 Serial.println("watchdog barks!!!");
58 wdt.StopWatchdog();
59}
Notes and Warnings NA
- WDT:: StartWatchdog
Description
Start the watchdog counting.
Syntax
void StartWatchdog(void);
Parameters
The function requires no input parameter.
Returns
The function returns nothing.
Example Code
Example: WatchdogTimer
You may refer to the code in previous section of WDT::InitWatchdog
.
Notes and Warnings
NA
- WDT:: StopWatchdog
Description
Stop the watchdog counting.
Syntax
void StopWatchdog(void);
Parameters
The function requires no input parameter.
Returns
The function returns nothing.
Example Code
Example: WatchdogTimer
You may refer to the code in previous section of WDT::InitWatchdog
.
Notes and Warnings
NA
- WDT:: RefreshWatchdog
Description
Refresh the watchdog counting to prevent WDT timeout.
Syntax
void RefreshWatchdog(void);
Parameters
The function requires no input parameter.
Returns
The function returns nothing.
Example Code
Example: WatchdogTimer
You may refer to the code in previous section of WDT::InitWatchdog
.
Notes and Warnings
NA
- WDT:: InitWatchdogIRQ
Description
Switch the watchdog timer to interrupt mode and register a watchdog timer timeout interrupt handler. The interrupt handler will be called when the watchdog timer is timeout.
Syntax
void WDT::InitWatchdogIRQ(wdt_irq_handler handler, uint32_t id)
Parameters
handler
: the callback function for WDT timeout interrupt.
id
: the parameter for the callback function
Returns
The function returns nothing.
Example Code
Example: WatchdogTimer
You may refer to the code in previous section of WDT::InitWatchdog
.
Notes and Warnings
NA