4
4
#[ path = "../examples/utils/mod.rs" ]
5
5
mod utils;
6
6
7
+ mod common;
8
+
7
9
use utils:: logger:: debug;
8
10
9
- use core:: ops:: FnMut ;
10
- use core:: result:: Result ;
11
11
use fugit:: { ExtU32 , HertzU32 , MicrosDurationU32 } ;
12
12
use hal:: delay:: DelayExt ;
13
13
use hal:: stm32;
@@ -16,15 +16,6 @@ use stm32g4xx_hal as hal;
16
16
pub const F_SYS : HertzU32 = HertzU32 :: MHz ( 16 ) ;
17
17
pub const CYCLES_PER_US : u32 = F_SYS . raw ( ) / 1_000_000 ;
18
18
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
-
28
19
#[ defmt_test:: tests]
29
20
mod tests {
30
21
use embedded_hal:: pwm:: SetDutyCycle ;
@@ -47,6 +38,9 @@ mod tests {
47
38
stm32:: GPIOA ,
48
39
} ;
49
40
41
+ use crate :: common:: { await_hi, await_lo, is_pax_low} ;
42
+ type Timer = crate :: common:: Timer < { crate :: CYCLES_PER_US } > ;
43
+
50
44
#[ test]
51
45
fn gpio_push_pull ( ) {
52
46
use super :: * ;
@@ -61,19 +55,20 @@ mod tests {
61
55
let gpioa = dp. GPIOA . split ( & mut rcc) ;
62
56
let _pa1_important_dont_use_as_output = gpioa. pa1 . into_floating_input ( ) ;
63
57
let mut pin = gpioa. pa8 . into_push_pull_output ( ) ;
58
+ let pin_num = 8 ; // PA8
64
59
65
60
pin. set_high ( ) ;
66
61
delay. delay ( 1 . millis ( ) ) ; // Give the pin plenty of time to go high
67
62
{
68
63
let gpioa = unsafe { & * GPIOA :: PTR } ;
69
- assert ! ( !is_pax_low( gpioa, 8 ) ) ;
64
+ assert ! ( !is_pax_low( gpioa, pin_num ) ) ;
70
65
}
71
66
72
67
pin. set_low ( ) ;
73
68
delay. delay ( 1 . millis ( ) ) ; // Give the pin plenty of time to go low
74
69
{
75
70
let gpioa = unsafe { & * GPIOA :: PTR } ;
76
- assert ! ( is_pax_low( gpioa, 8 ) ) ;
71
+ assert ! ( is_pax_low( gpioa, pin_num ) ) ;
77
72
}
78
73
}
79
74
@@ -90,27 +85,28 @@ mod tests {
90
85
let gpioa = dp. GPIOA . split ( & mut rcc) ;
91
86
let _pa1_important_dont_use_as_output = gpioa. pa1 . into_floating_input ( ) ;
92
87
let mut pin = gpioa. pa8 . into_open_drain_output ( ) ;
88
+ let pin_num = 8 ; // PA8
93
89
94
90
// Enable pull-up resistor
95
91
{
96
92
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 ( ) ) ;
98
94
}
99
95
100
96
pin. set_high ( ) ;
101
97
delay. delay ( 1 . millis ( ) ) ; // Give the pin plenty of time to go high
102
98
assert ! ( pin. is_high( ) ) ;
103
99
{
104
100
let gpioa = unsafe { & * GPIOA :: PTR } ;
105
- assert ! ( !is_pax_low( gpioa, 8 ) ) ;
101
+ assert ! ( !is_pax_low( gpioa, pin_num ) ) ;
106
102
}
107
103
108
104
pin. set_low ( ) ;
109
105
delay. delay ( 1 . millis ( ) ) ; // Give the pin plenty of time to go low
110
106
assert ! ( pin. is_low( ) ) ;
111
107
{
112
108
let gpioa = unsafe { & * GPIOA :: PTR } ;
113
- assert ! ( is_pax_low( gpioa, 8 ) ) ;
109
+ assert ! ( is_pax_low( gpioa, pin_num ) ) ;
114
110
}
115
111
}
116
112
@@ -121,14 +117,15 @@ mod tests {
121
117
// TODO: Is it ok to steal these?
122
118
let mut cp = unsafe { stm32:: CorePeripherals :: steal ( ) } ;
123
119
let dp = unsafe { stm32:: Peripherals :: steal ( ) } ;
124
- enable_timer ( & mut cp) ;
120
+ let timer = Timer :: enable_timer ( & mut cp) ;
125
121
126
122
let mut rcc = dp. RCC . constrain ( ) ;
127
123
assert_eq ! ( rcc. clocks. sys_clk, F_SYS ) ;
128
124
129
125
let gpioa = dp. GPIOA . split ( & mut rcc) ;
130
126
let _pa1_important_dont_use_as_output = gpioa. pa1 . into_floating_input ( ) ;
131
127
let pin: PA8 < AF6 > = gpioa. pa8 . into_alternate ( ) ;
128
+ let pin_num = 8 ; // PA8
132
129
133
130
let mut pwm = dp. TIM1 . pwm ( pin, 1000u32 . Hz ( ) , & mut rcc) ;
134
131
@@ -141,16 +138,15 @@ mod tests {
141
138
let max: MicrosDurationU32 = 505u32 . micros ( ) ;
142
139
143
140
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 ( ) ;
146
143
147
144
let mut hi_duration = 0 . micros ( ) ;
148
145
let mut lo_duration = 0 . micros ( ) ;
149
146
150
147
for _ in 0 ..10 {
151
148
// 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 ( ) ;
154
150
assert ! (
155
151
hi_duration > min && hi_duration < max,
156
152
"hi: {} < {} < {}" ,
@@ -159,7 +155,7 @@ mod tests {
159
155
max
160
156
) ;
161
157
162
- lo_duration = await_hi ( gpioa, max) . unwrap ( ) ;
158
+ lo_duration = await_hi ( & timer , gpioa, pin_num , max) . unwrap ( ) ;
163
159
assert ! (
164
160
lo_duration > min && lo_duration < max,
165
161
"lo: {} < {} < {}" ,
@@ -328,41 +324,3 @@ mod tests {
328
324
assert ! ( ( 20.0 ..35.0 ) . contains( & temp) , "20.0 < {} < 35.0" , temp) ;
329
325
}
330
326
}
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
- }
0 commit comments