mirror of
https://github.com/tinygo-org/tinygo.git
synced 2026-08-14 07:53:40 +00:00
samd51,rp2040,nrf528xx,stm32: implement watchdog
This commit is contained in:
@@ -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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user