|
| 1 | +//! This demos the RTC Watchdog Timer (RWDT). |
| 2 | +//! The RWDT is initially configured to trigger an interrupt after a given timeout. |
| 3 | +//! Then, upon expiration, the RWDT is restarted and then reconfigured to reset both the main |
| 4 | +//! system and the RTC. |
| 5 | +
|
| 6 | +#![no_std] |
| 7 | +#![no_main] |
| 8 | + |
| 9 | +use core::cell::RefCell; |
| 10 | + |
| 11 | +use bare_metal::Mutex; |
| 12 | + |
| 13 | +use esp32c3_hal::{ |
| 14 | + clock::ClockControl, |
| 15 | + interrupt, |
| 16 | + pac::{self, Peripherals}, |
| 17 | + prelude::*, |
| 18 | + Rtc, |
| 19 | +}; |
| 20 | +use esp_hal_common::Rwdt; |
| 21 | +use panic_halt as _; |
| 22 | +use riscv_rt::entry; |
| 23 | + |
| 24 | +static mut RWDT: Mutex<RefCell<Option<Rwdt>>> = Mutex::new(RefCell::new(None)); |
| 25 | + |
| 26 | +#[entry] |
| 27 | +fn main() -> ! { |
| 28 | + let peripherals = Peripherals::take().unwrap(); |
| 29 | + let system = peripherals.SYSTEM.split(); |
| 30 | + let _clocks = ClockControl::boot_defaults(system.clock_control).freeze(); |
| 31 | + |
| 32 | + let mut rtc = Rtc::new(peripherals.RTC_CNTL); |
| 33 | + |
| 34 | + // Disable watchdog timers |
| 35 | + rtc.swd.disable(); |
| 36 | + rtc.rwdt.disable(); |
| 37 | + |
| 38 | + rtc.rwdt.start(2000u64.millis()); |
| 39 | + rtc.rwdt.listen(); |
| 40 | + |
| 41 | + interrupt::enable(pac::Interrupt::RTC_CORE, interrupt::Priority::Priority1).unwrap(); |
| 42 | + |
| 43 | + riscv::interrupt::free(|_cs| unsafe { |
| 44 | + RWDT.get_mut().replace(Some(rtc.rwdt)); |
| 45 | + }); |
| 46 | + |
| 47 | + unsafe { |
| 48 | + riscv::interrupt::enable(); |
| 49 | + } |
| 50 | + |
| 51 | + loop {} |
| 52 | +} |
| 53 | + |
| 54 | +#[interrupt] |
| 55 | +fn RTC_CORE() { |
| 56 | + riscv::interrupt::free(|cs| unsafe { |
| 57 | + esp_println::println!("RWDT Interrupt"); |
| 58 | + |
| 59 | + let mut rwdt = RWDT.borrow(*cs).borrow_mut(); |
| 60 | + let rwdt = rwdt.as_mut().unwrap(); |
| 61 | + |
| 62 | + rwdt.clear_interrupt(); |
| 63 | + |
| 64 | + esp_println::println!("Restarting in 5 seconds..."); |
| 65 | + |
| 66 | + rwdt.start(5000u64.millis()); |
| 67 | + rwdt.unlisten(); |
| 68 | + }); |
| 69 | +} |
0 commit comments