@@ -18,7 +18,7 @@ mod tests {
1818 use embedded_hal:: pwm:: SetDutyCycle ;
1919 use fugit:: RateExtU32 ;
2020 use stm32g4xx_hal:: {
21- delay:: SYSTDelayExt , gpio:: GpioExt , pwm:: PwmExt , rcc:: RccExt , stm32:: GPIOA ,
21+ delay:: SYSTDelayExt , gpio:: { GpioExt , AF6 } , pwm:: PwmExt , rcc:: RccExt , stm32:: GPIOA ,
2222 } ;
2323
2424 #[ test]
@@ -31,35 +31,55 @@ mod tests {
3131 let mut delay = cp. SYST . delay ( & rcc. clocks ) ;
3232
3333 let gpioa = dp. GPIOA . split ( & mut rcc) ;
34- let pin = gpioa. pa8 . into_alternate ( ) ;
34+ let pin: stm32g4xx_hal :: gpio :: gpioa :: PA8 < stm32g4xx_hal :: gpio :: Alternate < AF6 > > = gpioa. pa8 . into_alternate ( ) ;
3535
36- let mut pwm = dp. TIM1 . pwm ( pin, 1000u32 . Hz ( ) , & mut rcc) ;
36+ // let mut pwm = dp.TIM1.pwm(pin, 1000u32.Hz(), &mut rcc);
3737
38- let _ = pwm. set_duty_cycle_percent ( 50 ) ;
39- pwm. enable ( ) ;
38+ //pwm.set_duty_cycle_percent(50).unwrap();
39+ //pwm.enable();
40+
4041
41- let gpioa = unsafe { & * GPIOA :: ptr ( ) } ;
42+ let gpioa = unsafe { & * GPIOA :: PTR } ;
4243
44+ let get_pin_state = || unsafe { ( 0x4800_0004 as * const u32 ) . read_volatile ( ) } ;
45+
46+ // TODO: This is a very bad way to measure time
4347 let min: MicrosDurationU32 = 490u32 . micros ( ) ; // Some extra on min for cpu overhead
4448 let max: MicrosDurationU32 = 505u32 . micros ( ) ;
4549
46- println ! ( "Awaiting rising edge..." ) ;
47- await_lo ( & gpioa, & mut delay, max) . unwrap ( ) ;
48- await_hi ( & gpioa, & mut delay, max) . unwrap ( ) ;
50+ {
51+ println ! ( "Awaiting first rising edge..." ) ;
52+ //println!("{}", gpioa.odr().read().odr(8).is_high());
53+ println ! ( "{}" , get_pin_state( ) ) ;
54+ //let duration_until_lo = await_lo(&gpioa, &mut delay, max);//.unwrap();
55+ }
56+ /*
57+ //println!("Low..., Waited ~{}us until low", duration_until_lo);
58+ let lo_duration = await_hi(&gpioa, &mut delay, max);//.unwrap();
59+ //println!("High..., Low half period: {}us", lo_duration);
60+ }
4961
5062 for _ in 0..10 {
5163 // Make sure the timer half periods are within 490-505us
5264
53- let hi_duration = await_lo ( & gpioa, & mut delay, max) . unwrap ( ) ;
54- let lo_duration = await_hi ( & gpioa, & mut delay, max) . unwrap ( ) ;
65+ let hi_duration = await_lo(&gpioa, &mut delay, max);//.unwrap();
66+ println!("Low..., High half period: {}us", hi_duration);
67+ //assert!(hi_duration > min, "{} > {}", hi_duration, min);
5568
56- assert ! ( hi_duration > min) ;
57- assert ! ( lo_duration > min) ;
69+ let lo_duration = await_hi(&gpioa, &mut delay, max);//.unwrap();
70+ println!("High..., Low half period: {}us", lo_duration);
71+ //assert!(lo_duration > min, "{} > {}", lo_duration, min);
5872 }
73+
74+ println!("Done!");
75+ for i in (0..5).rev() {
76+ println!("{}", i);
77+ delay.delay(1000.millis());
78+ }*/
5979 }
6080}
6181
62- #[ derive( Debug ) ]
82+ #[ derive( Debug , defmt :: Format ) ]
6383struct ErrorTimedOut ;
6484
6585fn await_lo (
@@ -87,7 +107,7 @@ fn await_p(
87107 if p ( ) {
88108 return Ok ( i. micros ( ) ) ;
89109 }
90- delay. delay ( 1_u32 . micros ( ) ) ;
110+ // delay.delay(1_u32.micros());
91111 }
92112 Err ( ErrorTimedOut )
93113}
0 commit comments