Skip to content

Commit

Permalink
HRTIM - Prevent future dataraces for DMA
Browse files Browse the repository at this point in the history
  • Loading branch information
usbalbin committed Apr 19, 2024
1 parent 6c619a9 commit f8018d1
Show file tree
Hide file tree
Showing 12 changed files with 28 additions and 16 deletions.
2 changes: 1 addition & 1 deletion examples/hrtim/adc-trigger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ fn main() -> ! {
let period = 0xFFFF;
let (hr_control, ..) = dp.HRTIM_COMMON.hr_control(&mut rcc).wait_for_calibration();
let mut hr_control = hr_control.constrain();
let (mut timer, (mut cr1, _cr2, mut cr3, mut cr4), (mut out1, mut out2)) = dp
let (mut timer, (mut cr1, _cr2, mut cr3, mut cr4), (mut out1, mut out2), ..) = dp
.HRTIM_TIMA
.pwm_advanced((pin_a, pin_b), &mut rcc)
.prescaler(prescaler)
Expand Down
4 changes: 2 additions & 2 deletions examples/hrtim/capture-dma.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ fn main() -> ! {
.finalize(&mut hr_control);

let mut hr_control = hr_control.constrain();
let (timer, (mut cr1, _cr2, _cr3, _cr4), mut out1) = dp
let (timer, (mut cr1, _cr2, _cr3, _cr4), mut out1, dma_ch) = dp
.HRTIM_TIMA
.pwm_advanced(pin_a, &mut rcc)
.prescaler(prescaler)
Expand All @@ -113,7 +113,7 @@ fn main() -> ! {

let first_buffer = cortex_m::singleton!(: [u32; 16] = [0; 16]).unwrap();
let mut transfer = streams.0.into_circ_peripheral_to_memory_transfer(
capture.enable_dma(),
capture.enable_dma(dma_ch),
&mut first_buffer[..],
config,
);
Expand Down
2 changes: 1 addition & 1 deletion examples/hrtim/capture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ fn main() -> ! {
.finalize(&mut hr_control);

let mut hr_control = hr_control.constrain();
let (mut timer, (mut cr1, _cr2, _cr3, _cr4), mut out1) = dp
let (mut timer, (mut cr1, _cr2, _cr3, _cr4), mut out1, ..) = dp
.HRTIM_TIMA
.pwm_advanced(pin_a, &mut rcc)
.prescaler(prescaler)
Expand Down
2 changes: 1 addition & 1 deletion examples/hrtim/eev-comp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ fn main() -> ! {
// ------------------------- .--------------------------------------
// . . * . . .
// . . * . . .
let (mut timer, (mut cr1, _cr2, _cr3, _cr4), mut out1) = dp
let (mut timer, (mut cr1, _cr2, _cr3, _cr4), mut out1, ..) = dp
.HRTIM_TIMA
.pwm_advanced(pin_a, &mut rcc)
.prescaler(prescaler)
Expand Down
2 changes: 1 addition & 1 deletion examples/hrtim/eev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ fn main() -> ! {
// ------------------------- ------------------------------------------
// . . * . . .
// . . * . . .
let (mut timer, (mut cr1, _cr2, _cr3, _cr4), mut out1) = dp
let (mut timer, (mut cr1, _cr2, _cr3, _cr4), mut out1, ..) = dp
.HRTIM_TIMA
.pwm_advanced(pin_a, &mut rcc)
.prescaler(prescaler)
Expand Down
2 changes: 1 addition & 1 deletion examples/hrtim/flt-comp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ fn main() -> ! {
// ----------------------------------------- --------------------------
// . . . * . .
// . . . * . .
let (mut timer, (mut cr1, _cr2, _cr3, _cr4), mut out1) = dp
let (mut timer, (mut cr1, _cr2, _cr3, _cr4), mut out1, ..) = dp
.HRTIM_TIMA
.pwm_advanced(pin_a, &mut rcc)
.prescaler(prescaler)
Expand Down
2 changes: 1 addition & 1 deletion examples/hrtim/flt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ fn main() -> ! {
// ----------------------------------------- --------------------------
// . . . * . .
// . . . * . .
let (mut timer, (mut cr1, _cr2, _cr3, _cr4), mut out1) = dp
let (mut timer, (mut cr1, _cr2, _cr3, _cr4), mut out1, ..) = dp
.HRTIM_TIMA
.pwm_advanced(pin_a, &mut rcc)
.prescaler(prescaler)
Expand Down
2 changes: 1 addition & 1 deletion examples/hrtim/hrtim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ fn main() -> ! {
let (hr_control, ..) = dp.HRTIM_COMMON.hr_control(&mut rcc).wait_for_calibration();
let mut hr_control = hr_control.constrain();

let (mut timer, (mut cr1, _cr2, _cr3, _cr4), (mut out1, mut out2)) = dp
let (mut timer, (mut cr1, _cr2, _cr3, _cr4), (mut out1, mut out2), ..) = dp
.HRTIM_TIMA
.pwm_advanced((pin_a, pin_b), &mut rcc)
.prescaler(prescaler)
Expand Down
4 changes: 2 additions & 2 deletions examples/hrtim/master.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ fn main() -> ! {
let (hr_control, ..) = dp.HRTIM_COMMON.hr_control(&mut rcc).wait_for_calibration();
let mut hr_control = hr_control.constrain();

let (mut timer, (mut cr1, _cr2, _cr3, _cr4), (mut out1, mut out2)) = dp
let (mut timer, (mut cr1, _cr2, _cr3, _cr4), (mut out1, mut out2), ..) = dp
.HRTIM_TIMA
.pwm_advanced((pin_a, pin_b), &mut rcc)
.prescaler(prescaler)
Expand All @@ -88,7 +88,7 @@ fn main() -> ! {
.timer_mode(HrTimerMode::SingleShotRetriggerable)
.finalize(&mut hr_control);

let (mut mtimer, (mut mcr1, _mcr2, _mcr3, _mcr4)) = dp
let (mut mtimer, (mut mcr1, _mcr2, _mcr3, _mcr4), ..) = dp
.HRTIM_MASTER
.pwm_advanced((), &mut rcc)
.prescaler(prescaler)
Expand Down
4 changes: 3 additions & 1 deletion src/hrtim/capture.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use super::timer;
use crate::dma::mux::DmaMuxResources;
use crate::dma::traits::TargetAddress;
use crate::dma::PeripheralToMemory;
Expand Down Expand Up @@ -123,7 +124,8 @@ macro_rules! impl_capture {
tim.$dier.modify(|_r, w| w.$cptXie().bit(enable));
}

pub fn enable_dma(self) -> HrCapt<$TIMX, PSCL, $CH, Dma> {
pub fn enable_dma(self, _ch: timer::DmaChannel<$TIMX>) -> HrCapt<$TIMX, PSCL, $CH, Dma> {
// SAFETY: We own the only insance of this timers dma channel, no one else can do this
let tim = unsafe { &*$TIMX::ptr() };
tim.$dier.modify(|_r, w| w.$cptXde().set_bit());
HrCapt {
Expand Down
11 changes: 7 additions & 4 deletions src/hrtim/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -592,14 +592,15 @@ macro_rules! hrtim_hal {
pub fn finalize(self, _control: &mut HrPwmControl) -> (
HrTim<$TIMX, PSCL,
HrCapt<$TIMX, PSCL, capture::Ch1, capture::NoDma>,
HrCapt<$TIMX, PSCL, capture::Ch2, capture::NoDma>>,
(
HrCapt<$TIMX, PSCL, capture::Ch2, capture::NoDma>
>, (
HrCr1<$TIMX, PSCL>,
HrCr2<$TIMX, PSCL>,
HrCr3<$TIMX, PSCL>,
HrCr4<$TIMX, PSCL>
),
OUT
OUT,
timer::DmaChannel<$TIMX>,
) {

hrtim_finalize_body!(
Expand Down Expand Up @@ -754,7 +755,9 @@ macro_rules! hrtim_hal_master {
HrCr2<$TIMX, PSCL>,
HrCr3<$TIMX, PSCL>,
HrCr4<$TIMX, PSCL>
)) {
),
timer::DmaChannel<$TIMX>,
) {

hrtim_finalize_body!(self, MasterPreloadSource, $TIMX: ($timXcr, $ck_psc, $perXr, $perx, $tXcen, $rep, $rep, $dier, $repie),)
}
Expand Down
7 changes: 7 additions & 0 deletions src/hrtim/timer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ pub struct HrTim<TIM, PSCL, CPT1, CPT2> {
capture_ch2: CPT2,
}

/// This is the DMA channel of a HRTIM timer
///
/// Every HRTIM timer including the master timer has a DMA channel
pub struct DmaChannel<TIM> {
_x: PhantomData<TIM>,
}

pub trait HrTimer {
type Timer;
type Prescaler: HrtimPrescaler;
Expand Down

0 comments on commit f8018d1

Please sign in to comment.