Skip to content

Commit 67f5d30

Browse files
committed
Add pwm test
1 parent e3e22ab commit 67f5d30

File tree

2 files changed

+101
-0
lines changed

2 files changed

+101
-0
lines changed

Cargo.toml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ optional = true
6666
[dev-dependencies]
6767
cortex-m-rt = "0.7.2"
6868
defmt-rtt = "0.4.0"
69+
defmt-test = "0.3.2"
6970
cortex-m-rtic = "1.1.4"
7071
cortex-m-semihosting = "0.3.5"
7172
panic-probe = { version = "0.3.0", features = ["print-defmt"] }
@@ -125,3 +126,10 @@ required-features = ["usb"]
125126
[[example]]
126127
name = "cordic"
127128
required-features = ["cordic"]
129+
130+
[[test]]
131+
name = "pwm"
132+
harness = false
133+
134+
[lib]
135+
test = false

tests/pwm.rs

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
#![no_std]
2+
#![no_main]
3+
4+
#[path ="../examples/utils/mod.rs"]
5+
mod utils;
6+
7+
use utils::logger::println;
8+
9+
use core::ops::FnMut;
10+
use core::result::Result;
11+
use fugit::{ExtU32, MicrosDurationU32};
12+
use hal::delay::{DelayExt, SystDelay};
13+
use hal::stm32;
14+
use stm32g4xx_hal as hal;
15+
16+
#[defmt_test::tests]
17+
mod tests {
18+
use embedded_hal::pwm::SetDutyCycle;
19+
use fugit::RateExtU32;
20+
use stm32g4xx_hal::{
21+
delay::SYSTDelayExt, gpio::GpioExt, pwm::PwmExt, rcc::RccExt, stm32::GPIOA,
22+
};
23+
24+
#[test]
25+
fn foo() {
26+
use super::*;
27+
28+
let cp = stm32::CorePeripherals::take().expect("cannot take peripherals");
29+
let dp = stm32::Peripherals::take().expect("cannot take peripherals");
30+
let mut rcc = dp.RCC.constrain();
31+
let mut delay = cp.SYST.delay(&rcc.clocks);
32+
33+
let gpioa = dp.GPIOA.split(&mut rcc);
34+
let pin = gpioa.pa8.into_alternate();
35+
36+
let mut pwm = dp.TIM1.pwm(pin, 1000u32.Hz(), &mut rcc);
37+
38+
let _ = pwm.set_duty_cycle_percent(50);
39+
pwm.enable();
40+
41+
let gpioa = unsafe { &*GPIOA::ptr() };
42+
43+
let min: MicrosDurationU32 = 490u32.micros();// Some extra on min for cpu overhead
44+
let max: MicrosDurationU32 = 505u32.micros();
45+
46+
println!("Awaiting rising edge...");
47+
await_lo(&gpioa, &mut delay, max).unwrap();
48+
await_hi(&gpioa, &mut delay, max).unwrap();
49+
50+
for _ in 0..10 {
51+
// Make sure the timer half periods are within 490-505us
52+
53+
let hi_duration = await_lo(&gpioa, &mut delay, max).unwrap();
54+
let lo_duration = await_hi(&gpioa, &mut delay, max).unwrap();
55+
56+
assert!(hi_duration > min);
57+
assert!(lo_duration > min);
58+
}
59+
}
60+
}
61+
62+
#[derive(Debug)]
63+
struct ErrorTimedOut;
64+
65+
fn await_lo(
66+
gpioa: &stm32::gpioa::RegisterBlock,
67+
delay: &mut SystDelay,
68+
timeout: MicrosDurationU32,
69+
) -> Result<MicrosDurationU32, ErrorTimedOut> {
70+
await_p(|| gpioa.idr().read().idr(8).is_low(), delay, timeout)
71+
}
72+
73+
fn await_hi(
74+
gpioa: &stm32::gpioa::RegisterBlock,
75+
delay: &mut SystDelay,
76+
timeout: MicrosDurationU32,
77+
) -> Result<MicrosDurationU32, ErrorTimedOut> {
78+
await_p(|| gpioa.idr().read().idr(8).is_high(), delay, timeout)
79+
}
80+
81+
fn await_p(
82+
mut p: impl FnMut() -> bool,
83+
delay: &mut SystDelay,
84+
timeout: MicrosDurationU32,
85+
) -> Result<MicrosDurationU32, ErrorTimedOut> {
86+
for i in 0..timeout.ticks() {
87+
if p() {
88+
return Ok(i.micros());
89+
}
90+
delay.delay(1_u32.micros());
91+
}
92+
Err(ErrorTimedOut)
93+
}

0 commit comments

Comments
 (0)