use rp2350_pac::{
XOSC,
xosc::{Ctrl, Dormant},
};
use crate::Steal;
pub const XOSC_HZ: u32 = 12_000_000;
pub const STARTUP_DELAY_MULTIPLIER: u32 = 1;
const XOSC_STABLE_TIMEOUT_SPINS: u32 = 8_000_000;
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum FrequencyRange {
Range1_15MHz = 0,
Range10_30MHz = 1,
Range25_60MHz = 2,
Range40_100MHz = 3,
}
impl FrequencyRange {
#[inline]
pub const fn to_bits(self) -> u32 {
match self {
FrequencyRange::Range1_15MHz => 0xaa0, FrequencyRange::Range10_30MHz => 0xaa1, FrequencyRange::Range25_60MHz => 0xaa2, FrequencyRange::Range40_100MHz => 0xaa3, }
}
#[inline]
pub const fn from_frequency(freq_hz: u32) -> Self {
if freq_hz < 10_000_000 {
FrequencyRange::Range1_15MHz
} else if freq_hz < 25_000_000 {
FrequencyRange::Range10_30MHz
} else if freq_hz < 40_000_000 {
FrequencyRange::Range25_60MHz
} else {
FrequencyRange::Range40_100MHz
}
}
}
pub struct Xosc {
xosc: XOSC,
freq_hz: u32,
}
impl Xosc {
pub fn init(xosc: XOSC, freq_hz: u32) -> Self {
let range = FrequencyRange::from_frequency(freq_hz);
let freq_khz = freq_hz / 1000;
let startup_delay = ((freq_khz + 128) / 256) * STARTUP_DELAY_MULTIPLIER;
xosc.ctrl.write_freq_range(range.to_bits());
xosc.startup.write_delay(startup_delay);
const ENABLE_VALUE: u32 = 0xFAB; xosc.ctrl.write_enable(ENABLE_VALUE);
crate::spin::spin_wait_max(
"XOSC stable",
|| xosc.status.stable(),
XOSC_STABLE_TIMEOUT_SPINS,
);
Self { xosc, freq_hz }
}
#[inline]
pub unsafe fn steal() -> Steal<Self> {
Steal(Self {
xosc: unsafe { XOSC::steal() },
freq_hz: XOSC_HZ,
})
}
#[inline]
pub fn freq_hz(&self) -> u32 {
self.freq_hz
}
#[inline]
pub unsafe fn regs(&self) -> &rp2350_pac::xosc::RegisterBlock {
&self.xosc
}
#[inline]
pub fn release(self) -> XOSC {
self.xosc
}
#[inline]
pub fn init_default(xosc: XOSC) -> Self {
Self::init(xosc, XOSC_HZ)
}
#[inline]
pub fn stable(&self) -> bool {
self.xosc.status.stable()
}
#[inline]
pub fn is_enabled(&self) -> bool {
self.xosc.status.enabled()
}
#[inline]
pub fn wait_stable(&self) {
crate::spin::spin_wait_max("XOSC stable", || self.stable(), XOSC_STABLE_TIMEOUT_SPINS);
}
#[inline]
pub fn enable(&self) {
self.xosc.ctrl.write_enable(Ctrl::ENABLE_ENABLE_VAL);
}
pub fn disable(&self) {
self.xosc.ctrl.write_enable(Ctrl::ENABLE_DISABLE_VAL);
crate::spin::spin_wait("XOSC disable", || !self.stable());
}
pub fn dormant(&self) {
self.xosc.dormant.write_dormant(Dormant::DORMANT_DORMANT);
self.wait_stable();
}
#[inline]
pub fn wake(&mut self) {
self.xosc.dormant.write_dormant(Dormant::DORMANT_WAKE);
}
#[inline]
pub fn current_freq_range(&self) -> u32 {
self.xosc.status.freq_range()
}
#[inline]
pub fn bad_write(&self) -> bool {
self.xosc.status.badwrite()
}
#[inline]
pub fn clear_bad_write(&mut self) {
self.xosc.status.set_badwrite();
}
#[inline]
pub fn set_startup_delay(&mut self, delay: u16, multiply_by_4: bool) {
self.xosc.startup.write_delay(delay as u32);
if multiply_by_4 {
self.xosc.startup.set_x4();
} else {
self.xosc.startup.clr_x4();
}
}
#[inline]
pub fn start_count(&mut self, count: u16) {
self.xosc.count.write_count(count as u32);
}
#[inline]
pub fn count_done(&self) -> bool {
self.xosc.count.count() == 0
}
pub fn delay_cycles(&mut self, cycles: u16) {
self.start_count(cycles.max(4));
while !self.count_done() {
core::hint::spin_loop();
}
}
}