2023-01-17 18:54:23 +01:00
|
|
|
use crate::pac::flash::vals::Latency;
|
2023-10-11 00:12:33 +02:00
|
|
|
use crate::pac::rcc::vals::Sw;
|
|
|
|
pub use crate::pac::rcc::vals::{Hpre as AHBPrescaler, Hsidiv as HSIPrescaler, Ppre as APBPrescaler};
|
2023-01-17 18:54:23 +01:00
|
|
|
use crate::pac::{FLASH, RCC};
|
|
|
|
use crate::rcc::{set_freqs, Clocks};
|
|
|
|
use crate::time::Hertz;
|
|
|
|
|
|
|
|
/// HSI speed
|
|
|
|
pub const HSI_FREQ: Hertz = Hertz(48_000_000);
|
|
|
|
|
|
|
|
/// System clock mux source
|
|
|
|
#[derive(Clone, Copy)]
|
|
|
|
pub enum ClockSrc {
|
|
|
|
HSE(Hertz),
|
|
|
|
HSI(HSIPrescaler),
|
|
|
|
LSI,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Clocks configutation
|
|
|
|
pub struct Config {
|
|
|
|
pub mux: ClockSrc,
|
|
|
|
pub ahb_pre: AHBPrescaler,
|
|
|
|
pub apb_pre: APBPrescaler,
|
2023-10-11 03:53:27 +02:00
|
|
|
pub ls: super::LsConfig,
|
2023-01-17 18:54:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Config {
|
|
|
|
#[inline]
|
|
|
|
fn default() -> Config {
|
|
|
|
Config {
|
2023-10-11 00:12:33 +02:00
|
|
|
mux: ClockSrc::HSI(HSIPrescaler::DIV1),
|
2023-09-16 17:41:11 -05:00
|
|
|
ahb_pre: AHBPrescaler::DIV1,
|
|
|
|
apb_pre: APBPrescaler::DIV1,
|
2023-10-11 03:53:27 +02:00
|
|
|
ls: Default::default(),
|
2023-01-17 18:54:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) unsafe fn init(config: Config) {
|
|
|
|
let (sys_clk, sw) = match config.mux {
|
|
|
|
ClockSrc::HSI(div) => {
|
|
|
|
// Enable HSI
|
|
|
|
RCC.cr().write(|w| {
|
|
|
|
w.set_hsidiv(div);
|
|
|
|
w.set_hsion(true)
|
|
|
|
});
|
|
|
|
while !RCC.cr().read().hsirdy() {}
|
|
|
|
|
2023-10-11 00:12:33 +02:00
|
|
|
(HSI_FREQ / div, Sw::HSI)
|
2023-01-17 18:54:23 +01:00
|
|
|
}
|
|
|
|
ClockSrc::HSE(freq) => {
|
|
|
|
// Enable HSE
|
|
|
|
RCC.cr().write(|w| w.set_hseon(true));
|
|
|
|
while !RCC.cr().read().hserdy() {}
|
|
|
|
|
2023-10-11 00:12:33 +02:00
|
|
|
(freq, Sw::HSE)
|
2023-01-17 18:54:23 +01:00
|
|
|
}
|
|
|
|
ClockSrc::LSI => {
|
|
|
|
// Enable LSI
|
|
|
|
RCC.csr2().write(|w| w.set_lsion(true));
|
|
|
|
while !RCC.csr2().read().lsirdy() {}
|
2023-10-11 03:53:27 +02:00
|
|
|
(super::LSI_FREQ, Sw::LSI)
|
2023-01-17 18:54:23 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-10-11 03:53:27 +02:00
|
|
|
let rtc = config.ls.init();
|
|
|
|
|
2023-01-17 18:54:23 +01:00
|
|
|
// Determine the flash latency implied by the target clock speed
|
|
|
|
// RM0454 § 3.3.4:
|
2023-10-11 00:12:33 +02:00
|
|
|
let target_flash_latency = if sys_clk <= Hertz(24_000_000) {
|
2023-01-17 18:54:23 +01:00
|
|
|
Latency::WS0
|
|
|
|
} else {
|
|
|
|
Latency::WS1
|
|
|
|
};
|
|
|
|
|
|
|
|
// Increase the number of cycles we wait for flash if the new value is higher
|
|
|
|
// There's no harm in waiting a little too much before the clock change, but we'll
|
|
|
|
// crash immediately if we don't wait enough after the clock change
|
|
|
|
let mut set_flash_latency_after = false;
|
|
|
|
FLASH.acr().modify(|w| {
|
|
|
|
// Is the current flash latency less than what we need at the new SYSCLK?
|
2023-06-29 01:51:19 +02:00
|
|
|
if w.latency().to_bits() <= target_flash_latency.to_bits() {
|
2023-01-17 18:54:23 +01:00
|
|
|
// We must increase the number of wait states now
|
|
|
|
w.set_latency(target_flash_latency)
|
|
|
|
} else {
|
|
|
|
// We may decrease the number of wait states later
|
|
|
|
set_flash_latency_after = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// RM0490 § 3.3.4:
|
|
|
|
// > Prefetch is enabled by setting the PRFTEN bit of the FLASH access control register
|
|
|
|
// > (FLASH_ACR). This feature is useful if at least one wait state is needed to access the
|
|
|
|
// > Flash memory.
|
|
|
|
//
|
|
|
|
// Enable flash prefetching if we have at least one wait state, and disable it otherwise.
|
2023-06-29 01:51:19 +02:00
|
|
|
w.set_prften(target_flash_latency.to_bits() > 0);
|
2023-01-17 18:54:23 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
if !set_flash_latency_after {
|
|
|
|
// Spin until the effective flash latency is compatible with the clock change
|
2023-06-29 01:51:19 +02:00
|
|
|
while FLASH.acr().read().latency().to_bits() < target_flash_latency.to_bits() {}
|
2023-01-17 18:54:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Configure SYSCLK source, HCLK divisor, and PCLK divisor all at once
|
2023-10-11 00:12:33 +02:00
|
|
|
let (sw, hpre, ppre) = (sw.into(), config.ahb_pre, config.apb_pre);
|
2023-01-17 18:54:23 +01:00
|
|
|
RCC.cfgr().modify(|w| {
|
|
|
|
w.set_sw(sw);
|
|
|
|
w.set_hpre(hpre);
|
|
|
|
w.set_ppre(ppre);
|
|
|
|
});
|
|
|
|
|
|
|
|
if set_flash_latency_after {
|
|
|
|
// We can make the flash require fewer wait states
|
|
|
|
// Spin until the SYSCLK changes have taken effect
|
|
|
|
loop {
|
|
|
|
let cfgr = RCC.cfgr().read();
|
|
|
|
if cfgr.sw() == sw && cfgr.hpre() == hpre && cfgr.ppre() == ppre {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the flash latency to require fewer wait states
|
|
|
|
FLASH.acr().modify(|w| w.set_latency(target_flash_latency));
|
|
|
|
}
|
|
|
|
|
2023-10-11 00:12:33 +02:00
|
|
|
let ahb_freq = sys_clk / config.ahb_pre;
|
2023-01-17 18:54:23 +01:00
|
|
|
|
|
|
|
let (apb_freq, apb_tim_freq) = match config.apb_pre {
|
2023-09-16 17:41:11 -05:00
|
|
|
APBPrescaler::DIV1 => (ahb_freq, ahb_freq),
|
2023-01-17 18:54:23 +01:00
|
|
|
pre => {
|
2023-10-11 00:12:33 +02:00
|
|
|
let freq = ahb_freq / pre;
|
|
|
|
(freq, freq * 2u32)
|
2023-01-17 18:54:23 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
set_freqs(Clocks {
|
2023-10-16 20:04:10 -05:00
|
|
|
hsi: None,
|
|
|
|
lse: None,
|
2023-10-11 00:12:33 +02:00
|
|
|
sys: sys_clk,
|
2023-10-15 19:51:35 -05:00
|
|
|
hclk1: ahb_freq,
|
|
|
|
pclk1: apb_freq,
|
|
|
|
pclk1_tim: apb_tim_freq,
|
2023-10-11 03:53:27 +02:00
|
|
|
rtc,
|
2023-01-17 18:54:23 +01:00
|
|
|
});
|
|
|
|
}
|