From 5cf3fbece4adcf8d1d4475593ab9d6caf1e30f62 Mon Sep 17 00:00:00 2001 From: Vincent Stakenburg Date: Fri, 10 Jun 2022 16:10:54 +0200 Subject: [PATCH 1/4] 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 717ebb95..7be0c77e 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 00000000..4353c403 --- /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 {} From 74bbf5aa02a10efce498054ad436a6140540f917 Mon Sep 17 00:00:00 2001 From: Vincent Stakenburg Date: Fri, 24 Jun 2022 14:19:29 +0200 Subject: [PATCH 2/4] address review --- embassy-stm32/src/wdg/mod.rs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/embassy-stm32/src/wdg/mod.rs b/embassy-stm32/src/wdg/mod.rs index 4353c403..8d2663d9 100644 --- a/embassy-stm32/src/wdg/mod.rs +++ b/embassy-stm32/src/wdg/mod.rs @@ -2,9 +2,7 @@ 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> { @@ -12,7 +10,7 @@ pub struct IndependentWatchdog<'d, T: Instance> { } impl<'d, T: Instance> IndependentWatchdog<'d, T> { - pub fn new(_instance: impl Unborrow, presc: Prescaler) -> Self { + pub fn new(_instance: impl Unborrow + 'd, presc: Prescaler) -> Self { unborrow!(_instance); let wdg = T::regs(); @@ -30,7 +28,7 @@ impl<'d, T: Instance> IndependentWatchdog<'d, T> { T::regs().kr().write(|w| w.set_key(Key::START)); } - pub unsafe fn feed(&mut self) { + pub unsafe fn pet(&mut self) { T::regs().kr().write(|w| w.set_key(Key::RESET)); } } From f2ad9c2d9d892d4656f05e04c54cb24922d71d97 Mon Sep 17 00:00:00 2001 From: Vincent Stakenburg Date: Tue, 28 Jun 2022 12:51:08 +0200 Subject: [PATCH 3/4] rebase and fix unborrow --- embassy-stm32/src/wdg/mod.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/embassy-stm32/src/wdg/mod.rs b/embassy-stm32/src/wdg/mod.rs index 8d2663d9..ce3af148 100644 --- a/embassy-stm32/src/wdg/mod.rs +++ b/embassy-stm32/src/wdg/mod.rs @@ -1,7 +1,6 @@ use core::marker::PhantomData; -use embassy::util::Unborrow; -use embassy_hal_common::unborrow; +use embassy_hal_common::{unborrow, Unborrow}; use stm32_metapac::iwdg::vals::Key; pub use stm32_metapac::iwdg::vals::Pr as Prescaler; From d1d07cd9e3b86b2d387b26b652e3caebe114e36b Mon Sep 17 00:00:00 2001 From: Vincent Stakenburg Date: Tue, 28 Jun 2022 13:15:23 +0200 Subject: [PATCH 4/4] fix case when chip has multiple iwdg --- embassy-stm32/src/wdg/mod.rs | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/embassy-stm32/src/wdg/mod.rs b/embassy-stm32/src/wdg/mod.rs index ce3af148..da25692a 100644 --- a/embassy-stm32/src/wdg/mod.rs +++ b/embassy-stm32/src/wdg/mod.rs @@ -40,10 +40,14 @@ mod sealed { pub trait Instance: sealed::Instance {} -impl sealed::Instance for crate::peripherals::IWDG { - fn regs() -> crate::pac::iwdg::Iwdg { - crate::pac::IWDG - } -} +foreach_peripheral!( + (iwdg, $inst:ident) => { + impl sealed::Instance for crate::peripherals::$inst { + fn regs() -> crate::pac::iwdg::Iwdg { + crate::pac::$inst + } + } -impl Instance for crate::peripherals::IWDG {} + impl Instance for crate::peripherals::$inst {} + }; +);