Remove lifetime, use pac fields

This commit is contained in:
kalkyl 2022-12-24 03:22:51 +01:00
parent eaad0cc1dc
commit e090ab1915
2 changed files with 10 additions and 10 deletions

View File

@ -14,12 +14,12 @@ use crate::pac;
use crate::peripherals::WATCHDOG; use crate::peripherals::WATCHDOG;
/// Watchdog peripheral /// Watchdog peripheral
pub struct Watchdog<'d> { pub struct Watchdog {
phantom: PhantomData<&'d WATCHDOG>, phantom: PhantomData<WATCHDOG>,
load_value: u32, // decremented by 2 per tick (µs) load_value: u32, // decremented by 2 per tick (µs)
} }
impl<'d> Watchdog<'d> { impl Watchdog {
/// Create a new `Watchdog` /// Create a new `Watchdog`
pub fn new(_watchdog: WATCHDOG) -> Self { pub fn new(_watchdog: WATCHDOG) -> Self {
Self { Self {
@ -35,12 +35,12 @@ impl<'d> Watchdog<'d> {
/// * `cycles` - Total number of tick cycles before the next tick is generated. /// * `cycles` - Total number of tick cycles before the next tick is generated.
/// It is expected to be the frequency in MHz of clk_ref. /// It is expected to be the frequency in MHz of clk_ref.
pub fn enable_tick_generation(&mut self, cycles: u8) { pub fn enable_tick_generation(&mut self, cycles: u8) {
const WATCHDOG_TICK_ENABLE_BITS: u32 = 0x200;
unsafe { unsafe {
let watchdog = pac::WATCHDOG; let watchdog = pac::WATCHDOG;
watchdog watchdog.tick().write(|w| {
.tick() w.set_enable(true);
.write_value(pac::watchdog::regs::Tick(WATCHDOG_TICK_ENABLE_BITS | cycles as u32)) w.set_cycles(cycles.into())
});
} }
} }

View File

@ -22,11 +22,11 @@ async fn main(_spawner: Spawner) {
led.set_high(); led.set_high();
Timer::after(Duration::from_secs(2)).await; Timer::after(Duration::from_secs(2)).await;
// Set to watchdog to reset if it's not reloaded within 1.05 seconds, and start it // Set to watchdog to reset if it's not fed within 1.05 seconds, and start it
watchdog.start(Duration::from_millis(1_050)); watchdog.start(Duration::from_millis(1_050));
info!("Started the watchdog timer"); info!("Started the watchdog timer");
// Blink once a second for 5 seconds, refreshing the watchdog timer once a second to avoid a reset // Blink once a second for 5 seconds, feed the watchdog timer once a second to avoid a reset
for _ in 1..=5 { for _ in 1..=5 {
led.set_low(); led.set_low();
Timer::after(Duration::from_millis(500)).await; Timer::after(Duration::from_millis(500)).await;
@ -38,7 +38,7 @@ async fn main(_spawner: Spawner) {
info!("Stopped feeding, device will reset in 1.05 seconds"); info!("Stopped feeding, device will reset in 1.05 seconds");
// Blink 10 times per second, not feeding the watchdog. // Blink 10 times per second, not feeding the watchdog.
// The processor should reset in 1.05 seconds, or 5 blinks time // The processor should reset in 1.05 seconds.
loop { loop {
led.set_low(); led.set_low();
Timer::after(Duration::from_millis(100)).await; Timer::after(Duration::from_millis(100)).await;