|
| 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