From 5cf3fbece4adcf8d1d4475593ab9d6caf1e30f62 Mon Sep 17 00:00:00 2001 From: Vincent Stakenburg Date: Fri, 10 Jun 2022 16:10:54 +0200 Subject: [PATCH] initial independent watchdog implementation --- embassy-stm32/src/lib.rs | 3 +++ embassy-stm32/src/wdg/mod.rs | 52 ++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 embassy-stm32/src/wdg/mod.rs diff --git a/embassy-stm32/src/lib.rs b/embassy-stm32/src/lib.rs index 717ebb95e..7be0c77ea 100644 --- a/embassy-stm32/src/lib.rs +++ b/embassy-stm32/src/lib.rs @@ -55,6 +55,9 @@ pub mod usb; #[cfg(any(otgfs, otghs))] pub mod usb_otg; +#[cfg(iwdg)] +pub mod wdg; + #[cfg(feature = "subghz")] pub mod subghz; diff --git a/embassy-stm32/src/wdg/mod.rs b/embassy-stm32/src/wdg/mod.rs new file mode 100644 index 000000000..4353c403b --- /dev/null +++ b/embassy-stm32/src/wdg/mod.rs @@ -0,0 +1,52 @@ +use core::marker::PhantomData; + +use embassy::util::Unborrow; +use embassy_hal_common::unborrow; + +use stm32_metapac::iwdg::vals::Key; + +pub use stm32_metapac::iwdg::vals::Pr as Prescaler; + +pub struct IndependentWatchdog<'d, T: Instance> { + wdg: PhantomData<&'d mut T>, +} + +impl<'d, T: Instance> IndependentWatchdog<'d, T> { + pub fn new(_instance: impl Unborrow, presc: Prescaler) -> Self { + unborrow!(_instance); + + let wdg = T::regs(); + unsafe { + wdg.kr().write(|w| w.set_key(Key::ENABLE)); + wdg.pr().write(|w| w.set_pr(presc)); + } + + IndependentWatchdog { + wdg: PhantomData::default(), + } + } + + pub unsafe fn unleash(&mut self) { + T::regs().kr().write(|w| w.set_key(Key::START)); + } + + pub unsafe fn feed(&mut self) { + T::regs().kr().write(|w| w.set_key(Key::RESET)); + } +} + +mod sealed { + pub trait Instance { + fn regs() -> crate::pac::iwdg::Iwdg; + } +} + +pub trait Instance: sealed::Instance {} + +impl sealed::Instance for crate::peripherals::IWDG { + fn regs() -> crate::pac::iwdg::Iwdg { + crate::pac::IWDG + } +} + +impl Instance for crate::peripherals::IWDG {}