2022-06-10 16:10:54 +02:00
|
|
|
use core::marker::PhantomData;
|
|
|
|
|
2023-07-28 13:23:22 +02:00
|
|
|
use embassy_hal_internal::{into_ref, Peripheral};
|
2022-07-10 19:38:30 +02:00
|
|
|
use stm32_metapac::iwdg::vals::{Key, Pr};
|
|
|
|
|
2022-07-10 19:59:36 +02:00
|
|
|
use crate::rcc::LSI_FREQ;
|
2022-06-10 16:10:54 +02:00
|
|
|
|
|
|
|
pub struct IndependentWatchdog<'d, T: Instance> {
|
|
|
|
wdg: PhantomData<&'d mut T>,
|
|
|
|
}
|
|
|
|
|
2022-07-10 19:38:30 +02:00
|
|
|
// 12-bit counter
|
|
|
|
const MAX_RL: u16 = 0xFFF;
|
|
|
|
|
2022-07-10 23:00:33 +02:00
|
|
|
/// Calculates maximum watchdog timeout in us (RL = 0xFFF) for a given prescaler
|
2023-05-22 14:22:27 +02:00
|
|
|
const fn get_timeout_us(prescaler: u16, reload_value: u16) -> u32 {
|
|
|
|
1_000_000 * (reload_value + 1) as u32 / (LSI_FREQ.0 / prescaler as u32)
|
2022-07-10 19:38:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Calculates watchdog reload value for the given prescaler and desired timeout
|
2022-11-09 14:10:46 +01:00
|
|
|
const fn reload_value(prescaler: u16, timeout_us: u32) -> u16 {
|
2023-05-22 14:22:27 +02:00
|
|
|
(timeout_us / prescaler as u32 * LSI_FREQ.0 / 1_000_000) as u16 - 1
|
2022-07-10 19:38:30 +02:00
|
|
|
}
|
|
|
|
|
2022-06-10 16:10:54 +02:00
|
|
|
impl<'d, T: Instance> IndependentWatchdog<'d, T> {
|
2022-07-10 23:00:33 +02:00
|
|
|
/// Creates an IWDG (Independent Watchdog) instance with a given timeout value in microseconds.
|
|
|
|
///
|
|
|
|
/// [Self] has to be started with [Self::unleash()].
|
|
|
|
/// Once timer expires, MCU will be reset. To prevent this, timer must be reloaded by repeatedly calling [Self::pet()] within timeout interval.
|
2022-07-23 14:00:19 +02:00
|
|
|
pub fn new(_instance: impl Peripheral<P = T> + 'd, timeout_us: u32) -> Self {
|
|
|
|
into_ref!(_instance);
|
2022-06-10 16:10:54 +02:00
|
|
|
|
2022-07-10 19:38:30 +02:00
|
|
|
// Find lowest prescaler value, which makes watchdog period longer or equal to timeout.
|
|
|
|
// This iterates from 4 (2^2) to 256 (2^8).
|
|
|
|
let psc_power = unwrap!((2..=8).find(|psc_power| {
|
2022-11-09 14:10:46 +01:00
|
|
|
let psc = 2u16.pow(*psc_power);
|
2023-05-22 14:22:27 +02:00
|
|
|
timeout_us <= get_timeout_us(psc, MAX_RL)
|
2022-07-10 19:38:30 +02:00
|
|
|
}));
|
|
|
|
|
|
|
|
// Prescaler value
|
2022-11-09 14:10:46 +01:00
|
|
|
let psc = 2u16.pow(psc_power);
|
2022-07-10 19:38:30 +02:00
|
|
|
|
|
|
|
// Convert prescaler power to PR register value
|
|
|
|
let pr = psc_power as u8 - 2;
|
|
|
|
assert!(pr <= 0b110);
|
|
|
|
|
|
|
|
// Reload value
|
2022-07-10 23:00:33 +02:00
|
|
|
let rl = reload_value(psc, timeout_us);
|
2022-07-10 19:38:30 +02:00
|
|
|
|
2022-06-10 16:10:54 +02:00
|
|
|
let wdg = T::regs();
|
2023-06-19 03:07:26 +02:00
|
|
|
wdg.kr().write(|w| w.set_key(Key::ENABLE));
|
2023-06-29 01:51:19 +02:00
|
|
|
wdg.pr().write(|w| w.set_pr(Pr::from_bits(pr)));
|
2023-06-19 03:07:26 +02:00
|
|
|
wdg.rlr().write(|w| w.set_rl(rl));
|
2022-06-10 16:10:54 +02:00
|
|
|
|
2023-05-22 14:22:27 +02:00
|
|
|
trace!(
|
|
|
|
"Watchdog configured with {}us timeout, desired was {}us (PR={}, RL={})",
|
|
|
|
get_timeout_us(psc, rl),
|
|
|
|
timeout_us,
|
|
|
|
pr,
|
|
|
|
rl
|
|
|
|
);
|
|
|
|
|
2023-08-06 22:00:39 +02:00
|
|
|
IndependentWatchdog { wdg: PhantomData }
|
2022-06-10 16:10:54 +02:00
|
|
|
}
|
|
|
|
|
2023-06-19 03:07:26 +02:00
|
|
|
pub fn unleash(&mut self) {
|
2022-06-10 16:10:54 +02:00
|
|
|
T::regs().kr().write(|w| w.set_key(Key::START));
|
|
|
|
}
|
|
|
|
|
2023-06-19 03:07:26 +02:00
|
|
|
pub fn pet(&mut self) {
|
2022-06-10 16:10:54 +02:00
|
|
|
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 {}
|
|
|
|
|
2022-06-28 13:15:23 +02:00
|
|
|
foreach_peripheral!(
|
|
|
|
(iwdg, $inst:ident) => {
|
|
|
|
impl sealed::Instance for crate::peripherals::$inst {
|
|
|
|
fn regs() -> crate::pac::iwdg::Iwdg {
|
|
|
|
crate::pac::$inst
|
|
|
|
}
|
|
|
|
}
|
2022-06-10 16:10:54 +02:00
|
|
|
|
2022-06-28 13:15:23 +02:00
|
|
|
impl Instance for crate::peripherals::$inst {}
|
|
|
|
};
|
|
|
|
);
|
2023-05-22 14:22:27 +02:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn can_compute_timeout_us() {
|
|
|
|
assert_eq!(125, get_timeout_us(4, 0));
|
|
|
|
assert_eq!(512_000, get_timeout_us(4, MAX_RL));
|
|
|
|
|
|
|
|
assert_eq!(8_000, get_timeout_us(256, 0));
|
2023-08-06 22:00:39 +02:00
|
|
|
assert_eq!(32_768_000, get_timeout_us(256, MAX_RL));
|
2023-05-22 14:22:27 +02:00
|
|
|
|
2023-08-06 22:00:39 +02:00
|
|
|
assert_eq!(8_000_000, get_timeout_us(64, 3999));
|
2023-05-22 14:22:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn can_compute_reload_value() {
|
|
|
|
assert_eq!(0xFFF, reload_value(4, 512_000));
|
2023-08-06 22:00:39 +02:00
|
|
|
assert_eq!(0xFFF, reload_value(256, 32_768_000));
|
2023-05-22 14:22:27 +02:00
|
|
|
|
2023-08-06 22:00:39 +02:00
|
|
|
assert_eq!(3999, reload_value(64, 8_000_000));
|
2023-05-22 14:22:27 +02:00
|
|
|
}
|
|
|
|
}
|