2023-09-15 01:53:27 +02:00
|
|
|
use super::bd::BackupDomain;
|
2023-08-27 15:35:13 +02:00
|
|
|
pub use super::bus::{AHBPrescaler, APBPrescaler};
|
2023-09-15 01:53:27 +02:00
|
|
|
use super::RtcClockSource;
|
2022-01-04 23:58:13 +01:00
|
|
|
use crate::pac::rcc::vals::{Hpre, Msirange, Plldiv, Pllmul, Pllsrc, Ppre, Sw};
|
2022-02-24 05:57:17 +01:00
|
|
|
use crate::pac::RCC;
|
|
|
|
#[cfg(crs)]
|
2023-06-27 23:49:30 +02:00
|
|
|
use crate::pac::{crs, CRS, SYSCFG};
|
2022-01-04 23:58:13 +01:00
|
|
|
use crate::rcc::{set_freqs, Clocks};
|
2022-07-11 00:36:10 +02:00
|
|
|
use crate::time::Hertz;
|
2021-05-25 13:32:10 +02:00
|
|
|
|
2022-07-10 19:59:36 +02:00
|
|
|
/// HSI speed
|
|
|
|
pub const HSI_FREQ: Hertz = Hertz(16_000_000);
|
|
|
|
|
|
|
|
/// LSI speed
|
|
|
|
pub const LSI_FREQ: Hertz = Hertz(32_000);
|
2021-06-11 17:45:07 +02:00
|
|
|
|
2021-05-25 17:09:01 +02:00
|
|
|
/// System clock mux source
|
|
|
|
#[derive(Clone, Copy)]
|
|
|
|
pub enum ClockSrc {
|
|
|
|
MSI(MSIRange),
|
|
|
|
PLL(PLLSource, PLLMul, PLLDiv),
|
|
|
|
HSE(Hertz),
|
|
|
|
HSI16,
|
|
|
|
}
|
|
|
|
|
2021-11-28 16:46:08 +01:00
|
|
|
/// MSI Clock Range
|
|
|
|
///
|
|
|
|
/// These ranges control the frequency of the MSI. Internally, these ranges map
|
|
|
|
/// to the `MSIRANGE` bits in the `RCC_ICSCR` register.
|
|
|
|
#[derive(Clone, Copy)]
|
|
|
|
pub enum MSIRange {
|
|
|
|
/// Around 65.536 kHz
|
|
|
|
Range0,
|
|
|
|
/// Around 131.072 kHz
|
|
|
|
Range1,
|
|
|
|
/// Around 262.144 kHz
|
|
|
|
Range2,
|
|
|
|
/// Around 524.288 kHz
|
|
|
|
Range3,
|
|
|
|
/// Around 1.048 MHz
|
|
|
|
Range4,
|
|
|
|
/// Around 2.097 MHz (reset value)
|
|
|
|
Range5,
|
|
|
|
/// Around 4.194 MHz
|
|
|
|
Range6,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for MSIRange {
|
|
|
|
fn default() -> MSIRange {
|
|
|
|
MSIRange::Range5
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// PLL divider
|
|
|
|
#[derive(Clone, Copy)]
|
|
|
|
pub enum PLLDiv {
|
|
|
|
Div2,
|
|
|
|
Div3,
|
|
|
|
Div4,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// PLL multiplier
|
|
|
|
#[derive(Clone, Copy)]
|
|
|
|
pub enum PLLMul {
|
|
|
|
Mul3,
|
|
|
|
Mul4,
|
|
|
|
Mul6,
|
|
|
|
Mul8,
|
|
|
|
Mul12,
|
|
|
|
Mul16,
|
|
|
|
Mul24,
|
|
|
|
Mul32,
|
|
|
|
Mul48,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// PLL clock input source
|
|
|
|
#[derive(Clone, Copy)]
|
|
|
|
pub enum PLLSource {
|
|
|
|
HSI16,
|
|
|
|
HSE(Hertz),
|
|
|
|
}
|
|
|
|
|
2022-01-04 23:58:13 +01:00
|
|
|
impl From<PLLMul> for Pllmul {
|
|
|
|
fn from(val: PLLMul) -> Pllmul {
|
|
|
|
match val {
|
2021-05-26 13:29:11 +02:00
|
|
|
PLLMul::Mul3 => Pllmul::MUL3,
|
|
|
|
PLLMul::Mul4 => Pllmul::MUL4,
|
|
|
|
PLLMul::Mul6 => Pllmul::MUL6,
|
|
|
|
PLLMul::Mul8 => Pllmul::MUL8,
|
|
|
|
PLLMul::Mul12 => Pllmul::MUL12,
|
|
|
|
PLLMul::Mul16 => Pllmul::MUL16,
|
|
|
|
PLLMul::Mul24 => Pllmul::MUL24,
|
|
|
|
PLLMul::Mul32 => Pllmul::MUL32,
|
|
|
|
PLLMul::Mul48 => Pllmul::MUL48,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-04 23:58:13 +01:00
|
|
|
impl From<PLLDiv> for Plldiv {
|
|
|
|
fn from(val: PLLDiv) -> Plldiv {
|
|
|
|
match val {
|
2021-05-26 13:29:11 +02:00
|
|
|
PLLDiv::Div2 => Plldiv::DIV2,
|
|
|
|
PLLDiv::Div3 => Plldiv::DIV3,
|
|
|
|
PLLDiv::Div4 => Plldiv::DIV4,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-04 23:58:13 +01:00
|
|
|
impl From<PLLSource> for Pllsrc {
|
|
|
|
fn from(val: PLLSource) -> Pllsrc {
|
|
|
|
match val {
|
2021-05-26 13:29:11 +02:00
|
|
|
PLLSource::HSI16 => Pllsrc::HSI16,
|
|
|
|
PLLSource::HSE(_) => Pllsrc::HSE,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-04 23:58:13 +01:00
|
|
|
impl From<MSIRange> for Msirange {
|
|
|
|
fn from(val: MSIRange) -> Msirange {
|
|
|
|
match val {
|
2021-05-26 13:29:11 +02:00
|
|
|
MSIRange::Range0 => Msirange::RANGE0,
|
|
|
|
MSIRange::Range1 => Msirange::RANGE1,
|
|
|
|
MSIRange::Range2 => Msirange::RANGE2,
|
|
|
|
MSIRange::Range3 => Msirange::RANGE3,
|
|
|
|
MSIRange::Range4 => Msirange::RANGE4,
|
|
|
|
MSIRange::Range5 => Msirange::RANGE5,
|
|
|
|
MSIRange::Range6 => Msirange::RANGE6,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-25 17:09:01 +02:00
|
|
|
/// Clocks configutation
|
|
|
|
pub struct Config {
|
2022-01-04 11:18:59 +01:00
|
|
|
pub mux: ClockSrc,
|
|
|
|
pub ahb_pre: AHBPrescaler,
|
|
|
|
pub apb1_pre: APBPrescaler,
|
|
|
|
pub apb2_pre: APBPrescaler,
|
2022-02-24 05:57:17 +01:00
|
|
|
#[cfg(crs)]
|
2022-01-04 23:58:13 +01:00
|
|
|
pub enable_hsi48: bool,
|
2023-09-15 01:53:27 +02:00
|
|
|
pub rtc: Option<RtcClockSource>,
|
2021-05-25 17:09:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Config {
|
|
|
|
#[inline]
|
|
|
|
fn default() -> Config {
|
|
|
|
Config {
|
|
|
|
mux: ClockSrc::MSI(MSIRange::default()),
|
2023-09-17 00:41:11 +02:00
|
|
|
ahb_pre: AHBPrescaler::DIV1,
|
|
|
|
apb1_pre: APBPrescaler::DIV1,
|
|
|
|
apb2_pre: APBPrescaler::DIV1,
|
2022-02-24 05:57:17 +01:00
|
|
|
#[cfg(crs)]
|
2022-01-04 23:58:13 +01:00
|
|
|
enable_hsi48: false,
|
2023-09-15 01:53:27 +02:00
|
|
|
rtc: None,
|
2021-05-25 17:09:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-04 23:58:13 +01:00
|
|
|
pub(crate) unsafe fn init(config: Config) {
|
|
|
|
let (sys_clk, sw) = match config.mux {
|
|
|
|
ClockSrc::MSI(range) => {
|
|
|
|
// Set MSI range
|
|
|
|
RCC.icscr().write(|w| w.set_msirange(range.into()));
|
2021-05-25 17:09:01 +02:00
|
|
|
|
2022-01-04 23:58:13 +01:00
|
|
|
// Enable MSI
|
|
|
|
RCC.cr().write(|w| w.set_msion(true));
|
|
|
|
while !RCC.cr().read().msirdy() {}
|
2021-05-25 17:09:01 +02:00
|
|
|
|
2022-01-04 23:58:13 +01:00
|
|
|
let freq = 32_768 * (1 << (range as u8 + 1));
|
|
|
|
(freq, Sw::MSI)
|
|
|
|
}
|
|
|
|
ClockSrc::HSI16 => {
|
|
|
|
// Enable HSI16
|
|
|
|
RCC.cr().write(|w| w.set_hsi16on(true));
|
|
|
|
while !RCC.cr().read().hsi16rdyf() {}
|
2021-06-02 14:28:33 +02:00
|
|
|
|
2022-07-10 19:59:36 +02:00
|
|
|
(HSI_FREQ.0, Sw::HSI16)
|
2022-01-04 23:58:13 +01:00
|
|
|
}
|
|
|
|
ClockSrc::HSE(freq) => {
|
|
|
|
// Enable HSE
|
|
|
|
RCC.cr().write(|w| w.set_hseon(true));
|
|
|
|
while !RCC.cr().read().hserdy() {}
|
2021-06-02 14:28:33 +02:00
|
|
|
|
2022-01-04 23:58:13 +01:00
|
|
|
(freq.0, Sw::HSE)
|
|
|
|
}
|
|
|
|
ClockSrc::PLL(src, mul, div) => {
|
|
|
|
let freq = match src {
|
|
|
|
PLLSource::HSE(freq) => {
|
|
|
|
// Enable HSE
|
|
|
|
RCC.cr().write(|w| w.set_hseon(true));
|
|
|
|
while !RCC.cr().read().hserdy() {}
|
|
|
|
freq.0
|
|
|
|
}
|
|
|
|
PLLSource::HSI16 => {
|
|
|
|
// Enable HSI
|
|
|
|
RCC.cr().write(|w| w.set_hsi16on(true));
|
|
|
|
while !RCC.cr().read().hsi16rdyf() {}
|
2022-07-10 19:59:36 +02:00
|
|
|
HSI_FREQ.0
|
2022-01-04 23:58:13 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Disable PLL
|
|
|
|
RCC.cr().modify(|w| w.set_pllon(false));
|
|
|
|
while RCC.cr().read().pllrdy() {}
|
|
|
|
|
|
|
|
let freq = match mul {
|
|
|
|
PLLMul::Mul3 => freq * 3,
|
|
|
|
PLLMul::Mul4 => freq * 4,
|
|
|
|
PLLMul::Mul6 => freq * 6,
|
|
|
|
PLLMul::Mul8 => freq * 8,
|
|
|
|
PLLMul::Mul12 => freq * 12,
|
|
|
|
PLLMul::Mul16 => freq * 16,
|
|
|
|
PLLMul::Mul24 => freq * 24,
|
|
|
|
PLLMul::Mul32 => freq * 32,
|
|
|
|
PLLMul::Mul48 => freq * 48,
|
|
|
|
};
|
|
|
|
|
|
|
|
let freq = match div {
|
|
|
|
PLLDiv::Div2 => freq / 2,
|
|
|
|
PLLDiv::Div3 => freq / 3,
|
|
|
|
PLLDiv::Div4 => freq / 4,
|
|
|
|
};
|
2022-07-11 00:36:10 +02:00
|
|
|
assert!(freq <= 32_000_000);
|
2022-01-04 23:58:13 +01:00
|
|
|
|
|
|
|
RCC.cfgr().write(move |w| {
|
|
|
|
w.set_pllmul(mul.into());
|
|
|
|
w.set_plldiv(div.into());
|
|
|
|
w.set_pllsrc(src.into());
|
2021-06-02 14:28:33 +02:00
|
|
|
});
|
|
|
|
|
2022-01-04 23:58:13 +01:00
|
|
|
// Enable PLL
|
|
|
|
RCC.cr().modify(|w| w.set_pllon(true));
|
|
|
|
while !RCC.cr().read().pllrdy() {}
|
2021-06-02 14:28:33 +02:00
|
|
|
|
2022-01-04 23:58:13 +01:00
|
|
|
(freq, Sw::PLL)
|
2021-06-02 14:28:33 +02:00
|
|
|
}
|
2022-01-04 23:58:13 +01:00
|
|
|
};
|
|
|
|
|
2023-09-15 01:53:27 +02:00
|
|
|
config.rtc.map(|rtc| {
|
|
|
|
BackupDomain::configure_ls(rtc, None);
|
|
|
|
});
|
|
|
|
|
2022-01-04 23:58:13 +01:00
|
|
|
RCC.cfgr().modify(|w| {
|
|
|
|
w.set_sw(sw);
|
|
|
|
w.set_hpre(config.ahb_pre.into());
|
|
|
|
w.set_ppre1(config.apb1_pre.into());
|
|
|
|
w.set_ppre2(config.apb2_pre.into());
|
|
|
|
});
|
|
|
|
|
|
|
|
let ahb_freq: u32 = match config.ahb_pre {
|
2023-09-17 00:41:11 +02:00
|
|
|
AHBPrescaler::DIV1 => sys_clk,
|
2022-01-04 23:58:13 +01:00
|
|
|
pre => {
|
|
|
|
let pre: Hpre = pre.into();
|
2023-06-29 01:51:19 +02:00
|
|
|
let pre = 1 << (pre.to_bits() as u32 - 7);
|
2022-01-04 23:58:13 +01:00
|
|
|
sys_clk / pre
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let (apb1_freq, apb1_tim_freq) = match config.apb1_pre {
|
2023-09-17 00:41:11 +02:00
|
|
|
APBPrescaler::DIV1 => (ahb_freq, ahb_freq),
|
2022-01-04 23:58:13 +01:00
|
|
|
pre => {
|
|
|
|
let pre: Ppre = pre.into();
|
2023-06-29 01:51:19 +02:00
|
|
|
let pre: u8 = 1 << (pre.to_bits() - 3);
|
2022-01-04 23:58:13 +01:00
|
|
|
let freq = ahb_freq / pre as u32;
|
|
|
|
(freq, freq * 2)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let (apb2_freq, apb2_tim_freq) = match config.apb2_pre {
|
2023-09-17 00:41:11 +02:00
|
|
|
APBPrescaler::DIV1 => (ahb_freq, ahb_freq),
|
2022-01-04 23:58:13 +01:00
|
|
|
pre => {
|
|
|
|
let pre: Ppre = pre.into();
|
2023-06-29 01:51:19 +02:00
|
|
|
let pre: u8 = 1 << (pre.to_bits() - 3);
|
2023-03-17 04:21:39 +01:00
|
|
|
let freq = ahb_freq / pre as u32;
|
2022-01-04 23:58:13 +01:00
|
|
|
(freq, freq * 2)
|
|
|
|
}
|
|
|
|
};
|
2021-05-25 17:09:01 +02:00
|
|
|
|
2022-02-24 05:57:17 +01:00
|
|
|
#[cfg(crs)]
|
2022-01-04 23:58:13 +01:00
|
|
|
if config.enable_hsi48 {
|
|
|
|
// Reset SYSCFG peripheral
|
|
|
|
RCC.apb2rstr().modify(|w| w.set_syscfgrst(true));
|
|
|
|
RCC.apb2rstr().modify(|w| w.set_syscfgrst(false));
|
2021-05-25 17:09:01 +02:00
|
|
|
|
2022-01-04 23:58:13 +01:00
|
|
|
// Enable SYSCFG peripheral
|
|
|
|
RCC.apb2enr().modify(|w| w.set_syscfgen(true));
|
2021-05-25 17:09:01 +02:00
|
|
|
|
2022-01-04 23:58:13 +01:00
|
|
|
// Reset CRS peripheral
|
|
|
|
RCC.apb1rstr().modify(|w| w.set_crsrst(true));
|
|
|
|
RCC.apb1rstr().modify(|w| w.set_crsrst(false));
|
2021-05-25 17:09:01 +02:00
|
|
|
|
2022-01-04 23:58:13 +01:00
|
|
|
// Enable CRS peripheral
|
|
|
|
RCC.apb1enr().modify(|w| w.set_crsen(true));
|
2021-05-25 17:09:01 +02:00
|
|
|
|
2022-01-04 23:58:13 +01:00
|
|
|
// Initialize CRS
|
|
|
|
CRS.cfgr().write(|w|
|
2021-05-25 17:09:01 +02:00
|
|
|
|
2022-01-04 23:58:13 +01:00
|
|
|
// Select LSE as synchronization source
|
2023-06-27 23:49:30 +02:00
|
|
|
w.set_syncsrc(crs::vals::Syncsrc::LSE));
|
2022-01-04 23:58:13 +01:00
|
|
|
CRS.cr().modify(|w| {
|
|
|
|
w.set_autotrimen(true);
|
|
|
|
w.set_cen(true);
|
|
|
|
});
|
2021-05-25 17:09:01 +02:00
|
|
|
|
2022-01-04 23:58:13 +01:00
|
|
|
// Enable VREFINT reference for HSI48 oscillator
|
|
|
|
SYSCFG.cfgr3().modify(|w| {
|
|
|
|
w.set_enref_hsi48(true);
|
|
|
|
w.set_en_vrefint(true);
|
|
|
|
});
|
2021-05-25 17:09:01 +02:00
|
|
|
|
2022-01-04 23:58:13 +01:00
|
|
|
// Select HSI48 as USB clock
|
|
|
|
RCC.ccipr().modify(|w| w.set_hsi48msel(true));
|
2021-05-25 17:09:01 +02:00
|
|
|
|
2022-01-04 23:58:13 +01:00
|
|
|
// Enable dedicated USB clock
|
|
|
|
RCC.crrcr().modify(|w| w.set_hsi48on(true));
|
|
|
|
while !RCC.crrcr().read().hsi48rdy() {}
|
2021-05-25 17:09:01 +02:00
|
|
|
}
|
|
|
|
|
2022-01-04 23:58:13 +01:00
|
|
|
set_freqs(Clocks {
|
2022-07-11 00:36:10 +02:00
|
|
|
sys: Hertz(sys_clk),
|
|
|
|
ahb1: Hertz(ahb_freq),
|
|
|
|
apb1: Hertz(apb1_freq),
|
|
|
|
apb2: Hertz(apb2_freq),
|
|
|
|
apb1_tim: Hertz(apb1_tim_freq),
|
|
|
|
apb2_tim: Hertz(apb2_tim_freq),
|
2022-01-04 23:58:13 +01:00
|
|
|
});
|
2021-05-25 13:32:10 +02:00
|
|
|
}
|