2023-10-11 00:12:33 +02:00
|
|
|
pub use crate::pac::rcc::vals::{
|
|
|
|
Hpre as AHBPrescaler, Hsepre as HsePrescaler, Pllm, Plln, Pllp, Pllq, Pllr, Pllsrc as PllSource,
|
|
|
|
Ppre as APBPrescaler, Sw as Sysclk,
|
|
|
|
};
|
2023-08-27 16:07:34 +02:00
|
|
|
use crate::rcc::bd::{BackupDomain, RtcClockSource};
|
2023-07-30 17:18:54 +02:00
|
|
|
use crate::rcc::Clocks;
|
|
|
|
use crate::time::{khz, mhz, Hertz};
|
2021-06-14 11:41:02 +02:00
|
|
|
|
|
|
|
/// HSI speed
|
2022-07-10 19:59:36 +02:00
|
|
|
pub const HSI_FREQ: Hertz = Hertz(16_000_000);
|
|
|
|
|
|
|
|
/// LSI speed
|
|
|
|
pub const LSI_FREQ: Hertz = Hertz(32_000);
|
2021-06-14 11:41:02 +02:00
|
|
|
|
2023-07-24 00:01:34 +02:00
|
|
|
pub struct Hse {
|
|
|
|
pub prediv: HsePrescaler,
|
|
|
|
|
|
|
|
pub frequency: Hertz,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct PllMux {
|
|
|
|
/// Source clock selection.
|
|
|
|
pub source: PllSource,
|
|
|
|
|
|
|
|
/// PLL pre-divider (DIVM). Must be between 1 and 63.
|
2023-10-11 00:12:33 +02:00
|
|
|
pub prediv: Pllm,
|
2023-07-24 00:01:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Pll {
|
|
|
|
/// PLL multiplication factor. Must be between 4 and 512.
|
2023-10-11 00:12:33 +02:00
|
|
|
pub mul: Plln,
|
2023-07-24 00:01:34 +02:00
|
|
|
|
|
|
|
/// PLL P division factor. If None, PLL P output is disabled. Must be between 1 and 128.
|
|
|
|
/// On PLL1, it must be even (in particular, it cannot be 1.)
|
2023-10-11 00:12:33 +02:00
|
|
|
pub divp: Option<Pllp>,
|
2023-07-24 00:01:34 +02:00
|
|
|
/// PLL Q division factor. If None, PLL Q output is disabled. Must be between 1 and 128.
|
2023-10-11 00:12:33 +02:00
|
|
|
pub divq: Option<Pllq>,
|
2023-07-24 00:01:34 +02:00
|
|
|
/// PLL R division factor. If None, PLL R output is disabled. Must be between 1 and 128.
|
2023-10-11 00:12:33 +02:00
|
|
|
pub divr: Option<Pllr>,
|
2021-06-14 11:41:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Clocks configutation
|
|
|
|
pub struct Config {
|
2023-07-24 00:01:34 +02:00
|
|
|
pub hse: Option<Hse>,
|
|
|
|
pub lse: Option<Hertz>,
|
2023-09-18 01:41:45 +02:00
|
|
|
pub lsi: bool,
|
2023-07-24 00:01:34 +02:00
|
|
|
pub sys: Sysclk,
|
|
|
|
pub mux: Option<PllMux>,
|
2023-08-06 18:11:53 +02:00
|
|
|
pub rtc: Option<RtcClockSource>,
|
2023-07-24 00:01:34 +02:00
|
|
|
|
|
|
|
pub pll: Option<Pll>,
|
|
|
|
pub pllsai: Option<Pll>,
|
|
|
|
|
|
|
|
pub ahb1_pre: AHBPrescaler,
|
|
|
|
pub ahb2_pre: AHBPrescaler,
|
|
|
|
pub ahb3_pre: AHBPrescaler,
|
2022-01-04 11:18:59 +01:00
|
|
|
pub apb1_pre: APBPrescaler,
|
|
|
|
pub apb2_pre: APBPrescaler,
|
2021-06-14 11:41:02 +02:00
|
|
|
}
|
|
|
|
|
2023-07-25 01:25:15 +02:00
|
|
|
pub const WPAN_DEFAULT: Config = Config {
|
|
|
|
hse: Some(Hse {
|
|
|
|
frequency: mhz(32),
|
2023-10-11 00:12:33 +02:00
|
|
|
prediv: HsePrescaler::DIV1,
|
2023-07-25 01:25:15 +02:00
|
|
|
}),
|
|
|
|
lse: Some(khz(32)),
|
2023-10-11 00:12:33 +02:00
|
|
|
sys: Sysclk::PLL,
|
2023-07-25 01:25:15 +02:00
|
|
|
mux: Some(PllMux {
|
2023-10-11 00:12:33 +02:00
|
|
|
source: PllSource::HSE,
|
|
|
|
prediv: Pllm::DIV2,
|
2023-07-25 01:25:15 +02:00
|
|
|
}),
|
2023-09-16 17:19:09 +02:00
|
|
|
rtc: Some(RtcClockSource::LSE),
|
2023-09-18 01:41:45 +02:00
|
|
|
lsi: false,
|
2023-07-25 01:25:15 +02:00
|
|
|
|
|
|
|
pll: Some(Pll {
|
2023-10-11 00:12:33 +02:00
|
|
|
mul: Plln::MUL12,
|
|
|
|
divp: Some(Pllp::DIV3),
|
|
|
|
divq: Some(Pllq::DIV4),
|
|
|
|
divr: Some(Pllr::DIV3),
|
2023-07-25 01:25:15 +02:00
|
|
|
}),
|
|
|
|
pllsai: None,
|
|
|
|
|
2023-09-17 00:41:11 +02:00
|
|
|
ahb1_pre: AHBPrescaler::DIV1,
|
|
|
|
ahb2_pre: AHBPrescaler::DIV2,
|
|
|
|
ahb3_pre: AHBPrescaler::DIV1,
|
|
|
|
apb1_pre: APBPrescaler::DIV1,
|
|
|
|
apb2_pre: APBPrescaler::DIV1,
|
2023-07-25 01:25:15 +02:00
|
|
|
};
|
|
|
|
|
2021-06-14 11:41:02 +02:00
|
|
|
impl Default for Config {
|
|
|
|
#[inline]
|
|
|
|
fn default() -> Config {
|
|
|
|
Config {
|
2023-07-25 01:25:15 +02:00
|
|
|
hse: None,
|
|
|
|
lse: None,
|
2023-10-11 00:12:33 +02:00
|
|
|
sys: Sysclk::HSI16,
|
2023-07-25 01:25:15 +02:00
|
|
|
mux: None,
|
|
|
|
pll: None,
|
2023-07-24 00:01:34 +02:00
|
|
|
pllsai: None,
|
2023-08-06 18:11:53 +02:00
|
|
|
rtc: None,
|
2023-09-18 01:41:45 +02:00
|
|
|
lsi: false,
|
2023-07-24 00:01:34 +02:00
|
|
|
|
2023-09-17 00:41:11 +02:00
|
|
|
ahb1_pre: AHBPrescaler::DIV1,
|
|
|
|
ahb2_pre: AHBPrescaler::DIV1,
|
|
|
|
ahb3_pre: AHBPrescaler::DIV1,
|
|
|
|
apb1_pre: APBPrescaler::DIV1,
|
|
|
|
apb2_pre: APBPrescaler::DIV1,
|
2021-06-14 11:41:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-24 00:01:34 +02:00
|
|
|
pub(crate) fn compute_clocks(config: &Config) -> Clocks {
|
2023-10-11 00:12:33 +02:00
|
|
|
let hse_clk = config.hse.as_ref().map(|hse| hse.frequency / hse.prediv);
|
2023-07-24 00:01:34 +02:00
|
|
|
|
|
|
|
let mux_clk = config.mux.as_ref().map(|pll_mux| {
|
|
|
|
(match pll_mux.source {
|
2023-10-11 00:12:33 +02:00
|
|
|
PllSource::HSE => hse_clk.unwrap(),
|
|
|
|
PllSource::HSI16 => HSI_FREQ,
|
2023-07-24 00:01:34 +02:00
|
|
|
_ => unreachable!(),
|
|
|
|
} / pll_mux.prediv)
|
|
|
|
});
|
2021-06-14 11:41:02 +02:00
|
|
|
|
2023-07-24 00:01:34 +02:00
|
|
|
let (pll_r, _pll_q, _pll_p) = match &config.pll {
|
|
|
|
Some(pll) => {
|
|
|
|
let pll_vco = mux_clk.unwrap() * pll.mul as u32;
|
|
|
|
|
|
|
|
(
|
|
|
|
pll.divr.map(|divr| pll_vco / divr),
|
|
|
|
pll.divq.map(|divq| pll_vco / divq),
|
|
|
|
pll.divp.map(|divp| pll_vco / divp),
|
|
|
|
)
|
2021-06-14 11:41:02 +02:00
|
|
|
}
|
2023-07-24 00:01:34 +02:00
|
|
|
None => (None, None, None),
|
|
|
|
};
|
2021-06-14 11:41:02 +02:00
|
|
|
|
2023-07-24 00:01:34 +02:00
|
|
|
let sys_clk = match config.sys {
|
|
|
|
Sysclk::HSE => hse_clk.unwrap(),
|
2023-10-11 00:12:33 +02:00
|
|
|
Sysclk::HSI16 => HSI_FREQ,
|
|
|
|
Sysclk::PLL => pll_r.unwrap(),
|
2023-07-24 00:01:34 +02:00
|
|
|
_ => unreachable!(),
|
|
|
|
};
|
|
|
|
|
2023-10-11 00:12:33 +02:00
|
|
|
let ahb1_clk = sys_clk / config.ahb1_pre;
|
|
|
|
let ahb2_clk = sys_clk / config.ahb2_pre;
|
|
|
|
let ahb3_clk = sys_clk / config.ahb3_pre;
|
2022-01-04 23:58:13 +01:00
|
|
|
|
2023-07-24 00:01:34 +02:00
|
|
|
let (apb1_clk, apb1_tim_clk) = match config.apb1_pre {
|
2023-09-17 00:41:11 +02:00
|
|
|
APBPrescaler::DIV1 => (ahb1_clk, ahb1_clk),
|
2022-01-04 23:58:13 +01:00
|
|
|
pre => {
|
2023-10-11 00:12:33 +02:00
|
|
|
let freq = ahb1_clk / pre;
|
2023-07-24 00:01:34 +02:00
|
|
|
(freq, freq * 2u32)
|
2022-01-04 23:58:13 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-07-24 00:01:34 +02:00
|
|
|
let (apb2_clk, apb2_tim_clk) = match config.apb2_pre {
|
2023-09-17 00:41:11 +02:00
|
|
|
APBPrescaler::DIV1 => (ahb1_clk, ahb1_clk),
|
2022-01-04 23:58:13 +01:00
|
|
|
pre => {
|
2023-10-11 00:12:33 +02:00
|
|
|
let freq = ahb1_clk / pre;
|
2023-07-24 00:01:34 +02:00
|
|
|
(freq, freq * 2u32)
|
2022-01-04 23:58:13 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-08-06 18:11:53 +02:00
|
|
|
let rtc_clk = match config.rtc {
|
|
|
|
Some(RtcClockSource::LSI) => Some(LSI_FREQ),
|
|
|
|
Some(RtcClockSource::LSE) => Some(config.lse.unwrap()),
|
|
|
|
_ => None,
|
|
|
|
};
|
|
|
|
|
2023-07-24 00:01:34 +02:00
|
|
|
Clocks {
|
|
|
|
sys: sys_clk,
|
|
|
|
ahb1: ahb1_clk,
|
|
|
|
ahb2: ahb2_clk,
|
|
|
|
ahb3: ahb3_clk,
|
|
|
|
apb1: apb1_clk,
|
|
|
|
apb2: apb2_clk,
|
|
|
|
apb1_tim: apb1_tim_clk,
|
|
|
|
apb2_tim: apb2_tim_clk,
|
2023-08-06 18:11:53 +02:00
|
|
|
rtc: rtc_clk,
|
2023-08-30 02:51:21 +02:00
|
|
|
rtc_hse: None,
|
2023-07-24 00:01:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn configure_clocks(config: &Config) {
|
|
|
|
let rcc = crate::pac::RCC;
|
|
|
|
|
|
|
|
let needs_hsi = if let Some(pll_mux) = &config.mux {
|
2023-10-11 00:12:33 +02:00
|
|
|
pll_mux.source == PllSource::HSI16
|
2023-07-24 00:01:34 +02:00
|
|
|
} else {
|
|
|
|
false
|
|
|
|
};
|
|
|
|
|
2023-10-11 00:12:33 +02:00
|
|
|
if needs_hsi || config.sys == Sysclk::HSI16 {
|
2023-07-24 00:01:34 +02:00
|
|
|
rcc.cr().modify(|w| {
|
|
|
|
w.set_hsion(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
while !rcc.cr().read().hsirdy() {}
|
|
|
|
}
|
|
|
|
|
2023-09-09 01:20:58 +02:00
|
|
|
rcc.cfgr().modify(|w| w.set_stopwuck(true));
|
2023-07-24 00:01:34 +02:00
|
|
|
|
2023-09-18 01:41:45 +02:00
|
|
|
BackupDomain::configure_ls(
|
|
|
|
config.rtc.unwrap_or(RtcClockSource::NOCLOCK),
|
|
|
|
config.lsi,
|
|
|
|
config.lse.map(|_| Default::default()),
|
|
|
|
);
|
2023-07-24 00:01:34 +02:00
|
|
|
|
|
|
|
match &config.hse {
|
|
|
|
Some(hse) => {
|
|
|
|
rcc.cr().modify(|w| {
|
2023-10-11 00:12:33 +02:00
|
|
|
w.set_hsepre(hse.prediv);
|
2023-07-24 00:01:34 +02:00
|
|
|
w.set_hseon(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
while !rcc.cr().read().hserdy() {}
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
|
|
|
|
match &config.mux {
|
|
|
|
Some(pll_mux) => {
|
|
|
|
rcc.pllcfgr().modify(|w| {
|
|
|
|
w.set_pllm(pll_mux.prediv);
|
|
|
|
w.set_pllsrc(pll_mux.source.into());
|
|
|
|
});
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
};
|
|
|
|
|
|
|
|
match &config.pll {
|
|
|
|
Some(pll) => {
|
|
|
|
rcc.pllcfgr().modify(|w| {
|
2023-10-11 00:12:33 +02:00
|
|
|
w.set_plln(pll.mul);
|
2023-07-24 00:01:34 +02:00
|
|
|
pll.divp.map(|divp| {
|
|
|
|
w.set_pllpen(true);
|
2023-10-11 00:12:33 +02:00
|
|
|
w.set_pllp(divp)
|
2023-07-24 00:01:34 +02:00
|
|
|
});
|
|
|
|
pll.divq.map(|divq| {
|
|
|
|
w.set_pllqen(true);
|
2023-10-11 00:12:33 +02:00
|
|
|
w.set_pllq(divq)
|
2023-07-24 00:01:34 +02:00
|
|
|
});
|
2023-07-25 00:19:45 +02:00
|
|
|
pll.divr.map(|divr| {
|
2023-10-11 00:12:33 +02:00
|
|
|
w.set_pllren(true);
|
|
|
|
w.set_pllr(divr);
|
2023-07-25 00:19:45 +02:00
|
|
|
});
|
2023-07-24 00:01:34 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
rcc.cr().modify(|w| w.set_pllon(true));
|
|
|
|
|
|
|
|
while !rcc.cr().read().pllrdy() {}
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
|
|
|
|
rcc.cfgr().modify(|w| {
|
|
|
|
w.set_sw(config.sys.into());
|
2023-10-11 00:12:33 +02:00
|
|
|
w.set_hpre(config.ahb1_pre);
|
|
|
|
w.set_ppre1(config.apb1_pre);
|
|
|
|
w.set_ppre2(config.apb2_pre);
|
2023-07-24 00:01:34 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
rcc.extcfgr().modify(|w| {
|
2023-10-11 00:12:33 +02:00
|
|
|
w.set_c2hpre(config.ahb2_pre);
|
|
|
|
w.set_shdhpre(config.ahb3_pre);
|
2022-01-04 23:58:13 +01:00
|
|
|
});
|
2021-06-14 11:41:02 +02:00
|
|
|
}
|