Skip to content

Commit e79fc7f

Browse files
machine/sam: create new file for common watchdog implementation
1 parent a6f0ee3 commit e79fc7f

File tree

3 files changed

+51
-74
lines changed

3 files changed

+51
-74
lines changed

src/machine/machine_atsam_watchdog.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//go:build sam
2+
3+
package machine
4+
5+
import "device/sam"
6+
7+
// Watchdog provides access to the hardware watchdog available on SAM.
8+
var Watchdog = &watchdogImpl{}
9+
10+
const (
11+
// WatchdogMaxTimeout in milliseconds (16s)
12+
WatchdogMaxTimeout = (16384 * 1000) / 1024
13+
)
14+
15+
type watchdogImpl struct{}
16+
17+
func (wd *watchdogImpl) configureBase(config WatchdogConfig) error {
18+
// 1.024kHz clock
19+
cycles := int((int64(config.TimeoutMillis) * 1024) / 1000)
20+
21+
// period is expressed as a power-of-two, starting at 8 / 1024ths of a second
22+
period := uint8(0)
23+
cfgCycles := 8
24+
for cfgCycles < cycles {
25+
period++
26+
cfgCycles <<= 1
27+
28+
if period >= 0xB {
29+
break
30+
}
31+
}
32+
33+
sam.WDT.CONFIG.Set(period << sam.WDT_CONFIG_PER_Pos)
34+
return nil
35+
}
36+
37+
// Update the watchdog, indicating that `source` is healthy.
38+
func (wd *watchdogImpl) Update() {
39+
sam.WDT.CLEAR.Set(sam.WDT_CLEAR_CLEAR_KEY)
40+
}

src/machine/machine_atsamd21.go

Lines changed: 10 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -2028,23 +2028,7 @@ func checkFlashError() error {
20282028
return nil
20292029
}
20302030

2031-
// Watchdog provides access to the hardware watchdog available
2032-
// in the SAMD21.
2033-
var Watchdog = &watchdogImpl{}
2034-
2035-
const (
2036-
// WatchdogMaxTimeout in milliseconds (16s)
2037-
WatchdogMaxTimeout = (16384 * 1000) / 1024
2038-
)
2039-
2040-
type watchdogImpl struct{}
2041-
2042-
// Configure the watchdog.
2043-
//
2044-
// This method should not be called after the watchdog is started and on
2045-
// some platforms attempting to reconfigure after starting the watchdog
2046-
// is explicitly forbidden / will not work.
2047-
func (wd *watchdogImpl) Configure(config WatchdogConfig) error {
2031+
func (wd *watchdogImpl) configureClock() {
20482032
// Use OSCULP32K as source for Generic Clock Generator 8, divided by 32 to get 1.024kHz
20492033
sam.GCLK.GENDIV.Set(sam.GCLK_CLKCTRL_GEN_GCLK8 | (32 << sam.GCLK_GENDIV_DIV_Pos))
20502034
sam.GCLK.GENCTRL.Set(sam.GCLK_CLKCTRL_GEN_GCLK8 | (sam.GCLK_GENCTRL_SRC_OSCULP32K << sam.GCLK_GENCTRL_SRC_Pos) | sam.GCLK_GENCTRL_GENEN)
@@ -2055,34 +2039,20 @@ func (wd *watchdogImpl) Configure(config WatchdogConfig) error {
20552039

20562040
// Power on the watchdog peripheral
20572041
sam.PM.APBAMASK.SetBits(sam.PM_APBAMASK_WDT_)
2042+
}
20582043

2059-
// 1.024kHz clock
2060-
cycles := int((int64(config.TimeoutMillis) * 1024) / 1000)
2061-
2062-
// period is expressed as a power-of-two, starting at 8 / 1024ths of a second
2063-
period := uint8(0)
2064-
cfgCycles := 8
2065-
for cfgCycles < cycles {
2066-
period++
2067-
cfgCycles <<= 1
2068-
2069-
if period >= 0xB {
2070-
break
2071-
}
2072-
}
2073-
2074-
sam.WDT.CONFIG.Set(period << sam.WDT_CONFIG_PER_Pos)
2075-
2076-
return nil
2044+
// Configure the watchdog.
2045+
//
2046+
// This method should not be called after the watchdog is started and on
2047+
// some platforms attempting to reconfigure after starting the watchdog
2048+
// is explicitly forbidden / will not work.
2049+
func (wd *watchdogImpl) Configure(config WatchdogConfig) error {
2050+
wd.configureClock()
2051+
return wd.configureBase(config)
20772052
}
20782053

20792054
// Starts the watchdog.
20802055
func (wd *watchdogImpl) Start() error {
20812056
sam.WDT.CTRL.SetBits(sam.WDT_CTRL_ENABLE)
20822057
return nil
20832058
}
2084-
2085-
// Update the watchdog, indicating that `source` is healthy.
2086-
func (wd *watchdogImpl) Update() {
2087-
sam.WDT.CLEAR.Set(sam.WDT_CLEAR_CLEAR_KEY)
2088-
}

src/machine/machine_atsamd51.go

Lines changed: 1 addition & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2312,50 +2312,17 @@ func checkFlashError() error {
23122312
return nil
23132313
}
23142314

2315-
// Watchdog provides access to the hardware watchdog available
2316-
// in the SAMD51.
2317-
var Watchdog = &watchdogImpl{}
2318-
2319-
const (
2320-
// WatchdogMaxTimeout in milliseconds (16s)
2321-
WatchdogMaxTimeout = (16384 * 1000) / 1024 // CYC16384/1024kHz
2322-
)
2323-
2324-
type watchdogImpl struct{}
2325-
23262315
// Configure the watchdog.
23272316
//
23282317
// This method should not be called after the watchdog is started and on
23292318
// some platforms attempting to reconfigure after starting the watchdog
23302319
// is explicitly forbidden / will not work.
23312320
func (wd *watchdogImpl) Configure(config WatchdogConfig) error {
2332-
// 1.024kHz clock
2333-
cycles := int((int64(config.TimeoutMillis) * 1024) / 1000)
2334-
2335-
// period is expressed as a power-of-two, starting at 8 / 1024ths of a second
2336-
period := uint8(0)
2337-
cfgCycles := 8
2338-
for cfgCycles < cycles {
2339-
period++
2340-
cfgCycles <<= 1
2341-
2342-
if period >= 0xB {
2343-
break
2344-
}
2345-
}
2346-
2347-
sam.WDT.CONFIG.Set(period << sam.WDT_CONFIG_PER_Pos)
2348-
2349-
return nil
2321+
return wd.configureBase(config)
23502322
}
23512323

23522324
// Starts the watchdog.
23532325
func (wd *watchdogImpl) Start() error {
23542326
sam.WDT.CTRLA.SetBits(sam.WDT_CTRLA_ENABLE)
23552327
return nil
23562328
}
2357-
2358-
// Update the watchdog, indicating that `source` is healthy.
2359-
func (wd *watchdogImpl) Update() {
2360-
sam.WDT.CLEAR.Set(sam.WDT_CLEAR_CLEAR_KEY)
2361-
}

0 commit comments

Comments
 (0)