11#![ no_std]
22#![ no_main]
33
4- #[ path ="../examples/utils/mod.rs" ]
4+ #[ path = "../examples/utils/mod.rs" ]
55mod utils;
66
77use utils:: logger:: println;
@@ -15,70 +15,146 @@ use stm32g4xx_hal as hal;
1515
1616#[ defmt_test:: tests]
1717mod tests {
18- use embedded_hal:: pwm:: SetDutyCycle ;
18+ use embedded_hal:: {
19+ digital:: { InputPin , OutputPin } ,
20+ pwm:: SetDutyCycle ,
21+ } ;
1922 use fugit:: RateExtU32 ;
2023 use stm32g4xx_hal:: {
21- delay:: SYSTDelayExt , gpio:: { GpioExt , AF6 } , pwm:: PwmExt , rcc:: RccExt , stm32:: GPIOA ,
24+ delay:: SYSTDelayExt ,
25+ gpio:: { GpioExt , AF6 } ,
26+ pwm:: PwmExt ,
27+ rcc:: RccExt ,
28+ stm32:: GPIOA ,
2229 } ;
2330
31+ #[ test]
32+ fn gpio_push_pull ( ) {
33+ use super :: * ;
34+
35+ // TODO: Is it ok to steal these?
36+ let cp = unsafe { stm32:: CorePeripherals :: steal ( ) } ;
37+ let dp = unsafe { stm32:: Peripherals :: steal ( ) } ;
38+ let mut rcc = dp. RCC . constrain ( ) ;
39+ let mut delay = cp. SYST . delay ( & rcc. clocks ) ;
40+
41+ let gpioa = dp. GPIOA . split ( & mut rcc) ;
42+ let mut pin = gpioa. pa8 . into_push_pull_output ( ) ;
43+
44+ pin. set_high ( ) . unwrap ( ) ;
45+ delay. delay ( 1 . millis ( ) ) ; // Give the pin plenty of time to go high
46+ assert ! ( pin. is_high( ) . unwrap( ) ) ;
47+ {
48+ let gpioa = unsafe { & * GPIOA :: PTR } ;
49+ assert ! ( !is_pa8_low( gpioa) ) ;
50+ }
51+
52+ pin. set_low ( ) . unwrap ( ) ;
53+ delay. delay ( 1 . millis ( ) ) ; // Give the pin plenty of time to go low
54+ assert ! ( pin. is_low( ) . unwrap( ) ) ;
55+ {
56+ let gpioa = unsafe { & * GPIOA :: PTR } ;
57+ assert ! ( is_pa8_low( gpioa) ) ;
58+ }
59+ }
60+
61+ #[ test]
62+ fn gpio_open_drain ( ) {
63+ use super :: * ;
64+
65+ // TODO: Is it ok to steal these?
66+ let cp = unsafe { stm32:: CorePeripherals :: steal ( ) } ;
67+ let dp = unsafe { stm32:: Peripherals :: steal ( ) } ;
68+ let mut rcc = dp. RCC . constrain ( ) ;
69+ let mut delay = cp. SYST . delay ( & rcc. clocks ) ;
70+
71+ let gpioa = dp. GPIOA . split ( & mut rcc) ;
72+ let mut pin = gpioa. pa8 . into_open_drain_output ( ) ;
73+
74+ // Enable pull-up resistor
75+ {
76+ let gpioa = unsafe { & * GPIOA :: PTR } ;
77+ gpioa. pupdr ( ) . modify ( |_, w| w. pupdr8 ( ) . pull_up ( ) ) ;
78+ }
79+
80+ pin. set_high ( ) . unwrap ( ) ;
81+ delay. delay ( 1 . millis ( ) ) ; // Give the pin plenty of time to go high
82+ assert ! ( pin. is_high( ) . unwrap( ) ) ;
83+ {
84+ let gpioa = unsafe { & * GPIOA :: PTR } ;
85+ assert ! ( !is_pa8_low( gpioa) ) ;
86+ }
87+
88+ pin. set_low ( ) . unwrap ( ) ;
89+ delay. delay ( 1 . millis ( ) ) ; // Give the pin plenty of time to go low
90+ assert ! ( pin. is_low( ) . unwrap( ) ) ;
91+ {
92+ let gpioa = unsafe { & * GPIOA :: PTR } ;
93+ assert ! ( is_pa8_low( gpioa) ) ;
94+ }
95+ }
96+
2497 #[ test]
2598 fn foo ( ) {
2699 use super :: * ;
27100
28- let cp = stm32:: CorePeripherals :: take ( ) . expect ( "cannot take peripherals" ) ;
29- let dp = stm32:: Peripherals :: take ( ) . expect ( "cannot take peripherals" ) ;
101+ // TODO: Is it ok to steal these?
102+ let cp = unsafe { stm32:: CorePeripherals :: steal ( ) } ;
103+ let dp = unsafe { stm32:: Peripherals :: steal ( ) } ;
30104 let mut rcc = dp. RCC . constrain ( ) ;
31105 let mut delay = cp. SYST . delay ( & rcc. clocks ) ;
32106
33107 let gpioa = dp. GPIOA . split ( & mut rcc) ;
34- let pin: stm32g4xx_hal:: gpio:: gpioa:: PA8 < stm32g4xx_hal:: gpio:: Alternate < AF6 > > = gpioa. pa8 . into_alternate ( ) ;
108+ let pin: stm32g4xx_hal:: gpio:: gpioa:: PA8 < stm32g4xx_hal:: gpio:: Alternate < AF6 > > =
109+ gpioa. pa8 . into_alternate ( ) ;
35110
36- // let mut pwm = dp.TIM1.pwm(pin, 1000u32.Hz(), &mut rcc);
111+ let mut pwm = dp. TIM1 . pwm ( pin, 1000u32 . Hz ( ) , & mut rcc) ;
37112
38- //pwm.set_duty_cycle_percent(50).unwrap();
39- //pwm.enable();
40-
113+ pwm. set_duty_cycle_percent ( 50 ) . unwrap ( ) ;
114+ pwm. enable ( ) ;
41115
42116 let gpioa = unsafe { & * GPIOA :: PTR } ;
43117
44- let get_pin_state = || unsafe { ( 0x4800_0004 as * const u32 ) . read_volatile ( ) } ;
118+ // let get_pin_state = || unsafe { (0x4800_0004 as *const u32).read_volatile() };
45119
46120 // TODO: This is a very bad way to measure time
47- let min: MicrosDurationU32 = 490u32 . micros ( ) ; // Some extra on min for cpu overhead
121+ let min: MicrosDurationU32 = 490u32 . micros ( ) ; // Some extra on min for cpu overhead
48122 let max: MicrosDurationU32 = 505u32 . micros ( ) ;
49123
50124 {
51125 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);
126+ let duration_until_lo = await_lo ( & gpioa, & mut delay, max) . unwrap ( ) ;
127+ println ! ( "Low..., Waited ~{} until low" , duration_until_lo) ;
128+ let lo_duration = await_hi ( & gpioa, & mut delay, max) . unwrap ( ) ;
129+ println ! ( "High..., Low half period: {}" , lo_duration) ;
60130 }
61131
62132 for _ in 0 ..10 {
63133 // Make sure the timer half periods are within 490-505us
64134
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);
135+ let hi_duration = await_lo ( & gpioa, & mut delay, max) . unwrap ( ) ;
136+ println ! ( "Low..., High half period: {}" , hi_duration) ;
137+ assert ! ( hi_duration > min, "{} > {}" , hi_duration, min) ;
68138
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);
139+ let lo_duration = await_hi ( & gpioa, & mut delay, max) . unwrap ( ) ;
140+ println ! ( "High..., Low half period: {}" , lo_duration) ;
141+ assert ! ( lo_duration > min, "{} > {}" , lo_duration, min) ;
72142 }
73143
74144 println ! ( "Done!" ) ;
75145 for i in ( 0 ..5 ) . rev ( ) {
76146 println ! ( "{}" , i) ;
77147 delay. delay ( 1000 . millis ( ) ) ;
78- }*/
148+ }
149+
150+ pwm. disable ( ) ;
79151 }
80152}
81153
154+ fn is_pa8_low ( gpioa : & stm32:: gpioa:: RegisterBlock ) -> bool {
155+ gpioa. idr ( ) . read ( ) . idr ( 8 ) . is_low ( )
156+ }
157+
82158#[ derive( Debug , defmt:: Format ) ]
83159struct ErrorTimedOut ;
84160
@@ -87,15 +163,15 @@ fn await_lo(
87163 delay : & mut SystDelay ,
88164 timeout : MicrosDurationU32 ,
89165) -> Result < MicrosDurationU32 , ErrorTimedOut > {
90- await_p ( || gpioa . idr ( ) . read ( ) . idr ( 8 ) . is_low ( ) , delay, timeout)
166+ await_p ( || is_pa8_low ( gpioa ) , delay, timeout)
91167}
92168
93169fn await_hi (
94170 gpioa : & stm32:: gpioa:: RegisterBlock ,
95171 delay : & mut SystDelay ,
96172 timeout : MicrosDurationU32 ,
97173) -> Result < MicrosDurationU32 , ErrorTimedOut > {
98- await_p ( || gpioa . idr ( ) . read ( ) . idr ( 8 ) . is_high ( ) , delay, timeout)
174+ await_p ( || ! is_pa8_low ( gpioa ) , delay, timeout)
99175}
100176
101177fn await_p (
@@ -107,7 +183,7 @@ fn await_p(
107183 if p ( ) {
108184 return Ok ( i. micros ( ) ) ;
109185 }
110- // delay.delay(1_u32.micros());
186+ delay. delay ( 1_u32 . micros ( ) ) ;
111187 }
112188 Err ( ErrorTimedOut )
113189}
0 commit comments