2023-06-04 04:05:24 +02:00
|
|
|
use stm32_metapac::rcc::vals::{Hpre, Pllsrc, Ppre, Sw};
|
2023-05-25 16:06:02 +02:00
|
|
|
|
2022-01-04 23:58:13 +01:00
|
|
|
use crate::pac::{PWR, RCC};
|
|
|
|
use crate::rcc::{set_freqs, Clocks};
|
2022-07-11 00:36:10 +02:00
|
|
|
use crate::time::Hertz;
|
2021-11-27 02:21:53 +01:00
|
|
|
|
|
|
|
/// HSI speed
|
2022-07-10 19:59:36 +02:00
|
|
|
pub const HSI_FREQ: Hertz = Hertz(16_000_000);
|
2021-11-27 02:21:53 +01:00
|
|
|
|
|
|
|
/// LSI speed
|
2022-07-10 19:59:36 +02:00
|
|
|
pub const LSI_FREQ: Hertz = Hertz(32_000);
|
2021-11-27 02:21:53 +01:00
|
|
|
|
|
|
|
/// System clock mux source
|
|
|
|
#[derive(Clone, Copy)]
|
|
|
|
pub enum ClockSrc {
|
|
|
|
HSE(Hertz),
|
|
|
|
HSI16,
|
2023-06-04 04:05:24 +02:00
|
|
|
PLL(PllSrc, PllM, PllN, PllClkDiv),
|
2021-11-27 02:21:53 +01:00
|
|
|
}
|
|
|
|
|
2021-11-28 16:46:08 +01:00
|
|
|
/// AHB prescaler
|
|
|
|
#[derive(Clone, Copy, PartialEq)]
|
|
|
|
pub enum AHBPrescaler {
|
|
|
|
NotDivided,
|
|
|
|
Div2,
|
|
|
|
Div4,
|
|
|
|
Div8,
|
|
|
|
Div16,
|
|
|
|
Div64,
|
|
|
|
Div128,
|
|
|
|
Div256,
|
|
|
|
Div512,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// APB prescaler
|
|
|
|
#[derive(Clone, Copy)]
|
|
|
|
pub enum APBPrescaler {
|
|
|
|
NotDivided,
|
|
|
|
Div2,
|
|
|
|
Div4,
|
|
|
|
Div8,
|
|
|
|
Div16,
|
|
|
|
}
|
|
|
|
|
2023-06-04 04:05:24 +02:00
|
|
|
/// PLL clock input source
|
|
|
|
#[derive(Clone, Copy, Debug)]
|
|
|
|
pub enum PllSrc {
|
|
|
|
HSI16,
|
|
|
|
HSE(Hertz),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Into<Pllsrc> for PllSrc {
|
|
|
|
fn into(self) -> Pllsrc {
|
|
|
|
match self {
|
|
|
|
PllSrc::HSE(..) => Pllsrc::HSE,
|
|
|
|
PllSrc::HSI16 => Pllsrc::HSI16,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Copy)]
|
|
|
|
pub enum PllClkDiv {
|
|
|
|
Div2,
|
|
|
|
Div4,
|
|
|
|
Div6,
|
|
|
|
Div8,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PllClkDiv {
|
|
|
|
pub fn to_div(self) -> u32 {
|
|
|
|
let val: u8 = self.into();
|
|
|
|
(val as u32 + 1) * 2
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<PllClkDiv> for u8 {
|
|
|
|
fn from(val: PllClkDiv) -> u8 {
|
|
|
|
match val {
|
|
|
|
PllClkDiv::Div2 => 0b00,
|
|
|
|
PllClkDiv::Div4 => 0b01,
|
|
|
|
PllClkDiv::Div6 => 0b10,
|
|
|
|
PllClkDiv::Div8 => 0b11,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
seq_macro::seq!(N in 8..=127 {
|
|
|
|
#[derive(Clone, Copy)]
|
|
|
|
pub enum PllN {
|
|
|
|
#(
|
|
|
|
Mul~N,
|
|
|
|
)*
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<PllN> for u8 {
|
|
|
|
fn from(val: PllN) -> u8 {
|
|
|
|
match val {
|
|
|
|
#(
|
|
|
|
PllN::Mul~N => N,
|
|
|
|
)*
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PllN {
|
|
|
|
pub fn to_mul(self) -> u32 {
|
|
|
|
match self {
|
|
|
|
#(
|
|
|
|
PllN::Mul~N => N,
|
|
|
|
)*
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Pre-division
|
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
pub enum PllM {
|
|
|
|
Div1,
|
|
|
|
Div2,
|
|
|
|
Div3,
|
|
|
|
Div4,
|
|
|
|
Div5,
|
|
|
|
Div6,
|
|
|
|
Div7,
|
|
|
|
Div8,
|
|
|
|
Div9,
|
|
|
|
Div10,
|
|
|
|
Div11,
|
|
|
|
Div12,
|
|
|
|
Div13,
|
|
|
|
Div14,
|
|
|
|
Div15,
|
|
|
|
Div16,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PllM {
|
|
|
|
pub fn to_div(self) -> u32 {
|
|
|
|
let val: u8 = self.into();
|
|
|
|
val as u32 + 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<PllM> for u8 {
|
|
|
|
fn from(val: PllM) -> u8 {
|
|
|
|
match val {
|
|
|
|
PllM::Div1 => 0b0000,
|
|
|
|
PllM::Div2 => 0b0001,
|
|
|
|
PllM::Div3 => 0b0010,
|
|
|
|
PllM::Div4 => 0b0011,
|
|
|
|
PllM::Div5 => 0b0100,
|
|
|
|
PllM::Div6 => 0b0101,
|
|
|
|
PllM::Div7 => 0b0110,
|
|
|
|
PllM::Div8 => 0b0111,
|
|
|
|
PllM::Div9 => 0b1000,
|
|
|
|
PllM::Div10 => 0b1001,
|
|
|
|
PllM::Div11 => 0b1010,
|
|
|
|
PllM::Div12 => 0b1011,
|
|
|
|
PllM::Div13 => 0b1100,
|
|
|
|
PllM::Div14 => 0b1101,
|
|
|
|
PllM::Div15 => 0b1110,
|
|
|
|
PllM::Div16 => 0b1111,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-25 16:06:02 +02:00
|
|
|
impl AHBPrescaler {
|
|
|
|
const fn div(self) -> u32 {
|
|
|
|
match self {
|
|
|
|
AHBPrescaler::NotDivided => 1,
|
|
|
|
AHBPrescaler::Div2 => 2,
|
|
|
|
AHBPrescaler::Div4 => 4,
|
|
|
|
AHBPrescaler::Div8 => 8,
|
|
|
|
AHBPrescaler::Div16 => 16,
|
|
|
|
AHBPrescaler::Div64 => 64,
|
|
|
|
AHBPrescaler::Div128 => 128,
|
|
|
|
AHBPrescaler::Div256 => 256,
|
|
|
|
AHBPrescaler::Div512 => 512,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl APBPrescaler {
|
|
|
|
const fn div(self) -> u32 {
|
2021-11-27 02:21:53 +01:00
|
|
|
match self {
|
|
|
|
APBPrescaler::NotDivided => 1,
|
2023-05-25 16:06:02 +02:00
|
|
|
APBPrescaler::Div2 => 2,
|
|
|
|
APBPrescaler::Div4 => 4,
|
|
|
|
APBPrescaler::Div8 => 8,
|
|
|
|
APBPrescaler::Div16 => 16,
|
2021-11-27 02:21:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-25 16:06:02 +02:00
|
|
|
impl Into<Ppre> for APBPrescaler {
|
|
|
|
fn into(self) -> Ppre {
|
2021-11-27 02:21:53 +01:00
|
|
|
match self {
|
2023-05-25 16:06:02 +02:00
|
|
|
APBPrescaler::NotDivided => Ppre::DIV1,
|
|
|
|
APBPrescaler::Div2 => Ppre::DIV2,
|
|
|
|
APBPrescaler::Div4 => Ppre::DIV4,
|
|
|
|
APBPrescaler::Div8 => Ppre::DIV8,
|
|
|
|
APBPrescaler::Div16 => Ppre::DIV16,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Into<Hpre> for AHBPrescaler {
|
|
|
|
fn into(self) -> Hpre {
|
|
|
|
match self {
|
|
|
|
AHBPrescaler::NotDivided => Hpre::DIV1,
|
|
|
|
AHBPrescaler::Div2 => Hpre::DIV2,
|
|
|
|
AHBPrescaler::Div4 => Hpre::DIV4,
|
|
|
|
AHBPrescaler::Div8 => Hpre::DIV8,
|
|
|
|
AHBPrescaler::Div16 => Hpre::DIV16,
|
|
|
|
AHBPrescaler::Div64 => Hpre::DIV64,
|
|
|
|
AHBPrescaler::Div128 => Hpre::DIV128,
|
|
|
|
AHBPrescaler::Div256 => Hpre::DIV256,
|
|
|
|
AHBPrescaler::Div512 => Hpre::DIV512,
|
2021-11-27 02:21:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Clocks configutation
|
|
|
|
pub struct Config {
|
2022-01-04 11:18:59 +01:00
|
|
|
pub mux: ClockSrc,
|
|
|
|
pub ahb_pre: AHBPrescaler,
|
|
|
|
pub apb1_pre: APBPrescaler,
|
|
|
|
pub apb2_pre: APBPrescaler,
|
|
|
|
pub low_power_run: bool,
|
2021-11-27 02:21:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Config {
|
|
|
|
#[inline]
|
|
|
|
fn default() -> Config {
|
|
|
|
Config {
|
|
|
|
mux: ClockSrc::HSI16,
|
|
|
|
ahb_pre: AHBPrescaler::NotDivided,
|
|
|
|
apb1_pre: APBPrescaler::NotDivided,
|
|
|
|
apb2_pre: APBPrescaler::NotDivided,
|
|
|
|
low_power_run: false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-04 23:58:13 +01:00
|
|
|
pub(crate) unsafe fn init(config: Config) {
|
|
|
|
let (sys_clk, sw) = match config.mux {
|
|
|
|
ClockSrc::HSI16 => {
|
|
|
|
// Enable HSI16
|
|
|
|
RCC.cr().write(|w| w.set_hsion(true));
|
|
|
|
while !RCC.cr().read().hsirdy() {}
|
2021-11-27 02:21:53 +01:00
|
|
|
|
2023-05-25 16:06:02 +02:00
|
|
|
(HSI_FREQ.0, Sw::HSI16)
|
2021-11-27 02:21:53 +01:00
|
|
|
}
|
2022-01-04 23:58:13 +01:00
|
|
|
ClockSrc::HSE(freq) => {
|
|
|
|
// Enable HSE
|
|
|
|
RCC.cr().write(|w| w.set_hseon(true));
|
|
|
|
while !RCC.cr().read().hserdy() {}
|
2021-11-27 02:21:53 +01:00
|
|
|
|
2023-05-25 16:06:02 +02:00
|
|
|
(freq.0, Sw::HSE)
|
2021-11-27 02:21:53 +01:00
|
|
|
}
|
2023-06-04 04:05:24 +02:00
|
|
|
ClockSrc::PLL(src, prediv, mul, div) => {
|
|
|
|
let src_freq = match src {
|
|
|
|
PllSrc::HSI16 => {
|
2023-06-04 16:58:44 +02:00
|
|
|
// Enable HSI16 as clock source for PLL
|
2023-06-04 04:05:24 +02:00
|
|
|
RCC.cr().write(|w| w.set_hsion(true));
|
|
|
|
while !RCC.cr().read().hsirdy() {}
|
|
|
|
|
|
|
|
HSI_FREQ.0
|
|
|
|
}
|
|
|
|
PllSrc::HSE(freq) => {
|
2023-06-04 16:58:44 +02:00
|
|
|
// Enable HSE as clock source for PLL
|
2023-06-04 04:05:24 +02:00
|
|
|
RCC.cr().write(|w| w.set_hseon(true));
|
|
|
|
while !RCC.cr().read().hserdy() {}
|
|
|
|
|
|
|
|
freq.0
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-06-04 16:58:44 +02:00
|
|
|
// Make sure PLL is disabled while we configure it
|
2023-06-04 04:05:24 +02:00
|
|
|
RCC.cr().modify(|w| w.set_pllon(false));
|
|
|
|
while RCC.cr().read().pllrdy() {}
|
|
|
|
|
|
|
|
let freq = src_freq / prediv.to_div() * mul.to_mul() / div.to_div();
|
|
|
|
assert!(freq <= 170_000_000);
|
|
|
|
|
|
|
|
RCC.pllcfgr().write(move |w| {
|
|
|
|
w.set_plln(mul.into());
|
|
|
|
w.set_pllm(prediv.into());
|
|
|
|
w.set_pllr(div.into());
|
|
|
|
w.set_pllsrc(src.into());
|
|
|
|
});
|
|
|
|
|
2023-06-04 16:58:44 +02:00
|
|
|
// Enable PLL
|
2023-06-04 04:05:24 +02:00
|
|
|
RCC.cr().modify(|w| w.set_pllon(true));
|
|
|
|
while !RCC.cr().read().pllrdy() {}
|
|
|
|
RCC.pllcfgr().modify(|w| w.set_pllren(true));
|
|
|
|
|
|
|
|
(freq, Sw::PLLRCLK)
|
|
|
|
}
|
2022-01-04 23:58:13 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
RCC.cfgr().modify(|w| {
|
2023-05-25 16:06:02 +02:00
|
|
|
w.set_sw(sw);
|
2022-01-04 23:58:13 +01:00
|
|
|
w.set_hpre(config.ahb_pre.into());
|
|
|
|
w.set_ppre1(config.apb1_pre.into());
|
|
|
|
w.set_ppre2(config.apb2_pre.into());
|
|
|
|
});
|
|
|
|
|
|
|
|
let ahb_freq: u32 = match config.ahb_pre {
|
|
|
|
AHBPrescaler::NotDivided => sys_clk,
|
2023-05-25 16:06:02 +02:00
|
|
|
pre => sys_clk / pre.div(),
|
2022-01-04 23:58:13 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
let (apb1_freq, apb1_tim_freq) = match config.apb1_pre {
|
|
|
|
APBPrescaler::NotDivided => (ahb_freq, ahb_freq),
|
|
|
|
pre => {
|
2023-05-25 16:06:02 +02:00
|
|
|
let freq = ahb_freq / pre.div();
|
2022-01-04 23:58:13 +01:00
|
|
|
(freq, freq * 2)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let (apb2_freq, apb2_tim_freq) = match config.apb2_pre {
|
|
|
|
APBPrescaler::NotDivided => (ahb_freq, ahb_freq),
|
|
|
|
pre => {
|
2023-05-25 16:06:02 +02:00
|
|
|
let freq = ahb_freq / pre.div();
|
2022-01-04 23:58:13 +01:00
|
|
|
(freq, freq * 2)
|
2021-11-27 02:21:53 +01:00
|
|
|
}
|
2022-01-04 23:58:13 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
if config.low_power_run {
|
2022-07-11 00:36:10 +02:00
|
|
|
assert!(sys_clk <= 2_000_000);
|
2022-01-04 23:58:13 +01:00
|
|
|
PWR.cr1().modify(|w| w.set_lpr(true));
|
2021-11-27 02:21:53 +01:00
|
|
|
}
|
|
|
|
|
2022-01-04 23:58:13 +01:00
|
|
|
set_freqs(Clocks {
|
2022-07-11 00:36:10 +02:00
|
|
|
sys: Hertz(sys_clk),
|
|
|
|
ahb1: Hertz(ahb_freq),
|
|
|
|
ahb2: Hertz(ahb_freq),
|
|
|
|
apb1: Hertz(apb1_freq),
|
|
|
|
apb1_tim: Hertz(apb1_tim_freq),
|
|
|
|
apb2: Hertz(apb2_freq),
|
|
|
|
apb2_tim: Hertz(apb2_tim_freq),
|
2022-01-04 23:58:13 +01:00
|
|
|
});
|
2021-11-27 02:21:53 +01:00
|
|
|
}
|