esp-hal/esp32c6-hal/examples/rtc_watchdog.rs
Juraj Sadel 40bf086a0c
Add PeripheralClockControl argument to timg, wdt, sha, usb-serial-jtag and uart constructors (#463)
* PeripheralClockControl timer

* Add PeripheralClockControl to timg, wdt, sha, usb-serial-jtag and uart

* ESP32 updated examples

* ESP32C2 updated examples

* ESP32C3 updated examples

* ESP32S2 updated examples

* ESP32S3 updated examples

* ESP32C6 updated examples

* cargo fmt
2023-03-31 09:51:34 +02:00

87 lines
2.0 KiB
Rust

//! This demos the RTC Watchdog Timer (RWDT).
//! The RWDT is initially configured to trigger an interrupt after a given
//! timeout. Then, upon expiration, the RWDT is restarted and then reconfigured
//! to reset both the main system and the RTC.
#![no_std]
#![no_main]
use core::cell::RefCell;
use critical_section::Mutex;
use esp32c6_hal::{
clock::ClockControl,
interrupt,
peripherals::{self, Peripherals},
prelude::*,
riscv,
timer::TimerGroup,
Rtc,
Rwdt,
};
use esp_backtrace as _;
static RWDT: Mutex<RefCell<Option<Rwdt>>> = Mutex::new(RefCell::new(None));
#[entry]
fn main() -> ! {
let peripherals = Peripherals::take();
let mut system = peripherals.PCR.split();
let clocks = ClockControl::boot_defaults(system.clock_control).freeze();
let timer_group0 = TimerGroup::new(
peripherals.TIMG0,
&clocks,
&mut system.peripheral_clock_control,
);
let mut wdt0 = timer_group0.wdt;
let timer_group1 = TimerGroup::new(
peripherals.TIMG1,
&clocks,
&mut system.peripheral_clock_control,
);
let mut wdt1 = timer_group1.wdt;
wdt0.disable();
wdt1.disable();
let mut rtc = Rtc::new(peripherals.LP_CLKRST);
// Disable watchdog timers
rtc.swd.disable();
rtc.rwdt.disable();
rtc.rwdt.start(2000u64.millis());
rtc.rwdt.listen();
interrupt::enable(
peripherals::Interrupt::LP_WDT,
interrupt::Priority::Priority1,
)
.unwrap();
critical_section::with(|cs| RWDT.borrow_ref_mut(cs).replace(rtc.rwdt));
unsafe {
riscv::interrupt::enable();
}
loop {}
}
#[interrupt]
fn LP_WDT() {
critical_section::with(|cs| {
esp_println::println!("RWDT Interrupt");
let mut rwdt = RWDT.borrow_ref_mut(cs);
let rwdt = rwdt.as_mut().unwrap();
rwdt.clear_interrupt();
esp_println::println!("Restarting in 5 seconds...");
rwdt.start(5000u64.millis());
rwdt.unlisten();
});
}