Skip to content

Commit 9d0a1f6

Browse files
gustavoniheijessebraham
authored andcommitted
esp32[c3|s2|s3]-hal: Add example for monitoring the XTAL frequency
Signed-off-by: Gustavo Henrique Nihei <[email protected]>
1 parent 61c0731 commit 9d0a1f6

File tree

4 files changed

+280
-0
lines changed

4 files changed

+280
-0
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
//! This demos a simple monitor for the XTAL frequency, by relying on a special
2+
//! feature of the TIMG0 (Timer Group 0). This feature counts the number of XTAL
3+
//! clock cycles within a given number of RTC_SLOW_CLK cycles.
4+
5+
#![no_std]
6+
#![no_main]
7+
8+
use core::cell::RefCell;
9+
10+
use esp32_hal::{
11+
clock::ClockControl,
12+
interrupt,
13+
pac::{self, Peripherals},
14+
prelude::*,
15+
Rtc,
16+
};
17+
use panic_halt as _;
18+
use xtensa_lx::mutex::{CriticalSectionMutex, Mutex};
19+
use xtensa_lx_rt::entry;
20+
21+
static mut RTC: CriticalSectionMutex<RefCell<Option<Rtc>>> =
22+
CriticalSectionMutex::new(RefCell::new(None));
23+
24+
#[entry]
25+
fn main() -> ! {
26+
let peripherals = Peripherals::take().unwrap();
27+
let system = peripherals.DPORT.split();
28+
let clocks = ClockControl::boot_defaults(system.clock_control).freeze();
29+
30+
let mut rtc = Rtc::new(peripherals.RTC_CNTL);
31+
32+
// Disable watchdog timer
33+
rtc.rwdt.disable();
34+
35+
rtc.rwdt.start(2000u64.millis());
36+
rtc.rwdt.listen();
37+
38+
esp_println::println!(
39+
"{: <10} XTAL frequency: {} MHz",
40+
"[Expected]",
41+
clocks.xtal_clock.to_MHz()
42+
);
43+
44+
interrupt::enable(pac::Interrupt::RTC_CORE, interrupt::Priority::Priority1).unwrap();
45+
46+
unsafe {
47+
(&RTC).lock(|data| (*data).replace(Some(rtc)));
48+
}
49+
50+
loop {}
51+
}
52+
53+
#[interrupt]
54+
fn RTC_CORE() {
55+
unsafe {
56+
(&RTC).lock(|data| {
57+
let mut rtc = data.borrow_mut();
58+
let rtc = rtc.as_mut().unwrap();
59+
60+
esp_println::println!(
61+
"{: <10} XTAL frequency: {} MHz",
62+
"[Monitor]",
63+
rtc.estimate_xtal_frequency()
64+
);
65+
66+
rtc.rwdt.clear_interrupt();
67+
});
68+
}
69+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
//! This demos a simple monitor for the XTAL frequency, by relying on a special feature of the
2+
//! TIMG0 (Timer Group 0). This feature counts the number of XTAL clock cycles within a given
3+
//! number of RTC_SLOW_CLK cycles.
4+
5+
#![no_std]
6+
#![no_main]
7+
8+
use core::cell::RefCell;
9+
10+
use bare_metal::Mutex;
11+
12+
use esp32c3_hal::{
13+
clock::ClockControl,
14+
interrupt,
15+
pac::{self, Peripherals},
16+
prelude::*,
17+
Rtc,
18+
};
19+
use panic_halt as _;
20+
use riscv_rt::entry;
21+
22+
static mut RTC: Mutex<RefCell<Option<Rtc>>> = Mutex::new(RefCell::new(None));
23+
24+
#[entry]
25+
fn main() -> ! {
26+
let peripherals = Peripherals::take().unwrap();
27+
let system = peripherals.SYSTEM.split();
28+
let clocks = ClockControl::boot_defaults(system.clock_control).freeze();
29+
30+
let mut rtc = Rtc::new(peripherals.RTC_CNTL);
31+
32+
// Disable watchdog timers
33+
rtc.swd.disable();
34+
rtc.rwdt.disable();
35+
36+
rtc.rwdt.start(2000u64.millis());
37+
rtc.rwdt.listen();
38+
39+
esp_println::println!(
40+
"{: <10} XTAL frequency: {} MHz",
41+
"[Expected]",
42+
clocks.xtal_clock.to_MHz()
43+
);
44+
45+
interrupt::enable(pac::Interrupt::RTC_CORE, interrupt::Priority::Priority1).unwrap();
46+
47+
riscv::interrupt::free(|_cs| unsafe {
48+
RTC.get_mut().replace(Some(rtc));
49+
});
50+
51+
unsafe {
52+
riscv::interrupt::enable();
53+
}
54+
55+
loop {}
56+
}
57+
58+
#[interrupt]
59+
fn RTC_CORE() {
60+
riscv::interrupt::free(|cs| unsafe {
61+
let mut rtc = RTC.borrow(*cs).borrow_mut();
62+
let rtc = rtc.as_mut().unwrap();
63+
64+
esp_println::println!(
65+
"{: <10} XTAL frequency: {} MHz",
66+
"[Monitor]",
67+
rtc.estimate_xtal_frequency()
68+
);
69+
70+
rtc.rwdt.clear_interrupt();
71+
});
72+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
//! This demos a simple monitor for the XTAL frequency, by relying on a special
2+
//! feature of the TIMG0 (Timer Group 0). This feature counts the number of XTAL
3+
//! clock cycles within a given number of RTC_SLOW_CLK cycles.
4+
5+
#![no_std]
6+
#![no_main]
7+
8+
use core::cell::RefCell;
9+
10+
use esp32s2_hal::{
11+
clock::ClockControl,
12+
interrupt,
13+
pac::{self, Peripherals},
14+
prelude::*,
15+
Rtc,
16+
};
17+
use panic_halt as _;
18+
use xtensa_lx::mutex::{CriticalSectionMutex, Mutex};
19+
use xtensa_lx_rt::entry;
20+
21+
static mut RTC: CriticalSectionMutex<RefCell<Option<Rtc>>> =
22+
CriticalSectionMutex::new(RefCell::new(None));
23+
24+
#[entry]
25+
fn main() -> ! {
26+
let peripherals = Peripherals::take().unwrap();
27+
let system = peripherals.SYSTEM.split();
28+
let clocks = ClockControl::boot_defaults(system.clock_control).freeze();
29+
30+
let mut rtc = Rtc::new(peripherals.RTC_CNTL);
31+
32+
// Disable watchdog timer
33+
rtc.rwdt.disable();
34+
35+
rtc.rwdt.start(2000u64.millis());
36+
rtc.rwdt.listen();
37+
38+
esp_println::println!(
39+
"{: <10} XTAL frequency: {} MHz",
40+
"[Expected]",
41+
clocks.xtal_clock.to_MHz()
42+
);
43+
44+
interrupt::enable(pac::Interrupt::RTC_CORE, interrupt::Priority::Priority1).unwrap();
45+
46+
unsafe {
47+
(&RTC).lock(|data| (*data).replace(Some(rtc)));
48+
}
49+
50+
loop {}
51+
}
52+
53+
#[interrupt]
54+
fn RTC_CORE() {
55+
unsafe {
56+
(&RTC).lock(|data| {
57+
let mut rtc = data.borrow_mut();
58+
let rtc = rtc.as_mut().unwrap();
59+
60+
esp_println::println!(
61+
"{: <10} XTAL frequency: {} MHz",
62+
"[Monitor]",
63+
rtc.estimate_xtal_frequency()
64+
);
65+
66+
rtc.rwdt.clear_interrupt();
67+
});
68+
}
69+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
//! This demos a simple monitor for the XTAL frequency, by relying on a special
2+
//! feature of the TIMG0 (Timer Group 0). This feature counts the number of XTAL
3+
//! clock cycles within a given number of RTC_SLOW_CLK cycles.
4+
5+
#![no_std]
6+
#![no_main]
7+
8+
use core::cell::RefCell;
9+
10+
use esp32s3_hal::{
11+
clock::ClockControl,
12+
interrupt,
13+
pac::{self, Peripherals},
14+
prelude::*,
15+
Rtc,
16+
};
17+
use panic_halt as _;
18+
use xtensa_lx::mutex::{CriticalSectionMutex, Mutex};
19+
use xtensa_lx_rt::entry;
20+
21+
static mut RTC: CriticalSectionMutex<RefCell<Option<Rtc>>> =
22+
CriticalSectionMutex::new(RefCell::new(None));
23+
24+
#[entry]
25+
fn main() -> ! {
26+
let peripherals = Peripherals::take().unwrap();
27+
let system = peripherals.SYSTEM.split();
28+
let clocks = ClockControl::boot_defaults(system.clock_control).freeze();
29+
30+
let mut rtc = Rtc::new(peripherals.RTC_CNTL);
31+
32+
// Disable watchdog timers
33+
rtc.swd.disable();
34+
rtc.rwdt.disable();
35+
36+
rtc.rwdt.start(2000u64.millis());
37+
rtc.rwdt.listen();
38+
39+
esp_println::println!(
40+
"{: <10} XTAL frequency: {} MHz",
41+
"[Expected]",
42+
clocks.xtal_clock.to_MHz()
43+
);
44+
45+
interrupt::enable(pac::Interrupt::RTC_CORE, interrupt::Priority::Priority1).unwrap();
46+
47+
unsafe {
48+
(&RTC).lock(|data| (*data).replace(Some(rtc)));
49+
}
50+
51+
loop {}
52+
}
53+
54+
#[interrupt]
55+
fn RTC_CORE() {
56+
unsafe {
57+
(&RTC).lock(|data| {
58+
let mut rtc = data.borrow_mut();
59+
let rtc = rtc.as_mut().unwrap();
60+
61+
esp_println::println!(
62+
"{: <10} XTAL frequency: {} MHz",
63+
"[Monitor]",
64+
rtc.estimate_xtal_frequency()
65+
);
66+
67+
rtc.rwdt.clear_interrupt();
68+
});
69+
}
70+
}

0 commit comments

Comments
 (0)