2021-04-24 21:38:17 +10:00
|
|
|
//! Crystal Oscillator (XOSC)
|
|
|
|
// See [Chapter 2 Section 16](https://datasheets.raspberrypi.org/rp2040/rp2040_datasheet.pdf) for more details
|
|
|
|
|
2021-04-25 07:38:49 +10:00
|
|
|
use core::convert::TryInto;
|
2021-04-29 11:15:41 +10:00
|
|
|
use core::{convert::Infallible, ops::RangeInclusive};
|
2021-04-24 21:38:17 +10:00
|
|
|
|
|
|
|
use embedded_time::{
|
2021-04-29 11:15:41 +10:00
|
|
|
duration::{Duration, Milliseconds},
|
2021-04-24 21:38:17 +10:00
|
|
|
fixed_point::FixedPoint,
|
2021-04-29 11:15:41 +10:00
|
|
|
fraction::Fraction,
|
|
|
|
rate::{Hertz, Megahertz, Rate},
|
2021-04-24 21:38:17 +10:00
|
|
|
};
|
|
|
|
|
|
|
|
use nb::Error::WouldBlock;
|
|
|
|
|
|
|
|
/// State of the Crystal Oscillator (typestate trait)
|
|
|
|
pub trait State {}
|
|
|
|
|
|
|
|
/// XOSC is disabled (typestate)
|
|
|
|
pub struct Disabled;
|
|
|
|
|
|
|
|
/// XOSC is initialized, ie we've given parameters (typestate)
|
|
|
|
pub struct Initialized {
|
2021-04-29 11:15:41 +10:00
|
|
|
freq_hz: Hertz,
|
2021-04-24 21:38:17 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Stable state (typestate)
|
2021-04-29 11:15:41 +10:00
|
|
|
pub struct Stable {
|
|
|
|
freq_hz: Hertz,
|
2021-04-24 21:38:17 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
/// XOSC is in dormant mode (see Chapter 2, Section 16, §5)
|
|
|
|
pub struct Dormant;
|
|
|
|
|
|
|
|
impl State for Disabled {}
|
|
|
|
impl State for Initialized {}
|
|
|
|
impl State for Stable {}
|
|
|
|
impl State for Dormant {}
|
|
|
|
|
|
|
|
/// Possible errors when initializing the CrystalOscillator
|
|
|
|
pub enum Error {
|
|
|
|
/// Frequency is out of the 1-15MHz range (see datasheet)
|
|
|
|
FrequencyOutOfRange,
|
|
|
|
|
|
|
|
/// Argument is bad : overflows, ...
|
2021-04-29 11:15:41 +10:00
|
|
|
BadArgument,
|
2021-04-24 21:38:17 +10:00
|
|
|
}
|
|
|
|
|
2021-04-26 00:34:48 +10:00
|
|
|
/// Blocking helper method to setup the XOSC without going through all the steps.
|
2021-04-29 11:15:41 +10:00
|
|
|
pub fn setup_xosc_blocking(
|
|
|
|
xosc_dev: rp2040_pac::XOSC,
|
|
|
|
frequency: Hertz,
|
|
|
|
) -> Result<CrystalOscillator<Stable>, Error> {
|
2021-04-26 00:34:48 +10:00
|
|
|
let initialized_xosc = CrystalOscillator::new(xosc_dev).initialize(frequency)?;
|
|
|
|
|
|
|
|
let stable_xosc_token = nb::block!(initialized_xosc.await_stabilization()).unwrap();
|
|
|
|
|
|
|
|
Ok(initialized_xosc.get_stable(stable_xosc_token))
|
|
|
|
}
|
|
|
|
|
2021-04-24 21:38:17 +10:00
|
|
|
/// A Crystal Oscillator.
|
|
|
|
pub struct CrystalOscillator<S: State> {
|
|
|
|
device: rp2040_pac::XOSC,
|
2021-04-29 11:15:41 +10:00
|
|
|
state: S,
|
2021-04-24 21:38:17 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<S: State> CrystalOscillator<S> {
|
|
|
|
/// Transitions the oscillator to another state.
|
|
|
|
fn transition<To: State>(self, state: To) -> CrystalOscillator<To> {
|
|
|
|
CrystalOscillator {
|
|
|
|
device: self.device,
|
2021-04-29 11:15:41 +10:00
|
|
|
state: state,
|
2021-04-24 21:38:17 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Releases the underlying device.
|
|
|
|
pub fn free(self) -> rp2040_pac::XOSC {
|
|
|
|
self.device
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl CrystalOscillator<Disabled> {
|
|
|
|
/// Creates a new CrystalOscillator from the underlying device.
|
|
|
|
pub fn new(dev: rp2040_pac::XOSC) -> Self {
|
|
|
|
CrystalOscillator {
|
|
|
|
device: dev,
|
2021-04-29 11:15:41 +10:00
|
|
|
state: Disabled,
|
2021-04-24 21:38:17 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Initializes the XOSC : frequency range is set, startup delay is calculated and set.
|
2021-04-25 07:38:49 +10:00
|
|
|
pub fn initialize(self, frequency: Hertz) -> Result<CrystalOscillator<Initialized>, Error> {
|
2021-04-29 11:15:41 +10:00
|
|
|
const ALLOWED_FREQUENCY_RANGE: RangeInclusive<Megahertz<u32>> =
|
|
|
|
Megahertz(1)..=Megahertz(15);
|
2021-04-25 07:38:49 +10:00
|
|
|
const STABLE_DELAY: Milliseconds = Milliseconds(1_u32);
|
2021-04-25 08:36:46 +10:00
|
|
|
const DIVIDER: Fraction = Fraction::new(256, 1);
|
2021-04-24 21:38:17 +10:00
|
|
|
|
2021-04-25 16:58:43 +10:00
|
|
|
let freq_mhz: Megahertz = frequency.into();
|
|
|
|
|
|
|
|
if !ALLOWED_FREQUENCY_RANGE.contains(&freq_mhz) {
|
2021-04-29 11:15:41 +10:00
|
|
|
return Err(Error::FrequencyOutOfRange);
|
2021-04-24 21:38:17 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
self.device.ctrl.write(|w| {
|
|
|
|
w.freq_range()._1_15mhz();
|
|
|
|
w
|
|
|
|
});
|
|
|
|
|
2021-04-25 08:36:46 +10:00
|
|
|
//1 ms = 10e-3 sec and Freq = 1/T where T is in seconds so 1ms converts to 1000Hz
|
2021-04-29 11:15:41 +10:00
|
|
|
let delay_to_hz: Hertz = STABLE_DELAY.to_rate().map_err(|_| Error::BadArgument)?;
|
2021-04-24 21:38:17 +10:00
|
|
|
|
2021-04-25 08:36:46 +10:00
|
|
|
//startup_delay = ((freq_hz * 10e-3) / 256) = ((freq_hz / 1000) / 256)
|
|
|
|
//See Chapter 2, Section 16, §3)
|
2021-04-25 07:38:49 +10:00
|
|
|
//We do the calculation first.
|
2021-04-29 11:15:41 +10:00
|
|
|
let startup_delay = frequency
|
|
|
|
.checked_div(delay_to_hz.integer())
|
|
|
|
.and_then(|r| r.to_generic::<u32>(DIVIDER).ok())
|
|
|
|
.ok_or(Error::BadArgument)?;
|
2021-04-24 21:38:17 +10:00
|
|
|
|
2021-04-25 07:38:49 +10:00
|
|
|
//Then we check if it fits into an u16.
|
2021-04-29 11:15:41 +10:00
|
|
|
let startup_delay: u16 = (*startup_delay.integer())
|
|
|
|
.try_into()
|
|
|
|
.map_err(|_| Error::BadArgument)?;
|
2021-04-25 07:38:49 +10:00
|
|
|
|
2021-04-24 21:38:17 +10:00
|
|
|
self.device.startup.write(|w| unsafe {
|
2021-04-25 16:58:43 +10:00
|
|
|
w.delay().bits(startup_delay);
|
2021-04-24 21:38:17 +10:00
|
|
|
w
|
|
|
|
});
|
|
|
|
|
|
|
|
self.device.ctrl.write(|w| {
|
|
|
|
w.enable().enable();
|
|
|
|
w
|
|
|
|
});
|
|
|
|
|
2021-04-29 11:15:41 +10:00
|
|
|
Ok(self.transition(Initialized { freq_hz: frequency }))
|
2021-04-24 21:38:17 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A token that's given when the oscillator is stablilzed, and can be exchanged to proceed to the next stage.
|
|
|
|
pub struct StableOscillatorToken {
|
2021-04-29 11:15:41 +10:00
|
|
|
_private: (),
|
2021-04-24 21:38:17 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
impl CrystalOscillator<Initialized> {
|
|
|
|
/// One has to wait for the startup delay before using the oscillator, ie awaiting stablilzation of the XOSC
|
|
|
|
pub fn await_stabilization(&self) -> nb::Result<StableOscillatorToken, Infallible> {
|
|
|
|
if self.device.status.read().stable().bit_is_clear() {
|
2021-04-29 11:15:41 +10:00
|
|
|
return Err(WouldBlock);
|
2021-04-24 21:38:17 +10:00
|
|
|
}
|
|
|
|
|
2021-04-29 11:15:41 +10:00
|
|
|
Ok(StableOscillatorToken { _private: () })
|
2021-04-24 21:38:17 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the stablilzed oscillator
|
|
|
|
pub fn get_stable(self, _token: StableOscillatorToken) -> CrystalOscillator<Stable> {
|
|
|
|
let freq_hz = self.state.freq_hz;
|
2021-04-29 11:15:41 +10:00
|
|
|
self.transition(Stable { freq_hz })
|
2021-04-24 21:38:17 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl CrystalOscillator<Stable> {
|
|
|
|
/// Operating frequency of the XOSC in hertz
|
2021-04-25 07:38:49 +10:00
|
|
|
pub fn operating_frequency(&self) -> Hertz {
|
2021-04-24 21:38:17 +10:00
|
|
|
self.state.freq_hz
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Disables the XOSC
|
2021-04-25 07:38:49 +10:00
|
|
|
pub fn disable(self) -> CrystalOscillator<Disabled> {
|
2021-04-29 11:15:41 +10:00
|
|
|
self.device.ctrl.modify(|_r, w| {
|
2021-04-24 21:38:17 +10:00
|
|
|
w.enable().disable();
|
|
|
|
w
|
|
|
|
});
|
|
|
|
|
2021-04-25 07:38:49 +10:00
|
|
|
self.transition(Disabled)
|
2021-04-24 21:38:17 +10:00
|
|
|
}
|
|
|
|
|
2021-04-25 07:38:49 +10:00
|
|
|
/// Put the XOSC in DORMANT state.
|
|
|
|
/// This method is marked unsafe because prior to switch the XOSC into DORMANT state,
|
|
|
|
/// PLLs must be stopped and IRQs have to be properly configured.
|
|
|
|
/// This method does not do any of that, it merely switches the XOSC to DORMANT state.
|
|
|
|
/// See Chapter 2, Section 16, §5) for details.
|
|
|
|
pub unsafe fn dormant(self) -> CrystalOscillator<Dormant> {
|
2021-04-24 21:38:17 +10:00
|
|
|
//taken from the C SDK
|
|
|
|
const XOSC_DORMANT_VALUE: u32 = 0x636f6d61;
|
|
|
|
|
2021-04-29 11:11:11 +10:00
|
|
|
self.device.dormant.write(|w| {
|
2021-04-24 21:38:17 +10:00
|
|
|
w.bits(XOSC_DORMANT_VALUE);
|
|
|
|
w
|
|
|
|
});
|
|
|
|
|
|
|
|
self.transition(Dormant)
|
|
|
|
}
|
|
|
|
}
|