ch32_hal/wdg/iwdg.rs
1//! Independent watchdog (IWDG).
2//!
3//! A 12-bit down-counter clocked from LSI (RM §7). LSI keeps running in stop
4//! and standby, so the counter reaches zero and resets the chip even when the
5//! core is stalled. Once [`IndependentWatchdog::unleash`] has been called the
6//! watchdog cannot be stopped short of a reset.
7//!
8//! The API follows embassy-stm32's `wdg` module: the constructor takes a µs
9//! timeout, [`unleash`](IndependentWatchdog::unleash) starts the watchdog and
10//! [`pet`](IndependentWatchdog::pet) reloads it.
11
12use crate::{pac, peripherals, Peri, PeripheralType};
13
14/// The counter is 12 bits wide, so the reload value tops out at 0x0fff.
15const MAX_RELOAD: u16 = 0x0fff;
16
17/// IWDG counter clock before the prescaler.
18///
19/// The CH32X0 family has no LSI: its IWDG runs on HSI / 1024 = 46.875 kHz (CH32X035 datasheet
20/// V2.2, §1.3 clock tree and the IWDG paragraph). Elsewhere it is the LSI.
21#[cfg(ch32x0)]
22const CLOCK_HZ: u32 = 48_000_000 / 1024;
23#[cfg(not(ch32x0))]
24const CLOCK_HZ: u32 = crate::rcc::LSI_FREQ.0;
25
26/// Independent watchdog (IWDG) driver.
27///
28/// [`new`](Self::new) configures the timeout but leaves the watchdog stopped,
29/// so construction alone cannot reset the chip. Start it with
30/// [`unleash`](Self::unleash) once the system can meet the deadline.
31pub struct IndependentWatchdog<'d, T: Instance> {
32 _inner: Peri<'d, T>,
33}
34
35impl<'d, T: Instance> IndependentWatchdog<'d, T> {
36 /// Configure the watchdog for a `timeout_us` microsecond period.
37 ///
38 /// The period is `(reload + 1) * prescaler / clock`; the clock is the LSI, or HSI / 1024 on
39 /// CH32X0. Both are RC oscillators with a tolerance, so leave margin and pet well before
40 /// the nominal deadline. Panics if the timeout does not fit the 12-bit
41 /// counter with the largest prescaler (32 s at 32.768 kHz, 26 s at
42 /// 40 kHz).
43 pub fn new(inner: Peri<'d, T>, timeout_us: u32) -> Self {
44 let (prescaler, reload) = prescaler_and_reload(timeout_us);
45 let regs = T::regs();
46
47 // Unlock PSCR/RLDR and configure them while the watchdog is stopped.
48 // Starting the watchdog forces LSI on (RM §3.3.5.4); the vendor
49 // sequence also configures first, so no clock has to be running yet.
50 regs.ctlr().write(|w| w.set_key(0x5555));
51 regs.pscr().write(|w| w.set_pr(prescaler));
52 regs.rldr().write(|w| w.set_rl(reload));
53
54 Self { _inner: inner }
55 }
56
57 /// Start the watchdog. This is irreversible until the next reset.
58 pub fn unleash(&mut self) {
59 // The counter holds its reset value until the reload key is written,
60 // so load RLDR before enabling.
61 T::regs().ctlr().write(|w| w.set_key(0xaaaa));
62 T::regs().ctlr().write(|w| w.set_key(0xcccc));
63 }
64
65 /// Reload the counter from RLDR. Call this well inside every period.
66 pub fn pet(&mut self) {
67 T::regs().ctlr().write(|w| w.set_key(0xaaaa));
68 }
69}
70
71/// Pick the smallest prescaler that can express `timeout_us` in 12 bits.
72fn prescaler_and_reload(timeout_us: u32) -> (u8, u16) {
73 let clock = u64::from(CLOCK_HZ);
74
75 for prescaler_power in 2u8..=8u8 {
76 let prescaler = 1u32 << prescaler_power;
77 let ticks = (u64::from(timeout_us) * clock).div_ceil(u64::from(prescaler) * 1_000_000);
78 if ticks <= u64::from(MAX_RELOAD) + 1 {
79 let ticks = ticks.max(1) as u16;
80 return (prescaler_power - 2, ticks - 1);
81 }
82 }
83
84 panic!("independent watchdog timeout of {} us is out of range", timeout_us);
85}
86
87trait SealedInstance {
88 fn regs() -> pac::iwdg::Iwdg;
89}
90
91/// IWDG instance trait.
92#[allow(private_bounds)]
93pub trait Instance: SealedInstance + PeripheralType {}
94
95foreach_peripheral!(
96 (iwdg, $inst:ident) => {
97 impl SealedInstance for peripherals::$inst {
98 fn regs() -> pac::iwdg::Iwdg {
99 pac::$inst
100 }
101 }
102
103 impl Instance for peripherals::$inst {}
104 };
105);