Skip to content

Commit

Permalink
HRTIM: Move some inherent methods to traits on HrTim
Browse files Browse the repository at this point in the history
  • Loading branch information
usbalbin committed Jan 24, 2024
1 parent ff2a1fa commit 9ad72a4
Showing 1 changed file with 39 additions and 14 deletions.
53 changes: 39 additions & 14 deletions src/hrtim/timer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,27 @@ pub trait HrTimer {
fn as_period_adc_trigger(&self) -> super::adc_trigger::TimerPeriod<Self::Timer>;
}

pub trait HrSlaveTimer: HrTimer
{
type CaptureCh1: super::capture::HrCapture;
type CaptureCh2: super::capture::HrCapture;

/// Start listening to the specified event
fn enable_reset_event<E: super::event::TimerResetEventSource<Self::Timer, Self::Prescaler>>(
&mut self,
_event: &E,
);

/// Stop listening to the specified event
fn disable_reset_event<E: super::event::TimerResetEventSource<Self::Timer, Self::Prescaler>>(
&mut self,
_event: &E,
);

fn capture_ch1(&mut self) -> &mut Self::CaptureCh1;
fn capture_ch2(&mut self) -> &mut Self::CaptureCh2;
}

macro_rules! hrtim_timer {
($(
$TIMX:ident:
Expand Down Expand Up @@ -135,8 +156,11 @@ macro_rules! hrtim_timer {
}
}

$(// Only for Non-Master timers
impl<PSCL> HrTim<$TIMX, PSCL> {
$(
impl<PSCL> HrSlaveTimer for HrTim<$TIMX, PSCL> {
type CaptureCh1 = HrCapt<Self::Timer, Self::Prescaler, capture::Ch1>;
type CaptureCh2 = HrCapt<Self::Timer, Self::Prescaler, capture::Ch2>;

/// Reset this timer every time the specified event occurs
///
/// Behaviour depends on `timer_mode`:
Expand All @@ -152,20 +176,31 @@ macro_rules! hrtim_timer {
/// * `HrTimerMode::Continuous`: Enabling the timer enables and starts it simultaneously.
/// When the counter reaches the PER value, it rolls-over to 0x0000 and resumes counting.
/// The counter can be reset at any time
pub fn enable_reset_event<E: super::event::TimerResetEventSource<$TIMX, PSCL>>(&mut self, _event: &E) {
fn enable_reset_event<E: super::event::TimerResetEventSource<Self::Timer, Self::Prescaler>>(&mut self, _event: &E) {
let tim = unsafe { &*$TIMX::ptr() };

unsafe { tim.$rstXr.modify(|r, w| w.bits(r.bits() | E::BITS)); }
}

/// Stop listening to the specified event
pub fn disable_reset_event<E: super::event::TimerResetEventSource<$TIMX, PSCL>>(&mut self, _event: &E) {
fn disable_reset_event<E: super::event::TimerResetEventSource<Self::Timer, Self::Prescaler>>(&mut self, _event: &E) {
let tim = unsafe { &*$TIMX::ptr() };

unsafe { tim.$rstXr.modify(|r, w| w.bits(r.bits() & !E::BITS)); }
}

/// Access the timers first capture channel
fn capture_ch1(&mut self) -> &mut Self::CaptureCh1 {
&mut self.capture_ch1
}

/// Access the timers second capture channel
fn capture_ch2(&mut self) -> &mut Self::CaptureCh2 {
&mut self.capture_ch2
}
}


/// Timer Period event
impl<DST, PSCL> super::event::EventSource<DST, PSCL> for HrTim<$TIMX, PSCL> {
// $rstXr
Expand Down Expand Up @@ -228,16 +263,6 @@ hrtim_timer_adc_trigger! {
HRTIM_TIMF: [(Adc13: [(PER: 1 << 24), (RST: 1 << 28)]), (Adc24: [(PER: 1 << 24), ]), (Adc579: [(PER: 30), (RST: 31)]), (Adc6810: [(PER: 31), ])]
}

impl<TIM, PSCL> HrTim<TIM, PSCL> {
pub fn capture_ch1(&mut self) -> &mut HrCapt<TIM, PSCL, capture::Ch1> {
&mut self.capture_ch1
}

pub fn capture_ch2(&mut self) -> &mut HrCapt<TIM, PSCL, capture::Ch2> {
&mut self.capture_ch2
}
}

/// Master Timer Period event
impl<DST, PSCL> super::event::TimerResetEventSource<DST, PSCL> for HrTim<HRTIM_MASTER, PSCL> {
const BITS: u32 = 1 << 4; // MSTPER
Expand Down

0 comments on commit 9ad72a4

Please sign in to comment.