2023-09-16 03:44:01 +02:00
|
|
|
use stm32_metapac::rcc::vals::{Pllsrc, Sw};
|
|
|
|
|
|
|
|
use crate::pac::{FLASH, RCC};
|
|
|
|
use crate::rcc::{set_freqs, Clocks};
|
|
|
|
use crate::time::Hertz;
|
|
|
|
|
|
|
|
/// HSI speed
|
|
|
|
pub const HSI_FREQ: Hertz = Hertz(16_000_000);
|
|
|
|
|
|
|
|
pub use crate::pac::pwr::vals::Vos as VoltageScale;
|
|
|
|
pub use crate::pac::rcc::vals::{Hpre as AHBPrescaler, Ppre as APBPrescaler};
|
|
|
|
|
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
pub enum ClockSrc {
|
|
|
|
HSE(Hertz),
|
|
|
|
HSI16,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Copy, Debug)]
|
|
|
|
pub enum PllSrc {
|
|
|
|
HSE(Hertz),
|
|
|
|
HSI16,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Into<Pllsrc> for PllSrc {
|
|
|
|
fn into(self) -> Pllsrc {
|
|
|
|
match self {
|
2023-10-11 01:01:27 +02:00
|
|
|
PllSrc::HSE(..) => Pllsrc::HSE,
|
2023-09-16 03:44:01 +02:00
|
|
|
PllSrc::HSI16 => Pllsrc::HSI16,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Into<Sw> for ClockSrc {
|
|
|
|
fn into(self) -> Sw {
|
|
|
|
match self {
|
2023-10-11 01:01:27 +02:00
|
|
|
ClockSrc::HSE(..) => Sw::HSE,
|
2023-09-16 03:44:01 +02:00
|
|
|
ClockSrc::HSI16 => Sw::HSI16,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Config {
|
|
|
|
pub mux: ClockSrc,
|
|
|
|
pub ahb_pre: AHBPrescaler,
|
|
|
|
pub apb1_pre: APBPrescaler,
|
|
|
|
pub apb2_pre: APBPrescaler,
|
|
|
|
pub apb7_pre: APBPrescaler,
|
2023-10-11 03:53:27 +02:00
|
|
|
pub ls: super::LsConfig,
|
2023-09-16 03:44:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Config {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
|
|
|
mux: ClockSrc::HSI16,
|
|
|
|
ahb_pre: AHBPrescaler::DIV1,
|
|
|
|
apb1_pre: APBPrescaler::DIV1,
|
|
|
|
apb2_pre: APBPrescaler::DIV1,
|
|
|
|
apb7_pre: APBPrescaler::DIV1,
|
2023-10-11 03:53:27 +02:00
|
|
|
ls: Default::default(),
|
2023-09-16 03:44:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) unsafe fn init(config: Config) {
|
|
|
|
let sys_clk = match config.mux {
|
|
|
|
ClockSrc::HSE(freq) => {
|
|
|
|
RCC.cr().write(|w| w.set_hseon(true));
|
|
|
|
while !RCC.cr().read().hserdy() {}
|
|
|
|
|
2023-09-17 02:30:50 +02:00
|
|
|
freq
|
2023-09-16 03:44:01 +02:00
|
|
|
}
|
|
|
|
ClockSrc::HSI16 => {
|
|
|
|
RCC.cr().write(|w| w.set_hsion(true));
|
|
|
|
while !RCC.cr().read().hsirdy() {}
|
|
|
|
|
2023-09-17 02:30:50 +02:00
|
|
|
HSI_FREQ
|
2023-09-16 03:44:01 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// TODO make configurable
|
|
|
|
let power_vos = VoltageScale::RANGE1;
|
|
|
|
|
|
|
|
// states and programming delay
|
|
|
|
let wait_states = match power_vos {
|
2023-09-17 02:30:50 +02:00
|
|
|
VoltageScale::RANGE1 => match sys_clk.0 {
|
2023-09-16 03:44:01 +02:00
|
|
|
..=32_000_000 => 0,
|
|
|
|
..=64_000_000 => 1,
|
|
|
|
..=96_000_000 => 2,
|
|
|
|
..=100_000_000 => 3,
|
|
|
|
_ => 4,
|
|
|
|
},
|
2023-09-17 02:30:50 +02:00
|
|
|
VoltageScale::RANGE2 => match sys_clk.0 {
|
2023-09-16 03:44:01 +02:00
|
|
|
..=8_000_000 => 0,
|
|
|
|
..=16_000_000 => 1,
|
|
|
|
_ => 2,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
FLASH.acr().modify(|w| {
|
|
|
|
w.set_latency(wait_states);
|
|
|
|
});
|
|
|
|
|
|
|
|
RCC.cfgr1().modify(|w| {
|
|
|
|
w.set_sw(config.mux.into());
|
|
|
|
});
|
|
|
|
|
|
|
|
RCC.cfgr2().modify(|w| {
|
2023-10-11 00:12:33 +02:00
|
|
|
w.set_hpre(config.ahb_pre);
|
|
|
|
w.set_ppre1(config.apb1_pre);
|
|
|
|
w.set_ppre2(config.apb2_pre);
|
2023-09-16 03:44:01 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
RCC.cfgr3().modify(|w| {
|
2023-10-11 00:12:33 +02:00
|
|
|
w.set_ppre7(config.apb7_pre);
|
2023-09-16 03:44:01 +02:00
|
|
|
});
|
|
|
|
|
2023-09-17 02:30:50 +02:00
|
|
|
let ahb_freq = sys_clk / config.ahb_pre;
|
|
|
|
let (apb1_freq, apb1_tim_freq) = match config.apb1_pre {
|
|
|
|
APBPrescaler::DIV1 => (ahb_freq, ahb_freq),
|
|
|
|
pre => {
|
|
|
|
let freq = ahb_freq / pre;
|
|
|
|
(freq, freq * 2u32)
|
2023-09-16 03:44:01 +02:00
|
|
|
}
|
|
|
|
};
|
2023-09-17 02:30:50 +02:00
|
|
|
let (apb2_freq, apb2_tim_freq) = match config.apb2_pre {
|
|
|
|
APBPrescaler::DIV1 => (ahb_freq, ahb_freq),
|
|
|
|
pre => {
|
|
|
|
let freq = ahb_freq / pre;
|
|
|
|
(freq, freq * 2u32)
|
2023-09-16 03:44:01 +02:00
|
|
|
}
|
|
|
|
};
|
2023-09-17 02:30:50 +02:00
|
|
|
let (apb7_freq, _apb7_tim_freq) = match config.apb7_pre {
|
|
|
|
APBPrescaler::DIV1 => (ahb_freq, ahb_freq),
|
|
|
|
pre => {
|
|
|
|
let freq = ahb_freq / pre;
|
|
|
|
(freq, freq * 2u32)
|
2023-09-16 03:44:01 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-10-11 03:53:27 +02:00
|
|
|
let rtc = config.ls.init();
|
|
|
|
|
2023-09-16 03:44:01 +02:00
|
|
|
set_freqs(Clocks {
|
2023-09-17 02:30:50 +02:00
|
|
|
sys: sys_clk,
|
2023-10-16 02:51:35 +02:00
|
|
|
hclk1: ahb_freq,
|
|
|
|
hclk2: ahb_freq,
|
|
|
|
hclk4: ahb_freq,
|
|
|
|
pclk1: apb1_freq,
|
|
|
|
pclk2: apb2_freq,
|
|
|
|
pclk7: apb7_freq,
|
|
|
|
pclk1_tim: apb1_tim_freq,
|
|
|
|
pclk2_tim: apb2_tim_freq,
|
2023-10-11 03:53:27 +02:00
|
|
|
rtc,
|
2023-09-16 03:44:01 +02:00
|
|
|
});
|
|
|
|
}
|