@@ -95,6 +95,7 @@ macro_rules! gpio {
95
95
State ,
96
96
Active ,
97
97
Debugger ,
98
+ Pxx
98
99
} ;
99
100
100
101
/// GPIO parts
@@ -150,13 +151,19 @@ macro_rules! gpio {
150
151
}
151
152
}
152
153
153
- /// Partially erased pin
154
- pub struct $PXx <MODE > {
154
+ /// Partially erased pin. Only used in the Pxx enum
155
+ pub struct Generic <MODE > {
155
156
i: u8 ,
156
157
_mode: PhantomData <MODE >,
157
158
}
158
159
159
- impl <MODE > OutputPin for $PXx<Output <MODE >> {
160
+ impl <MODE > Generic <MODE > {
161
+ pub fn downgrade( self ) -> Pxx <MODE > {
162
+ Pxx :: $PXx( self )
163
+ }
164
+ }
165
+
166
+ impl <MODE > OutputPin for Generic <Output <MODE >> {
160
167
type Error = Void ;
161
168
fn set_high( & mut self ) -> Result <( ) , Self :: Error > {
162
169
// NOTE(unsafe) atomic write to a stateless register
@@ -169,7 +176,7 @@ macro_rules! gpio {
169
176
}
170
177
}
171
178
172
- impl <MODE > InputPin for $PXx <Input <MODE >> {
179
+ impl <MODE > InputPin for Generic <Input <MODE >> {
173
180
type Error = Void ;
174
181
fn is_high( & self ) -> Result <bool , Self :: Error > {
175
182
self . is_low( ) . map( |b| !b)
@@ -181,7 +188,7 @@ macro_rules! gpio {
181
188
}
182
189
}
183
190
184
- impl <MODE > StatefulOutputPin for $PXx <Output <MODE >> {
191
+ impl <MODE > StatefulOutputPin for Generic <Output <MODE >> {
185
192
fn is_set_high( & self ) -> Result <bool , Self :: Error > {
186
193
self . is_set_low( ) . map( |b| !b)
187
194
}
@@ -192,9 +199,9 @@ macro_rules! gpio {
192
199
}
193
200
}
194
201
195
- impl <MODE > toggleable:: Default for $PXx <Output <MODE >> { }
202
+ impl <MODE > toggleable:: Default for Generic <Output <MODE >> { }
196
203
197
- impl InputPin for $PXx <Output <OpenDrain >> {
204
+ impl InputPin for Generic <Output <OpenDrain >> {
198
205
type Error = Void ;
199
206
fn is_high( & self ) -> Result <bool , Self :: Error > {
200
207
self . is_low( ) . map( |b| !b)
@@ -206,6 +213,10 @@ macro_rules! gpio {
206
213
}
207
214
}
208
215
216
+ pub type $PXx<MODE > = Pxx <MODE >;
217
+
218
+
219
+
209
220
$(
210
221
/// Pin
211
222
pub struct $PXi<MODE > {
@@ -442,15 +453,20 @@ macro_rules! gpio {
442
453
443
454
impl <MODE > $PXi<MODE > where MODE : Active {
444
455
/// Erases the pin number from the type
445
- ///
446
- /// This is useful when you want to collect the pins into an array where you
447
- /// need all the elements to have the same type
448
- pub fn downgrade( self ) -> $PXx<MODE > {
449
- $PXx {
456
+ fn into_generic( self ) -> Generic <MODE > {
457
+ Generic {
450
458
i: $i,
451
459
_mode: self . _mode,
452
460
}
453
461
}
462
+
463
+ /// Erases the pin number and port from the type
464
+ ///
465
+ /// This is useful when you want to collect the pins into an array where you
466
+ /// need all the elements to have the same type
467
+ pub fn downgrade( self ) -> Pxx <MODE > {
468
+ self . into_generic( ) . downgrade( )
469
+ }
454
470
}
455
471
456
472
impl <MODE > OutputPin for $PXi<Output <MODE >> {
@@ -507,6 +523,71 @@ macro_rules! gpio {
507
523
}
508
524
}
509
525
526
+ macro_rules! impl_pxx {
527
+ ( $( ( $port: ident :: $pin: ident) ) ,* ) => {
528
+ use void:: Void ;
529
+ use embedded_hal:: digital:: v2:: { InputPin , StatefulOutputPin , OutputPin } ;
530
+
531
+ pub enum Pxx <MODE > {
532
+ $(
533
+ $pin( $port:: Generic <MODE >)
534
+ ) ,*
535
+ }
536
+
537
+ impl <MODE > OutputPin for Pxx <Output <MODE >> {
538
+ type Error = Void ;
539
+ fn set_high( & mut self ) -> Result <( ) , Void > {
540
+ match self {
541
+ $( Pxx :: $pin( pin) => pin. set_high( ) ) ,*
542
+ }
543
+ }
544
+
545
+ fn set_low( & mut self ) -> Result <( ) , Void > {
546
+ match self {
547
+ $( Pxx :: $pin( pin) => pin. set_low( ) ) ,*
548
+ }
549
+ }
550
+ }
551
+
552
+ impl <MODE > StatefulOutputPin for Pxx <Output <MODE >> {
553
+ fn is_set_high( & self ) -> Result <bool , Self :: Error > {
554
+ match self {
555
+ $( Pxx :: $pin( pin) => pin. is_set_high( ) ) ,*
556
+ }
557
+ }
558
+
559
+ fn is_set_low( & self ) -> Result <bool , Self :: Error > {
560
+ match self {
561
+ $( Pxx :: $pin( pin) => pin. is_set_low( ) ) ,*
562
+ }
563
+ }
564
+ }
565
+
566
+ impl <MODE > InputPin for Pxx <Input <MODE >> {
567
+ type Error = Void ;
568
+ fn is_high( & self ) -> Result <bool , Void > {
569
+ match self {
570
+ $( Pxx :: $pin( pin) => pin. is_high( ) ) ,*
571
+ }
572
+ }
573
+
574
+ fn is_low( & self ) -> Result <bool , Void > {
575
+ match self {
576
+ $( Pxx :: $pin( pin) => pin. is_low( ) ) ,*
577
+ }
578
+ }
579
+ }
580
+ }
581
+ }
582
+
583
+ impl_pxx ! {
584
+ ( gpioa:: PAx ) ,
585
+ ( gpiob:: PBx ) ,
586
+ ( gpioc:: PCx ) ,
587
+ ( gpiod:: PDx ) ,
588
+ ( gpioe:: PEx )
589
+ }
590
+
510
591
gpio ! ( GPIOA , gpioa, gpioa, PAx , [
511
592
PA0 : ( pa0, 0 , Input <Floating >, CRL ) ,
512
593
PA1 : ( pa1, 1 , Input <Floating >, CRL ) ,
0 commit comments