diff options
| author | Vincent Stakenburg <[email protected]> | 2022-06-10 16:10:54 +0200 |
|---|---|---|
| committer | Vincent Stakenburg <[email protected]> | 2022-06-28 12:46:17 +0200 |
| commit | 5cf3fbece4adcf8d1d4475593ab9d6caf1e30f62 (patch) | |
| tree | 8e7890fc1fc3563b8595957de338e0cf0c1fdfac | |
| parent | c7703ba17c5da3d294544c3a4f451bb7bd04ccfb (diff) | |
initial independent watchdog implementation
| -rw-r--r-- | embassy-stm32/src/lib.rs | 3 | ||||
| -rw-r--r-- | embassy-stm32/src/wdg/mod.rs | 52 |
2 files changed, 55 insertions, 0 deletions
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; | |||
| 55 | #[cfg(any(otgfs, otghs))] | 55 | #[cfg(any(otgfs, otghs))] |
| 56 | pub mod usb_otg; | 56 | pub mod usb_otg; |
| 57 | 57 | ||
| 58 | #[cfg(iwdg)] | ||
| 59 | pub mod wdg; | ||
| 60 | |||
| 58 | #[cfg(feature = "subghz")] | 61 | #[cfg(feature = "subghz")] |
| 59 | pub mod subghz; | 62 | pub mod subghz; |
| 60 | 63 | ||
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 @@ | |||
| 1 | use core::marker::PhantomData; | ||
| 2 | |||
| 3 | use embassy::util::Unborrow; | ||
| 4 | use embassy_hal_common::unborrow; | ||
| 5 | |||
| 6 | use stm32_metapac::iwdg::vals::Key; | ||
| 7 | |||
| 8 | pub use stm32_metapac::iwdg::vals::Pr as Prescaler; | ||
| 9 | |||
| 10 | pub struct IndependentWatchdog<'d, T: Instance> { | ||
| 11 | wdg: PhantomData<&'d mut T>, | ||
| 12 | } | ||
| 13 | |||
| 14 | impl<'d, T: Instance> IndependentWatchdog<'d, T> { | ||
| 15 | pub fn new(_instance: impl Unborrow<Target = T>, presc: Prescaler) -> Self { | ||
| 16 | unborrow!(_instance); | ||
| 17 | |||
| 18 | let wdg = T::regs(); | ||
| 19 | unsafe { | ||
| 20 | wdg.kr().write(|w| w.set_key(Key::ENABLE)); | ||
| 21 | wdg.pr().write(|w| w.set_pr(presc)); | ||
| 22 | } | ||
| 23 | |||
| 24 | IndependentWatchdog { | ||
| 25 | wdg: PhantomData::default(), | ||
| 26 | } | ||
| 27 | } | ||
| 28 | |||
| 29 | pub unsafe fn unleash(&mut self) { | ||
| 30 | T::regs().kr().write(|w| w.set_key(Key::START)); | ||
| 31 | } | ||
| 32 | |||
| 33 | pub unsafe fn feed(&mut self) { | ||
| 34 | T::regs().kr().write(|w| w.set_key(Key::RESET)); | ||
| 35 | } | ||
| 36 | } | ||
| 37 | |||
| 38 | mod sealed { | ||
| 39 | pub trait Instance { | ||
| 40 | fn regs() -> crate::pac::iwdg::Iwdg; | ||
| 41 | } | ||
| 42 | } | ||
| 43 | |||
| 44 | pub trait Instance: sealed::Instance {} | ||
| 45 | |||
| 46 | impl sealed::Instance for crate::peripherals::IWDG { | ||
| 47 | fn regs() -> crate::pac::iwdg::Iwdg { | ||
| 48 | crate::pac::IWDG | ||
| 49 | } | ||
| 50 | } | ||
| 51 | |||
| 52 | impl Instance for crate::peripherals::IWDG {} | ||
