samd51,rp2040,nrf528xx,stm32: implement watchdog

This commit is contained in:
Kenneth Bell
2023-08-06 22:15:47 +01:00
committed by Ron Evans
parent 756cdf44ed
commit f4375d0452
8 changed files with 291 additions and 12 deletions
+49
View File
@@ -2299,3 +2299,52 @@ func checkFlashError() error {
return nil
}
// Watchdog provides access to the hardware watchdog available
// in the SAMD51.
var Watchdog = &watchdogImpl{}
const (
// WatchdogMaxTimeout in milliseconds (16s)
WatchdogMaxTimeout = (16384 * 1000) / 1024 // CYC16384/1024kHz
)
type watchdogImpl struct{}
// 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.
func (wd *watchdogImpl) Configure(config WatchdogConfig) error {
// 1.024kHz clock
cycles := int((int64(config.TimeoutMillis) * 1024) / 1000)
// period is expressed as a power-of-two, starting at 8 / 1024ths of a second
period := uint8(0)
cfgCycles := 8
for cfgCycles < cycles {
period++
cfgCycles <<= 1
if period >= 0xB {
break
}
}
sam.WDT.CONFIG.Set(period << sam.WDT_CONFIG_PER_Pos)
return nil
}
// Starts the watchdog.
func (wd *watchdogImpl) Start() error {
sam.WDT.CTRLA.SetBits(sam.WDT_CTRLA_ENABLE)
return nil
}
// Update the watchdog, indicating that `source` is healthy.
func (wd *watchdogImpl) Update() {
// 0xA5 = magic value (see datasheet)
sam.WDT.CLEAR.Set(0xA5)
}