use crate::Steal;
use rp2350_pac::{PLL_SYS, PLL_USB};
macro_rules! impl_pll {
($Pll:ident, $PLL:ident, $pll_mod:ident) => {
pub struct $Pll {
pll: $PLL,
}
impl $Pll {
#[inline]
pub fn init(pll: $PLL) -> Self {
Self { pll }
}
#[inline]
pub unsafe fn steal() -> Steal<Self> {
Steal(Self {
pll: unsafe { $PLL::steal() },
})
}
#[inline]
pub unsafe fn regs(&self) -> &rp2350_pac::$pll_mod::RegisterBlock {
&self.pll
}
#[inline]
pub fn release(self) -> $PLL {
self.pll
}
#[inline]
pub fn is_locked(&self) -> bool {
self.pll.cs.lock()
}
#[inline]
pub fn is_lock_n(&self) -> bool {
self.pll.cs.lock_n()
}
#[inline]
pub fn clear_lock_n(&mut self) {
self.pll.cs.set_lock_n();
}
#[inline]
pub fn is_bypass(&self) -> bool {
self.pll.cs.bypass()
}
#[inline]
pub fn set_bypass(&mut self, bypass: bool) {
if bypass {
self.pll.cs.set_bypass();
} else {
self.pll.cs.clr_bypass();
}
}
#[inline]
pub fn refdiv(&self) -> u8 {
self.pll.cs.refdiv() as u8
}
#[inline]
pub fn set_refdiv(&mut self, div: u8) {
let div = div.clamp(1, 63) as u32;
self.pll.cs.write_refdiv(div);
}
#[inline]
pub fn power_down(&mut self) {
self.pll.pwr.set_pd();
}
#[inline]
pub fn power_up(&mut self) {
self.pll.pwr.clr_pd();
}
#[inline]
pub fn is_powered_down(&self) -> bool {
self.pll.pwr.pd()
}
#[inline]
pub fn vco_power_down(&mut self) {
self.pll.pwr.set_vcopd();
}
#[inline]
pub fn vco_power_up(&mut self) {
self.pll.pwr.clr_vcopd();
}
#[inline]
pub fn is_vco_powered_down(&self) -> bool {
self.pll.pwr.vcopd()
}
#[inline]
pub fn postdiv_power_down(&mut self) {
self.pll.pwr.set_postdivpd();
}
#[inline]
pub fn postdiv_power_up(&mut self) {
self.pll.pwr.clr_postdivpd();
}
#[inline]
pub fn is_postdiv_powered_down(&self) -> bool {
self.pll.pwr.postdivpd()
}
#[inline]
pub fn dsm_power_down(&mut self) {
self.pll.pwr.set_dsmpd();
}
#[inline]
pub fn dsm_power_up(&mut self) {
self.pll.pwr.clr_dsmpd();
}
#[inline]
pub fn fbdiv(&self) -> u16 {
self.pll.fbdiv_int.fbdiv_int() as u16
}
#[inline]
pub fn set_fbdiv(&mut self, div: u16) {
let div = div.clamp(16, 320) as u32;
self.pll.fbdiv_int.write_fbdiv_int(div);
}
#[inline]
pub fn postdiv1(&self) -> u8 {
self.pll.prim.postdiv1() as u8
}
#[inline]
pub fn postdiv2(&self) -> u8 {
self.pll.prim.postdiv2() as u8
}
#[inline]
pub fn set_postdiv(&mut self, postdiv1: u8, postdiv2: u8) {
let postdiv1 = postdiv1.clamp(1, 7) as u32;
let postdiv2 = postdiv2.clamp(1, 7) as u32;
self.pll.prim.write_postdiv1(postdiv1);
self.pll.prim.write_postdiv2(postdiv2);
}
#[inline]
pub fn is_intr_lock_n(&self) -> bool {
self.pll.intr.lock_n_sticky()
}
#[inline]
pub fn clear_intr_lock_n(&mut self) {
self.pll.intr.set_lock_n_sticky();
}
#[inline]
pub fn enable_intr_lock_n(&mut self) {
self.pll.inte.set_lock_n_sticky();
}
#[inline]
pub fn disable_intr_lock_n(&mut self) {
self.pll.inte.clr_lock_n_sticky();
}
#[inline]
pub fn is_intr_lock_n_enabled(&self) -> bool {
self.pll.inte.lock_n_sticky()
}
#[inline]
pub fn is_ints_lock_n(&self) -> bool {
self.pll.ints.lock_n_sticky()
}
pub fn configure(&mut self, refdiv: u8, fbdiv: u16, postdiv1: u8, postdiv2: u8) {
self.set_refdiv(refdiv);
self.set_fbdiv(fbdiv);
let power_on_mask = (1 << 0) | (1 << 5); self.pll.pwr.clr_bits(power_on_mask);
crate::spin::spin_wait("PLL lock", || self.is_locked());
self.set_postdiv(postdiv1, postdiv2);
self.postdiv_power_up();
self.clear_lock_n();
}
#[inline]
pub fn wait_lock(&self) {
while !self.is_locked() {
rp2350_rt::asm::nop();
}
}
pub fn disable(&mut self) {
self.postdiv_power_down();
self.vco_power_down();
self.power_down();
}
}
};
}
impl_pll!(PllSys, PLL_SYS, pll_sys);
impl_pll!(PllUsb, PLL_USB, pll_usb);
pub struct PllConfig {
pub refdiv: u8,
pub fbdiv: u16,
pub postdiv1: u8,
pub postdiv2: u8,
}
impl PllConfig {
pub const fn sys_125mhz() -> Self {
Self {
refdiv: 1,
fbdiv: 125,
postdiv1: 6,
postdiv2: 2,
}
}
pub const fn sys_133mhz() -> Self {
Self {
refdiv: 1,
fbdiv: 133,
postdiv1: 6,
postdiv2: 2,
}
}
pub const fn sys_150mhz() -> Self {
Self {
refdiv: 1,
fbdiv: 125,
postdiv1: 5,
postdiv2: 2,
}
}
pub const fn usb_48mhz() -> Self {
Self {
refdiv: 1,
fbdiv: 100,
postdiv1: 5,
postdiv2: 5,
}
}
pub const fn vco_freq_khz(&self, xosc_khz: u32) -> u32 {
(xosc_khz / self.refdiv as u32) * self.fbdiv as u32
}
pub const fn output_freq_khz(&self, xosc_khz: u32) -> u32 {
self.vco_freq_khz(xosc_khz) / (self.postdiv1 as u32 * self.postdiv2 as u32)
}
}