ambiq_hal/
clock.rs

1use crate::time::Hertz;
2
3pub const CLKGEN_FREQ_MAX_HZ: Hertz = Hertz(48_000_000);
4pub const CLKGEN_CLKKEY: u32 = 71;
5
6pub use pac::CLKGEN;
7
8#[allow(unused)]
9fn clk_cfg<F, R>(f: F) -> R
10where
11    F: FnOnce() -> R,
12{
13    cortex_m::interrupt::free(|_| {
14        unsafe {
15            (*pac::CLKGEN::ptr())
16                .clkkey
17                .write(|w| w.bits(CLKGEN_CLKKEY));
18        }
19
20        let r = f();
21
22        unsafe {
23            (*pac::CLKGEN::ptr()).clkkey.write(|w| w.bits(0x00));
24        }
25
26        r
27    })
28}
29
30pub struct ClockCtrl<'a> {
31    clkgen: &'a mut CLKGEN,
32}
33
34impl<'a> ClockCtrl<'a> {
35    pub fn new(clkgen: &'a mut CLKGEN) -> ClockCtrl {
36        ClockCtrl { clkgen }
37    }
38
39    /// Enable the XT oscilator for RTC
40    pub fn enable_xt(&mut self) {
41        self.clkgen.octrl.modify(|_, w| w.stopxt().en());
42    }
43
44    /// Disable the XT oscilator for RTC
45    pub fn disable_xt(&mut self) {
46        self.clkgen.octrl.modify(|_, w| w.stopxt().stop());
47    }
48
49    pub fn rtc_use_xt(&mut self) {
50        self.clkgen.octrl.write(|w| w.stopxt().en().osel().rtc_xt());
51    }
52
53    pub fn rtc_use_lfrc(&mut self) {
54        self.clkgen.octrl.modify(|_, w| w.osel().rtc_lfrc());
55    }
56}