esp-hal/examples/src/bin/clock_monitor.rs
Juraj Sadel 1e6165e04b
Runtime ISR binding and simple public API docs for RTC (#1405)
* Runtime ISR binding and simple public API docs for RTC

* changelog

* import dependencies instead of using full paths
2024-04-05 14:54:13 +00:00

55 lines
1.5 KiB
Rust

//! This demos a simple monitor for the XTAL frequency, by relying on a special
//! feature of the TIMG0 (Timer Group 0). This feature counts the number of XTAL
//! clock cycles within a given number of RTC_SLOW_CLK cycles.
//% CHIPS: esp32 esp32c2 esp32c3 esp32c6 esp32h2 esp32s2 esp32s3
#![no_std]
#![no_main]
use core::cell::RefCell;
use critical_section::Mutex;
use esp_backtrace as _;
use esp_hal::{clock::ClockControl, peripherals::Peripherals, prelude::*, rtc_cntl::Rtc};
use esp_println::println;
static RTC: Mutex<RefCell<Option<Rtc>>> = Mutex::new(RefCell::new(None));
#[entry]
fn main() -> ! {
let peripherals = Peripherals::take();
let system = peripherals.SYSTEM.split();
let clocks = ClockControl::boot_defaults(system.clock_control).freeze();
let mut rtc = Rtc::new(peripherals.LPWR, Some(interrupt_handler));
rtc.rwdt.set_timeout(2000.millis());
rtc.rwdt.listen();
println!(
"{: <10} XTAL frequency: {} MHz",
"[Expected]",
clocks.xtal_clock.to_MHz()
);
critical_section::with(|cs| RTC.borrow_ref_mut(cs).replace(rtc));
loop {}
}
#[handler(priority = esp_hal::interrupt::Priority::min())]
fn interrupt_handler() {
critical_section::with(|cs| {
let mut rtc = RTC.borrow_ref_mut(cs);
let rtc = rtc.as_mut().unwrap();
println!(
"{: <10} XTAL frequency: {} MHz",
"[Monitor]",
rtc.estimate_xtal_frequency()
);
rtc.rwdt.clear_interrupt();
});
}