You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
For API documentation, see the [machine API](../../../reference/machine#watchdog)
9
+
10
+
Many microcontrollers include some form of watchdog peripheral. Watchdog peripherals are used to restart the microprocessor in the event of a fault condition (typically a software bug). Watchdogs are typically based around a fixed-frequency hardware timer that counts down from a configured value to zero. On reaching zero a reset is triggered. To prevent a reset the software must periodically update the watchdog, causing the counter to return to it's initial value.
11
+
12
+
There are multiple variants of watchdog functionality. Some do not permit the watchdog to be disabled once enabled, others provide for multiple 'sources' enabling applications to have hardware protection over multiple subsystems. The maximum time for the watchdog countdown can vary from hours to seconds on different hardware.
13
+
14
+
Tinygo provides an abstraction supported by most watchdog hardware:
15
+
16
+
1. No ability to disable a watchdog after it is enabled.
17
+
18
+
2. Where supported, the watchdog is automatically disabled if a debugger is attached.
19
+
20
+
3. The watchdog is not disabled during sleep (`time.Sleep`) or blocking I/O, so critical loops must take care to not sleep for too long or block on I/O indefinitely.
21
+
22
+
4. A single 'source' is supported on all platforms with Watchdog support.
23
+
24
+
5. The CPU is not guaranteed to reset exactly at the requested time. Some hardware does not allow precise timing - the next largest supported timeout is used instead.
25
+
26
+
A typical use of Watchdog support in a simple app would look something like this:
27
+
28
+
```go
29
+
void main() {
30
+
31
+
// ...
32
+
33
+
config:= machine.WatchdogConfig{
34
+
TimeoutMillis: 1000,
35
+
}
36
+
machine.Watchdog.Configure(config)
37
+
38
+
machine.Watchdog.Start()
39
+
40
+
for {
41
+
machine.Watchdog.Update()
42
+
43
+
//
44
+
// Main logic - poll GPIO, write to display, etc
45
+
//
46
+
}
47
+
}
48
+
```
49
+
50
+
In this example, the watchdog is configured for updates at least every 1s. One second is a safe default value supported on all hardware. Consult `machine.WatchdogMaxTimeout` for the hardware specific maximum value.
This method should not be called after the watchdog is started and on some platforms attempting to reconfigure after starting the watchdog is explicitly forbidden / will not work.
341
+
342
+
```go
343
+
func (wd *Watchdog) Start() error
344
+
```
345
+
Starts the watchdog. After calling this method, `Update` must be called periodically.
346
+
347
+
```go
348
+
func (wd *Watchdog) Update()
349
+
```
350
+
Updates the watchdog, indicating that the app is healthy. This method must be called periodically to prevent the CPU from resetting.
0 commit comments