Skip to content

Commit 991f9f1

Browse files
committed
Cleanup tests
1 parent d76ab4c commit 991f9f1

File tree

3 files changed

+89
-61
lines changed

3 files changed

+89
-61
lines changed

tests/common/mod.rs

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
use fugit::{ExtU32, MicrosDurationU32};
2+
use stm32g4xx_hal::stm32;
3+
4+
#[non_exhaustive]
5+
pub struct Timer<const CYCLES_PER_US: u32>;
6+
7+
impl<const CYCLES_PER_US: u32> Timer<CYCLES_PER_US> {
8+
#[allow(dead_code)]
9+
pub fn enable_timer(cp: &mut stm32::CorePeripherals) -> Self {
10+
cp.DCB.enable_trace();
11+
cp.DWT.enable_cycle_counter();
12+
13+
Timer
14+
}
15+
16+
/// Returns duration since timer start
17+
pub fn now(&self) -> MicrosDurationU32 {
18+
(stm32::DWT::cycle_count() / CYCLES_PER_US).micros()
19+
}
20+
}
21+
22+
#[allow(dead_code)]
23+
pub fn is_pax_low(gpioa: &stm32::gpioa::RegisterBlock, pin: u8) -> bool {
24+
gpioa.idr().read().idr(pin).is_low()
25+
}
26+
27+
#[allow(dead_code)]
28+
#[derive(Debug, defmt::Format)]
29+
pub struct ErrorTimedOut;
30+
31+
#[allow(dead_code)]
32+
pub fn await_lo<const CYCLES_PER_US: u32>(
33+
timer: &Timer<CYCLES_PER_US>,
34+
gpioa: &stm32::gpioa::RegisterBlock,
35+
pin: u8,
36+
timeout: MicrosDurationU32,
37+
) -> Result<MicrosDurationU32, ErrorTimedOut> {
38+
await_p(timer, || is_pax_low(gpioa, pin), timeout)
39+
}
40+
41+
#[allow(dead_code)]
42+
pub fn await_hi<const CYCLES_PER_US: u32>(
43+
timer: &Timer<CYCLES_PER_US>,
44+
gpioa: &stm32::gpioa::RegisterBlock,
45+
pin: u8,
46+
timeout: MicrosDurationU32,
47+
) -> Result<MicrosDurationU32, ErrorTimedOut> {
48+
await_p(timer, || !is_pax_low(gpioa, pin), timeout)
49+
}
50+
51+
#[allow(dead_code)]
52+
pub fn await_p<const CYCLES_PER_US: u32>(
53+
timer: &Timer<CYCLES_PER_US>,
54+
mut p: impl FnMut() -> bool,
55+
timeout: MicrosDurationU32,
56+
) -> Result<MicrosDurationU32, ErrorTimedOut> {
57+
let before = timer.now();
58+
59+
loop {
60+
let passed_time = timer.now() - before;
61+
if p() {
62+
return Ok(passed_time);
63+
}
64+
if passed_time > timeout {
65+
return Err(ErrorTimedOut);
66+
}
67+
}
68+
}

tests/nucleo-g474.rs

Lines changed: 18 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
#[path = "../examples/utils/mod.rs"]
55
mod utils;
66

7+
mod common;
8+
79
use utils::logger::debug;
810

9-
use core::ops::FnMut;
10-
use core::result::Result;
1111
use fugit::{ExtU32, HertzU32, MicrosDurationU32};
1212
use hal::delay::DelayExt;
1313
use hal::stm32;
@@ -16,15 +16,6 @@ use stm32g4xx_hal as hal;
1616
pub const F_SYS: HertzU32 = HertzU32::MHz(16);
1717
pub const CYCLES_PER_US: u32 = F_SYS.raw() / 1_000_000;
1818

19-
pub fn enable_timer(cp: &mut stm32::CorePeripherals) {
20-
cp.DCB.enable_trace();
21-
cp.DWT.enable_cycle_counter();
22-
}
23-
24-
pub fn now() -> MicrosDurationU32 {
25-
(stm32::DWT::cycle_count() / CYCLES_PER_US).micros()
26-
}
27-
2819
#[defmt_test::tests]
2920
mod tests {
3021
use embedded_hal::pwm::SetDutyCycle;
@@ -47,6 +38,9 @@ mod tests {
4738
stm32::GPIOA,
4839
};
4940

41+
use crate::common::{await_hi, await_lo, is_pax_low};
42+
type Timer = crate::common::Timer<{ crate::CYCLES_PER_US }>;
43+
5044
#[test]
5145
fn gpio_push_pull() {
5246
use super::*;
@@ -61,19 +55,20 @@ mod tests {
6155
let gpioa = dp.GPIOA.split(&mut rcc);
6256
let _pa1_important_dont_use_as_output = gpioa.pa1.into_floating_input();
6357
let mut pin = gpioa.pa8.into_push_pull_output();
58+
let pin_num = 8; // PA8
6459

6560
pin.set_high();
6661
delay.delay(1.millis()); // Give the pin plenty of time to go high
6762
{
6863
let gpioa = unsafe { &*GPIOA::PTR };
69-
assert!(!is_pax_low(gpioa, 8));
64+
assert!(!is_pax_low(gpioa, pin_num));
7065
}
7166

7267
pin.set_low();
7368
delay.delay(1.millis()); // Give the pin plenty of time to go low
7469
{
7570
let gpioa = unsafe { &*GPIOA::PTR };
76-
assert!(is_pax_low(gpioa, 8));
71+
assert!(is_pax_low(gpioa, pin_num));
7772
}
7873
}
7974

@@ -90,27 +85,28 @@ mod tests {
9085
let gpioa = dp.GPIOA.split(&mut rcc);
9186
let _pa1_important_dont_use_as_output = gpioa.pa1.into_floating_input();
9287
let mut pin = gpioa.pa8.into_open_drain_output();
88+
let pin_num = 8; // PA8
9389

9490
// Enable pull-up resistor
9591
{
9692
let gpioa = unsafe { &*GPIOA::PTR };
97-
gpioa.pupdr().modify(|_, w| w.pupdr8().pull_up());
93+
gpioa.pupdr().modify(|_, w| w.pupdr(pin_num).pull_up());
9894
}
9995

10096
pin.set_high();
10197
delay.delay(1.millis()); // Give the pin plenty of time to go high
10298
assert!(pin.is_high());
10399
{
104100
let gpioa = unsafe { &*GPIOA::PTR };
105-
assert!(!is_pax_low(gpioa, 8));
101+
assert!(!is_pax_low(gpioa, pin_num));
106102
}
107103

108104
pin.set_low();
109105
delay.delay(1.millis()); // Give the pin plenty of time to go low
110106
assert!(pin.is_low());
111107
{
112108
let gpioa = unsafe { &*GPIOA::PTR };
113-
assert!(is_pax_low(gpioa, 8));
109+
assert!(is_pax_low(gpioa, pin_num));
114110
}
115111
}
116112

@@ -121,14 +117,15 @@ mod tests {
121117
// TODO: Is it ok to steal these?
122118
let mut cp = unsafe { stm32::CorePeripherals::steal() };
123119
let dp = unsafe { stm32::Peripherals::steal() };
124-
enable_timer(&mut cp);
120+
let timer = Timer::enable_timer(&mut cp);
125121

126122
let mut rcc = dp.RCC.constrain();
127123
assert_eq!(rcc.clocks.sys_clk, F_SYS);
128124

129125
let gpioa = dp.GPIOA.split(&mut rcc);
130126
let _pa1_important_dont_use_as_output = gpioa.pa1.into_floating_input();
131127
let pin: PA8<AF6> = gpioa.pa8.into_alternate();
128+
let pin_num = 8; // PA8
132129

133130
let mut pwm = dp.TIM1.pwm(pin, 1000u32.Hz(), &mut rcc);
134131

@@ -141,16 +138,15 @@ mod tests {
141138
let max: MicrosDurationU32 = 505u32.micros();
142139

143140
debug!("Awaiting first rising edge...");
144-
let duration_until_lo = await_lo(gpioa, max).unwrap();
145-
let first_lo_duration = await_hi(gpioa, max).unwrap();
141+
let duration_until_lo = await_lo(&timer, gpioa, pin_num, max).unwrap();
142+
let first_lo_duration = await_hi(&timer, gpioa, pin_num, max).unwrap();
146143

147144
let mut hi_duration = 0.micros();
148145
let mut lo_duration = 0.micros();
149146

150147
for _ in 0..10 {
151148
// Make sure the timer half periods are within 495-505us
152-
153-
hi_duration = await_lo(gpioa, max).unwrap();
149+
hi_duration = await_lo(&timer, gpioa, pin_num, max).unwrap();
154150
assert!(
155151
hi_duration > min && hi_duration < max,
156152
"hi: {} < {} < {}",
@@ -159,7 +155,7 @@ mod tests {
159155
max
160156
);
161157

162-
lo_duration = await_hi(gpioa, max).unwrap();
158+
lo_duration = await_hi(&timer, gpioa, pin_num, max).unwrap();
163159
assert!(
164160
lo_duration > min && lo_duration < max,
165161
"lo: {} < {} < {}",
@@ -328,41 +324,3 @@ mod tests {
328324
assert!((20.0..35.0).contains(&temp), "20.0 < {} < 35.0", temp);
329325
}
330326
}
331-
332-
fn is_pax_low(gpioa: &stm32::gpioa::RegisterBlock, x: u8) -> bool {
333-
gpioa.idr().read().idr(x).is_low()
334-
}
335-
336-
#[derive(Debug, defmt::Format)]
337-
struct ErrorTimedOut;
338-
339-
fn await_lo(
340-
gpioa: &stm32::gpioa::RegisterBlock,
341-
timeout: MicrosDurationU32,
342-
) -> Result<MicrosDurationU32, ErrorTimedOut> {
343-
await_p(|| is_pax_low(gpioa, 8), timeout)
344-
}
345-
346-
fn await_hi(
347-
gpioa: &stm32::gpioa::RegisterBlock,
348-
timeout: MicrosDurationU32,
349-
) -> Result<MicrosDurationU32, ErrorTimedOut> {
350-
await_p(|| !is_pax_low(gpioa, 8), timeout)
351-
}
352-
353-
fn await_p(
354-
mut p: impl FnMut() -> bool,
355-
timeout: MicrosDurationU32,
356-
) -> Result<MicrosDurationU32, ErrorTimedOut> {
357-
let before = now();
358-
359-
loop {
360-
let passed_time = now() - before;
361-
if p() {
362-
return Ok(passed_time);
363-
}
364-
if passed_time > timeout {
365-
return Err(ErrorTimedOut);
366-
}
367-
}
368-
}

tests/nucleo-g474_w_jumpers.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
#[path = "../examples/utils/mod.rs"]
77
mod utils;
88

9+
mod common;
10+
911
use stm32g4xx_hal::adc::{self, AdcClaim, AdcCommonExt};
1012
use stm32g4xx_hal::comparator::{self, ComparatorSplit};
1113
use stm32g4xx_hal::dac::{self, DacExt, DacOut};
@@ -150,7 +152,7 @@ mod tests {
150152
/// | \
151153
/// dac ---> | + \
152154
/// | *----->
153-
/// Vref---> | - /
155+
/// Vref---> | - /
154156
/// | /
155157
/// | /
156158
///

0 commit comments

Comments
 (0)