2023-06-29 01:51:19 +02:00
|
|
|
use stm32_metapac::flash::vals::Latency;
|
|
|
|
|
2022-06-12 22:15:44 +02:00
|
|
|
use super::{set_freqs, Clocks};
|
2022-02-14 02:12:06 +01:00
|
|
|
use crate::pac::rcc::vals::{Hpre, Pllmul, Pllsrc, Ppre, Sw, Usbsw};
|
2021-07-29 15:24:42 +02:00
|
|
|
use crate::pac::{FLASH, RCC};
|
2021-06-23 01:07:48 +02:00
|
|
|
use crate::time::Hertz;
|
|
|
|
|
2022-07-10 19:59:36 +02:00
|
|
|
/// HSI speed
|
|
|
|
pub const HSI_FREQ: Hertz = Hertz(8_000_000);
|
|
|
|
|
|
|
|
/// LSI speed
|
|
|
|
pub const LSI_FREQ: Hertz = Hertz(40_000);
|
2021-06-23 01:07:48 +02:00
|
|
|
|
|
|
|
/// Configuration of the clocks
|
|
|
|
///
|
|
|
|
/// hse takes precedence over hsi48 if both are enabled
|
|
|
|
#[non_exhaustive]
|
|
|
|
#[derive(Default)]
|
|
|
|
pub struct Config {
|
|
|
|
pub hse: Option<Hertz>,
|
|
|
|
pub bypass_hse: bool,
|
|
|
|
pub usb_pll: bool,
|
|
|
|
|
2022-02-14 02:12:06 +01:00
|
|
|
#[cfg(not(stm32f0x0))]
|
2021-06-23 01:07:48 +02:00
|
|
|
pub hsi48: bool,
|
|
|
|
|
|
|
|
pub sys_ck: Option<Hertz>,
|
|
|
|
pub hclk: Option<Hertz>,
|
|
|
|
pub pclk: Option<Hertz>,
|
|
|
|
}
|
|
|
|
|
2022-01-04 23:58:13 +01:00
|
|
|
pub(crate) unsafe fn init(config: Config) {
|
2022-07-10 19:59:36 +02:00
|
|
|
let sysclk = config.sys_ck.map(|v| v.0).unwrap_or(HSI_FREQ.0);
|
2021-06-23 01:07:48 +02:00
|
|
|
|
2022-01-04 23:58:13 +01:00
|
|
|
let (src_clk, use_hsi48) = config.hse.map(|v| (v.0, false)).unwrap_or_else(|| {
|
2022-02-14 02:12:06 +01:00
|
|
|
#[cfg(not(stm32f0x0))]
|
2022-01-04 23:58:13 +01:00
|
|
|
if config.hsi48 {
|
|
|
|
return (48_000_000, true);
|
2021-06-23 01:07:48 +02:00
|
|
|
}
|
2022-07-10 19:59:36 +02:00
|
|
|
(HSI_FREQ.0, false)
|
2022-01-04 23:58:13 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
let (pllmul_bits, real_sysclk) = if sysclk == src_clk {
|
|
|
|
(None, sysclk)
|
|
|
|
} else {
|
|
|
|
let prediv = if config.hse.is_some() { 1 } else { 2 };
|
|
|
|
let pllmul = (2 * prediv * sysclk + src_clk) / src_clk / 2;
|
|
|
|
let pllmul = pllmul.max(2).min(16);
|
|
|
|
|
|
|
|
let pllmul_bits = pllmul as u8 - 2;
|
|
|
|
let real_sysclk = pllmul * src_clk / prediv;
|
|
|
|
(Some(pllmul_bits), real_sysclk)
|
|
|
|
};
|
|
|
|
|
|
|
|
let hpre_bits = config
|
|
|
|
.hclk
|
|
|
|
.map(|hclk| match real_sysclk / hclk.0 {
|
|
|
|
0 => unreachable!(),
|
|
|
|
1 => 0b0111,
|
|
|
|
2 => 0b1000,
|
|
|
|
3..=5 => 0b1001,
|
|
|
|
6..=11 => 0b1010,
|
|
|
|
12..=39 => 0b1011,
|
|
|
|
40..=95 => 0b1100,
|
|
|
|
96..=191 => 0b1101,
|
|
|
|
192..=383 => 0b1110,
|
|
|
|
_ => 0b1111,
|
|
|
|
})
|
|
|
|
.unwrap_or(0b0111);
|
|
|
|
let hclk = real_sysclk / (1 << (hpre_bits - 0b0111));
|
|
|
|
|
|
|
|
let ppre_bits = config
|
|
|
|
.pclk
|
|
|
|
.map(|pclk| match hclk / pclk.0 {
|
|
|
|
0 => unreachable!(),
|
|
|
|
1 => 0b011,
|
|
|
|
2 => 0b100,
|
|
|
|
3..=5 => 0b101,
|
|
|
|
6..=11 => 0b110,
|
|
|
|
_ => 0b111,
|
|
|
|
})
|
|
|
|
.unwrap_or(0b011);
|
|
|
|
|
|
|
|
let ppre: u8 = 1 << (ppre_bits - 0b011);
|
|
|
|
let pclk = hclk / u32::from(ppre);
|
|
|
|
|
|
|
|
let timer_mul = if ppre == 1 { 1 } else { 2 };
|
|
|
|
|
|
|
|
FLASH.acr().write(|w| {
|
2023-06-29 01:51:19 +02:00
|
|
|
w.set_latency(if real_sysclk <= 24_000_000 {
|
|
|
|
Latency::WS0
|
2021-06-23 01:07:48 +02:00
|
|
|
} else {
|
2023-06-29 01:51:19 +02:00
|
|
|
Latency::WS1
|
|
|
|
});
|
2022-01-04 23:58:13 +01:00
|
|
|
});
|
2021-06-23 01:07:48 +02:00
|
|
|
|
2022-01-04 23:58:13 +01:00
|
|
|
match (config.hse.is_some(), use_hsi48) {
|
|
|
|
(true, _) => {
|
|
|
|
RCC.cr().modify(|w| {
|
|
|
|
w.set_csson(true);
|
|
|
|
w.set_hseon(true);
|
2022-02-14 02:12:06 +01:00
|
|
|
w.set_hsebyp(config.bypass_hse);
|
2022-01-04 23:58:13 +01:00
|
|
|
});
|
|
|
|
while !RCC.cr().read().hserdy() {}
|
2021-06-23 01:07:48 +02:00
|
|
|
|
2022-01-04 23:58:13 +01:00
|
|
|
if pllmul_bits.is_some() {
|
|
|
|
RCC.cfgr().modify(|w| w.set_pllsrc(Pllsrc::HSE_DIV_PREDIV))
|
2021-06-23 01:07:48 +02:00
|
|
|
}
|
2022-01-04 23:58:13 +01:00
|
|
|
}
|
2022-02-14 02:12:06 +01:00
|
|
|
// use_hsi48 will always be false for stm32f0x0
|
|
|
|
#[cfg(not(stm32f0x0))]
|
2022-01-04 23:58:13 +01:00
|
|
|
(false, true) => {
|
|
|
|
RCC.cr2().modify(|w| w.set_hsi48on(true));
|
|
|
|
while !RCC.cr2().read().hsi48rdy() {}
|
2021-06-23 01:07:48 +02:00
|
|
|
|
2022-01-04 23:58:13 +01:00
|
|
|
if pllmul_bits.is_some() {
|
2022-06-12 22:15:44 +02:00
|
|
|
RCC.cfgr().modify(|w| w.set_pllsrc(Pllsrc::HSI48_DIV_PREDIV))
|
2021-06-23 01:07:48 +02:00
|
|
|
}
|
2022-01-04 23:58:13 +01:00
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
RCC.cr().modify(|w| w.set_hsion(true));
|
|
|
|
while !RCC.cr().read().hsirdy() {}
|
2021-06-23 01:07:48 +02:00
|
|
|
|
2022-01-04 23:58:13 +01:00
|
|
|
if pllmul_bits.is_some() {
|
|
|
|
RCC.cfgr().modify(|w| w.set_pllsrc(Pllsrc::HSI_DIV2))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-06-23 01:07:48 +02:00
|
|
|
|
2022-01-04 23:58:13 +01:00
|
|
|
if config.usb_pll {
|
|
|
|
RCC.cfgr3().modify(|w| w.set_usbsw(Usbsw::PLLCLK));
|
|
|
|
}
|
|
|
|
// TODO: Option to use CRS (Clock Recovery)
|
2021-06-23 01:07:48 +02:00
|
|
|
|
2022-01-04 23:58:13 +01:00
|
|
|
if let Some(pllmul_bits) = pllmul_bits {
|
2023-06-29 01:51:19 +02:00
|
|
|
RCC.cfgr().modify(|w| w.set_pllmul(Pllmul::from_bits(pllmul_bits)));
|
2022-01-04 23:58:13 +01:00
|
|
|
|
|
|
|
RCC.cr().modify(|w| w.set_pllon(true));
|
|
|
|
while !RCC.cr().read().pllrdy() {}
|
|
|
|
|
|
|
|
RCC.cfgr().modify(|w| {
|
2023-06-29 01:51:19 +02:00
|
|
|
w.set_ppre(Ppre::from_bits(ppre_bits));
|
|
|
|
w.set_hpre(Hpre::from_bits(hpre_bits));
|
2022-01-04 23:58:13 +01:00
|
|
|
w.set_sw(Sw::PLL)
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
RCC.cfgr().modify(|w| {
|
2023-06-29 01:51:19 +02:00
|
|
|
w.set_ppre(Ppre::from_bits(ppre_bits));
|
|
|
|
w.set_hpre(Hpre::from_bits(hpre_bits));
|
2022-01-04 23:58:13 +01:00
|
|
|
|
|
|
|
if config.hse.is_some() {
|
|
|
|
w.set_sw(Sw::HSE);
|
|
|
|
} else if use_hsi48 {
|
2022-02-14 02:12:06 +01:00
|
|
|
#[cfg(not(stm32f0x0))]
|
2022-01-04 23:58:13 +01:00
|
|
|
w.set_sw(Sw::HSI48);
|
2021-06-23 01:07:48 +02:00
|
|
|
} else {
|
2022-01-04 23:58:13 +01:00
|
|
|
w.set_sw(Sw::HSI)
|
2021-06-23 01:07:48 +02:00
|
|
|
}
|
2022-01-04 23:58:13 +01:00
|
|
|
})
|
2021-06-23 01:07:48 +02:00
|
|
|
}
|
|
|
|
|
2022-01-04 23:58:13 +01:00
|
|
|
set_freqs(Clocks {
|
|
|
|
sys: Hertz(real_sysclk),
|
|
|
|
apb1: Hertz(pclk),
|
|
|
|
apb2: Hertz(pclk),
|
|
|
|
apb1_tim: Hertz(pclk * timer_mul),
|
|
|
|
apb2_tim: Hertz(pclk * timer_mul),
|
2022-02-14 02:12:06 +01:00
|
|
|
ahb1: Hertz(hclk),
|
2022-01-04 23:58:13 +01:00
|
|
|
});
|
2021-06-23 01:07:48 +02:00
|
|
|
}
|