use core::marker::PhantomData;
use registers;
use crate::cmu;
pub trait TimerExt<Clk, Timer> {
fn with_clock(self, clock: Clk) -> Timer;
}
pub struct Channel0 {}
pub struct Channel1 {}
pub struct Channel2 {}
pub struct Channels<C0, C1, C2> {
pub channel0: C0,
pub channel1: C1,
pub channel2: C2,
}
pub struct TimerChannel<Timer, Channel> {
_phantom: PhantomData<(Timer, Channel)>,
}
pub struct RoutedTimerChannel<Timer, Channel, Pin> {
pin: Pin,
channel: TimerChannel<Timer, Channel>
}
macro_rules! timer {
($TIMERn: ident, $TIMERnClk: ident, $TimerN: ident, $timerN: ident) => {
impl TimerExt<cmu::$TIMERnClk, $TimerN> for registers::$TIMERn {
fn with_clock(self, mut clock: cmu::$TIMERnClk) -> $TimerN {
clock.enable();
$TimerN { register: self, clock }
}
}
pub struct $TimerN {
register: registers::$TIMERn,
clock: cmu::$TIMERnClk,
}
impl embedded_hal::Pwm for $TimerN {
type Channel = i32; type Time = (); type Duty = u16;
fn enable(&mut self, channel: Self::Channel) {
match channel {
0 => {
#[cfg(feature = "chip-efm32gg")]
self.register.route.modify(|_, w| w.cc0pen().set_bit());
#[cfg(feature = "chip-efr32xg1")]
self.register.routepen.modify(|_, w| w.cc0pen().set_bit());
self.register.cc0_ctrl.modify(|_, w| w.mode().variant(registers::$timerN::cc0_ctrl::MODEW::PWM));
},
1 => {
#[cfg(feature = "chip-efm32gg")]
self.register.route.modify(|_, w| w.cc1pen().set_bit());
#[cfg(feature = "chip-efr32xg1")]
self.register.routepen.modify(|_, w| w.cc1pen().set_bit());
self.register.cc1_ctrl.modify(|_, w| w.mode().variant(registers::$timerN::cc1_ctrl::MODEW::PWM));
},
2 => {
#[cfg(feature = "chip-efm32gg")]
self.register.route.modify(|_, w| w.cc2pen().set_bit());
#[cfg(feature = "chip-efr32xg1")]
self.register.routepen.modify(|_, w| w.cc2pen().set_bit());
self.register.cc2_ctrl.modify(|_, w| w.mode().variant(registers::$timerN::cc2_ctrl::MODEW::PWM));
},
_ => panic!("Nonexistent channel"),
}
}
#[cfg(feature = "chip-efm32gg")]
fn disable(&mut self, channel: Self::Channel) {
match channel {
0 => self.register.route.modify(|_, w| w.cc0pen().clear_bit()),
1 => self.register.route.modify(|_, w| w.cc1pen().clear_bit()),
2 => self.register.route.modify(|_, w| w.cc2pen().clear_bit()),
_ => panic!("Nonexistent channel"),
}
}
#[cfg(feature = "chip-efr32xg1")]
fn disable(&mut self, channel: Self::Channel) {
match channel {
0 => self.register.routepen.modify(|_, w| w.cc0pen().clear_bit()),
1 => self.register.routepen.modify(|_, w| w.cc1pen().clear_bit()),
2 => self.register.routepen.modify(|_, w| w.cc2pen().clear_bit()),
_ => panic!("Nonexistent channel"),
}
}
fn get_period(&self) -> () {
unimplemented!();
}
fn set_period<P>(&mut self, time: P) {
unimplemented!();
}
fn get_max_duty(&self) -> Self::Duty {
self.register.top.read().top().bits()
}
fn set_duty(&mut self, channel: i32, duty: u16) {
match channel {
0 => self.register.cc0_ccv.modify(|_, w| unsafe { w.ccv().bits(duty) }),
1 => self.register.cc1_ccv.modify(|_, w| unsafe { w.ccv().bits(duty) }),
2 => self.register.cc2_ccv.modify(|_, w| unsafe { w.ccv().bits(duty) }),
_ => panic!("Nonexistent channel"),
}
}
fn get_duty(&self, channel: i32) -> u16 {
match channel {
0 => self.register.cc0_ccv.read().ccv().bits(),
1 => self.register.cc1_ccv.read().ccv().bits(),
2 => self.register.cc2_ccv.read().ccv().bits(),
_ => panic!("Nonexistent channel"),
}
}
}
impl $TimerN {
pub fn set_inverted(&mut self, channel: <Self as embedded_hal::Pwm>::Channel, inverted: bool) {
match channel {
0 => self.register.cc0_ctrl.modify(|_, w| w.outinv().bit(inverted)),
1 => self.register.cc1_ctrl.modify(|_, w| w.outinv().bit(inverted)),
2 => self.register.cc2_ctrl.modify(|_, w| w.outinv().bit(inverted)),
_ => panic!("Nonexistent channel"),
}
}
pub fn set_top(&mut self, top: u16) {
self.register.top.modify(|_, w| unsafe { w.top().bits(top) });
}
pub fn interrupt_enable(&mut self, interrupt: InterruptFlag) {
self.register.ien.modify(|r, w| unsafe { w.bits(interrupt.bits() | r.bits()) });
}
pub fn interrupt_is_pending(interrupt: InterruptFlag) -> bool {
let reg = unsafe { &*registers::$TIMERn::ptr() };
reg.if_.read().bits() & interrupt.bits() != 0
}
pub fn interrupt_unpend(interrupt: InterruptFlag) {
unsafe {
let reg = &*registers::$TIMERn::ptr();
reg.ifc.write(|w| w.bits(interrupt.bits()) );
}
}
pub fn enable_outputcompare(&mut self, channel: i32) {
match channel {
0 => self.register.cc0_ctrl.modify(|_, w| w.mode().variant(registers::$timerN::cc0_ctrl::MODEW::OUTPUTCOMPARE)),
1 => self.register.cc1_ctrl.modify(|_, w| w.mode().variant(registers::$timerN::cc1_ctrl::MODEW::OUTPUTCOMPARE)),
2 => self.register.cc2_ctrl.modify(|_, w| w.mode().variant(registers::$timerN::cc2_ctrl::MODEW::OUTPUTCOMPARE)),
_ => panic!("Nonexistent channel"),
}
}
pub fn start(&mut self) {
self.register.cmd.write(|w| w.start().bit(true));
}
pub fn split(self) -> Channels<
TimerChannel<$TimerN, Channel0>,
TimerChannel<$TimerN, Channel1>,
TimerChannel<$TimerN, Channel2>,
> {
Channels {
channel0: TimerChannel { _phantom: PhantomData },
channel1: TimerChannel { _phantom: PhantomData },
channel2: TimerChannel { _phantom: PhantomData },
}
}
pub unsafe fn with_registers<T>(&mut self, action: impl FnOnce(&mut registers::$TIMERn) ->T) -> T {
action(&mut self.register)
}
}
timerchannel!($TIMERn, $TimerN, $timerN, Channel0, cc0pen, cc0_ctrl, cc0_ccv);
timerchannel!($TIMERn, $TimerN, $timerN, Channel1, cc1pen, cc1_ctrl, cc1_ccv);
timerchannel!($TIMERn, $TimerN, $timerN, Channel2, cc2pen, cc2_ctrl, cc2_ccv);
}
}
macro_rules! timerchannel {
($TIMERn: ident, $TimerN: ident, $timerN: ident, $ChannelX: ident, $ccXpen: ident, $ccX_ctrl: ident, $ccX_ccv: ident) => {
impl TimerChannel<$TimerN, $ChannelX> {
fn register(&self) -> *mut registers::$timerN::RegisterBlock {
registers::$TIMERn::ptr() as *mut _
}
}
impl<P> RoutedTimerChannel<$TimerN, $ChannelX, P> {
pub fn set_inverted(&mut self, inverted: bool) {
unsafe { &mut *self.channel.register() }.$ccX_ctrl.modify(|_, w| w.outinv().bit(inverted));
}
}
impl<P> embedded_hal::PwmPin for RoutedTimerChannel<$TimerN, $ChannelX, P> {
type Duty = u16;
fn enable(&mut self) {
unsafe { &mut *self.channel.register() }.$ccX_ctrl.modify(|_, w| w.mode().pwm());
#[cfg(feature = "chip-efm32gg")]
{
unsafe { &mut *self.channel.register() }.route.modify(|_, w| w.$ccXpen().set_bit());
}
#[cfg(feature = "chip-efr32xg1")]
{
unsafe { &mut *self.channel.register() }.routepen.modify(|_, w| w.$ccXpen().set_bit());
}
}
#[cfg(feature = "chip-efm32gg")]
fn disable(&mut self) {
unsafe { &mut *self.channel.register() }.route.modify(|_, w| w.$ccXpen().clear_bit());
}
#[cfg(feature = "chip-efr32xg1")]
fn disable(&mut self) {
unsafe { &mut *self.channel.register() }.routepen.modify(|_, w| w.$ccXpen().clear_bit());
}
fn get_duty(&self) -> Self::Duty {
unsafe { &*self.channel.register() }.$ccX_ccv.read().ccv().bits() as Self::Duty
}
fn get_max_duty(&self) -> Self::Duty {
unsafe { &*self.channel.register() }.top.read().bits() as Self::Duty
}
fn set_duty(&mut self, duty: Self::Duty) {
unsafe { &mut *self.channel.register() }.$ccX_ccv.modify(|_, w| unsafe { w.ccv().bits(duty) })
}
}
}
}
#[derive(Copy, Clone)]
pub enum InterruptFlag {
OF = 1,
UF = 2,
CC0 = 16,
CC1 = 32,
CC2 = 64,
ICBOF0 = 256,
ICBOF1 = 512,
ICBOF2 = 1024,
}
impl InterruptFlag {
const fn bits(&self) -> u32 { *self as u32 }
}
timer!(TIMER0, TIMER0Clk, Timer0, timer0);
timer!(TIMER1, TIMER1Clk, Timer1, timer1);
#[cfg(feature = "_has_timer2")]
timer!(TIMER2, TIMER2Clk, Timer2, timer2);
use crate::gpio;
#[cfg(feature = "chip-efm32gg")]
impl Timer2 {
pub fn configure_route0(
&mut self,
) {
self.register.route.modify(|_, w| w.location().variant(registers::timer2::route::LOCATIONW::LOC0));
self.register.cmd.write(|w| w.start().bit(true));
}
}
#[cfg(feature = "chip-efm32gg")]
impl Timer1 {
pub fn configure_route2(
&mut self,
_cc0: gpio::pins::PB0<gpio::Output>,
_cc1: gpio::pins::PB1<gpio::Output>,
_cc2: gpio::pins::PB2<gpio::Output>
) {
self.register.route.modify(|_, w| w.location().variant(registers::timer1::route::LOCATIONW::LOC2));
self.register.cmd.write(|w| w.start().bit(true));
}
}
#[cfg(feature = "chip-efr32xg1")]
impl TimerChannel<Timer0, Channel0> {
pub fn route(self, pin: gpio::pins::PD11<gpio::Output>) -> RoutedTimerChannel<Timer0, Channel0, gpio::pins::PD11<gpio::Output>> {
unsafe { &mut *self.register() }.routeloc0.modify(|_, w| w.cc0loc().loc19());
RoutedTimerChannel {
channel: self,
pin
}
}
}
#[cfg(feature = "chip-efr32xg1")]
impl TimerChannel<Timer0, Channel1> {
pub fn route(self, pin: gpio::pins::PD12<gpio::Output>) -> RoutedTimerChannel<Timer0, Channel1, gpio::pins::PD12<gpio::Output>> {
unsafe { &mut *self.register() }.routeloc0.modify(|_, w| w.cc1loc().loc19());
RoutedTimerChannel {
channel: self,
pin
}
}
}