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

96 lines
2.5 KiB
Rust
Raw Normal View History

#![macro_use]
use crate::peripherals;
2021-05-27 09:50:11 +02:00
use crate::time::Hertz;
use core::mem::MaybeUninit;
/// Frozen clock frequencies
///
/// The existence of this value indicates that the clock configuration can no longer be changed
#[derive(Clone, Copy)]
pub struct Clocks {
pub sys: Hertz,
pub ahb: Hertz,
pub apb1: Hertz,
pub apb2: Hertz,
2021-05-27 09:50:11 +02:00
}
static mut CLOCK_FREQS: MaybeUninit<Clocks> = MaybeUninit::uninit();
/// Sets the clock frequencies
///
/// Safety: Sets a mutable global.
pub unsafe fn set_freqs(freqs: Clocks) {
CLOCK_FREQS.as_mut_ptr().write(freqs);
}
/// Safety: Reads a mutable global.
pub unsafe fn get_freqs() -> &'static Clocks {
&*CLOCK_FREQS.as_ptr()
}
cfg_if::cfg_if! {
if #[cfg(rcc_h7)] {
mod h7;
pub use h7::*;
} else if #[cfg(rcc_l0)] {
mod l0;
pub use l0::*;
} else {
#[derive(Default)]
pub struct Config {}
2021-05-26 21:46:57 +02:00
pub unsafe fn init(_config: Config) {
}
}
}
pub(crate) mod sealed {
pub trait RccPeripheral {
fn frequency() -> crate::time::Hertz;
fn reset();
fn enable();
fn disable();
}
}
pub trait RccPeripheral: sealed::RccPeripheral + 'static {}
crate::pac::peripheral_rcc!(
($inst:ident, $clk:ident, $enable:ident, $reset:ident, $perien:ident, $perirst:ident) => {
impl sealed::RccPeripheral for peripherals::$inst {
fn frequency() -> crate::time::Hertz {
critical_section::with(|_| {
unsafe {
let freqs = get_freqs();
freqs.$clk
}
})
}
fn enable() {
2021-06-08 13:10:40 +02:00
critical_section::with(|_| {
unsafe {
crate::pac::RCC.$enable().modify(|w| w.$perien(true));
}
})
}
fn disable() {
2021-06-08 13:10:40 +02:00
critical_section::with(|_| {
unsafe {
crate::pac::RCC.$enable().modify(|w| w.$perien(false));
}
})
}
fn reset() {
2021-06-08 13:10:40 +02:00
critical_section::with(|_| {
unsafe {
crate::pac::RCC.$reset().modify(|w| w.$perirst(true));
crate::pac::RCC.$reset().modify(|w| w.$perirst(false));
}
})
}
}
impl RccPeripheral for peripherals::$inst {}
};
);