use core::cell::Cell;
use crate::Steal;
use pastey::paste;
use rp2350_pac::PSM;
use rp2350_pac::psm::Wdsel;
const WATCHDOG_NON_REBOOT_MAGIC: u32 = 0x6ab73121;
const WATCHDOG_REBOOT_MAGIC: u32 = 0xb007c0d3;
const PSM_WDSEL_RESET_BITS: u32 = Wdsel::PROC1
| Wdsel::PROC0
| Wdsel::ACCESSCTRL
| Wdsel::SIO
| Wdsel::XIP
| Wdsel::SRAM9
| Wdsel::SRAM8
| Wdsel::SRAM7
| Wdsel::SRAM6
| Wdsel::SRAM5
| Wdsel::SRAM4
| Wdsel::SRAM3
| Wdsel::SRAM2
| Wdsel::SRAM1
| Wdsel::SRAM0
| Wdsel::BOOTRAM
| Wdsel::ROM
| Wdsel::BUSFABRIC
| Wdsel::PSM_READY
| Wdsel::CLOCKS
| Wdsel::RESETS
| Wdsel::OTP
| Wdsel::PROC_COLD;
const WATCHDOG_LOAD_MAX: u32 = 0x00ffffff;
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum ResetReason {
PowerOn,
WatchdogTimeout,
WatchdogForce,
Unknown(u32),
}
pub struct Watchdog {
watchdog: rp2350_pac::WATCHDOG,
load_value: Cell<u32>,
}
impl Watchdog {
#[inline]
pub fn init(watchdog: rp2350_pac::WATCHDOG) -> Self {
Self {
watchdog,
load_value: Cell::new(0),
}
}
#[inline]
pub unsafe fn steal() -> Steal<Self> {
Steal(Self::init(unsafe { rp2350_pac::WATCHDOG::steal() }))
}
#[inline]
pub unsafe fn regs(&self) -> &rp2350_pac::watchdog::RegisterBlock {
&self.watchdog
}
#[inline]
pub fn release(self) -> rp2350_pac::WATCHDOG {
self.watchdog
}
pub fn start(&self, timeout_us: u32) {
self.load_value.set(timeout_us.min(WATCHDOG_LOAD_MAX));
self.watchdog.load.write_load(self.load_value.get());
self.watchdog.ctrl.set_enable();
}
#[inline]
pub fn feed(&self) {
self.watchdog.load.write_load(self.load_value.get());
}
#[inline]
pub fn feed_with(&self, timeout_us: u32) {
self.load_value.set(timeout_us.min(WATCHDOG_LOAD_MAX));
self.watchdog.load.write_load(self.load_value.get());
}
#[inline]
pub fn disable(&self) {
self.watchdog.ctrl.clr_enable();
}
#[inline]
pub fn is_enabled(&self) -> bool {
self.watchdog.ctrl.enable()
}
#[inline]
pub fn time_remaining(&self) -> u32 {
self.watchdog.ctrl.time()
}
#[inline]
pub fn time_remaining_ms(&self) -> u32 {
self.time_remaining() / 1000
}
#[inline]
pub fn force_reset(&self) {
self.watchdog.ctrl.trigger_trigger();
}
#[inline]
pub fn pause_on_debug0(&self, pause: bool) {
if pause {
self.watchdog.ctrl.set_pause_dbg0();
} else {
self.watchdog.ctrl.clr_pause_dbg0();
}
}
#[inline]
pub fn pause_on_debug1(&self, pause: bool) {
if pause {
self.watchdog.ctrl.set_pause_dbg1();
} else {
self.watchdog.ctrl.clr_pause_dbg1();
}
}
#[inline]
pub fn pause_on_jtag(&self, pause: bool) {
if pause {
self.watchdog.ctrl.set_pause_jtag();
} else {
self.watchdog.ctrl.clr_pause_jtag();
}
}
pub fn enable_debug_pause(&self) {
self.pause_on_debug0(true);
self.pause_on_debug1(true);
self.pause_on_jtag(true);
}
pub fn disable_debug_pause(&self) {
self.pause_on_debug0(false);
self.pause_on_debug1(false);
self.pause_on_jtag(false);
}
pub fn reset_reason(&self) -> ResetReason {
let reason = &self.watchdog.reason;
if reason.timer() {
ResetReason::WatchdogTimeout
} else if reason.force() {
ResetReason::WatchdogForce
} else if reason.read_bits() == 0 {
ResetReason::PowerOn
} else {
ResetReason::Unknown(reason.read_bits())
}
}
#[inline]
pub fn was_watchdog_reset(&self) -> bool {
self.watchdog.reason.timer()
}
#[inline]
pub fn was_force_reset(&self) -> bool {
self.watchdog.reason.force()
}
#[inline]
pub fn caused_reboot(&self) -> bool {
self.watchdog.reason.read_bits() != 0
}
#[inline]
pub fn enable_caused_reboot(&self) -> bool {
self.watchdog.reason.read_bits() != 0
&& self.watchdog.scratch4.read_bits() == WATCHDOG_NON_REBOOT_MAGIC
}
pub fn read_scratch(&self, index: usize) -> u32 {
assert!(index < 8, "scratch index must be 0-7");
match index {
0 => self.watchdog.scratch0.read_bits(),
1 => self.watchdog.scratch1.read_bits(),
2 => self.watchdog.scratch2.read_bits(),
3 => self.watchdog.scratch3.read_bits(),
4 => self.watchdog.scratch4.read_bits(),
5 => self.watchdog.scratch5.read_bits(),
6 => self.watchdog.scratch6.read_bits(),
7 => self.watchdog.scratch7.read_bits(),
_ => unreachable!(),
}
}
pub fn write_scratch(&self, index: usize, value: u32) {
assert!(index < 8, "scratch index must be 0-7");
match index {
0 => self.watchdog.scratch0.write_bits(value),
1 => self.watchdog.scratch1.write_bits(value),
2 => self.watchdog.scratch2.write_bits(value),
3 => self.watchdog.scratch3.write_bits(value),
4 => self.watchdog.scratch4.write_bits(value),
5 => self.watchdog.scratch5.write_bits(value),
6 => self.watchdog.scratch6.write_bits(value),
7 => self.watchdog.scratch7.write_bits(value),
_ => unreachable!(),
}
}
pub fn enable_watchdog(&self, delay_ms: u32, pause_on_debug: bool) {
self.watchdog.scratch4.write_bits(WATCHDOG_NON_REBOOT_MAGIC);
self.configure_watchdog_reset(delay_ms, pause_on_debug);
}
pub fn reboot(&self, delay_ms: u32) -> ! {
self.watchdog.scratch4.write_bits(0);
self.configure_watchdog_reset(delay_ms, false);
loop {
rp2350_rt::asm::wfi();
}
}
pub unsafe fn reboot_to_address(&self, pc: u32, sp: u32, delay_ms: u32) -> ! {
self.watchdog.ctrl.clr_enable();
let pc_thumb = pc | 1;
self.watchdog.scratch4.write_bits(WATCHDOG_REBOOT_MAGIC);
self.watchdog
.scratch5
.write_bits(pc_thumb ^ (WATCHDOG_REBOOT_MAGIC.wrapping_neg()));
self.watchdog.scratch6.write_bits(sp);
self.watchdog.scratch7.write_bits(pc_thumb);
self.configure_watchdog_reset(delay_ms, false);
loop {
rp2350_rt::asm::wfi();
}
}
fn configure_watchdog_reset(&self, delay_ms: u32, pause_on_debug: bool) {
let psm = unsafe { PSM::steal() };
self.watchdog.ctrl.clr_enable();
psm.wdsel.set_bits(PSM_WDSEL_RESET_BITS);
if pause_on_debug {
self.watchdog.ctrl.set_pause_dbg0();
self.watchdog.ctrl.set_pause_dbg1();
self.watchdog.ctrl.set_pause_jtag();
} else {
self.watchdog.ctrl.clr_pause_dbg0();
self.watchdog.ctrl.clr_pause_dbg1();
self.watchdog.ctrl.clr_pause_jtag();
}
if delay_ms == 0 {
self.watchdog.ctrl.trigger_trigger();
} else {
let load_value = (delay_ms.saturating_mul(1000)).min(WATCHDOG_LOAD_MAX);
self.watchdog.load.write_load(load_value);
self.watchdog.ctrl.set_enable();
}
}
}
macro_rules! impl_scratch {
($($n:literal),+ $(,)?) => {
paste! {
$(
impl Watchdog {
#[doc = concat!("Read scratch register ", $n, ".")]
#[inline]
pub fn [<scratch $n>](&self) -> u32 {
self.watchdog.[<scratch $n>].read_bits()
}
#[doc = concat!("Write scratch register ", $n, ".")]
#[inline]
pub fn [<set_scratch $n>](&self, value: u32) {
self.watchdog.[<scratch $n>].write_bits(value);
}
}
)+
}
};
}
impl_scratch!(0, 1, 2, 3, 4, 5, 6, 7);
pub fn reboot(delay_ms: u32) -> ! {
unsafe { Watchdog::steal() }.reboot(delay_ms)
}
pub unsafe fn reboot_to_address(pc: u32, sp: u32, delay_ms: u32) -> ! {
unsafe { Watchdog::steal().reboot_to_address(pc, sp, delay_ms) }
}
#[inline]
pub fn caused_reboot() -> bool {
unsafe { Watchdog::steal() }.caused_reboot()
}
#[inline]
pub fn enable_caused_reboot() -> bool {
unsafe { Watchdog::steal() }.enable_caused_reboot()
}