ch32_hal/wdg/wwdg.rs
1//! Window watchdog (WWDG).
2//!
3//! A 7-bit down-counter clocked from PCLK1 / 4096 divided by 1, 2, 4 or 8
4//! (RM §8). The counter resets the chip when it falls from 0x40 to 0x3f, and
5//! also when it is refreshed before it has dropped below the window value.
6//!
7//! The API follows embassy-stm32's `wdg` module: the constructor takes the
8//! timeout and the closed-window duration in µs and starts the watchdog, and
9//! [`pet`](WindowWatchdog::pet) refreshes it.
10
11use crate::{pac, peripherals, Peri};
12
13/// The counter starts at 0x40 + the number of ticks in the period.
14const RESET_OFFSET: u8 = 0x3f;
15
16/// Window watchdog (WWDG) driver.
17///
18/// The watchdog is running as soon as [`new`](Self::new) returns; there is no
19/// separate start step and no way to stop it short of a reset.
20pub struct WindowWatchdog<'d, T: WwdgInstance> {
21 _peri: Peri<'d, T>,
22 /// Counter value written on every [`pet`](Self::pet).
23 counter: u8,
24 /// Refresh is only allowed once the live counter is below this value.
25 window: u8,
26}
27
28impl<'d, T: WwdgInstance> WindowWatchdog<'d, T> {
29 /// Start the watchdog with a `timeout_us` period and a `window_us` closed
30 /// window.
31 ///
32 /// A refresh during the closed window resets the chip; pass `0` to allow
33 /// a refresh at any time. Panics if `window_us >= timeout_us` or if the
34 /// timeout does not fit the 7-bit counter at any prescaler (the maximum
35 /// is PCLK1 / 4096 / 8 * 64 ticks).
36 pub fn new(peri: Peri<'d, T>, timeout_us: u32, window_us: u32) -> Self {
37 assert!(window_us < timeout_us, "WWDG window must be shorter than the timeout");
38
39 T::enable_and_reset();
40 let pclk1 = crate::rcc::clocks().pclk1.0;
41 let (prescaler, ticks) = prescaler_and_ticks(pclk1, timeout_us);
42 let counter = RESET_OFFSET + ticks;
43 let closed_ticks = (u64::from(window_us) * u64::from(pclk1) / (u64::from(prescaler) * 4096 * 1_000_000)) as u8;
44 let window = counter - closed_ticks;
45
46 let regs = T::regs();
47 regs.cfgr().write(|w| {
48 w.set_w(window);
49 w.set_wdgtb(prescaler.trailing_zeros() as u8);
50 });
51 // EWIF is raised even with the early-wakeup interrupt disabled.
52 regs.statr().write(|w| w.set_ewif(false));
53 regs.ctlr().write(|w| {
54 w.set_t(counter);
55 w.set_wdga(true);
56 });
57
58 Self {
59 _peri: peri,
60 counter,
61 window,
62 }
63 }
64
65 /// Reload the counter.
66 ///
67 /// Refreshing while the live counter is at or above [`window`](Self::window)
68 /// resets the chip, so time the call for the open part of the period.
69 pub fn pet(&mut self) {
70 // Write the complete control value: a read-modify-write could write
71 // back a stale, already-decremented counter value.
72 T::regs().ctlr().write(|w| {
73 w.set_t(self.counter);
74 w.set_wdga(true);
75 });
76 }
77
78 /// Read the live seven-bit down-counter.
79 pub fn counter(&self) -> u8 {
80 T::regs().ctlr().read().t()
81 }
82
83 /// Window value the live counter must fall below before a refresh.
84 pub fn window(&self) -> u8 {
85 self.window
86 }
87
88 /// Enable the early-wakeup interrupt at counter value 0x40.
89 ///
90 /// The caller must install and enable the WWDG interrupt handler; this
91 /// bit is only cleared by a peripheral reset.
92 pub fn enable_early_wakeup_interrupt(&mut self) {
93 T::regs().cfgr().modify(|w| w.set_ewi(true));
94 }
95
96 /// Whether the counter has reached 0x40 since EWIF was last cleared.
97 /// The flag is raised even with the early-wakeup interrupt disabled.
98 pub fn early_wakeup_pending(&self) -> bool {
99 T::regs().statr().read().ewif()
100 }
101
102 /// Clear the early-wakeup flag (write zero to clear).
103 pub fn clear_early_wakeup(&mut self) {
104 T::regs().statr().write(|w| w.set_ewif(false));
105 }
106}
107
108/// Pick the smallest prescaler that keeps the period within 64 ticks.
109fn prescaler_and_ticks(pclk1: u32, timeout_us: u32) -> (u32, u8) {
110 for prescaler_power in 0u8..4u8 {
111 let prescaler = 1u32 << prescaler_power;
112 let ticks = (u64::from(timeout_us) * u64::from(pclk1)).div_ceil(u64::from(prescaler) * 4096 * 1_000_000);
113 if ticks <= 64 {
114 return (prescaler, ticks.max(1) as u8);
115 }
116 }
117
118 panic!("window watchdog timeout of {} us is out of range", timeout_us);
119}
120
121trait SealedInstance {
122 fn regs() -> pac::wwdg::Wwdg;
123}
124
125/// WWDG instance trait.
126#[allow(private_bounds)]
127pub trait WwdgInstance:
128 SealedInstance + crate::peripheral::RccPeripheral + embassy_hal_internal::PeripheralType + 'static
129{
130}
131
132foreach_peripheral!(
133 (wwdg, $inst:ident) => {
134 impl SealedInstance for peripherals::$inst {
135 fn regs() -> pac::wwdg::Wwdg {
136 pac::$inst
137 }
138 }
139
140 impl WwdgInstance for peripherals::$inst {}
141 };
142);