Skip to content

Commit 89a488b

Browse files
author
James Munns
committed
Rework the type system shenanigans
1 parent e82f0b5 commit 89a488b

File tree

2 files changed

+268
-122
lines changed

2 files changed

+268
-122
lines changed

examples/wdt-demo/src/main.rs

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use {
2727
hal::{
2828
gpio::{Input, Level, Output, Pin, PullUp, PushPull},
2929
timer::Timer,
30-
wdt::{HdlN, NumHandles, Watchdog, WatchdogHandle},
30+
wdt::{count, handles::HdlN, Parts, Watchdog, WatchdogHandle},
3131
},
3232
nrf52840_hal as hal,
3333
rtt_target::{rprintln, rtt_init_print},
@@ -70,23 +70,36 @@ const APP: () = {
7070
let timer = Timer::new(ctx.device.TIMER0);
7171

7272
// Create a new watchdog instance
73-
let mut watchdog = Watchdog::new(ctx.device.WDT);
74-
73+
//
7574
// In case the watchdog is already running, just spin and let it expire, since
7675
// we can't configure it anyway. This usually happens when we first program
7776
// the device and the watchdog was previously active
78-
if watchdog.is_running() {
79-
rprintln!("Oops, watchdog already active. Let's just reset.");
80-
loop {
81-
continue;
77+
let (hdl0, hdl1, hdl2, hdl3) = match Watchdog::try_new(ctx.device.WDT) {
78+
Ok(mut watchdog) => {
79+
// Set the watchdog to timeout after 5 seconds (in 32.768kHz ticks)
80+
watchdog.set_lfosc_ticks(5 * 32768);
81+
82+
// Activate the watchdog with four handles
83+
let Parts {
84+
watchdog: _watchdog,
85+
handles,
86+
} = watchdog.activate::<count::Four>();
87+
88+
handles
8289
}
83-
}
84-
85-
// Set the watchdog to timeout after 5 seconds (in 32.768kHz ticks)
86-
watchdog.set_lfosc_ticks(5 * 32768);
87-
88-
// Activate the watchdog with four handles
89-
let wdt_parts = watchdog.activate(NumHandles::Four);
90+
Err(wdt) => match Watchdog::try_recover::<count::Four>(wdt) {
91+
Ok(Parts { handles, .. }) => {
92+
rprintln!("Oops, watchdog already active, but recovering!");
93+
handles
94+
}
95+
Err(_wdt) => {
96+
rprintln!("Oops, watchdog already active, resetting!");
97+
loop {
98+
continue;
99+
}
100+
}
101+
},
102+
};
90103

91104
// Enable the monotonic timer (CYCCNT)
92105
ctx.core.DCB.enable_trace();
@@ -113,10 +126,10 @@ const APP: () = {
113126
led2,
114127
led3,
115128
led4,
116-
hdl0: wdt_parts.hdl0.degrade(),
117-
hdl1: wdt_parts.hdl1.unwrap().degrade(),
118-
hdl2: wdt_parts.hdl2.unwrap().degrade(),
119-
hdl3: wdt_parts.hdl3.unwrap().degrade(),
129+
hdl0: hdl0.degrade(),
130+
hdl1: hdl1.degrade(),
131+
hdl2: hdl2.degrade(),
132+
hdl3: hdl3.degrade(),
120133
timer,
121134
}
122135
}

0 commit comments

Comments
 (0)