Skip to content

Commit a79ea53

Browse files
kenbelldeadprogram
authored andcommitted
Document watchdog peripheral
1 parent ff73846 commit a79ea53

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
---
2+
title: "Watchdog"
3+
weight: 2
4+
description: |
5+
How to interact with internal watchdog devices.
6+
---
7+
8+
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.

content/docs/reference/machine.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,34 @@ func (uart *UART) WriteByte(c byte) error
320320

321321
Write a single byte to the UART output.
322322

323+
## Watchdog
324+
325+
```go
326+
type WatchdogConfig struct {
327+
TimeoutMillis uint32
328+
}
329+
```
330+
331+
The `WatchdogConfig` struct contains configuration for the watchdog peripheral.
332+
333+
* `TimeoutMillis` is the requested maximum delay between calls to `Update`.
334+
335+
```go
336+
func (wd *Watchdog) Configure(config WatchdogConfig) error
337+
```
338+
Configures the watchdog.
339+
340+
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.
323351

324352
## Other
325353

0 commit comments

Comments
 (0)