Skip to content

Commit

Permalink
External clocks for timers (#128)
Browse files Browse the repository at this point in the history
* timers external clock

* disable tim15 for stm32g081
  • Loading branch information
dotcypress authored Nov 16, 2022
1 parent ea8bee6 commit 83dd084
Show file tree
Hide file tree
Showing 3 changed files with 152 additions and 13 deletions.
4 changes: 4 additions & 0 deletions src/rcc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,10 @@ impl Rcc {
}
}

pub fn trim_hsi_clocks(&mut self, value: u8) {
self.icscr.modify(|_, w| unsafe { w.hsitrim().bits(value) });
}

pub fn set_reset_mode(&mut self, mode: ResetMode) {
unsafe {
let flash = &(*FLASH::ptr());
Expand Down
75 changes: 62 additions & 13 deletions src/timer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
use crate::rcc::*;
use crate::stm32::*;
use crate::time::{Hertz, MicroSecond};
use core::marker::PhantomData;
use cortex_m::peripheral::syst::SystClkSource;
use cortex_m::peripheral::SYST;
use hal::timer::{CountDown, Periodic};
Expand Down Expand Up @@ -151,19 +152,6 @@ macro_rules! timers {
low | (_high << 16)
}

/// Releases the TIM peripheral
pub fn release(self) -> $TIM {
self.tim
}
}

impl TimerExt<$TIM> for $TIM {
fn timer(self, rcc: &mut Rcc) -> Timer<$TIM> {
Timer::$tim(self, rcc)
}
}

impl Timer<$TIM> {
pub fn start(&mut self, timeout: MicroSecond) {
// Pause the counter. Also set URS so that when we set UG below, it will
// generate an update event *without* triggering an interrupt.
Expand Down Expand Up @@ -196,6 +184,17 @@ macro_rules! timers {
Ok(())
}
}

/// Releases the TIM peripheral
pub fn release(self) -> $TIM {
self.tim
}
}

impl TimerExt<$TIM> for $TIM {
fn timer(self, rcc: &mut Rcc) -> Timer<$TIM> {
Timer::$tim(self, rcc)
}
}

impl CountDown for Timer<$TIM> {
Expand All @@ -218,6 +217,56 @@ macro_rules! timers {
}
}

pub enum ExternalClockMode {
Mode1,
Mode2,
}

pub trait ExternalClock {
fn mode(&self) -> ExternalClockMode;
}

macro_rules! timers_external_clocks {
($($TIM:ident: ($tim:ident, $sms:ident $(,$ece:ident)*),)+) => {
$(
impl Timer<$TIM> {
pub fn use_external_clock<C: ExternalClock>(&mut self, clk: C, freq: Hertz) {
self.clk = freq;
match clk.mode() {
ExternalClockMode::Mode1 => {
self.tim.smcr.modify(|_, w| unsafe { w.$sms().bits(0b111) });
$(
self.tim.smcr.modify(|_, w| w.$ece().clear_bit());
)*
},
ExternalClockMode::Mode2 => {
self.tim.smcr.modify(|_, w| unsafe { w.$sms().bits(0b0) });
$(
self.tim.smcr.modify(|_, w| w.$ece().set_bit());
)*
},
}
}
}
)+
}
}

timers_external_clocks! {
TIM1: (tim1, sms, ece),
TIM3: (tim3, sms, ece),
}

#[cfg(feature = "stm32g0x1")]
timers_external_clocks! {
TIM2: (tim2, sms, ece),
}

#[cfg(any(feature = "stm32g070", feature = "stm32g071"))]
timers_external_clocks! {
TIM15: (tim15, sms1),
}

timers! {
TIM1: (tim1, cnt),
TIM3: (tim3, cnt_l, cnt_h),
Expand Down
86 changes: 86 additions & 0 deletions src/timer/pins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,23 @@ pub trait TimerPin<TIM> {
fn release(self) -> Self;
}

pub struct TriggerPin<TIM, PIN: TimerPin<TIM>> {
pin: PIN,
tim: PhantomData<TIM>,
}

impl<TIM, PIN: TimerPin<TIM>> ExternalClock for TriggerPin<TIM, PIN> {
fn mode(&self) -> ExternalClockMode {
ExternalClockMode::Mode1
}
}

impl<TIM, PIN: TimerPin<TIM>> TriggerPin<TIM, PIN> {
pub fn release(self) -> PIN {
self.pin
}
}

macro_rules! timer_pins {
($TIMX:ident, [ $(($ch:ty, $pin:ty, $af_mode:expr),)+ ]) => {
$(
Expand All @@ -28,6 +45,75 @@ macro_rules! timer_pins {
};
}

macro_rules! trigger_pins {
($TIMX:ident, [ $(($pin:ty, $ccp:ident $(,$icf:ident)*),)+ ]) => {
$(
impl TriggerPin<$TIMX, $pin> {
pub fn new(pin: $pin, edge: SignalEdge) -> Self {
TimerPin::<$TIMX>::setup(&pin);
let tim = unsafe { &(*$TIMX::ptr()) };
let ts = match edge {
SignalEdge::All => 0b100,
SignalEdge::Falling => {
tim.ccer.modify(|_, w| w.$ccp().set_bit());
0b101
},
SignalEdge::Rising => {
tim.ccer.modify(|_, w| w.$ccp().clear_bit());
0b101
}
};

tim.smcr.modify(|_, w| unsafe { w.ts().bits(ts) });

Self {
pin,
tim: PhantomData,
}
}

$(
pub fn with_filter(pin: $pin, edge: SignalEdge, capture_filter: u8) -> Self {
unsafe {
let tim = &(*$TIMX::ptr()) ;
tim.ccmr1_input().modify(|_, w| w.$icf().bits(capture_filter));
}
Self::new(pin, edge)
}
)*
}
)+
};
}

trigger_pins!(TIM1, [
(PA8<DefaultMode>, cc1p),
(PC8<DefaultMode>, cc1p),
(PA9<DefaultMode>, cc2p),
(PB3<DefaultMode>, cc2p),
(PC9<DefaultMode>, cc2p),
]);

#[cfg(feature = "stm32g0x1")]
trigger_pins!(TIM2, [
(PA0<DefaultMode>, cc1p, ic1f),
(PA5<DefaultMode>, cc1p, ic1f),
(PA15<DefaultMode>, cc1p, ic1f),
(PC4<DefaultMode>, cc1p, ic1f),
(PA1<DefaultMode>, cc2p, ic2f),
(PB3<DefaultMode>, cc2p, ic2f),
(PC5<DefaultMode>, cc2p, ic2f),
]);

trigger_pins!(TIM3, [
(PA6<DefaultMode>, cc1p, ic1f),
(PB4<DefaultMode>, cc1p, ic1f),
(PC6<DefaultMode>, cc1p, ic1f),
(PA7<DefaultMode>, cc2p, ic2f),
(PB5<DefaultMode>, cc2p, ic2f),
(PC7<DefaultMode>, cc2p, ic2f),
]);

timer_pins!(TIM1, [
(Channel1, PA8<DefaultMode>, AltFunction::AF2),
(Channel1, PC8<DefaultMode>, AltFunction::AF2),
Expand Down

0 comments on commit 83dd084

Please sign in to comment.