2021-04-25 18:12:38 +10:00
|
|
|
//! Phase-Locked Loops (PLL)
|
|
|
|
// See [Chapter 2 Section 18](https://datasheets.raspberrypi.org/rp2040/rp2040_datasheet.pdf) for more details
|
|
|
|
|
|
|
|
use core::{
|
2021-04-30 04:35:47 +10:00
|
|
|
convert::{Infallible, TryFrom, TryInto},
|
2021-04-25 18:12:38 +10:00
|
|
|
marker::PhantomData,
|
2021-04-30 04:35:47 +10:00
|
|
|
ops::{Deref, Range, RangeInclusive},
|
2021-04-25 18:12:38 +10:00
|
|
|
};
|
|
|
|
|
|
|
|
use embedded_time::{
|
|
|
|
fixed_point::FixedPoint,
|
2021-04-30 04:35:47 +10:00
|
|
|
rate::{Generic, Hertz, Rate},
|
2021-04-25 18:12:38 +10:00
|
|
|
};
|
|
|
|
|
|
|
|
use nb::Error::WouldBlock;
|
|
|
|
|
|
|
|
/// State of the PLL
|
|
|
|
pub trait State {}
|
|
|
|
|
2021-04-26 01:51:03 +10:00
|
|
|
/// PLL is disabled.
|
2021-04-30 05:06:11 +10:00
|
|
|
pub struct Disabled {
|
|
|
|
refdiv: u8,
|
|
|
|
fbdiv: u16,
|
|
|
|
post_div1: u8,
|
|
|
|
post_div2: u8,
|
|
|
|
}
|
2021-04-26 01:51:03 +10:00
|
|
|
|
|
|
|
/// PLL is configured, started and locking into its designated frequency.
|
|
|
|
pub struct Locking {
|
2021-04-25 18:12:38 +10:00
|
|
|
post_div1: u8,
|
2021-04-30 04:35:47 +10:00
|
|
|
post_div2: u8,
|
2021-04-25 18:12:38 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
/// PLL is locked : it delivers a steady frequency.
|
|
|
|
pub struct Locked;
|
|
|
|
|
|
|
|
impl State for Disabled {}
|
|
|
|
impl State for Locked {}
|
|
|
|
impl State for Locking {}
|
|
|
|
|
|
|
|
/// Trait to handle both underlying devices from the PAC (PLL_SYS & PLL_USB)
|
|
|
|
pub trait PhaseLockedLoopDevice: Deref<Target = rp2040_pac::pll_sys::RegisterBlock> {}
|
|
|
|
|
|
|
|
impl PhaseLockedLoopDevice for rp2040_pac::PLL_SYS {}
|
|
|
|
impl PhaseLockedLoopDevice for rp2040_pac::PLL_USB {}
|
|
|
|
|
|
|
|
/// A PLL.
|
|
|
|
pub struct PhaseLockedLoop<S: State, D: PhaseLockedLoopDevice> {
|
|
|
|
device: D,
|
2021-04-30 04:35:47 +10:00
|
|
|
state: S,
|
2021-04-25 18:12:38 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<S: State, D: PhaseLockedLoopDevice> PhaseLockedLoop<S, D> {
|
|
|
|
fn transition<To: State>(self, state: To) -> PhaseLockedLoop<To, D> {
|
|
|
|
PhaseLockedLoop {
|
|
|
|
device: self.device,
|
2021-05-05 15:55:51 +10:00
|
|
|
state,
|
2021-04-25 18:12:38 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Releases the underlying device.
|
2021-04-30 04:35:47 +10:00
|
|
|
pub fn free(self) -> D {
|
2021-04-25 18:12:38 +10:00
|
|
|
self.device
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Error type for the PLL module.
|
|
|
|
/// See Chapter 2, Section 18 §2 for details on constraints triggering these errors.
|
|
|
|
pub enum Error {
|
|
|
|
/// Proposed VCO frequency is out of range.
|
2021-05-09 17:42:31 +10:00
|
|
|
VcoFreqOutOfRange,
|
2021-04-25 18:12:38 +10:00
|
|
|
|
|
|
|
/// Feedback Divider value is out of range.
|
2021-05-09 17:42:31 +10:00
|
|
|
FeedbackDivOutOfRange,
|
2021-04-25 18:12:38 +10:00
|
|
|
|
|
|
|
/// Post Divider value is out of range.
|
|
|
|
PostDivOutOfRage,
|
|
|
|
|
|
|
|
/// Reference Frequency is out of range.
|
|
|
|
RefFreqOutOfRange,
|
|
|
|
|
|
|
|
/// Bad argument : overflows, bad conversion, ...
|
2021-04-30 04:35:47 +10:00
|
|
|
BadArgument,
|
2021-04-25 18:12:38 +10:00
|
|
|
}
|
|
|
|
|
2021-04-26 01:51:03 +10:00
|
|
|
/// Parameters for a PLL.
|
|
|
|
pub struct PLLConfig<R: Rate> {
|
|
|
|
/// Voltage Controlled Oscillator frequency.
|
|
|
|
pub vco_freq: R,
|
|
|
|
|
|
|
|
/// Reference divider
|
|
|
|
pub refdiv: u8,
|
|
|
|
|
|
|
|
/// Post Divider 1
|
|
|
|
pub post_div1: u8,
|
|
|
|
|
|
|
|
/// Post Divider 2
|
2021-04-30 04:35:47 +10:00
|
|
|
pub post_div2: u8,
|
2021-04-26 01:51:03 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Common configs for the two PLLs. Both assume the XOSC is cadenced at 12MHz !
|
|
|
|
/// See Chapter 2, Section 18, §2
|
|
|
|
pub mod common_configs {
|
|
|
|
use super::PLLConfig;
|
|
|
|
use embedded_time::rate::Megahertz;
|
|
|
|
|
|
|
|
/// Default, nominal configuration for PLL_SYS
|
|
|
|
pub const PLL_SYS_125MHZ: PLLConfig<Megahertz> = PLLConfig {
|
|
|
|
vco_freq: Megahertz(1500),
|
|
|
|
refdiv: 1,
|
|
|
|
post_div1: 6,
|
2021-04-30 04:35:47 +10:00
|
|
|
post_div2: 2,
|
2021-04-26 01:51:03 +10:00
|
|
|
};
|
|
|
|
|
|
|
|
/// Default, nominal configuration for PLL_USB.
|
|
|
|
pub const PLL_USB_48MHZ: PLLConfig<Megahertz> = PLLConfig {
|
|
|
|
vco_freq: Megahertz(480),
|
|
|
|
refdiv: 1,
|
|
|
|
post_div1: 5,
|
2021-04-30 04:35:47 +10:00
|
|
|
post_div2: 2,
|
2021-04-26 01:51:03 +10:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-04-25 18:12:38 +10:00
|
|
|
impl<D: PhaseLockedLoopDevice> PhaseLockedLoop<Disabled, D> {
|
2021-04-26 01:51:03 +10:00
|
|
|
/// Instantiates a new Phase-Locked-Loop device.
|
2021-04-30 05:06:11 +10:00
|
|
|
pub fn new<R: Rate>(
|
|
|
|
dev: D,
|
2021-04-30 04:35:47 +10:00
|
|
|
xosc_frequency: Generic<u32>,
|
|
|
|
config: PLLConfig<R>,
|
2021-04-30 05:06:11 +10:00
|
|
|
) -> Result<PhaseLockedLoop<Disabled, D>, Error>
|
2021-04-30 04:35:47 +10:00
|
|
|
where
|
|
|
|
R: Into<Hertz<u64>>,
|
|
|
|
{
|
2021-05-05 15:55:51 +10:00
|
|
|
const VCO_FREQ_RANGE: RangeInclusive<Hertz<u32>> =
|
|
|
|
Hertz(400_000_000)..=Hertz(1_600_000_000);
|
2021-04-25 18:12:38 +10:00
|
|
|
const POSTDIV_RANGE: Range<u8> = 1..7;
|
2021-04-26 01:51:03 +10:00
|
|
|
const FBDIV_RANGE: Range<u16> = 16..320;
|
2021-04-25 18:12:38 +10:00
|
|
|
|
2021-04-26 03:45:45 +10:00
|
|
|
//First we convert our rate to Hertz<u64> as all other rates can be converted to that.
|
|
|
|
let vco_freq: Hertz<u64> = config.vco_freq.into();
|
|
|
|
|
|
|
|
//Then we try to downscale to u32.
|
|
|
|
let vco_freq: Hertz<u32> = vco_freq.try_into().map_err(|_| Error::BadArgument)?;
|
2021-04-25 18:12:38 +10:00
|
|
|
|
|
|
|
if !VCO_FREQ_RANGE.contains(&vco_freq) {
|
2021-05-09 17:42:31 +10:00
|
|
|
return Err(Error::VcoFreqOutOfRange);
|
2021-04-25 18:12:38 +10:00
|
|
|
}
|
|
|
|
|
2021-04-30 04:35:47 +10:00
|
|
|
if !POSTDIV_RANGE.contains(&config.post_div1) || !POSTDIV_RANGE.contains(&config.post_div2)
|
|
|
|
{
|
|
|
|
return Err(Error::PostDivOutOfRage);
|
2021-04-25 18:12:38 +10:00
|
|
|
}
|
|
|
|
|
2021-04-26 01:51:03 +10:00
|
|
|
let ref_freq_range: Range<Hertz<u32>> = Hertz(5_000_000)..vco_freq.div(16);
|
2021-04-25 18:12:38 +10:00
|
|
|
|
2021-04-30 04:35:47 +10:00
|
|
|
let ref_freq_hz = Hertz::<u32>::try_from(xosc_frequency)
|
|
|
|
.map_err(|_| Error::BadArgument)?
|
|
|
|
.checked_div(&(config.refdiv as u32))
|
|
|
|
.ok_or(Error::BadArgument)?;
|
2021-04-25 18:12:38 +10:00
|
|
|
|
|
|
|
if !ref_freq_range.contains(&ref_freq_hz) {
|
2021-04-30 04:35:47 +10:00
|
|
|
return Err(Error::RefFreqOutOfRange);
|
2021-04-25 18:12:38 +10:00
|
|
|
}
|
|
|
|
|
2021-04-30 04:35:47 +10:00
|
|
|
let fbdiv = vco_freq
|
|
|
|
.checked_div(ref_freq_hz.integer())
|
|
|
|
.ok_or(Error::BadArgument)?;
|
2021-04-26 01:51:03 +10:00
|
|
|
|
2021-04-30 04:35:47 +10:00
|
|
|
let fbdiv: u16 = (*fbdiv.integer())
|
|
|
|
.try_into()
|
|
|
|
.map_err(|_| Error::BadArgument)?;
|
2021-04-25 18:12:38 +10:00
|
|
|
|
|
|
|
if !FBDIV_RANGE.contains(&fbdiv) {
|
2021-05-09 17:42:31 +10:00
|
|
|
return Err(Error::FeedbackDivOutOfRange);
|
2021-04-25 18:12:38 +10:00
|
|
|
}
|
|
|
|
|
2021-04-30 05:06:11 +10:00
|
|
|
let refdiv = config.refdiv;
|
|
|
|
let post_div1 = config.post_div1;
|
|
|
|
let post_div2 = config.post_div2;
|
|
|
|
|
|
|
|
Ok(PhaseLockedLoop {
|
|
|
|
state: Disabled {
|
|
|
|
refdiv,
|
|
|
|
fbdiv,
|
|
|
|
post_div1,
|
|
|
|
post_div2,
|
|
|
|
},
|
|
|
|
device: dev,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Configures and starts the PLL : it switches to Locking state.
|
|
|
|
pub fn initialize(self) -> PhaseLockedLoop<Locking, D> {
|
|
|
|
// Turn off PLL in case it is already running
|
|
|
|
self.device.pwr.reset();
|
|
|
|
self.device.fbdiv_int.reset();
|
|
|
|
|
|
|
|
self.device.cs.write(|w| unsafe {
|
|
|
|
w.refdiv().bits(self.state.refdiv);
|
|
|
|
w
|
|
|
|
});
|
|
|
|
|
2021-04-25 18:12:38 +10:00
|
|
|
self.device.fbdiv_int.write(|w| unsafe {
|
2021-04-30 05:06:11 +10:00
|
|
|
w.fbdiv_int().bits(self.state.fbdiv);
|
2021-04-25 18:12:38 +10:00
|
|
|
w
|
|
|
|
});
|
|
|
|
|
2021-04-30 04:21:02 +10:00
|
|
|
// Turn on PLL
|
2021-04-30 04:35:47 +10:00
|
|
|
self.device.pwr.modify(|_, w| {
|
2021-04-30 04:02:36 +10:00
|
|
|
w.pd().clear_bit();
|
|
|
|
w.vcopd().clear_bit();
|
2021-04-25 18:12:38 +10:00
|
|
|
w
|
|
|
|
});
|
|
|
|
|
2021-04-30 05:06:11 +10:00
|
|
|
let post_div1 = self.state.post_div1;
|
|
|
|
let post_div2 = self.state.post_div2;
|
2021-04-25 18:12:38 +10:00
|
|
|
|
2021-04-30 05:06:11 +10:00
|
|
|
self.transition(Locking {
|
2021-04-30 04:35:47 +10:00
|
|
|
post_div1,
|
|
|
|
post_div2,
|
2021-04-30 05:06:11 +10:00
|
|
|
})
|
2021-04-25 18:12:38 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A token that's given when the PLL is properly locked, so we can safely transition to the next state.
|
|
|
|
pub struct LockedPLLToken<D> {
|
2021-04-30 04:35:47 +10:00
|
|
|
_private: PhantomData<D>,
|
2021-04-25 18:12:38 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<D: PhaseLockedLoopDevice> PhaseLockedLoop<Locking, D> {
|
|
|
|
/// Awaits locking of the PLL.
|
|
|
|
pub fn await_lock(&self) -> nb::Result<LockedPLLToken<D>, Infallible> {
|
|
|
|
if self.device.cs.read().lock().bit_is_clear() {
|
|
|
|
return Err(WouldBlock);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(LockedPLLToken {
|
2021-04-30 04:35:47 +10:00
|
|
|
_private: PhantomData,
|
2021-04-25 18:12:38 +10:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Exchanges a token for a Locked PLL.
|
|
|
|
pub fn get_locked(self, _token: LockedPLLToken<D>) -> PhaseLockedLoop<Locked, D> {
|
|
|
|
// Set up post dividers
|
|
|
|
self.device.prim.write(|w| unsafe {
|
|
|
|
w.postdiv1().bits(self.state.post_div1);
|
|
|
|
w.postdiv2().bits(self.state.post_div2);
|
|
|
|
w
|
|
|
|
});
|
|
|
|
|
|
|
|
// Turn on post divider
|
2021-04-30 04:35:47 +10:00
|
|
|
self.device.pwr.modify(|_, w| {
|
2021-04-30 04:02:36 +10:00
|
|
|
w.postdivpd().clear_bit();
|
2021-04-25 18:12:38 +10:00
|
|
|
w
|
|
|
|
});
|
|
|
|
|
|
|
|
self.transition(Locked)
|
|
|
|
}
|
|
|
|
}
|