embassy/embassy-stm32/src/rcc/g0.rs

189 lines
4.6 KiB
Rust
Raw Normal View History

2022-03-04 18:04:12 +01:00
use crate::pac::rcc::vals::{Hpre, Hsidiv, Ppre, Sw};
use crate::pac::{PWR, RCC};
use crate::rcc::{set_freqs, Clocks};
2021-07-30 22:48:13 +02:00
use crate::time::Hertz;
use crate::time::U32Ext;
/// HSI speed
pub const HSI_FREQ: u32 = 16_000_000;
/// LSI speed
pub const LSI_FREQ: u32 = 32_000;
/// System clock mux source
#[derive(Clone, Copy)]
pub enum ClockSrc {
HSE(Hertz),
2021-08-31 07:48:22 +02:00
HSI16(HSI16Prescaler),
2021-07-30 22:48:13 +02:00
LSI,
}
2021-08-31 07:48:22 +02:00
#[derive(Clone, Copy)]
pub enum HSI16Prescaler {
NotDivided,
Div2,
Div4,
Div8,
Div16,
Div32,
Div64,
Div128,
}
impl Into<Hsidiv> for HSI16Prescaler {
fn into(self) -> Hsidiv {
2021-08-31 07:48:22 +02:00
match self {
HSI16Prescaler::NotDivided => Hsidiv::DIV1,
HSI16Prescaler::Div2 => Hsidiv::DIV2,
HSI16Prescaler::Div4 => Hsidiv::DIV4,
HSI16Prescaler::Div8 => Hsidiv::DIV8,
HSI16Prescaler::Div16 => Hsidiv::DIV16,
HSI16Prescaler::Div32 => Hsidiv::DIV32,
HSI16Prescaler::Div64 => Hsidiv::DIV64,
HSI16Prescaler::Div128 => Hsidiv::DIV128,
2021-08-31 07:48:22 +02: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,
}
impl Into<Ppre> for APBPrescaler {
fn into(self) -> Ppre {
2021-07-30 22:48:13 +02:00
match self {
APBPrescaler::NotDivided => Ppre::DIV1,
APBPrescaler::Div2 => Ppre::DIV2,
APBPrescaler::Div4 => Ppre::DIV4,
APBPrescaler::Div8 => Ppre::DIV8,
APBPrescaler::Div16 => Ppre::DIV16,
2021-07-30 22:48:13 +02:00
}
}
}
impl Into<Hpre> for AHBPrescaler {
fn into(self) -> Hpre {
2021-07-30 22:48:13 +02:00
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-07-30 22:48:13 +02:00
}
}
}
/// Clocks configutation
pub struct Config {
pub mux: ClockSrc,
pub ahb_pre: AHBPrescaler,
pub apb_pre: APBPrescaler,
pub low_power_run: bool,
2021-07-30 22:48:13 +02:00
}
impl Default for Config {
#[inline]
fn default() -> Config {
Config {
2021-08-31 07:48:22 +02:00
mux: ClockSrc::HSI16(HSI16Prescaler::NotDivided),
2021-07-30 22:48:13 +02:00
ahb_pre: AHBPrescaler::NotDivided,
apb_pre: APBPrescaler::NotDivided,
2021-08-31 07:51:49 +02:00
low_power_run: false,
2021-07-30 22:48:13 +02:00
}
}
}
pub(crate) unsafe fn init(config: Config) {
let (sys_clk, sw) = match config.mux {
ClockSrc::HSI16(div) => {
// Enable HSI16
let div: Hsidiv = div.into();
RCC.cr().write(|w| {
w.set_hsidiv(div);
w.set_hsion(true)
});
while !RCC.cr().read().hsirdy() {}
2021-07-30 22:48:13 +02:00
(HSI_FREQ >> div.0, Sw::HSI)
2021-07-30 22:48:13 +02:00
}
ClockSrc::HSE(freq) => {
// Enable HSE
RCC.cr().write(|w| w.set_hseon(true));
while !RCC.cr().read().hserdy() {}
2021-07-30 22:48:13 +02:00
(freq.0, Sw::HSE)
2021-07-30 22:48:13 +02:00
}
ClockSrc::LSI => {
// Enable LSI
RCC.csr().write(|w| w.set_lsion(true));
while !RCC.csr().read().lsirdy() {}
(LSI_FREQ, Sw::LSI)
2021-08-31 07:51:49 +02:00
}
};
RCC.cfgr().modify(|w| {
w.set_sw(sw.into());
w.set_hpre(config.ahb_pre.into());
w.set_ppre(config.apb_pre.into());
});
let ahb_div = match config.ahb_pre {
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,
};
let ahb_freq = sys_clk / ahb_div;
let (apb_freq, apb_tim_freq) = match config.apb_pre {
APBPrescaler::NotDivided => (ahb_freq, ahb_freq),
pre => {
let pre: Ppre = pre.into();
let pre: u8 = 1 << (pre.0 - 3);
let freq = ahb_freq / pre as u32;
(freq, freq * 2)
}
};
if config.low_power_run {
assert!(sys_clk.hz() <= 2_000_000.hz());
PWR.cr1().modify(|w| w.set_lpr(true));
2021-07-30 22:48:13 +02:00
}
set_freqs(Clocks {
sys: sys_clk.hz(),
2022-02-14 02:12:06 +01:00
ahb1: ahb_freq.hz(),
apb1: apb_freq.hz(),
apb1_tim: apb_tim_freq.hz(),
});
2021-07-30 22:48:13 +02:00
}