@@ -15,19 +15,19 @@ use core::cell::RefCell;
1515use critical_section:: Mutex ;
1616use esp_hal:: {
1717 delay:: Delay ,
18- gpio:: { AnyPin , GpioPin , Input , Io , Level , Output , Pull } ,
18+ gpio:: { AnyPin , ErasedPin , Input , Io , Level , Output , Pin , Pull } ,
1919 macros:: handler,
2020 timer:: timg:: TimerGroup ,
2121 InterruptConfigurable ,
2222} ;
2323use hil_test as _;
2424
2525static COUNTER : Mutex < RefCell < u32 > > = Mutex :: new ( RefCell :: new ( 0 ) ) ;
26- static INPUT_PIN : Mutex < RefCell < Option < Input < ' static > > > > = Mutex :: new ( RefCell :: new ( None ) ) ;
26+ static INPUT_PIN : Mutex < RefCell < Option < Input > > > = Mutex :: new ( RefCell :: new ( None ) ) ;
2727
28- struct Context < ' d > {
29- test_gpio1 : Input < ' d > ,
30- test_gpio2 : Output < ' d > ,
28+ struct Context {
29+ test_gpio1 : ErasedPin ,
30+ test_gpio2 : ErasedPin ,
3131 delay : Delay ,
3232}
3333
@@ -53,7 +53,7 @@ mod tests {
5353 use super :: * ;
5454
5555 #[ init]
56- fn init ( ) -> Context < ' static > {
56+ fn init ( ) -> Context {
5757 let peripherals = esp_hal:: init ( esp_hal:: Config :: default ( ) ) ;
5858
5959 let mut io = Io :: new ( peripherals. GPIO , peripherals. IO_MUX ) ;
@@ -67,20 +67,22 @@ mod tests {
6767 esp_hal_embassy:: init ( timg0. timer0 ) ;
6868
6969 Context {
70- test_gpio1 : Input :: new ( gpio1, Pull :: Down ) ,
71- test_gpio2 : Output :: new ( gpio2, Level :: Low ) ,
70+ test_gpio1 : gpio1. degrade ( ) ,
71+ test_gpio2 : gpio2. degrade ( ) ,
7272 delay,
7373 }
7474 }
7575
7676 #[ test]
77- async fn test_async_edge ( ctx : Context < ' static > ) {
77+ async fn test_async_edge ( ctx : Context ) {
7878 let counter = AtomicUsize :: new ( 0 ) ;
7979 let Context {
80- mut test_gpio1,
81- mut test_gpio2,
80+ test_gpio1,
81+ test_gpio2,
8282 ..
8383 } = ctx;
84+ let mut test_gpio1 = Input :: new ( test_gpio1, Pull :: Down ) ;
85+ let mut test_gpio2 = Output :: new ( test_gpio2, Level :: Low ) ;
8486 embassy_futures:: select:: select (
8587 async {
8688 loop {
@@ -102,8 +104,8 @@ mod tests {
102104 }
103105
104106 #[ test]
105- async fn test_a_pin_can_wait ( _ctx : Context < ' static > ) {
106- let mut first = Input :: new ( unsafe { GpioPin :: < 0 > :: steal ( ) } , Pull :: Down ) ;
107+ async fn test_a_pin_can_wait ( ctx : Context ) {
108+ let mut first = Input :: new ( ctx . test_gpio1 , Pull :: Down ) ;
107109
108110 embassy_futures:: select:: select (
109111 first. wait_for_rising_edge ( ) ,
@@ -115,69 +117,74 @@ mod tests {
115117 }
116118
117119 #[ test]
118- fn test_gpio_input ( ctx : Context < ' static > ) {
120+ fn test_gpio_input ( ctx : Context ) {
121+ let test_gpio1 = Input :: new ( ctx. test_gpio1 , Pull :: Down ) ;
119122 // `InputPin`:
120- assert_eq ! ( ctx . test_gpio1. is_low( ) , true ) ;
121- assert_eq ! ( ctx . test_gpio1. is_high( ) , false ) ;
123+ assert_eq ! ( test_gpio1. is_low( ) , true ) ;
124+ assert_eq ! ( test_gpio1. is_high( ) , false ) ;
122125 }
123126
124127 #[ test]
125- fn test_gpio_output ( mut ctx : Context < ' static > ) {
128+ fn test_gpio_output ( ctx : Context ) {
129+ let mut test_gpio2 = Output :: new ( ctx. test_gpio2 , Level :: Low ) ;
130+
126131 // `StatefulOutputPin`:
127- assert_eq ! ( ctx . test_gpio2. is_set_low( ) , true ) ;
128- assert_eq ! ( ctx . test_gpio2. is_set_high( ) , false ) ;
129- ctx . test_gpio2 . set_high ( ) ;
130- assert_eq ! ( ctx . test_gpio2. is_set_low( ) , false ) ;
131- assert_eq ! ( ctx . test_gpio2. is_set_high( ) , true ) ;
132+ assert_eq ! ( test_gpio2. is_set_low( ) , true ) ;
133+ assert_eq ! ( test_gpio2. is_set_high( ) , false ) ;
134+ test_gpio2. set_high ( ) ;
135+ assert_eq ! ( test_gpio2. is_set_low( ) , false ) ;
136+ assert_eq ! ( test_gpio2. is_set_high( ) , true ) ;
132137
133138 // `ToggleableOutputPin`:
134- ctx . test_gpio2 . toggle ( ) ;
135- assert_eq ! ( ctx . test_gpio2. is_set_low( ) , true ) ;
136- assert_eq ! ( ctx . test_gpio2. is_set_high( ) , false ) ;
137- ctx . test_gpio2 . toggle ( ) ;
138- assert_eq ! ( ctx . test_gpio2. is_set_low( ) , false ) ;
139- assert_eq ! ( ctx . test_gpio2. is_set_high( ) , true ) ;
139+ test_gpio2. toggle ( ) ;
140+ assert_eq ! ( test_gpio2. is_set_low( ) , true ) ;
141+ assert_eq ! ( test_gpio2. is_set_high( ) , false ) ;
142+ test_gpio2. toggle ( ) ;
143+ assert_eq ! ( test_gpio2. is_set_low( ) , false ) ;
144+ assert_eq ! ( test_gpio2. is_set_high( ) , true ) ;
140145 }
141146
142147 #[ test]
143- fn test_gpio_interrupt ( mut ctx : Context < ' static > ) {
148+ fn test_gpio_interrupt ( ctx : Context ) {
149+ let mut test_gpio1 = Input :: new ( ctx. test_gpio1 , Pull :: Down ) ;
150+ let mut test_gpio2 = Output :: new ( ctx. test_gpio2 , Level :: Low ) ;
151+
144152 critical_section:: with ( |cs| {
145153 * COUNTER . borrow_ref_mut ( cs) = 0 ;
146- ctx . test_gpio1 . listen ( Event :: AnyEdge ) ;
147- INPUT_PIN . borrow_ref_mut ( cs) . replace ( ctx . test_gpio1 ) ;
154+ test_gpio1. listen ( Event :: AnyEdge ) ;
155+ INPUT_PIN . borrow_ref_mut ( cs) . replace ( test_gpio1) ;
148156 } ) ;
149- ctx . test_gpio2 . set_high ( ) ;
157+ test_gpio2. set_high ( ) ;
150158 ctx. delay . delay_millis ( 1 ) ;
151- ctx . test_gpio2 . set_low ( ) ;
159+ test_gpio2. set_low ( ) ;
152160 ctx. delay . delay_millis ( 1 ) ;
153- ctx . test_gpio2 . set_high ( ) ;
161+ test_gpio2. set_high ( ) ;
154162 ctx. delay . delay_millis ( 1 ) ;
155- ctx . test_gpio2 . set_low ( ) ;
163+ test_gpio2. set_low ( ) ;
156164 ctx. delay . delay_millis ( 1 ) ;
157- ctx . test_gpio2 . set_high ( ) ;
165+ test_gpio2. set_high ( ) ;
158166 ctx. delay . delay_millis ( 1 ) ;
159- ctx . test_gpio2 . set_low ( ) ;
167+ test_gpio2. set_low ( ) ;
160168 ctx. delay . delay_millis ( 1 ) ;
161- ctx . test_gpio2 . set_high ( ) ;
169+ test_gpio2. set_high ( ) ;
162170 ctx. delay . delay_millis ( 1 ) ;
163- ctx . test_gpio2 . set_low ( ) ;
171+ test_gpio2. set_low ( ) ;
164172 ctx. delay . delay_millis ( 1 ) ;
165- ctx . test_gpio2 . set_high ( ) ;
173+ test_gpio2. set_high ( ) ;
166174 ctx. delay . delay_millis ( 1 ) ;
167175
168176 let count = critical_section:: with ( |cs| * COUNTER . borrow_ref ( cs) ) ;
169177 assert_eq ! ( count, 9 ) ;
170178
171- ctx. test_gpio1 = critical_section:: with ( |cs| INPUT_PIN . borrow_ref_mut ( cs) . take ( ) . unwrap ( ) ) ;
172- ctx. test_gpio1 . unlisten ( ) ;
179+ let mut test_gpio1 =
180+ critical_section:: with ( |cs| INPUT_PIN . borrow_ref_mut ( cs) . take ( ) . unwrap ( ) ) ;
181+ test_gpio1. unlisten ( ) ;
173182 }
174183
175184 #[ test]
176- fn test_gpio_od ( ctx : Context < ' static > ) {
177- let mut test_gpio1 =
178- OutputOpenDrain :: new ( unsafe { TestGpio1 :: steal ( ) } , Level :: High , Pull :: Up ) ;
179- let mut test_gpio2 =
180- OutputOpenDrain :: new ( unsafe { TestGpio2 :: steal ( ) } , Level :: High , Pull :: Up ) ;
185+ fn test_gpio_od ( ctx : Context ) {
186+ let mut test_gpio1 = OutputOpenDrain :: new ( ctx. test_gpio1 , Level :: High , Pull :: Up ) ;
187+ let mut test_gpio2 = OutputOpenDrain :: new ( ctx. test_gpio2 , Level :: High , Pull :: Up ) ;
181188
182189 ctx. delay . delay_millis ( 1 ) ;
183190
@@ -221,9 +228,9 @@ mod tests {
221228 }
222229
223230 #[ test]
224- fn test_gpio_flex ( ctx : Context < ' static > ) {
225- let mut test_gpio1 = Flex :: new ( unsafe { TestGpio1 :: steal ( ) } ) ;
226- let mut test_gpio2 = Flex :: new ( unsafe { TestGpio2 :: steal ( ) } ) ;
231+ fn test_gpio_flex ( ctx : Context ) {
232+ let mut test_gpio1 = Flex :: new ( ctx . test_gpio1 ) ;
233+ let mut test_gpio2 = Flex :: new ( ctx . test_gpio2 ) ;
227234
228235 test_gpio1. set_high ( ) ;
229236 test_gpio1. set_as_output ( ) ;
@@ -263,9 +270,9 @@ mod tests {
263270 // Tests touch pin (GPIO2) as AnyPin and Output
264271 // https://github.com/esp-rs/esp-hal/issues/1943
265272 #[ test]
266- fn test_gpio_touch_anypin_output ( ) {
267- let any_pin2 = AnyPin :: new ( unsafe { TestGpio1 :: steal ( ) } ) ;
268- let any_pin3 = AnyPin :: new ( unsafe { TestGpio2 :: steal ( ) } ) ;
273+ fn test_gpio_touch_anypin_output ( ctx : Context ) {
274+ let any_pin2 = AnyPin :: new ( ctx . test_gpio1 ) ;
275+ let any_pin3 = AnyPin :: new ( ctx . test_gpio2 ) ;
269276
270277 let out_pin = Output :: new ( any_pin2, Level :: High ) ;
271278 let in_pin = Input :: new ( any_pin3, Pull :: Down ) ;
@@ -277,9 +284,9 @@ mod tests {
277284 // Tests touch pin (GPIO2) as AnyPin and Input
278285 // https://github.com/esp-rs/esp-hal/issues/1943
279286 #[ test]
280- fn test_gpio_touch_anypin_input ( ) {
281- let any_pin2 = AnyPin :: new ( unsafe { TestGpio1 :: steal ( ) } ) ;
282- let any_pin3 = AnyPin :: new ( unsafe { TestGpio2 :: steal ( ) } ) ;
287+ fn test_gpio_touch_anypin_input ( ctx : Context ) {
288+ let any_pin2 = AnyPin :: new ( ctx . test_gpio1 ) ;
289+ let any_pin3 = AnyPin :: new ( ctx . test_gpio2 ) ;
283290
284291 let out_pin = Output :: new ( any_pin3, Level :: Low ) ;
285292 let in_pin = Input :: new ( any_pin2, Pull :: Down ) ;
0 commit comments