Skip to content

Commit 47d7a43

Browse files
committed
HRTIM: Capture cleanup
1 parent 749c064 commit 47d7a43

File tree

3 files changed

+31
-20
lines changed

3 files changed

+31
-20
lines changed

examples/hrtim/capture.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ fn main() -> ! {
5757
// PA8 (D7 on Nucleo G474RE)
5858
let pin_a: PA8<Alternate<AF13>> = gpioa.pa8.into_alternate();
5959

60+
// PB5 (D4 on Nucleo G474RE)
61+
let input = gpiob.pb5.into_pull_down_input();
62+
6063
// ...with a prescaler of 128 this gives us a HrTimer with a tick rate of 30MHz
6164
// With max the max period set, this would be 30MHz/2^16 ~= 458Hz...
6265
let prescaler = Pscl128;
@@ -71,10 +74,9 @@ fn main() -> ! {
7174
let (mut hr_control, _flt_inputs, eev_inputs) =
7275
dp.HRTIM_COMMON.hr_control(&mut rcc).wait_for_calibration();
7376

74-
// PB5 (D4 on Nucleo G474RE)
7577
let eev_input6 = eev_inputs
7678
.eev_input6
77-
.bind(gpiob.pb5.into_pull_down_input())
79+
.bind(input)
7880
.edge_or_polarity(external_event::EdgeOrPolarity::Edge(
7981
external_event::Edge::Falling,
8082
))

src/hrtim/capture.rs

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,26 @@ pub trait HrCapture {
3131
/// Get number of ticks relative to beginning of upcounting
3232
///
3333
/// where captures during down counting count as negative (before the upcount)
34+
///
35+
/// ````
36+
/// Counter
37+
/// ---------------------------------- <--- period
38+
/// \ ^ /
39+
/// \ | /
40+
/// \ | /
41+
/// \ | /
42+
/// Down count \ | / Up count
43+
/// \|/
44+
/// <-------------- 0 --------------> t
45+
/// Negative result | positive result
46+
/// ````
3447
fn get_signed(&self) -> i32 {
3548
let (value, dir) = self.get();
36-
let dir_bit = dir as i32;
37-
dir_bit << 31 | i32::from(value)
38-
}
3949

40-
/// Get number of ticks relative to beginning of upcounting
41-
///
42-
/// where captures during down counting count as larger (after upcount)
43-
fn get_unsigned(&self) -> u32 {
44-
let (value, dir) = self.get();
45-
let dir_bit = dir as u32;
46-
dir_bit << 16 | u32::from(value)
50+
match dir {
51+
CountingDirection::Up => i32::from(value),
52+
CountingDirection::Down => -i32::from(value),
53+
}
4754
}
4855

4956
fn clear_interrupt(&mut self);

src/hrtim/timer.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -127,14 +127,6 @@ macro_rules! hrtim_timer {
127127

128128
tim.$dier.modify(|_r, w| w.$repie().bit(enable));
129129
}
130-
131-
pub fn capture_ch1(&mut self) -> &mut HrCapt<$TIMX, PSCL, capture::Ch1> {
132-
&mut self.capture_ch1
133-
}
134-
135-
pub fn capture_ch2(&mut self) -> &mut HrCapt<$TIMX, PSCL, capture::Ch2> {
136-
&mut self.capture_ch2
137-
}
138130
}
139131

140132
$(// Only for Non-Master timers
@@ -230,6 +222,16 @@ hrtim_timer_adc_trigger! {
230222
HRTIM_TIMF: [(Adc13: [(PER: 1 << 24), (RST: 1 << 28)]), (Adc24: [(PER: 1 << 24), ]), (Adc579: [(PER: 30), (RST: 31)]), (Adc6810: [(PER: 31), ])]
231223
}
232224

225+
impl<TIM, PSCL> HrTim<TIM, PSCL> {
226+
pub fn capture_ch1(&mut self) -> &mut HrCapt<TIM, PSCL, capture::Ch1> {
227+
&mut self.capture_ch1
228+
}
229+
230+
pub fn capture_ch2(&mut self) -> &mut HrCapt<TIM, PSCL, capture::Ch2> {
231+
&mut self.capture_ch2
232+
}
233+
}
234+
233235
/// Master Timer Period event
234236
impl<DST, PSCL> super::event::TimerResetEventSource<DST, PSCL> for HrTim<HRTIM_MASTER, PSCL> {
235237
const BITS: u32 = 1 << 4; // MSTPER

0 commit comments

Comments
 (0)