aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVincent Stakenburg <[email protected]>2022-06-10 16:10:54 +0200
committerVincent Stakenburg <[email protected]>2022-06-28 12:46:17 +0200
commit5cf3fbece4adcf8d1d4475593ab9d6caf1e30f62 (patch)
tree8e7890fc1fc3563b8595957de338e0cf0c1fdfac
parentc7703ba17c5da3d294544c3a4f451bb7bd04ccfb (diff)
initial independent watchdog implementation
-rw-r--r--embassy-stm32/src/lib.rs3
-rw-r--r--embassy-stm32/src/wdg/mod.rs52
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))]
56pub mod usb_otg; 56pub mod usb_otg;
57 57
58#[cfg(iwdg)]
59pub mod wdg;
60
58#[cfg(feature = "subghz")] 61#[cfg(feature = "subghz")]
59pub mod subghz; 62pub 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 @@
1use core::marker::PhantomData;
2
3use embassy::util::Unborrow;
4use embassy_hal_common::unborrow;
5
6use stm32_metapac::iwdg::vals::Key;
7
8pub use stm32_metapac::iwdg::vals::Pr as Prescaler;
9
10pub struct IndependentWatchdog<'d, T: Instance> {
11 wdg: PhantomData<&'d mut T>,
12}
13
14impl<'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
38mod sealed {
39 pub trait Instance {
40 fn regs() -> crate::pac::iwdg::Iwdg;
41 }
42}
43
44pub trait Instance: sealed::Instance {}
45
46impl sealed::Instance for crate::peripherals::IWDG {
47 fn regs() -> crate::pac::iwdg::Iwdg {
48 crate::pac::IWDG
49 }
50}
51
52impl Instance for crate::peripherals::IWDG {}