-
Notifications
You must be signed in to change notification settings - Fork 909
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
samd51,rp2040,nrf528xx: implement watchdog
- Loading branch information
Showing
7 changed files
with
347 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"machine" | ||
"time" | ||
) | ||
|
||
const ( | ||
// CONFIG: change to true to demonstrate a second source | ||
// that needs to keep the CPU from resetting | ||
demoExtraSource = true | ||
) | ||
|
||
const ( | ||
watchdogSourceMain = 0 | ||
watchdogSourceImportantRoutine = 1 | ||
) | ||
|
||
func main() { | ||
//sleep for 2 secs for console | ||
time.Sleep(2 * time.Second) | ||
|
||
config := machine.WatchdogConfig{ | ||
TimeoutMillis: 1000, | ||
ExtraSources: 0, | ||
} | ||
|
||
if demoExtraSource { | ||
config.ExtraSources = 1 | ||
println("demo extra source: enabled") | ||
} else { | ||
println("demo extra source: disabled") | ||
} | ||
|
||
machine.Watchdog.Configure(config) | ||
|
||
// From this point the watchdog is running and Update must be | ||
// called periodically, per the config | ||
machine.Watchdog.Start() | ||
|
||
if demoExtraSource { | ||
go importantRoutine() | ||
} | ||
|
||
// This loop should complete because watchdog update is called | ||
// every 100ms, unless the 'importantRoutine' dies. | ||
start := time.Now() | ||
println("asserting source 0 for 3 secs") | ||
for i := 0; i < 30; i++ { | ||
time.Sleep(100 * time.Millisecond) | ||
machine.Watchdog.Update(watchdogSourceMain) | ||
fmt.Printf("alive @ %v\n", time.Now().Sub(start)) | ||
} | ||
|
||
// This loop should cause a watchdog reset after 1s since | ||
// there is no update call. | ||
start = time.Now() | ||
println("entering tight loop") | ||
for { | ||
time.Sleep(100 * time.Millisecond) | ||
fmt.Printf("alive @ %v\n", time.Now().Sub(start)) | ||
} | ||
} | ||
|
||
// This is an important goroutine that must keep running - if it fails, | ||
// the watchdog will force a reset. | ||
// | ||
// This example dies after 1 sec, so the CPU should be reset at 2 secs | ||
// into the first loop of main. | ||
func importantRoutine() { | ||
for i := 0; i < 10; i++ { | ||
time.Sleep(100 * time.Millisecond) | ||
machine.Watchdog.Update(watchdogSourceImportantRoutine) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
//go:build nrf52840 || nrf52833 || rp2040 || atsamd51 || atsame5x | ||
|
||
package machine | ||
|
||
// WatchdogConfig holds configuration for the watchdog timer. | ||
type WatchdogConfig struct { | ||
// The timeout (in milliseconds) before the watchdog fires. | ||
// | ||
// If the requested timeout exceeds `MaxTimeout` it will be rounded | ||
// down. | ||
TimeoutMillis uint32 | ||
|
||
// ExtraSources enables the the watchdog to only refresh when multiple | ||
// independant sources request it. | ||
// | ||
// Use this if there are multiple critical loops that must all be | ||
// functioning correctly. If any source does not call Update within | ||
// the watchdog timeout, the CPU will reset. | ||
// | ||
// Source 0 is implied, so `Update(0)`` must always be called periodically | ||
// once the watchdog is starting, setting `ExtraSources`` to `1` means that | ||
// `Update(0)` and `Update(1)`` must both be called periodically to avoid a | ||
// reset. | ||
ExtraSources int | ||
} | ||
|
||
// watchdog must be implemented by any platform supporting watchdog functionality | ||
type watchdog interface { | ||
// Configure the watchdog. | ||
// | ||
// 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. | ||
Configure(config WatchdogConfig) error | ||
|
||
// Starts the watchdog. | ||
Start() error | ||
|
||
// Update the watchdog, indicating that `source` is healthy. | ||
Update(source int) | ||
} | ||
|
||
// Ensure required public symbols var exists and meets interface spec | ||
var ( | ||
_ = watchdog(Watchdog) | ||
) | ||
|
||
// Ensure required public constants exist | ||
const ( | ||
_ = WatchdogMaxSources | ||
_ = WatchdogMaxTimeout | ||
) |