Skip to content

Commit 2924811

Browse files
committed
HRTIM: Some fixes for capture example
1 parent a823f21 commit 2924811

File tree

1 file changed

+24
-18
lines changed

1 file changed

+24
-18
lines changed

examples/hrtim/capture.rs

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ fn main() -> ! {
1616
hrtim::{
1717
capture::HrCapture, compare_register::HrCompareRegister, control::HrControltExt,
1818
external_event, external_event::ToExternalEventSource, output::HrOutput,
19-
timer::HrTimer, HrPwmAdvExt, Pscl4,
19+
timer::HrTimer, HrPwmAdvExt, Pscl128,
2020
},
2121
pwm,
2222
pwr::PwrExt,
@@ -47,30 +47,30 @@ fn main() -> ! {
4747
info!("Setup Gpio");
4848
let gpioa = dp.GPIOA.split(&mut rcc);
4949
let gpiob = dp.GPIOB.split(&mut rcc);
50+
51+
// PA8 (D7 on Nucleo G474RE)
5052
let pin_a: PA8<Alternate<AF13>> = gpioa.pa8.into_alternate();
5153

52-
// ...with a prescaler of 4 this gives us a HrTimer with a tick rate of 960MHz
53-
// With max the max period set, this would be 960MHz/2^16 ~= 15kHz...
54-
let prescaler = Pscl4;
54+
// ...with a prescaler of 128 this gives us a HrTimer with a tick rate of 30MHz
55+
// With max the max period set, this would be 30MHz/2^16 ~= 458Hz...
56+
let prescaler = Pscl128;
5557

5658
// . .
5759
// . 50% .
5860
// ------ ------
5961
//out1 | | | |
6062
// | | | |
6163
// -------- ---------- --------
62-
// . ^ ^
63-
// . | |
64-
//AD samlp pa0 temp
6564
let period = 0xFFFF;
6665
let (mut hr_control, _flt_inputs, eev_inputs) =
6766
dp.HRTIM_COMMON.hr_control(&mut rcc).wait_for_calibration();
6867

69-
let eev_input3 = eev_inputs
70-
.eev_input3
71-
.bind(gpiob.pb7.into_pull_down_input())
72-
.edge_or_polarity(external_event::EdgeOrPolarity::Polarity(
73-
pwm::Polarity::ActiveHigh,
68+
// PB5 (D4 on Nucleo G474RE)
69+
let eev_input6 = eev_inputs
70+
.eev_input6
71+
.bind(gpiob.pb5.into_pull_down_input())
72+
.edge_or_polarity(external_event::EdgeOrPolarity::Edge(
73+
external_event::Edge::Falling,
7474
))
7575
.finalize(&mut hr_control);
7676

@@ -87,17 +87,23 @@ fn main() -> ! {
8787

8888
cr1.set_duty(period / 2);
8989
timer.start(&mut hr_control);
90+
out1.enable();
9091

9192
let capture = timer.capture_ch1();
9293
capture.enable_interrupt(true, &mut hr_control);
93-
capture.add_event(&eev_input3);
94+
capture.add_event(&eev_input6);
9495

96+
let mut old_duty = 0;
9597
loop {
96-
if !capture.is_pending() {
97-
continue;
98+
for duty in (u32::from(period) / 10)..(9 * u32::from(period) / 10) {
99+
if !capture.is_pending() {
100+
continue;
101+
}
102+
let value = capture.get_signed();
103+
cr1.set_duty(duty as u16);
104+
capture.clear_interrupt();
105+
info!("Capture: {:?}, duty: {}, diff: {}", value, old_duty, value - old_duty as i32);
106+
old_duty = duty;
98107
}
99-
capture.clear_interrupt();
100-
101-
info!("Capture: {:?}", capture.get());
102108
}
103109
}

0 commit comments

Comments
 (0)