2022-03-27 17:40:49 +02:00
|
|
|
use crate::pac::flash::vals::Latency;
|
2023-10-09 02:48:22 +02:00
|
|
|
use crate::pac::rcc::vals::Sw;
|
|
|
|
pub use crate::pac::rcc::vals::{
|
|
|
|
Hpre as AHBPrescaler, Pllm as PLLPreDiv, Plln as PLLMul, Pllp as PLLPDiv, Pllq as PLLQDiv, Pllsrc as PLLSrc,
|
|
|
|
Ppre as APBPrescaler,
|
|
|
|
};
|
2023-09-24 23:54:32 +02:00
|
|
|
use crate::pac::{FLASH, RCC};
|
2023-09-06 01:04:09 +02:00
|
|
|
use crate::rcc::bd::BackupDomain;
|
2022-03-27 17:40:49 +02:00
|
|
|
use crate::rcc::{set_freqs, Clocks};
|
2023-09-06 01:04:09 +02:00
|
|
|
use crate::rtc::RtcClockSource;
|
2022-03-27 17:40:49 +02:00
|
|
|
use crate::time::Hertz;
|
|
|
|
|
|
|
|
/// 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);
|
2022-03-27 17:40:49 +02:00
|
|
|
|
2022-04-09 16:40:04 +02:00
|
|
|
#[derive(Clone, Copy)]
|
|
|
|
pub struct HSEConfig {
|
|
|
|
pub frequency: Hertz,
|
|
|
|
pub source: HSESrc,
|
|
|
|
}
|
|
|
|
|
2022-03-27 17:40:49 +02:00
|
|
|
/// System clock mux source
|
|
|
|
#[derive(Clone, Copy)]
|
|
|
|
pub enum ClockSrc {
|
2022-04-09 16:40:04 +02:00
|
|
|
HSE,
|
2022-03-27 17:40:49 +02:00
|
|
|
HSI,
|
2022-04-26 19:33:57 +02:00
|
|
|
PLL,
|
2022-03-27 17:40:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// HSE clock source
|
|
|
|
#[derive(Clone, Copy)]
|
|
|
|
pub enum HSESrc {
|
|
|
|
/// Crystal/ceramic resonator
|
|
|
|
Crystal,
|
|
|
|
/// External clock source, HSE bypassed
|
|
|
|
Bypass,
|
|
|
|
}
|
|
|
|
|
2022-04-26 19:33:57 +02:00
|
|
|
#[derive(Clone, Copy)]
|
|
|
|
pub struct PLLConfig {
|
|
|
|
pub pre_div: PLLPreDiv,
|
|
|
|
pub mul: PLLMul,
|
2023-10-09 02:48:22 +02:00
|
|
|
pub p_div: PLLPDiv,
|
|
|
|
pub q_div: PLLQDiv,
|
2022-04-26 19:33:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for PLLConfig {
|
|
|
|
fn default() -> Self {
|
|
|
|
PLLConfig {
|
2023-10-09 02:48:22 +02:00
|
|
|
pre_div: PLLPreDiv::DIV16,
|
|
|
|
mul: PLLMul::MUL192,
|
|
|
|
p_div: PLLPDiv::DIV2,
|
|
|
|
q_div: PLLQDiv::DIV4,
|
2022-04-26 19:33:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PLLConfig {
|
|
|
|
pub fn clocks(&self, src_freq: Hertz) -> PLLClocks {
|
|
|
|
let in_freq = src_freq / self.pre_div;
|
2023-10-09 02:48:22 +02:00
|
|
|
let vco_freq = src_freq / self.pre_div * self.mul;
|
|
|
|
let main_freq = vco_freq / self.p_div;
|
|
|
|
let pll48_freq = vco_freq / self.q_div;
|
2022-04-26 19:33:57 +02:00
|
|
|
PLLClocks {
|
|
|
|
in_freq,
|
|
|
|
vco_freq,
|
|
|
|
main_freq,
|
|
|
|
pll48_freq,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#[derive(Clone, Copy, PartialEq)]
|
|
|
|
pub struct PLLClocks {
|
|
|
|
pub in_freq: Hertz,
|
|
|
|
pub vco_freq: Hertz,
|
|
|
|
pub main_freq: Hertz,
|
|
|
|
pub pll48_freq: Hertz,
|
|
|
|
}
|
|
|
|
|
2023-09-18 03:00:59 +02:00
|
|
|
/// Voltage range of the power supply used.
|
|
|
|
///
|
|
|
|
/// Used to calculate flash waitstates. See
|
|
|
|
/// RM0033 - Table 3. Number of wait states according to Cortex®-M3 clock frequency
|
|
|
|
pub enum VoltageScale {
|
2023-09-19 10:20:25 +02:00
|
|
|
/// 2.7 to 3.6 V
|
2023-09-18 03:00:59 +02:00
|
|
|
Range0,
|
2023-09-19 10:20:25 +02:00
|
|
|
/// 2.4 to 2.7 V
|
2023-09-18 03:00:59 +02:00
|
|
|
Range1,
|
2023-09-19 10:20:25 +02:00
|
|
|
/// 2.1 to 2.4 V
|
2023-09-18 03:00:59 +02:00
|
|
|
Range2,
|
2023-09-19 10:20:25 +02:00
|
|
|
/// 1.8 to 2.1 V
|
2023-09-18 03:00:59 +02:00
|
|
|
Range3,
|
|
|
|
}
|
2022-03-27 17:40:49 +02:00
|
|
|
|
2023-04-22 21:26:40 +02:00
|
|
|
impl VoltageScale {
|
2022-03-27 17:40:49 +02:00
|
|
|
const fn wait_states(&self, ahb_freq: Hertz) -> Option<Latency> {
|
|
|
|
let ahb_freq = ahb_freq.0;
|
|
|
|
// Reference: RM0033 - Table 3. Number of wait states according to Cortex®-M3 clock
|
|
|
|
// frequency
|
|
|
|
match self {
|
2023-09-18 03:00:59 +02:00
|
|
|
VoltageScale::Range3 => {
|
2022-03-27 17:40:49 +02:00
|
|
|
if ahb_freq <= 16_000_000 {
|
|
|
|
Some(Latency::WS0)
|
|
|
|
} else if ahb_freq <= 32_000_000 {
|
|
|
|
Some(Latency::WS1)
|
|
|
|
} else if ahb_freq <= 48_000_000 {
|
|
|
|
Some(Latency::WS2)
|
|
|
|
} else if ahb_freq <= 64_000_000 {
|
|
|
|
Some(Latency::WS3)
|
|
|
|
} else if ahb_freq <= 80_000_000 {
|
|
|
|
Some(Latency::WS4)
|
|
|
|
} else if ahb_freq <= 96_000_000 {
|
|
|
|
Some(Latency::WS5)
|
|
|
|
} else if ahb_freq <= 112_000_000 {
|
|
|
|
Some(Latency::WS6)
|
|
|
|
} else if ahb_freq <= 120_000_000 {
|
|
|
|
Some(Latency::WS7)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
2023-09-18 03:00:59 +02:00
|
|
|
VoltageScale::Range2 => {
|
2022-03-27 17:40:49 +02:00
|
|
|
if ahb_freq <= 18_000_000 {
|
|
|
|
Some(Latency::WS0)
|
|
|
|
} else if ahb_freq <= 36_000_000 {
|
|
|
|
Some(Latency::WS1)
|
|
|
|
} else if ahb_freq <= 54_000_000 {
|
|
|
|
Some(Latency::WS2)
|
|
|
|
} else if ahb_freq <= 72_000_000 {
|
|
|
|
Some(Latency::WS3)
|
|
|
|
} else if ahb_freq <= 90_000_000 {
|
|
|
|
Some(Latency::WS4)
|
|
|
|
} else if ahb_freq <= 108_000_000 {
|
|
|
|
Some(Latency::WS5)
|
|
|
|
} else if ahb_freq <= 120_000_000 {
|
|
|
|
Some(Latency::WS6)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
2023-09-18 03:00:59 +02:00
|
|
|
VoltageScale::Range1 => {
|
2022-03-27 17:40:49 +02:00
|
|
|
if ahb_freq <= 24_000_000 {
|
|
|
|
Some(Latency::WS0)
|
|
|
|
} else if ahb_freq <= 48_000_000 {
|
|
|
|
Some(Latency::WS1)
|
|
|
|
} else if ahb_freq <= 72_000_000 {
|
|
|
|
Some(Latency::WS2)
|
|
|
|
} else if ahb_freq <= 96_000_000 {
|
|
|
|
Some(Latency::WS3)
|
|
|
|
} else if ahb_freq <= 120_000_000 {
|
|
|
|
Some(Latency::WS4)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
2023-09-18 03:00:59 +02:00
|
|
|
VoltageScale::Range0 => {
|
2022-03-27 17:40:49 +02:00
|
|
|
if ahb_freq <= 30_000_000 {
|
|
|
|
Some(Latency::WS0)
|
|
|
|
} else if ahb_freq <= 60_000_000 {
|
|
|
|
Some(Latency::WS1)
|
|
|
|
} else if ahb_freq <= 90_000_000 {
|
|
|
|
Some(Latency::WS2)
|
|
|
|
} else if ahb_freq <= 120_000_000 {
|
|
|
|
Some(Latency::WS3)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Clocks configuration
|
|
|
|
pub struct Config {
|
2022-04-09 16:40:04 +02:00
|
|
|
pub hse: Option<HSEConfig>,
|
2022-04-26 19:33:57 +02:00
|
|
|
pub hsi: bool,
|
|
|
|
pub pll_mux: PLLSrc,
|
|
|
|
pub pll: PLLConfig,
|
2022-03-27 17:40:49 +02:00
|
|
|
pub mux: ClockSrc,
|
2023-09-06 01:04:09 +02:00
|
|
|
pub rtc: Option<RtcClockSource>,
|
2023-09-18 01:41:45 +02:00
|
|
|
pub lsi: bool,
|
|
|
|
pub lse: Option<Hertz>,
|
2023-04-22 21:26:40 +02:00
|
|
|
pub voltage: VoltageScale,
|
2022-03-27 17:40:49 +02:00
|
|
|
pub ahb_pre: AHBPrescaler,
|
|
|
|
pub apb1_pre: APBPrescaler,
|
|
|
|
pub apb2_pre: APBPrescaler,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Config {
|
|
|
|
#[inline]
|
|
|
|
fn default() -> Config {
|
|
|
|
Config {
|
2022-04-09 16:40:04 +02:00
|
|
|
hse: None,
|
2022-04-26 19:33:57 +02:00
|
|
|
hsi: true,
|
|
|
|
pll_mux: PLLSrc::HSI,
|
|
|
|
pll: PLLConfig::default(),
|
2023-09-18 03:00:59 +02:00
|
|
|
voltage: VoltageScale::Range3,
|
2022-03-27 17:40:49 +02:00
|
|
|
mux: ClockSrc::HSI,
|
2023-09-06 01:04:09 +02:00
|
|
|
rtc: None,
|
2023-09-18 01:41:45 +02:00
|
|
|
lsi: false,
|
|
|
|
lse: None,
|
2023-09-17 00:41:11 +02:00
|
|
|
ahb_pre: AHBPrescaler::DIV1,
|
|
|
|
apb1_pre: APBPrescaler::DIV1,
|
|
|
|
apb2_pre: APBPrescaler::DIV1,
|
2022-03-27 17:40:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-26 19:33:57 +02:00
|
|
|
pub(crate) unsafe fn init(config: Config) {
|
|
|
|
// Make sure HSI is enabled
|
2022-04-09 16:40:04 +02:00
|
|
|
RCC.cr().write(|w| w.set_hsion(true));
|
|
|
|
while !RCC.cr().read().hsirdy() {}
|
|
|
|
|
|
|
|
if let Some(hse_config) = config.hse {
|
2022-04-26 19:33:57 +02:00
|
|
|
RCC.cr().modify(|w| {
|
|
|
|
w.set_hsebyp(match hse_config.source {
|
|
|
|
HSESrc::Bypass => true,
|
|
|
|
HSESrc::Crystal => false,
|
|
|
|
});
|
|
|
|
w.set_hseon(true)
|
|
|
|
});
|
|
|
|
while !RCC.cr().read().hserdy() {}
|
2022-04-09 16:40:04 +02:00
|
|
|
}
|
2022-04-26 19:33:57 +02:00
|
|
|
|
|
|
|
let pll_src_freq = match config.pll_mux {
|
|
|
|
PLLSrc::HSE => {
|
2022-04-30 10:41:17 +02:00
|
|
|
let hse_config = config
|
2022-04-26 19:33:57 +02:00
|
|
|
.hse
|
2022-04-30 10:41:17 +02:00
|
|
|
.unwrap_or_else(|| panic!("HSE must be configured to be used as PLL input"));
|
|
|
|
hse_config.frequency
|
2022-04-26 19:33:57 +02:00
|
|
|
}
|
2022-07-10 19:59:36 +02:00
|
|
|
PLLSrc::HSI => HSI_FREQ,
|
2022-04-26 19:33:57 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
// Reference: STM32F215xx/217xx datasheet Table 33. Main PLL characteristics
|
|
|
|
let pll_clocks = config.pll.clocks(pll_src_freq);
|
|
|
|
assert!(Hertz(950_000) <= pll_clocks.in_freq && pll_clocks.in_freq <= Hertz(2_100_000));
|
|
|
|
assert!(Hertz(192_000_000) <= pll_clocks.vco_freq && pll_clocks.vco_freq <= Hertz(432_000_000));
|
2022-06-12 22:15:44 +02:00
|
|
|
assert!(Hertz(24_000_000) <= pll_clocks.main_freq && pll_clocks.main_freq <= Hertz(120_000_000));
|
2022-04-26 19:33:57 +02:00
|
|
|
// USB actually requires == 48 MHz, but other PLL48 peripherals are fine with <= 48MHz
|
|
|
|
assert!(pll_clocks.pll48_freq <= Hertz(48_000_000));
|
|
|
|
|
|
|
|
RCC.pllcfgr().write(|w| {
|
2023-10-09 02:48:22 +02:00
|
|
|
w.set_pllsrc(config.pll_mux);
|
|
|
|
w.set_pllm(config.pll.pre_div);
|
|
|
|
w.set_plln(config.pll.mul);
|
|
|
|
w.set_pllp(config.pll.p_div);
|
|
|
|
w.set_pllq(config.pll.q_div);
|
2022-04-26 19:33:57 +02:00
|
|
|
});
|
|
|
|
|
2022-03-27 17:40:49 +02:00
|
|
|
let (sys_clk, sw) = match config.mux {
|
|
|
|
ClockSrc::HSI => {
|
2022-04-26 19:33:57 +02:00
|
|
|
assert!(config.hsi, "HSI must be enabled to be used as system clock");
|
2022-07-10 20:46:14 +02:00
|
|
|
(HSI_FREQ, Sw::HSI)
|
2022-03-27 17:40:49 +02:00
|
|
|
}
|
2022-04-09 16:40:04 +02:00
|
|
|
ClockSrc::HSE => {
|
|
|
|
let hse_config = config
|
|
|
|
.hse
|
2022-04-30 10:41:17 +02:00
|
|
|
.unwrap_or_else(|| panic!("HSE must be configured to be used as PLL input"));
|
2022-04-09 16:40:04 +02:00
|
|
|
(hse_config.frequency, Sw::HSE)
|
2022-03-27 17:40:49 +02:00
|
|
|
}
|
2022-04-26 19:33:57 +02:00
|
|
|
ClockSrc::PLL => {
|
|
|
|
RCC.cr().modify(|w| w.set_pllon(true));
|
|
|
|
while !RCC.cr().read().pllrdy() {}
|
|
|
|
(pll_clocks.main_freq, Sw::PLL)
|
|
|
|
}
|
2022-03-27 17:40:49 +02:00
|
|
|
};
|
|
|
|
// RM0033 Figure 9. Clock tree suggests max SYSCLK/HCLK is 168 MHz, but datasheet specifies PLL
|
|
|
|
// max output to be 120 MHz, so there's no way to get higher frequencies
|
|
|
|
assert!(sys_clk <= Hertz(120_000_000));
|
|
|
|
|
|
|
|
let ahb_freq = sys_clk / config.ahb_pre;
|
|
|
|
// Reference: STM32F215xx/217xx datasheet Table 13. General operating conditions
|
|
|
|
assert!(ahb_freq <= Hertz(120_000_000));
|
|
|
|
|
|
|
|
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-03-27 17:40:49 +02:00
|
|
|
pre => {
|
|
|
|
let freq = ahb_freq / pre;
|
|
|
|
(freq, Hertz(freq.0 * 2))
|
|
|
|
}
|
|
|
|
};
|
|
|
|
// Reference: STM32F215xx/217xx datasheet Table 13. General operating conditions
|
|
|
|
assert!(apb1_freq <= Hertz(30_000_000));
|
|
|
|
|
|
|
|
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-03-27 17:40:49 +02:00
|
|
|
pre => {
|
|
|
|
let freq = ahb_freq / pre;
|
|
|
|
(freq, Hertz(freq.0 * 2))
|
|
|
|
}
|
|
|
|
};
|
|
|
|
// Reference: STM32F215xx/217xx datasheet Table 13. General operating conditions
|
|
|
|
assert!(apb2_freq <= Hertz(60_000_000));
|
|
|
|
|
2023-08-16 14:11:09 +02:00
|
|
|
let flash_ws = unwrap!(config.voltage.wait_states(ahb_freq));
|
|
|
|
FLASH.acr().modify(|w| w.set_latency(flash_ws));
|
|
|
|
|
|
|
|
RCC.cfgr().modify(|w| {
|
|
|
|
w.set_sw(sw.into());
|
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-08-16 14:11:09 +02:00
|
|
|
});
|
|
|
|
while RCC.cfgr().read().sws().to_bits() != sw.to_bits() {}
|
|
|
|
|
|
|
|
// Turn off HSI to save power if we don't need it
|
|
|
|
if !config.hsi {
|
|
|
|
RCC.cr().modify(|w| w.set_hsion(false));
|
|
|
|
}
|
|
|
|
|
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-09-06 01:04:09 +02:00
|
|
|
|
2022-03-27 17:40:49 +02:00
|
|
|
set_freqs(Clocks {
|
|
|
|
sys: sys_clk,
|
|
|
|
ahb1: ahb_freq,
|
|
|
|
ahb2: ahb_freq,
|
|
|
|
ahb3: ahb_freq,
|
|
|
|
apb1: apb1_freq,
|
|
|
|
apb1_tim: apb1_tim_freq,
|
|
|
|
apb2: apb2_freq,
|
|
|
|
apb2_tim: apb2_tim_freq,
|
2022-04-26 19:33:57 +02:00
|
|
|
pll48: Some(pll_clocks.pll48_freq),
|
2022-03-27 17:40:49 +02:00
|
|
|
});
|
|
|
|
}
|