2023-09-18 03:00:59 +02:00
|
|
|
pub use crate::pac::pwr::vals::Vos as VoltageScale;
|
2023-10-11 00:12:33 +02:00
|
|
|
use crate::pac::rcc::vals::Sw;
|
|
|
|
pub use crate::pac::rcc::vals::{
|
|
|
|
Adcsel as AdcClockSource, Hpre as AHBPrescaler, Msirange as MSIRange, Pllm, Plln, Pllp, Pllq, Pllr,
|
|
|
|
Pllsrc as PllSource, Ppre as APBPrescaler,
|
|
|
|
};
|
2023-09-07 00:53:02 +02:00
|
|
|
use crate::pac::{FLASH, RCC};
|
2023-08-27 16:41:31 +02:00
|
|
|
use crate::rcc::bd::{BackupDomain, RtcClockSource};
|
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-06-16 15:12:07 +02:00
|
|
|
|
|
|
|
/// HSI speed
|
2022-07-10 19:59:36 +02:00
|
|
|
pub const HSI_FREQ: Hertz = Hertz(16_000_000);
|
2021-06-16 15:12:07 +02:00
|
|
|
|
2022-07-10 19:59:36 +02:00
|
|
|
/// LSI speed
|
|
|
|
pub const LSI_FREQ: Hertz = Hertz(32_000);
|
|
|
|
|
2023-10-11 01:01:27 +02:00
|
|
|
/// HSE speed
|
|
|
|
pub const HSE_FREQ: Hertz = Hertz(32_000_000);
|
2021-08-31 14:32:48 +02:00
|
|
|
|
2021-06-16 15:12:07 +02:00
|
|
|
/// System clock mux source
|
|
|
|
#[derive(Clone, Copy)]
|
|
|
|
pub enum ClockSrc {
|
2022-04-20 13:49:59 +02:00
|
|
|
MSI(MSIRange),
|
2023-10-11 01:01:27 +02:00
|
|
|
HSE,
|
2021-06-16 15:12:07 +02:00
|
|
|
HSI16,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Clocks configutation
|
|
|
|
pub struct Config {
|
2022-01-04 11:18:59 +01:00
|
|
|
pub mux: ClockSrc,
|
|
|
|
pub ahb_pre: AHBPrescaler,
|
2022-04-20 13:49:59 +02:00
|
|
|
pub shd_ahb_pre: AHBPrescaler,
|
2022-01-04 11:18:59 +01:00
|
|
|
pub apb1_pre: APBPrescaler,
|
|
|
|
pub apb2_pre: APBPrescaler,
|
2023-07-15 13:40:23 +02:00
|
|
|
pub rtc_mux: RtcClockSource,
|
2023-09-18 01:41:45 +02:00
|
|
|
pub lse: Option<Hertz>,
|
|
|
|
pub lsi: bool,
|
2023-09-05 12:14:04 +02:00
|
|
|
pub adc_clock_source: AdcClockSource,
|
2021-06-16 15:12:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Config {
|
|
|
|
#[inline]
|
|
|
|
fn default() -> Config {
|
|
|
|
Config {
|
2023-10-11 00:12:33 +02:00
|
|
|
mux: ClockSrc::MSI(MSIRange::RANGE4M),
|
2023-09-17 00:41:11 +02:00
|
|
|
ahb_pre: AHBPrescaler::DIV1,
|
|
|
|
shd_ahb_pre: AHBPrescaler::DIV1,
|
|
|
|
apb1_pre: APBPrescaler::DIV1,
|
|
|
|
apb2_pre: APBPrescaler::DIV1,
|
2023-08-27 16:41:31 +02:00
|
|
|
rtc_mux: RtcClockSource::LSI,
|
2023-09-18 01:41:45 +02:00
|
|
|
lsi: true,
|
|
|
|
lse: None,
|
2023-10-11 00:12:33 +02:00
|
|
|
adc_clock_source: AdcClockSource::HSI16,
|
2021-06-16 15:12:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-04 23:58:13 +01:00
|
|
|
pub(crate) unsafe fn init(config: Config) {
|
2022-04-20 13:49:59 +02:00
|
|
|
let (sys_clk, sw, vos) = match config.mux {
|
2023-10-11 00:12:33 +02:00
|
|
|
ClockSrc::HSI16 => (HSI_FREQ, Sw::HSI16, VoltageScale::RANGE2),
|
2023-10-11 01:01:27 +02:00
|
|
|
ClockSrc::HSE => (HSE_FREQ, Sw::HSE, VoltageScale::RANGE1),
|
2023-10-11 00:12:33 +02:00
|
|
|
ClockSrc::MSI(range) => (msirange_to_hertz(range), Sw::MSI, msirange_to_vos(range)),
|
2022-01-04 23:58:13 +01:00
|
|
|
};
|
|
|
|
|
2023-10-11 00:12:33 +02:00
|
|
|
let ahb_freq = sys_clk / config.ahb_pre;
|
|
|
|
let shd_ahb_freq = sys_clk / config.shd_ahb_pre;
|
2022-04-20 13:49:59 +02:00
|
|
|
|
2022-01-04 23:58:13 +01:00
|
|
|
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 => {
|
2023-10-11 00:12:33 +02:00
|
|
|
let freq = ahb_freq / pre;
|
|
|
|
(freq, freq * 2u32)
|
2021-06-16 15:12:07 +02:00
|
|
|
}
|
2022-01-04 23:58:13 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
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 => {
|
2023-10-11 00:12:33 +02:00
|
|
|
let freq = ahb_freq / pre;
|
|
|
|
(freq, freq * 2u32)
|
2022-01-04 23:58:13 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-04-20 13:49:59 +02:00
|
|
|
// Adjust flash latency
|
2023-10-11 00:12:33 +02:00
|
|
|
let flash_clk_src_freq = shd_ahb_freq;
|
2022-04-20 13:49:59 +02:00
|
|
|
let ws = match vos {
|
2023-10-11 00:12:33 +02:00
|
|
|
VoltageScale::RANGE1 => match flash_clk_src_freq.0 {
|
2022-04-20 13:49:59 +02:00
|
|
|
0..=18_000_000 => 0b000,
|
|
|
|
18_000_001..=36_000_000 => 0b001,
|
|
|
|
_ => 0b010,
|
|
|
|
},
|
2023-10-11 00:12:33 +02:00
|
|
|
VoltageScale::RANGE2 => match flash_clk_src_freq.0 {
|
2022-04-20 13:49:59 +02:00
|
|
|
0..=6_000_000 => 0b000,
|
|
|
|
6_000_001..=12_000_000 => 0b001,
|
|
|
|
_ => 0b010,
|
|
|
|
},
|
2023-09-18 03:00:59 +02:00
|
|
|
_ => unreachable!(),
|
2022-04-20 13:49:59 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
FLASH.acr().modify(|w| {
|
|
|
|
w.set_latency(ws);
|
|
|
|
});
|
|
|
|
|
|
|
|
while FLASH.acr().read().latency() != ws {}
|
|
|
|
|
2023-09-09 01:20:58 +02:00
|
|
|
// Enables the LSI if configured
|
2023-09-18 01:41:45 +02:00
|
|
|
BackupDomain::configure_ls(config.rtc_mux, config.lsi, config.lse.map(|_| Default::default()));
|
2023-07-15 13:40:23 +02:00
|
|
|
|
2022-06-26 22:59:39 +02:00
|
|
|
match config.mux {
|
|
|
|
ClockSrc::HSI16 => {
|
|
|
|
// Enable HSI16
|
|
|
|
RCC.cr().write(|w| w.set_hsion(true));
|
|
|
|
while !RCC.cr().read().hsirdy() {}
|
|
|
|
}
|
2023-10-11 01:01:27 +02:00
|
|
|
ClockSrc::HSE => {
|
|
|
|
// Enable HSE
|
2022-06-26 22:59:39 +02:00
|
|
|
RCC.cr().write(|w| {
|
|
|
|
w.set_hsebyppwr(true);
|
|
|
|
w.set_hseon(true);
|
|
|
|
});
|
|
|
|
while !RCC.cr().read().hserdy() {}
|
|
|
|
}
|
|
|
|
ClockSrc::MSI(range) => {
|
|
|
|
let cr = RCC.cr().read();
|
|
|
|
assert!(!cr.msion() || cr.msirdy());
|
|
|
|
RCC.cr().write(|w| {
|
|
|
|
w.set_msirgsel(true);
|
2023-10-11 00:12:33 +02:00
|
|
|
w.set_msirange(range);
|
2022-06-26 22:59:39 +02:00
|
|
|
w.set_msion(true);
|
2023-07-15 13:40:23 +02:00
|
|
|
|
2023-10-03 23:45:05 +02:00
|
|
|
if config.rtc_mux == RtcClockSource::LSE {
|
2023-07-15 13:40:23 +02:00
|
|
|
// If LSE is enabled, enable calibration of MSI
|
|
|
|
w.set_msipllen(true);
|
|
|
|
} else {
|
|
|
|
w.set_msipllen(false);
|
|
|
|
}
|
2022-06-26 22:59:39 +02:00
|
|
|
});
|
|
|
|
while !RCC.cr().read().msirdy() {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
RCC.extcfgr().modify(|w| {
|
2023-10-11 00:12:33 +02:00
|
|
|
w.set_shdhpre(config.shd_ahb_pre);
|
2022-06-26 22:59:39 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
RCC.cfgr().modify(|w| {
|
|
|
|
w.set_sw(sw.into());
|
2023-09-17 00:41:11 +02:00
|
|
|
w.set_hpre(config.ahb_pre);
|
2023-10-11 00:12:33 +02:00
|
|
|
w.set_ppre1(config.apb1_pre);
|
|
|
|
w.set_ppre2(config.apb2_pre);
|
2022-06-26 22:59:39 +02:00
|
|
|
});
|
|
|
|
|
2023-09-05 12:14:04 +02:00
|
|
|
// ADC clock MUX
|
2023-10-11 00:12:33 +02:00
|
|
|
RCC.ccipr().modify(|w| w.set_adcsel(config.adc_clock_source));
|
2023-09-05 12:14:04 +02:00
|
|
|
|
2022-06-26 22:59:39 +02:00
|
|
|
// TODO: switch voltage range
|
|
|
|
|
2022-01-04 23:58:13 +01:00
|
|
|
set_freqs(Clocks {
|
2023-10-11 00:12:33 +02:00
|
|
|
sys: sys_clk,
|
|
|
|
ahb1: ahb_freq,
|
|
|
|
ahb2: ahb_freq,
|
|
|
|
ahb3: shd_ahb_freq,
|
|
|
|
apb1: apb1_freq,
|
|
|
|
apb2: apb2_freq,
|
|
|
|
apb3: shd_ahb_freq,
|
|
|
|
apb1_tim: apb1_tim_freq,
|
|
|
|
apb2_tim: apb2_tim_freq,
|
2022-01-04 23:58:13 +01:00
|
|
|
});
|
2021-06-16 15:12:07 +02:00
|
|
|
}
|
2023-10-11 00:12:33 +02:00
|
|
|
|
|
|
|
fn msirange_to_hertz(range: MSIRange) -> Hertz {
|
|
|
|
match range {
|
|
|
|
MSIRange::RANGE100K => Hertz(100_000),
|
|
|
|
MSIRange::RANGE200K => Hertz(200_000),
|
|
|
|
MSIRange::RANGE400K => Hertz(400_000),
|
|
|
|
MSIRange::RANGE800K => Hertz(800_000),
|
|
|
|
MSIRange::RANGE1M => Hertz(1_000_000),
|
|
|
|
MSIRange::RANGE2M => Hertz(2_000_000),
|
|
|
|
MSIRange::RANGE4M => Hertz(4_000_000),
|
|
|
|
MSIRange::RANGE8M => Hertz(8_000_000),
|
|
|
|
MSIRange::RANGE16M => Hertz(16_000_000),
|
|
|
|
MSIRange::RANGE24M => Hertz(24_000_000),
|
|
|
|
MSIRange::RANGE32M => Hertz(32_000_000),
|
|
|
|
MSIRange::RANGE48M => Hertz(48_000_000),
|
|
|
|
_ => unreachable!(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn msirange_to_vos(range: MSIRange) -> VoltageScale {
|
|
|
|
if range.to_bits() > MSIRange::RANGE16M.to_bits() {
|
|
|
|
VoltageScale::RANGE1
|
|
|
|
} else {
|
|
|
|
VoltageScale::RANGE2
|
|
|
|
}
|
|
|
|
}
|