2021-06-25 03:38:03 +02:00
|
|
|
use pac::clocks::vals::*;
|
|
|
|
|
|
|
|
use crate::{pac, reset};
|
|
|
|
|
|
|
|
const XOSC_MHZ: u32 = 12;
|
|
|
|
|
2021-07-12 02:45:42 +02:00
|
|
|
/// safety: must be called exactly once at bootup
|
2022-11-30 02:46:04 +01:00
|
|
|
pub(crate) unsafe fn init() {
|
2021-06-25 03:38:03 +02:00
|
|
|
// Reset everything except:
|
|
|
|
// - QSPI (we're using it to run this code!)
|
|
|
|
// - PLLs (it may be suicide if that's what's clocking us)
|
2021-06-30 23:45:07 +02:00
|
|
|
// - USB, SYSCFG (breaks usb-to-swd on core1)
|
2021-06-25 03:38:03 +02:00
|
|
|
let mut peris = reset::ALL_PERIPHERALS;
|
|
|
|
peris.set_io_qspi(false);
|
|
|
|
peris.set_pads_qspi(false);
|
|
|
|
peris.set_pll_sys(false);
|
|
|
|
peris.set_pll_usb(false);
|
2021-06-30 23:45:07 +02:00
|
|
|
peris.set_usbctrl(false);
|
|
|
|
peris.set_syscfg(false);
|
2021-06-25 03:38:03 +02:00
|
|
|
reset::reset(peris);
|
|
|
|
|
2021-06-30 23:45:07 +02:00
|
|
|
// Remove reset from peripherals which are clocked only by clk_sys and
|
|
|
|
// clk_ref. Other peripherals stay in reset until we've configured clocks.
|
2021-06-25 03:38:03 +02:00
|
|
|
let mut peris = reset::ALL_PERIPHERALS;
|
|
|
|
peris.set_adc(false);
|
|
|
|
peris.set_rtc(false);
|
|
|
|
peris.set_spi0(false);
|
|
|
|
peris.set_spi1(false);
|
|
|
|
peris.set_uart0(false);
|
|
|
|
peris.set_uart1(false);
|
|
|
|
peris.set_usbctrl(false);
|
|
|
|
reset::unreset_wait(peris);
|
|
|
|
|
2021-06-30 23:45:07 +02:00
|
|
|
// Start tick in watchdog
|
2021-06-25 03:38:03 +02:00
|
|
|
// xosc 12 mhz
|
|
|
|
pac::WATCHDOG.tick().write(|w| {
|
|
|
|
w.set_cycles(XOSC_MHZ as u16);
|
|
|
|
w.set_enable(true);
|
|
|
|
});
|
|
|
|
|
2021-06-30 23:45:07 +02:00
|
|
|
// Disable resus that may be enabled from previous software
|
2021-06-25 03:38:03 +02:00
|
|
|
let c = pac::CLOCKS;
|
|
|
|
c.clk_sys_resus_ctrl()
|
|
|
|
.write_value(pac::clocks::regs::ClkSysResusCtrl(0));
|
|
|
|
|
2021-06-30 23:45:07 +02:00
|
|
|
// start XOSC
|
|
|
|
start_xosc();
|
2021-06-25 03:38:03 +02:00
|
|
|
|
|
|
|
// Before we touch PLLs, switch sys and ref cleanly away from their aux sources.
|
2022-06-12 22:15:44 +02:00
|
|
|
c.clk_sys_ctrl().modify(|w| w.set_src(ClkSysCtrlSrc::CLK_REF));
|
2021-06-25 03:38:03 +02:00
|
|
|
while c.clk_sys_selected().read() != 1 {}
|
2022-06-12 22:15:44 +02:00
|
|
|
c.clk_ref_ctrl().modify(|w| w.set_src(ClkRefCtrlSrc::ROSC_CLKSRC_PH));
|
2021-06-25 03:38:03 +02:00
|
|
|
while c.clk_ref_selected().read() != 1 {}
|
|
|
|
|
|
|
|
// Configure PLLs
|
|
|
|
// REF FBDIV VCO POSTDIV
|
|
|
|
// PLL SYS: 12 / 1 = 12MHz * 125 = 1500MHZ / 6 / 2 = 125MHz
|
|
|
|
// PLL USB: 12 / 1 = 12MHz * 40 = 480 MHz / 5 / 2 = 48MHz
|
|
|
|
configure_pll(pac::PLL_SYS, 1, 1500_000_000, 6, 2);
|
|
|
|
configure_pll(pac::PLL_USB, 1, 480_000_000, 5, 2);
|
|
|
|
|
|
|
|
// CLK_REF = XOSC (12MHz) / 1 = 12MHz2Mhz
|
|
|
|
c.clk_ref_ctrl().write(|w| {
|
|
|
|
w.set_src(ClkRefCtrlSrc::XOSC_CLKSRC);
|
|
|
|
});
|
|
|
|
while c.clk_ref_selected().read() != 1 << ClkRefCtrlSrc::XOSC_CLKSRC.0 {}
|
|
|
|
c.clk_ref_div().write(|w| w.set_int(1));
|
|
|
|
|
|
|
|
// CLK SYS = PLL SYS (125MHz) / 1 = 125MHz
|
|
|
|
c.clk_sys_ctrl().write(|w| {
|
|
|
|
w.set_src(ClkSysCtrlSrc::CLK_REF);
|
|
|
|
});
|
|
|
|
while c.clk_sys_selected().read() != 1 << ClkSysCtrlSrc::CLK_REF.0 {}
|
|
|
|
c.clk_sys_div().write(|w| w.set_int(1));
|
|
|
|
c.clk_sys_ctrl().write(|w| {
|
|
|
|
w.set_auxsrc(ClkSysCtrlAuxsrc::CLKSRC_PLL_SYS);
|
|
|
|
w.set_src(ClkSysCtrlSrc::CLKSRC_CLK_SYS_AUX);
|
|
|
|
});
|
|
|
|
while c.clk_sys_selected().read() != 1 << ClkSysCtrlSrc::CLKSRC_CLK_SYS_AUX.0 {}
|
|
|
|
|
|
|
|
// CLK USB = PLL USB (48MHz) / 1 = 48MHz
|
|
|
|
c.clk_usb_div().write(|w| w.set_int(1));
|
|
|
|
c.clk_usb_ctrl().write(|w| {
|
|
|
|
w.set_enable(true);
|
|
|
|
w.set_auxsrc(ClkUsbCtrlAuxsrc::CLKSRC_PLL_USB);
|
|
|
|
});
|
|
|
|
|
|
|
|
// CLK ADC = PLL USB (48MHZ) / 1 = 48MHz
|
|
|
|
c.clk_adc_div().write(|w| w.set_int(1));
|
|
|
|
c.clk_adc_ctrl().write(|w| {
|
|
|
|
w.set_enable(true);
|
|
|
|
w.set_auxsrc(ClkAdcCtrlAuxsrc::CLKSRC_PLL_USB);
|
|
|
|
});
|
|
|
|
|
|
|
|
// CLK RTC = PLL USB (48MHz) / 1024 = 46875Hz
|
|
|
|
c.clk_rtc_ctrl().modify(|w| {
|
|
|
|
w.set_enable(false);
|
|
|
|
});
|
|
|
|
c.clk_rtc_div().write(|w| w.set_int(1024));
|
|
|
|
c.clk_rtc_ctrl().write(|w| {
|
|
|
|
w.set_enable(true);
|
|
|
|
w.set_auxsrc(ClkRtcCtrlAuxsrc::CLKSRC_PLL_USB);
|
|
|
|
});
|
|
|
|
|
|
|
|
// CLK PERI = clk_sys. Used as reference clock for Peripherals. No dividers so just select and enable
|
|
|
|
// Normally choose clk_sys or clk_usb
|
|
|
|
c.clk_peri_ctrl().write(|w| {
|
|
|
|
w.set_enable(true);
|
|
|
|
w.set_auxsrc(ClkPeriCtrlAuxsrc::CLK_SYS);
|
|
|
|
});
|
2021-06-30 23:45:07 +02:00
|
|
|
|
|
|
|
// Peripheral clocks should now all be running
|
|
|
|
let peris = reset::ALL_PERIPHERALS;
|
|
|
|
reset::unreset_wait(peris);
|
2021-06-25 03:38:03 +02:00
|
|
|
}
|
|
|
|
|
2021-07-04 04:42:39 +02:00
|
|
|
pub(crate) fn _clk_sys_freq() -> u32 {
|
2021-06-25 03:38:03 +02:00
|
|
|
125_000_000
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn clk_peri_freq() -> u32 {
|
|
|
|
125_000_000
|
|
|
|
}
|
|
|
|
|
2022-09-16 06:45:27 +02:00
|
|
|
pub(crate) fn clk_rtc_freq() -> u32 {
|
2021-06-25 03:38:03 +02:00
|
|
|
46875
|
|
|
|
}
|
|
|
|
|
2021-06-30 23:45:07 +02:00
|
|
|
unsafe fn start_xosc() {
|
|
|
|
const XOSC_MHZ: u32 = 12;
|
|
|
|
pac::XOSC
|
|
|
|
.ctrl()
|
|
|
|
.write(|w| w.set_freq_range(pac::xosc::vals::CtrlFreqRange::_1_15MHZ));
|
|
|
|
|
|
|
|
let startup_delay = (((XOSC_MHZ * 1_000_000) / 1000) + 128) / 256;
|
2022-06-12 22:15:44 +02:00
|
|
|
pac::XOSC.startup().write(|w| w.set_delay(startup_delay as u16));
|
2021-06-30 23:45:07 +02:00
|
|
|
pac::XOSC.ctrl().write(|w| {
|
|
|
|
w.set_freq_range(pac::xosc::vals::CtrlFreqRange::_1_15MHZ);
|
|
|
|
w.set_enable(pac::xosc::vals::Enable::ENABLE);
|
|
|
|
});
|
|
|
|
while !pac::XOSC.status().read().stable() {}
|
|
|
|
}
|
|
|
|
|
2022-06-12 22:15:44 +02:00
|
|
|
unsafe fn configure_pll(p: pac::pll::Pll, refdiv: u32, vco_freq: u32, post_div1: u8, post_div2: u8) {
|
2021-06-25 06:23:20 +02:00
|
|
|
let ref_freq = XOSC_MHZ * 1_000_000 / refdiv;
|
2021-06-25 03:38:03 +02:00
|
|
|
|
2021-06-25 06:23:20 +02:00
|
|
|
let fbdiv = vco_freq / ref_freq;
|
|
|
|
assert!(fbdiv >= 16 && fbdiv <= 320);
|
|
|
|
assert!(post_div1 >= 1 && post_div1 <= 7);
|
|
|
|
assert!(post_div2 >= 1 && post_div2 <= 7);
|
2021-06-25 03:38:03 +02:00
|
|
|
assert!(post_div2 <= post_div1);
|
2021-06-25 06:23:20 +02:00
|
|
|
assert!(ref_freq <= (vco_freq / 16));
|
2021-06-25 03:38:03 +02:00
|
|
|
|
2021-06-30 23:45:07 +02:00
|
|
|
// do not disrupt PLL that is already correctly configured and operating
|
|
|
|
let cs = p.cs().read();
|
|
|
|
let prim = p.prim().read();
|
|
|
|
if cs.lock()
|
2023-04-21 00:57:28 +02:00
|
|
|
&& cs.refdiv() == refdiv as u8
|
|
|
|
&& p.fbdiv_int().read().fbdiv_int() == fbdiv as u16
|
2021-06-30 23:45:07 +02:00
|
|
|
&& prim.postdiv1() == post_div1
|
|
|
|
&& prim.postdiv2() == post_div2
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reset it
|
|
|
|
let mut peris = reset::Peripherals(0);
|
|
|
|
match p {
|
|
|
|
pac::PLL_SYS => peris.set_pll_sys(true),
|
|
|
|
pac::PLL_USB => peris.set_pll_usb(true),
|
|
|
|
_ => unreachable!(),
|
|
|
|
}
|
|
|
|
reset::reset(peris);
|
|
|
|
reset::unreset_wait(peris);
|
|
|
|
|
|
|
|
// Load VCO-related dividers before starting VCO
|
2021-06-25 06:23:20 +02:00
|
|
|
p.cs().write(|w| w.set_refdiv(refdiv as _));
|
2021-06-25 03:38:03 +02:00
|
|
|
p.fbdiv_int().write(|w| w.set_fbdiv_int(fbdiv as _));
|
|
|
|
|
2021-06-30 23:45:07 +02:00
|
|
|
// Turn on PLL
|
2021-06-25 03:38:03 +02:00
|
|
|
p.pwr().modify(|w| {
|
|
|
|
w.set_pd(false);
|
|
|
|
w.set_vcopd(false);
|
2021-06-30 23:45:07 +02:00
|
|
|
w.set_postdivpd(true);
|
2021-06-25 03:38:03 +02:00
|
|
|
});
|
|
|
|
|
2021-06-30 23:45:07 +02:00
|
|
|
// Wait for PLL to lock
|
2021-06-25 03:38:03 +02:00
|
|
|
while !p.cs().read().lock() {}
|
|
|
|
|
2021-06-30 23:45:07 +02:00
|
|
|
// Wait for PLL to lock
|
2021-06-25 03:38:03 +02:00
|
|
|
p.prim().write(|w| {
|
|
|
|
w.set_postdiv1(post_div1);
|
|
|
|
w.set_postdiv2(post_div2);
|
|
|
|
});
|
|
|
|
|
2021-06-30 23:45:07 +02:00
|
|
|
// Turn on post divider
|
2021-06-25 03:38:03 +02:00
|
|
|
p.pwr().modify(|w| w.set_postdivpd(false));
|
|
|
|
}
|
2022-11-30 02:46:04 +01:00
|
|
|
|
|
|
|
/// Random number generator based on the ROSC RANDOMBIT register.
|
|
|
|
///
|
|
|
|
/// This will not produce random values if the ROSC is stopped or run at some
|
|
|
|
/// harmonic of the bus frequency. With default clock settings these are not
|
|
|
|
/// issues.
|
|
|
|
pub struct RoscRng;
|
|
|
|
|
|
|
|
impl RoscRng {
|
|
|
|
fn next_u8() -> u8 {
|
|
|
|
let random_reg = pac::ROSC.randombit();
|
|
|
|
let mut acc = 0;
|
|
|
|
for _ in 0..u8::BITS {
|
|
|
|
acc <<= 1;
|
|
|
|
acc |= unsafe { random_reg.read().randombit() as u8 };
|
|
|
|
}
|
|
|
|
acc
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl rand_core::RngCore for RoscRng {
|
|
|
|
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), rand_core::Error> {
|
|
|
|
Ok(self.fill_bytes(dest))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn next_u32(&mut self) -> u32 {
|
|
|
|
rand_core::impls::next_u32_via_fill(self)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn next_u64(&mut self) -> u64 {
|
|
|
|
rand_core::impls::next_u64_via_fill(self)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn fill_bytes(&mut self, dest: &mut [u8]) {
|
|
|
|
dest.fill_with(Self::next_u8)
|
|
|
|
}
|
|
|
|
}
|