use registers;
use crate::time_util::Hertz;
pub trait CMUExt {
fn constrain(self) -> Cmu;
}
impl CMUExt for registers::CMU {
fn constrain(self) -> Cmu {
Cmu { _private: () }
}
}
pub struct Cmu {
_private: (),
}
pub trait FrozenClock {
fn get_frequency(&self) -> Hertz;
}
pub struct Clocks {
pub hfcoreclk: HFCoreClk,
pub i2c0: I2C0Clk,
pub gpio: GPIOClk,
pub timer0: TIMER0Clk,
pub timer1: TIMER1Clk,
#[cfg(feature = "_has_timer2")]
pub timer2: TIMER2Clk,
}
pub struct I2C0Clk {
_private: (),
}
impl I2C0Clk {
pub fn enable(&mut self) {
unsafe {
let cmu = &*registers::CMU::ptr();
cmu.hfperclken0.modify(|_, w| w.i2c0().set_bit());
}
}
}
macro_rules! timerclk {
($TIMERnClk: ident, $timerN: ident) => {
pub struct $TIMERnClk {
_private: (),
}
impl $TIMERnClk {
pub fn enable(&mut self) {
unsafe {
let cmu = &*registers::CMU::ptr();
cmu.hfperclken0.modify(|_, w| w.$timerN().set_bit());
}
}
}
}
}
timerclk!(TIMER0Clk, timer0);
timerclk!(TIMER1Clk, timer1);
#[cfg(feature = "_has_timer2")]
timerclk!(TIMER2Clk, timer2);
pub struct GPIOClk {
_private: (),
}
impl GPIOClk {
pub fn enable(&mut self) {
unsafe {
let cmu = &*registers::CMU::ptr();
#[cfg(feature = "chip-efm32gg")]
cmu.hfperclken0.modify(|_, w| w.gpio().bit(true));
#[cfg(feature = "chip-efr32xg1")]
cmu.hfbusclken0.modify(|_, w| w.gpio().bit(true));
}
}
}
impl Cmu {
pub fn split(self) -> Clocks {
Clocks {
hfcoreclk: HFCoreClk { _private: () },
i2c0: I2C0Clk { _private: () },
gpio: GPIOClk { _private: () },
timer0: TIMER0Clk { _private: () },
timer1: TIMER1Clk { _private: () },
#[cfg(feature = "_has_timer2")]
timer2: TIMER2Clk { _private: () },
}
}
}
pub struct HFCoreClk {
_private: (),
}
impl FrozenClock for HFCoreClk {
fn get_frequency(&self) -> Hertz {
#[cfg(feature = "chip-efm32gg")]
{
Hertz(14_000_000)
}
#[cfg(feature = "chip-efr32xg1")]
{
Hertz(19_000_000)
}
}
}