2022-01-04 23:58:13 +01:00
|
|
|
use crate::pac::flash::vals::Latency;
|
2024-02-14 00:10:59 +01:00
|
|
|
#[cfg(stm32f1)]
|
|
|
|
pub use crate::pac::rcc::vals::Adcpre as ADCPrescaler;
|
|
|
|
#[cfg(stm32f3)]
|
|
|
|
pub use crate::pac::rcc::vals::Adcpres as AdcPllPrescaler;
|
|
|
|
use crate::pac::rcc::vals::Pllsrc;
|
|
|
|
#[cfg(stm32f1)]
|
|
|
|
pub use crate::pac::rcc::vals::Pllxtpre as PllPreDiv;
|
|
|
|
#[cfg(any(stm32f0, stm32f3))]
|
|
|
|
pub use crate::pac::rcc::vals::Prediv as PllPreDiv;
|
|
|
|
pub use crate::pac::rcc::vals::{Hpre as AHBPrescaler, Pllmul as PllMul, Ppre as APBPrescaler, Sw as Sysclk};
|
2022-01-04 23:58:13 +01:00
|
|
|
use crate::pac::{FLASH, RCC};
|
2021-12-10 11:40:03 +05:30
|
|
|
use crate::time::Hertz;
|
|
|
|
|
2022-07-10 20:59:36 +03:00
|
|
|
/// HSI speed
|
|
|
|
pub const HSI_FREQ: Hertz = Hertz(8_000_000);
|
|
|
|
|
2024-02-12 02:17:33 +01:00
|
|
|
#[derive(Clone, Copy, Eq, PartialEq)]
|
|
|
|
pub enum HseMode {
|
|
|
|
/// crystal/ceramic oscillator (HSEBYP=0)
|
|
|
|
Oscillator,
|
|
|
|
/// external analog clock (low swing) (HSEBYP=1)
|
|
|
|
Bypass,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Copy, Eq, PartialEq)]
|
|
|
|
pub struct Hse {
|
|
|
|
/// HSE frequency.
|
|
|
|
pub freq: Hertz,
|
|
|
|
/// HSE mode.
|
|
|
|
pub mode: HseMode,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Copy, Eq, PartialEq)]
|
|
|
|
pub enum PllSource {
|
|
|
|
HSE,
|
|
|
|
HSI,
|
2024-02-14 00:10:59 +01:00
|
|
|
#[cfg(rcc_f0v4)]
|
|
|
|
HSI48,
|
2024-02-12 02:17:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Copy)]
|
|
|
|
pub struct Pll {
|
|
|
|
pub src: PllSource,
|
|
|
|
|
|
|
|
/// PLL pre-divider.
|
|
|
|
///
|
2024-02-14 00:10:59 +01:00
|
|
|
/// On some chips, this must be 2 if `src == HSI`. Init will panic if this is not the case.
|
2024-02-12 02:17:33 +01:00
|
|
|
pub prediv: PllPreDiv,
|
|
|
|
|
|
|
|
/// PLL multiplication factor.
|
|
|
|
pub mul: PllMul,
|
2023-09-05 16:46:57 -05:00
|
|
|
}
|
|
|
|
|
2024-02-14 00:10:59 +01:00
|
|
|
#[cfg(all(stm32f3, not(rcc_f37)))]
|
2023-09-05 16:46:57 -05:00
|
|
|
#[derive(Clone, Copy)]
|
2023-09-10 13:33:17 -05:00
|
|
|
pub enum AdcClockSource {
|
2024-02-12 02:17:33 +01:00
|
|
|
Pll(AdcPllPrescaler),
|
|
|
|
Hclk(AdcHclkPrescaler),
|
2023-09-10 13:33:17 -05:00
|
|
|
}
|
|
|
|
|
2024-02-14 00:10:59 +01:00
|
|
|
#[cfg(all(stm32f3, not(rcc_f37)))]
|
2024-02-12 02:17:33 +01:00
|
|
|
#[derive(Clone, Copy, PartialEq, Eq)]
|
|
|
|
pub enum AdcHclkPrescaler {
|
|
|
|
Div1,
|
|
|
|
Div2,
|
|
|
|
Div4,
|
2023-09-10 13:33:17 -05:00
|
|
|
}
|
|
|
|
|
2024-02-14 00:10:59 +01:00
|
|
|
#[cfg(stm32f334)]
|
2024-02-12 02:17:33 +01:00
|
|
|
#[derive(Clone, Copy, PartialEq, Eq)]
|
2023-09-10 13:33:17 -05:00
|
|
|
pub enum HrtimClockSource {
|
|
|
|
BusClk,
|
|
|
|
PllClk,
|
2023-09-05 16:46:57 -05:00
|
|
|
}
|
|
|
|
|
2021-12-10 11:40:03 +05:30
|
|
|
/// Clocks configutation
|
|
|
|
#[non_exhaustive]
|
|
|
|
pub struct Config {
|
2024-02-12 02:17:33 +01:00
|
|
|
pub hsi: bool,
|
|
|
|
pub hse: Option<Hse>,
|
2024-02-14 00:10:59 +01:00
|
|
|
#[cfg(crs)]
|
|
|
|
pub hsi48: Option<super::Hsi48Config>,
|
2024-02-12 02:17:33 +01:00
|
|
|
pub sys: Sysclk,
|
|
|
|
|
|
|
|
pub pll: Option<Pll>,
|
|
|
|
|
|
|
|
pub ahb_pre: AHBPrescaler,
|
|
|
|
pub apb1_pre: APBPrescaler,
|
2024-02-14 00:10:59 +01:00
|
|
|
#[cfg(not(stm32f0))]
|
2024-02-12 02:17:33 +01:00
|
|
|
pub apb2_pre: APBPrescaler,
|
|
|
|
|
2024-02-14 00:10:59 +01:00
|
|
|
#[cfg(stm32f1)]
|
|
|
|
pub adc_pre: ADCPrescaler,
|
|
|
|
|
|
|
|
#[cfg(all(stm32f3, not(rcc_f37)))]
|
2024-02-12 02:17:33 +01:00
|
|
|
pub adc: AdcClockSource,
|
2024-02-14 00:10:59 +01:00
|
|
|
#[cfg(all(stm32f3, not(rcc_f37), adc3_common))]
|
2024-02-12 02:17:33 +01:00
|
|
|
pub adc34: AdcClockSource,
|
2024-02-24 12:54:58 -08:00
|
|
|
|
2024-02-26 03:28:27 +01:00
|
|
|
/// Per-peripheral kernel clock selection muxes
|
|
|
|
pub mux: super::mux::ClockMux,
|
2024-02-12 02:17:33 +01:00
|
|
|
|
2023-10-11 03:53:27 +02:00
|
|
|
pub ls: super::LsConfig,
|
2021-12-10 11:40:03 +05:30
|
|
|
}
|
|
|
|
|
2024-02-12 02:17:33 +01:00
|
|
|
impl Default for Config {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
|
|
|
hsi: true,
|
|
|
|
hse: None,
|
2024-02-14 00:10:59 +01:00
|
|
|
#[cfg(crs)]
|
|
|
|
hsi48: Some(Default::default()),
|
2024-02-12 02:17:33 +01:00
|
|
|
sys: Sysclk::HSI,
|
|
|
|
pll: None,
|
|
|
|
ahb_pre: AHBPrescaler::DIV1,
|
|
|
|
apb1_pre: APBPrescaler::DIV1,
|
2024-02-14 00:10:59 +01:00
|
|
|
#[cfg(not(stm32f0))]
|
2024-02-12 02:17:33 +01:00
|
|
|
apb2_pre: APBPrescaler::DIV1,
|
|
|
|
ls: Default::default(),
|
|
|
|
|
2024-02-14 00:10:59 +01:00
|
|
|
#[cfg(stm32f1)]
|
|
|
|
// ensure ADC is not out of range by default even if APB2 is maxxed out (36mhz)
|
|
|
|
adc_pre: ADCPrescaler::DIV6,
|
|
|
|
|
|
|
|
#[cfg(all(stm32f3, not(rcc_f37)))]
|
2024-02-12 02:17:33 +01:00
|
|
|
adc: AdcClockSource::Hclk(AdcHclkPrescaler::Div1),
|
2024-02-14 00:10:59 +01:00
|
|
|
#[cfg(all(stm32f3, not(rcc_f37), adc3_common))]
|
2024-02-12 02:17:33 +01:00
|
|
|
adc34: AdcClockSource::Hclk(AdcHclkPrescaler::Div1),
|
2024-02-24 12:54:58 -08:00
|
|
|
|
2024-02-20 17:54:35 -08:00
|
|
|
mux: Default::default(),
|
2024-02-12 02:17:33 +01:00
|
|
|
}
|
|
|
|
}
|
2021-12-10 11:40:03 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/// Initialize and Set the clock frequencies
|
2022-01-04 19:25:50 +01:00
|
|
|
pub(crate) unsafe fn init(config: Config) {
|
2024-05-27 17:31:29 +02:00
|
|
|
// Turn on the HSI
|
|
|
|
RCC.cr().modify(|w| w.set_hsion(true));
|
|
|
|
while !RCC.cr().read().hsirdy() {}
|
|
|
|
|
|
|
|
// Use the HSI clock as system clock during the actual clock setup
|
|
|
|
RCC.cfgr().modify(|w| w.set_sw(Sysclk::HSI));
|
|
|
|
while RCC.cfgr().read().sws() != Sysclk::HSI {}
|
|
|
|
|
2024-02-12 02:17:33 +01:00
|
|
|
// Configure HSI
|
|
|
|
let hsi = match config.hsi {
|
2024-05-27 17:31:29 +02:00
|
|
|
false => None,
|
|
|
|
true => Some(HSI_FREQ),
|
2022-01-04 23:58:13 +01:00
|
|
|
};
|
2021-12-10 11:40:03 +05:30
|
|
|
|
2024-02-12 02:17:33 +01:00
|
|
|
// Configure HSE
|
|
|
|
let hse = match config.hse {
|
|
|
|
None => {
|
|
|
|
RCC.cr().modify(|w| w.set_hseon(false));
|
|
|
|
None
|
2022-05-11 20:54:09 +02:00
|
|
|
}
|
2024-02-12 02:17:33 +01:00
|
|
|
Some(hse) => {
|
|
|
|
match hse.mode {
|
|
|
|
HseMode::Bypass => assert!(max::HSE_BYP.contains(&hse.freq)),
|
|
|
|
HseMode::Oscillator => assert!(max::HSE_OSC.contains(&hse.freq)),
|
|
|
|
}
|
2021-12-10 11:40:03 +05:30
|
|
|
|
2024-02-12 02:17:33 +01:00
|
|
|
RCC.cr().modify(|w| w.set_hsebyp(hse.mode != HseMode::Oscillator));
|
|
|
|
RCC.cr().modify(|w| w.set_hseon(true));
|
|
|
|
while !RCC.cr().read().hserdy() {}
|
|
|
|
Some(hse.freq)
|
|
|
|
}
|
|
|
|
};
|
2021-12-10 11:40:03 +05:30
|
|
|
|
2024-02-14 00:10:59 +01:00
|
|
|
// configure HSI48
|
|
|
|
#[cfg(crs)]
|
|
|
|
let hsi48 = config.hsi48.map(|config| super::init_hsi48(config));
|
|
|
|
#[cfg(not(crs))]
|
|
|
|
let hsi48: Option<Hertz> = None;
|
|
|
|
|
2022-01-04 23:58:13 +01:00
|
|
|
// Enable PLL
|
2024-02-12 02:17:33 +01:00
|
|
|
let pll = config.pll.map(|pll| {
|
|
|
|
let (src_val, src_freq) = match pll.src {
|
2024-02-14 00:10:59 +01:00
|
|
|
#[cfg(any(rcc_f0v3, rcc_f0v4, rcc_f3v3))]
|
2024-02-12 02:17:33 +01:00
|
|
|
PllSource::HSI => (Pllsrc::HSI_DIV_PREDIV, unwrap!(hsi)),
|
2024-02-14 00:10:59 +01:00
|
|
|
#[cfg(not(any(rcc_f0v3, rcc_f0v4, rcc_f3v3)))]
|
2024-02-12 02:17:33 +01:00
|
|
|
PllSource::HSI => {
|
|
|
|
if pll.prediv != PllPreDiv::DIV2 {
|
|
|
|
panic!("if PLL source is HSI, PLL prediv must be 2.");
|
|
|
|
}
|
|
|
|
(Pllsrc::HSI_DIV2, unwrap!(hsi))
|
|
|
|
}
|
|
|
|
PllSource::HSE => (Pllsrc::HSE_DIV_PREDIV, unwrap!(hse)),
|
2024-02-14 00:10:59 +01:00
|
|
|
#[cfg(rcc_f0v4)]
|
|
|
|
PllSource::HSI48 => (Pllsrc::HSI48_DIV_PREDIV, unwrap!(hsi48)),
|
2024-02-12 02:17:33 +01:00
|
|
|
};
|
|
|
|
let in_freq = src_freq / pll.prediv;
|
|
|
|
assert!(max::PLL_IN.contains(&in_freq));
|
|
|
|
let out_freq = in_freq * pll.mul;
|
|
|
|
assert!(max::PLL_OUT.contains(&out_freq));
|
|
|
|
|
2024-02-14 00:10:59 +01:00
|
|
|
#[cfg(not(stm32f1))]
|
2024-02-12 02:17:33 +01:00
|
|
|
RCC.cfgr2().modify(|w| w.set_prediv(pll.prediv));
|
2022-05-11 20:56:57 +02:00
|
|
|
RCC.cfgr().modify(|w| {
|
2024-02-12 02:17:33 +01:00
|
|
|
w.set_pllmul(pll.mul);
|
|
|
|
w.set_pllsrc(src_val);
|
2024-02-14 00:10:59 +01:00
|
|
|
#[cfg(stm32f1)]
|
|
|
|
w.set_pllxtpre(pll.prediv);
|
2022-01-04 23:58:13 +01:00
|
|
|
});
|
|
|
|
RCC.cr().modify(|w| w.set_pllon(true));
|
|
|
|
while !RCC.cr().read().pllrdy() {}
|
2021-12-10 11:40:03 +05:30
|
|
|
|
2024-02-12 02:17:33 +01:00
|
|
|
out_freq
|
|
|
|
});
|
|
|
|
|
2024-02-25 16:12:32 -08:00
|
|
|
#[cfg(stm32f3)]
|
2024-02-25 16:25:42 -08:00
|
|
|
let pll_mul_2 = pll.map(|pll| pll * 2u32);
|
2024-02-25 16:12:32 -08:00
|
|
|
|
2024-02-14 00:10:59 +01:00
|
|
|
#[cfg(any(rcc_f1, rcc_f1cl, stm32f3))]
|
2024-02-12 02:17:33 +01:00
|
|
|
let usb = match pll {
|
2024-02-14 00:10:59 +01:00
|
|
|
Some(Hertz(72_000_000)) => Some(crate::pac::rcc::vals::Usbpre::DIV1_5),
|
|
|
|
Some(Hertz(48_000_000)) => Some(crate::pac::rcc::vals::Usbpre::DIV1),
|
2024-02-12 02:17:33 +01:00
|
|
|
_ => None,
|
2024-02-14 00:10:59 +01:00
|
|
|
}
|
|
|
|
.map(|usbpre| {
|
|
|
|
RCC.cfgr().modify(|w| w.set_usbpre(usbpre));
|
|
|
|
Hertz(48_000_000)
|
|
|
|
});
|
2024-02-12 02:17:33 +01:00
|
|
|
|
|
|
|
// Configure sysclk
|
|
|
|
let sys = match config.sys {
|
|
|
|
Sysclk::HSI => unwrap!(hsi),
|
|
|
|
Sysclk::HSE => unwrap!(hse),
|
|
|
|
Sysclk::PLL1_P => unwrap!(pll),
|
|
|
|
_ => unreachable!(),
|
|
|
|
};
|
|
|
|
|
|
|
|
let hclk = sys / config.ahb_pre;
|
|
|
|
let (pclk1, pclk1_tim) = super::util::calc_pclk(hclk, config.apb1_pre);
|
2024-02-14 00:10:59 +01:00
|
|
|
#[cfg(not(stm32f0))]
|
2024-02-12 02:17:33 +01:00
|
|
|
let (pclk2, pclk2_tim) = super::util::calc_pclk(hclk, config.apb2_pre);
|
2024-02-14 00:10:59 +01:00
|
|
|
#[cfg(stm32f0)]
|
|
|
|
let (pclk2, pclk2_tim) = (pclk1, pclk1_tim);
|
2024-02-12 02:17:33 +01:00
|
|
|
|
|
|
|
assert!(max::HCLK.contains(&hclk));
|
|
|
|
assert!(max::PCLK1.contains(&pclk1));
|
2024-02-14 00:10:59 +01:00
|
|
|
#[cfg(not(stm32f0))]
|
2024-02-12 02:17:33 +01:00
|
|
|
assert!(max::PCLK2.contains(&pclk2));
|
|
|
|
|
2024-02-14 00:10:59 +01:00
|
|
|
#[cfg(stm32f1)]
|
|
|
|
let adc = pclk2 / config.adc_pre;
|
|
|
|
#[cfg(stm32f1)]
|
|
|
|
assert!(max::ADC.contains(&adc));
|
|
|
|
|
2024-02-12 02:17:33 +01:00
|
|
|
// Set latency based on HCLK frquency
|
2024-02-14 00:10:59 +01:00
|
|
|
#[cfg(stm32f0)]
|
|
|
|
let latency = match hclk.0 {
|
|
|
|
..=24_000_000 => Latency::WS0,
|
|
|
|
_ => Latency::WS1,
|
|
|
|
};
|
|
|
|
#[cfg(any(stm32f1, stm32f3))]
|
2024-02-12 02:17:33 +01:00
|
|
|
let latency = match hclk.0 {
|
|
|
|
..=24_000_000 => Latency::WS0,
|
|
|
|
..=48_000_000 => Latency::WS1,
|
|
|
|
_ => Latency::WS2,
|
|
|
|
};
|
|
|
|
FLASH.acr().modify(|w| {
|
|
|
|
w.set_latency(latency);
|
|
|
|
// RM0316: "The prefetch buffer must be kept on when using a prescaler
|
|
|
|
// different from 1 on the AHB clock.", "Half-cycle access cannot be
|
|
|
|
// used when there is a prescaler different from 1 on the AHB clock"
|
2024-02-14 00:10:59 +01:00
|
|
|
#[cfg(stm32f3)]
|
2024-02-12 02:17:33 +01:00
|
|
|
if config.ahb_pre != AHBPrescaler::DIV1 {
|
|
|
|
w.set_hlfcya(false);
|
|
|
|
w.set_prftbe(true);
|
|
|
|
}
|
2024-02-14 00:10:59 +01:00
|
|
|
#[cfg(not(stm32f3))]
|
|
|
|
w.set_prftbe(true);
|
2024-02-12 02:17:33 +01:00
|
|
|
});
|
2021-12-10 11:40:03 +05:30
|
|
|
|
2022-01-04 23:58:13 +01:00
|
|
|
// Set prescalers
|
2022-05-11 20:56:57 +02:00
|
|
|
// CFGR has been written before (PLL, PLL48) don't overwrite these settings
|
2024-05-30 13:07:18 +02:00
|
|
|
RCC.cfgr().modify(|w| {
|
2024-02-14 00:10:59 +01:00
|
|
|
#[cfg(not(stm32f0))]
|
|
|
|
{
|
|
|
|
w.set_ppre1(config.apb1_pre);
|
|
|
|
w.set_ppre2(config.apb2_pre);
|
|
|
|
}
|
|
|
|
#[cfg(stm32f0)]
|
|
|
|
w.set_ppre(config.apb1_pre);
|
2024-02-12 02:17:33 +01:00
|
|
|
w.set_hpre(config.ahb_pre);
|
2024-02-14 00:10:59 +01:00
|
|
|
#[cfg(stm32f1)]
|
|
|
|
w.set_adcpre(config.adc_pre);
|
2022-01-04 23:58:13 +01:00
|
|
|
});
|
2021-12-10 11:40:03 +05:30
|
|
|
|
2022-01-04 23:58:13 +01:00
|
|
|
// Wait for the new prescalers to kick in
|
|
|
|
// "The clocks are divided with the new prescaler factor from
|
|
|
|
// 1 to 16 AHB cycles after write"
|
|
|
|
cortex_m::asm::delay(16);
|
2021-12-10 11:40:03 +05:30
|
|
|
|
2022-05-11 20:56:57 +02:00
|
|
|
// CFGR has been written before (PLL, PLL48, clock divider) don't overwrite these settings
|
2024-02-12 02:17:33 +01:00
|
|
|
RCC.cfgr().modify(|w| w.set_sw(config.sys));
|
2024-02-13 00:58:18 +01:00
|
|
|
while RCC.cfgr().read().sws() != config.sys {}
|
2021-12-10 11:40:03 +05:30
|
|
|
|
2024-05-27 17:31:29 +02:00
|
|
|
// Disable HSI if not used
|
|
|
|
if !config.hsi {
|
|
|
|
RCC.cr().modify(|w| w.set_hsion(false));
|
|
|
|
}
|
|
|
|
|
2024-02-12 02:17:33 +01:00
|
|
|
let rtc = config.ls.init();
|
|
|
|
|
2024-04-29 20:52:27 +02:00
|
|
|
// TODO: all this ADC stuff should probably go into the ADC module, not here.
|
|
|
|
// Most STM32s manage ADC clocks in a similar way with ADCx_COMMON.
|
2024-02-14 00:10:59 +01:00
|
|
|
#[cfg(all(stm32f3, not(rcc_f37)))]
|
2024-02-12 02:17:33 +01:00
|
|
|
use crate::pac::adccommon::vals::Ckmode;
|
|
|
|
|
2024-02-14 00:10:59 +01:00
|
|
|
#[cfg(all(stm32f3, not(rcc_f37)))]
|
2024-04-29 20:52:27 +02:00
|
|
|
let adc = {
|
|
|
|
#[cfg(peri_adc1_common)]
|
|
|
|
let common = crate::pac::ADC1_COMMON;
|
|
|
|
#[cfg(peri_adc12_common)]
|
|
|
|
let common = crate::pac::ADC12_COMMON;
|
|
|
|
|
|
|
|
match config.adc {
|
|
|
|
AdcClockSource::Pll(adcpres) => {
|
|
|
|
RCC.cfgr2().modify(|w| w.set_adc12pres(adcpres));
|
|
|
|
common.ccr().modify(|w| w.set_ckmode(Ckmode::ASYNCHRONOUS));
|
|
|
|
|
|
|
|
unwrap!(pll) / adcpres
|
|
|
|
}
|
|
|
|
AdcClockSource::Hclk(adcpres) => {
|
|
|
|
assert!(!(adcpres == AdcHclkPrescaler::Div1 && config.ahb_pre != AHBPrescaler::DIV1));
|
2023-09-10 13:33:17 -05:00
|
|
|
|
2024-04-29 20:52:27 +02:00
|
|
|
let (div, ckmode) = match adcpres {
|
|
|
|
AdcHclkPrescaler::Div1 => (1u32, Ckmode::SYNCDIV1),
|
|
|
|
AdcHclkPrescaler::Div2 => (2u32, Ckmode::SYNCDIV2),
|
|
|
|
AdcHclkPrescaler::Div4 => (4u32, Ckmode::SYNCDIV4),
|
|
|
|
};
|
|
|
|
common.ccr().modify(|w| w.set_ckmode(ckmode));
|
2023-09-10 13:33:17 -05:00
|
|
|
|
2024-04-29 20:52:27 +02:00
|
|
|
hclk / div
|
|
|
|
}
|
2024-02-12 02:17:33 +01:00
|
|
|
}
|
|
|
|
};
|
2023-09-10 13:33:17 -05:00
|
|
|
|
2024-02-14 00:10:59 +01:00
|
|
|
#[cfg(all(stm32f3, not(rcc_f37), adc3_common))]
|
2024-04-29 20:52:27 +02:00
|
|
|
let adc34 = {
|
|
|
|
#[cfg(peri_adc3_common)]
|
|
|
|
let common = crate::pac::ADC3_COMMON;
|
|
|
|
#[cfg(peri_adc34_common)]
|
|
|
|
let common = crate::pac::ADC34_COMMON;
|
|
|
|
|
|
|
|
match config.adc34 {
|
|
|
|
AdcClockSource::Pll(adcpres) => {
|
|
|
|
RCC.cfgr2().modify(|w| w.set_adc34pres(adcpres));
|
|
|
|
common.ccr().modify(|w| w.set_ckmode(Ckmode::ASYNCHRONOUS));
|
|
|
|
|
|
|
|
unwrap!(pll) / adcpres
|
|
|
|
}
|
|
|
|
AdcClockSource::Hclk(adcpres) => {
|
|
|
|
assert!(!(adcpres == AdcHclkPrescaler::Div1 && config.ahb_pre != AHBPrescaler::DIV1));
|
2023-09-15 17:35:53 -05:00
|
|
|
|
2024-04-29 20:52:27 +02:00
|
|
|
let (div, ckmode) = match adcpres {
|
|
|
|
AdcHclkPrescaler::Div1 => (1u32, Ckmode::SYNCDIV1),
|
|
|
|
AdcHclkPrescaler::Div2 => (2u32, Ckmode::SYNCDIV2),
|
|
|
|
AdcHclkPrescaler::Div4 => (4u32, Ckmode::SYNCDIV4),
|
|
|
|
};
|
|
|
|
common.ccr().modify(|w| w.set_ckmode(ckmode));
|
2023-09-10 13:33:17 -05:00
|
|
|
|
2024-04-29 20:52:27 +02:00
|
|
|
hclk / div
|
|
|
|
}
|
2024-02-12 02:17:33 +01:00
|
|
|
}
|
|
|
|
};
|
2023-09-10 13:33:17 -05:00
|
|
|
|
2024-02-24 12:54:58 -08:00
|
|
|
/*
|
|
|
|
TODO: Maybe add something like this to clock_mux? How can we autogenerate the data for this?
|
2023-09-10 13:33:17 -05:00
|
|
|
let hrtim = match config.hrtim {
|
|
|
|
// Must be configured after the bus is ready, otherwise it won't work
|
|
|
|
HrtimClockSource::BusClk => None,
|
|
|
|
HrtimClockSource::PllClk => {
|
|
|
|
use crate::pac::rcc::vals::Timsw;
|
|
|
|
|
|
|
|
// Make sure that we're using the PLL
|
2024-02-12 02:17:33 +01:00
|
|
|
let pll = unwrap!(pll);
|
|
|
|
assert!((pclk2 == pll) || (pclk2 * 2u32 == pll));
|
2023-09-10 13:33:17 -05:00
|
|
|
|
2023-10-17 20:31:44 -05:00
|
|
|
RCC.cfgr3().modify(|w| w.set_hrtim1sw(Timsw::PLL1_P));
|
2023-09-10 13:33:17 -05:00
|
|
|
|
2024-02-12 02:17:33 +01:00
|
|
|
Some(pll * 2u32)
|
2023-09-10 13:33:17 -05:00
|
|
|
}
|
|
|
|
};
|
2024-02-24 12:54:58 -08:00
|
|
|
*/
|
2023-09-10 13:33:17 -05:00
|
|
|
|
2024-02-20 17:54:35 -08:00
|
|
|
config.mux.init();
|
2024-02-15 19:50:42 -08:00
|
|
|
|
2024-02-02 22:42:32 +01:00
|
|
|
set_clocks!(
|
2024-02-12 02:17:33 +01:00
|
|
|
hsi: hsi,
|
|
|
|
hse: hse,
|
|
|
|
pll1_p: pll,
|
2024-02-25 16:12:32 -08:00
|
|
|
#[cfg(stm32f3)]
|
|
|
|
pll1_p_mul_2: pll_mul_2,
|
2024-02-26 03:00:04 +01:00
|
|
|
hsi_div_244: hsi.map(|h| h / 244u32),
|
2024-02-12 02:17:33 +01:00
|
|
|
sys: Some(sys),
|
2024-02-02 22:42:32 +01:00
|
|
|
pclk1: Some(pclk1),
|
|
|
|
pclk2: Some(pclk2),
|
2024-02-12 02:17:33 +01:00
|
|
|
pclk1_tim: Some(pclk1_tim),
|
|
|
|
pclk2_tim: Some(pclk2_tim),
|
2024-02-02 22:42:32 +01:00
|
|
|
hclk1: Some(hclk),
|
2024-02-14 00:10:59 +01:00
|
|
|
#[cfg(all(stm32f3, not(rcc_f37)))]
|
2024-02-12 02:17:33 +01:00
|
|
|
adc: Some(adc),
|
2024-02-14 00:10:59 +01:00
|
|
|
#[cfg(all(stm32f3, not(rcc_f37), adc3_common))]
|
2024-02-12 02:17:33 +01:00
|
|
|
adc34: Some(adc34),
|
2024-02-02 22:42:32 +01:00
|
|
|
rtc: rtc,
|
2024-02-14 00:10:59 +01:00
|
|
|
hsi48: hsi48,
|
|
|
|
#[cfg(any(rcc_f1, rcc_f1cl, stm32f3))]
|
2024-02-12 02:17:33 +01:00
|
|
|
usb: usb,
|
|
|
|
lse: None,
|
2024-02-02 22:42:32 +01:00
|
|
|
);
|
2022-01-04 23:58:13 +01:00
|
|
|
}
|
2021-12-10 11:40:03 +05:30
|
|
|
|
2024-02-14 00:10:59 +01:00
|
|
|
#[cfg(stm32f0)]
|
|
|
|
mod max {
|
|
|
|
use core::ops::RangeInclusive;
|
|
|
|
|
|
|
|
use crate::time::Hertz;
|
|
|
|
|
|
|
|
pub(crate) const HSE_OSC: RangeInclusive<Hertz> = Hertz(4_000_000)..=Hertz(32_000_000);
|
|
|
|
pub(crate) const HSE_BYP: RangeInclusive<Hertz> = Hertz(1_000_000)..=Hertz(32_000_000);
|
|
|
|
|
|
|
|
pub(crate) const HCLK: RangeInclusive<Hertz> = Hertz(0)..=Hertz(48_000_000);
|
|
|
|
pub(crate) const PCLK1: RangeInclusive<Hertz> = Hertz(0)..=Hertz(48_000_000);
|
|
|
|
|
|
|
|
pub(crate) const PLL_IN: RangeInclusive<Hertz> = Hertz(1_000_000)..=Hertz(24_000_000);
|
|
|
|
pub(crate) const PLL_OUT: RangeInclusive<Hertz> = Hertz(16_000_000)..=Hertz(48_000_000);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(stm32f1)]
|
|
|
|
mod max {
|
|
|
|
use core::ops::RangeInclusive;
|
|
|
|
|
|
|
|
use crate::time::Hertz;
|
|
|
|
|
|
|
|
#[cfg(not(rcc_f1cl))]
|
|
|
|
pub(crate) const HSE_OSC: RangeInclusive<Hertz> = Hertz(4_000_000)..=Hertz(16_000_000);
|
|
|
|
#[cfg(not(rcc_f1cl))]
|
|
|
|
pub(crate) const HSE_BYP: RangeInclusive<Hertz> = Hertz(1_000_000)..=Hertz(25_000_000);
|
|
|
|
|
|
|
|
#[cfg(rcc_f1cl)]
|
|
|
|
pub(crate) const HSE_OSC: RangeInclusive<Hertz> = Hertz(3_000_000)..=Hertz(25_000_000);
|
|
|
|
#[cfg(rcc_f1cl)]
|
|
|
|
pub(crate) const HSE_BYP: RangeInclusive<Hertz> = Hertz(1_000_000)..=Hertz(50_000_000);
|
|
|
|
|
|
|
|
pub(crate) const HCLK: RangeInclusive<Hertz> = Hertz(0)..=Hertz(72_000_000);
|
|
|
|
pub(crate) const PCLK1: RangeInclusive<Hertz> = Hertz(0)..=Hertz(36_000_000);
|
|
|
|
pub(crate) const PCLK2: RangeInclusive<Hertz> = Hertz(0)..=Hertz(72_000_000);
|
|
|
|
|
|
|
|
pub(crate) const PLL_IN: RangeInclusive<Hertz> = Hertz(1_000_000)..=Hertz(25_000_000);
|
|
|
|
pub(crate) const PLL_OUT: RangeInclusive<Hertz> = Hertz(16_000_000)..=Hertz(72_000_000);
|
|
|
|
|
|
|
|
pub(crate) const ADC: RangeInclusive<Hertz> = Hertz(0)..=Hertz(14_000_000);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(stm32f3)]
|
2024-02-12 02:17:33 +01:00
|
|
|
mod max {
|
|
|
|
use core::ops::RangeInclusive;
|
2021-12-10 11:40:03 +05:30
|
|
|
|
2024-02-12 02:17:33 +01:00
|
|
|
use crate::time::Hertz;
|
2021-12-10 11:40:03 +05:30
|
|
|
|
2024-02-12 02:17:33 +01:00
|
|
|
pub(crate) const HSE_OSC: RangeInclusive<Hertz> = Hertz(4_000_000)..=Hertz(32_000_000);
|
|
|
|
pub(crate) const HSE_BYP: RangeInclusive<Hertz> = Hertz(1_000_000)..=Hertz(32_000_000);
|
2021-12-10 11:40:03 +05:30
|
|
|
|
2024-02-12 02:17:33 +01:00
|
|
|
pub(crate) const HCLK: RangeInclusive<Hertz> = Hertz(0)..=Hertz(72_000_000);
|
|
|
|
pub(crate) const PCLK1: RangeInclusive<Hertz> = Hertz(0)..=Hertz(36_000_000);
|
|
|
|
pub(crate) const PCLK2: RangeInclusive<Hertz> = Hertz(0)..=Hertz(72_000_000);
|
2021-12-10 11:40:03 +05:30
|
|
|
|
2024-02-12 02:17:33 +01:00
|
|
|
pub(crate) const PLL_IN: RangeInclusive<Hertz> = Hertz(1_000_000)..=Hertz(24_000_000);
|
|
|
|
pub(crate) const PLL_OUT: RangeInclusive<Hertz> = Hertz(16_000_000)..=Hertz(72_000_000);
|
2021-12-10 11:40:03 +05:30
|
|
|
}
|