@@ -82,7 +82,7 @@ use crate::pac::EXTI;
82
82
mod partially_erased;
83
83
pub use partially_erased:: { PEPin , PartiallyErasedPin } ;
84
84
mod erased;
85
- pub use erased:: { EPin , ErasedPin } ;
85
+ pub use erased:: { AnyPin , ErasedPin } ;
86
86
87
87
mod hal_02;
88
88
mod hal_1;
@@ -99,6 +99,16 @@ pub enum IOPinSpeed {
99
99
Mhz50 = 0b11 ,
100
100
}
101
101
102
+ impl From < IOPinSpeed > for Mode {
103
+ fn from ( value : IOPinSpeed ) -> Self {
104
+ match value {
105
+ IOPinSpeed :: Mhz10 => Self :: Output ,
106
+ IOPinSpeed :: Mhz2 => Self :: Output2 ,
107
+ IOPinSpeed :: Mhz50 => Self :: Output50 ,
108
+ }
109
+ }
110
+ }
111
+
102
112
pub trait PinExt {
103
113
type Mode ;
104
114
@@ -202,12 +212,14 @@ mod sealed {
202
212
pub trait Interruptable { }
203
213
204
214
pub trait PinMode : Default {
205
- const CNF : u32 ;
206
- const MODE : u32 ;
215
+ const CNF : super :: Cnf ;
216
+ const MODE : super :: Mode ;
207
217
const PULL : Option < bool > = None ;
208
218
}
209
219
}
210
220
221
+ use crate :: pac:: gpioa:: crl:: { CNF0 as Cnf , MODE0 as Mode } ;
222
+
211
223
use sealed:: Interruptable ;
212
224
pub ( crate ) use sealed:: PinMode ;
213
225
@@ -270,10 +282,10 @@ where
270
282
. modify ( |r, w| unsafe { w. bits ( r. bits ( ) & !( 1 << pin_number) ) } ) ;
271
283
}
272
284
Edge :: Falling => {
273
- exti. ftsr ( )
274
- . modify ( |r, w| unsafe { w. bits ( r. bits ( ) | ( 1 << pin_number) ) } ) ;
275
285
exti. rtsr ( )
276
286
. modify ( |r, w| unsafe { w. bits ( r. bits ( ) & !( 1 << pin_number) ) } ) ;
287
+ exti. ftsr ( )
288
+ . modify ( |r, w| unsafe { w. bits ( r. bits ( ) | ( 1 << pin_number) ) } ) ;
277
289
}
278
290
Edge :: RisingFalling => {
279
291
exti. rtsr ( )
@@ -439,10 +451,6 @@ pub struct Pin<const P: char, const N: u8, MODE = Input<Floating>> {
439
451
mode : MODE ,
440
452
}
441
453
442
- impl < const P : char , const N : u8 , MODE > Pin < P , N , MODE > {
443
- const OFFSET : u32 = ( 4 * ( N as u32 ) ) % 32 ;
444
- }
445
-
446
454
/// Represents high or low configuration register
447
455
pub trait HL {
448
456
/// Configuration register associated to pin
@@ -518,25 +526,29 @@ impl<const P: char, const N: u8, MODE> Pin<P, N, MODE> {
518
526
#[ inline( always) ]
519
527
fn _set_high ( & mut self ) {
520
528
// NOTE(unsafe) atomic write to a stateless register
521
- unsafe { ( * Gpio :: < P > :: ptr ( ) ) . bsrr ( ) . write ( |w| w. bits ( 1 << N ) ) }
529
+ let gpio = unsafe { & ( * gpiox :: < P > ( ) ) } ;
530
+ gpio. bsrr ( ) . write ( |w| w. bs ( N ) . set_bit ( ) )
522
531
}
523
532
524
533
#[ inline( always) ]
525
534
fn _set_low ( & mut self ) {
526
535
// NOTE(unsafe) atomic write to a stateless register
527
- unsafe { ( * Gpio :: < P > :: ptr ( ) ) . bsrr ( ) . write ( |w| w. bits ( 1 << ( 16 + N ) ) ) }
536
+ let gpio = unsafe { & ( * gpiox :: < P > ( ) ) } ;
537
+ gpio. bsrr ( ) . write ( |w| w. br ( N ) . set_bit ( ) )
528
538
}
529
539
530
540
#[ inline( always) ]
531
541
fn _is_set_low ( & self ) -> bool {
532
542
// NOTE(unsafe) atomic read with no side effects
533
- unsafe { ( * Gpio :: < P > :: ptr ( ) ) . odr ( ) . read ( ) . bits ( ) & ( 1 << N ) == 0 }
543
+ let gpio = unsafe { & ( * gpiox :: < P > ( ) ) } ;
544
+ gpio. odr ( ) . read ( ) . odr ( N ) . bit_is_clear ( )
534
545
}
535
546
536
547
#[ inline( always) ]
537
548
fn _is_low ( & self ) -> bool {
538
549
// NOTE(unsafe) atomic read with no side effects
539
- unsafe { ( * Gpio :: < P > :: ptr ( ) ) . idr ( ) . read ( ) . bits ( ) & ( 1 << N ) == 0 }
550
+ let gpio = unsafe { & ( * gpiox :: < P > ( ) ) } ;
551
+ gpio. idr ( ) . read ( ) . idr ( N ) . bit_is_clear ( )
540
552
}
541
553
}
542
554
@@ -816,26 +828,20 @@ where
816
828
Self : HL ,
817
829
{
818
830
#[ inline( always) ]
819
- fn cr_modify ( & mut self , _cr : & mut <Self as HL >:: Cr , f : impl FnOnce ( u32 ) -> u32 ) {
820
- let gpio = unsafe { & ( * Gpio :: < P > :: ptr ( ) ) } ;
831
+ fn _set_speed ( & mut self , _cr : & mut <Self as HL >:: Cr , speed : IOPinSpeed ) {
832
+ let gpio = unsafe { & ( * gpiox :: < P > ( ) ) } ;
821
833
822
834
match N {
823
835
0 ..=7 => {
824
- gpio. crl ( ) . modify ( |r , w| unsafe { w . bits ( f ( r . bits ( ) ) ) } ) ;
836
+ gpio. crl ( ) . modify ( |_ , w| w . mode ( N ) . variant ( speed . into ( ) ) ) ;
825
837
}
826
838
8 ..=15 => {
827
- gpio. crh ( ) . modify ( |r, w| unsafe { w. bits ( f ( r. bits ( ) ) ) } ) ;
839
+ gpio. crh ( )
840
+ . modify ( |_, w| unsafe { w. mode ( N - 16 ) . bits ( speed as u8 ) } ) ;
828
841
}
829
842
_ => unreachable ! ( ) ,
830
843
}
831
844
}
832
-
833
- #[ inline( always) ]
834
- fn _set_speed ( & mut self , cr : & mut <Self as HL >:: Cr , speed : IOPinSpeed ) {
835
- self . cr_modify ( cr, |r_bits| {
836
- ( r_bits & !( 0b11 << Self :: OFFSET ) ) | ( ( speed as u32 ) << Self :: OFFSET )
837
- } ) ;
838
- }
839
845
}
840
846
841
847
impl < const P : char , const N : u8 , MODE > OutputSpeed for Pin < P , N , Output < MODE > >
@@ -898,69 +904,79 @@ where
898
904
}
899
905
}
900
906
907
+ impl PinMode for Analog {
908
+ const MODE : Mode = Mode :: Input ;
909
+ const CNF : Cnf = Cnf :: PushPull ;
910
+ }
911
+
901
912
impl PinMode for Input < Floating > {
902
- const CNF : u32 = 0b01 ;
903
- const MODE : u32 = 0b00 ;
913
+ const MODE : Mode = Mode :: Input ;
914
+ const CNF : Cnf = Cnf :: OpenDrain ;
904
915
}
905
916
906
917
impl PinMode for Input < PullDown > {
907
- const CNF : u32 = 0b10 ;
908
- const MODE : u32 = 0b00 ;
918
+ const MODE : Mode = Mode :: Input ;
919
+ const CNF : Cnf = Cnf :: AltPushPull ;
909
920
const PULL : Option < bool > = Some ( false ) ;
910
921
}
911
922
912
923
impl PinMode for Input < PullUp > {
913
- const CNF : u32 = 0b10 ;
914
- const MODE : u32 = 0b00 ;
924
+ const MODE : Mode = Mode :: Input ;
925
+ const CNF : Cnf = Cnf :: AltPushPull ;
915
926
const PULL : Option < bool > = Some ( true ) ;
916
927
}
917
928
918
- impl PinMode for Output < OpenDrain > {
919
- const CNF : u32 = 0b01 ;
920
- const MODE : u32 = 0b11 ;
921
- }
922
-
923
929
impl PinMode for Output < PushPull > {
924
- const CNF : u32 = 0b00 ;
925
- const MODE : u32 = 0b11 ;
930
+ const MODE : Mode = Mode :: Output50 ;
931
+ const CNF : Cnf = Cnf :: PushPull ;
926
932
}
927
933
928
- impl PinMode for Analog {
929
- const CNF : u32 = 0b00 ;
930
- const MODE : u32 = 0b00 ;
934
+ impl PinMode for Output < OpenDrain > {
935
+ const MODE : Mode = Mode :: Output50 ;
936
+ const CNF : Cnf = Cnf :: OpenDrain ;
931
937
}
932
938
933
939
impl PinMode for Alternate < PushPull > {
934
- const CNF : u32 = 0b10 ;
935
- const MODE : u32 = 0b11 ;
940
+ const MODE : Mode = Mode :: Output50 ;
941
+ const CNF : Cnf = Cnf :: AltPushPull ;
936
942
}
937
943
938
944
impl PinMode for Alternate < OpenDrain > {
939
- const CNF : u32 = 0b11 ;
940
- const MODE : u32 = 0b11 ;
945
+ const MODE : Mode = Mode :: Output50 ;
946
+ const CNF : Cnf = Cnf :: AltOpenDrain ;
941
947
}
942
948
943
949
impl < const P : char , const N : u8 , M > Pin < P , N , M >
944
950
where
945
951
Self : HL ,
946
952
{
947
- fn mode < MODE : PinMode > ( & mut self , cr : & mut <Self as HL >:: Cr ) {
948
- let gpio = unsafe { & ( * Gpio :: < P > :: ptr ( ) ) } ;
953
+ fn mode < MODE : PinMode > ( & mut self , _cr : & mut <Self as HL >:: Cr ) {
954
+ let gpio = unsafe { & ( * gpiox :: < P > ( ) ) } ;
949
955
950
956
// Input<PullUp> or Input<PullDown> mode
951
957
if let Some ( pull) = MODE :: PULL {
952
- if pull {
953
- gpio. bsrr ( ) . write ( |w| unsafe { w. bits ( 1 << N ) } ) ;
954
- } else {
955
- gpio. bsrr ( ) . write ( |w| unsafe { w. bits ( 1 << ( 16 + N ) ) } ) ;
956
- }
958
+ gpio. bsrr ( ) . write ( |w| {
959
+ if pull {
960
+ w. bs ( N ) . set_bit ( )
961
+ } else {
962
+ w. br ( N ) . set_bit ( )
963
+ }
964
+ } )
957
965
}
958
966
959
- let bits = ( MODE :: CNF << 2 ) | MODE :: MODE ;
960
-
961
- self . cr_modify ( cr, |r_bits| {
962
- ( r_bits & !( 0b1111 << Self :: OFFSET ) ) | ( bits << Self :: OFFSET )
963
- } ) ;
967
+ match N {
968
+ 0 ..=7 => {
969
+ gpio. crl ( )
970
+ . modify ( |_, w| w. mode ( N ) . variant ( MODE :: MODE ) . cnf ( N ) . variant ( MODE :: CNF ) ) ;
971
+ }
972
+ 8 ..=15 => {
973
+ gpio. crh ( ) . modify ( |_, w| unsafe {
974
+ w. mode ( N - 16 ) . bits ( MODE :: MODE as u8 ) ;
975
+ w. cnf ( N - 16 ) . bits ( MODE :: CNF as u8 )
976
+ } ) ;
977
+ }
978
+ _ => unreachable ! ( ) ,
979
+ }
964
980
}
965
981
966
982
#[ inline]
@@ -1105,20 +1121,17 @@ gpio!(GPIOG, gpiog, PGx, 'G', [
1105
1121
PG15 : ( pg15, 15 ) ,
1106
1122
] ) ;
1107
1123
1108
- struct Gpio < const P : char > ;
1109
- impl < const P : char > Gpio < P > {
1110
- const fn ptr ( ) -> * const crate :: pac:: gpioa:: RegisterBlock {
1111
- match P {
1112
- 'A' => crate :: pac:: GPIOA :: ptr ( ) ,
1113
- 'B' => crate :: pac:: GPIOB :: ptr ( ) as _ ,
1114
- 'C' => crate :: pac:: GPIOC :: ptr ( ) as _ ,
1115
- 'D' => crate :: pac:: GPIOD :: ptr ( ) as _ ,
1116
- 'E' => crate :: pac:: GPIOE :: ptr ( ) as _ ,
1117
- #[ cfg( any( feature = "xl" , feature = "high" ) ) ]
1118
- 'F' => crate :: pac:: GPIOF :: ptr ( ) as _ ,
1119
- #[ cfg( any( feature = "xl" , feature = "high" ) ) ]
1120
- 'G' => crate :: pac:: GPIOG :: ptr ( ) as _ ,
1121
- _ => unreachable ! ( ) ,
1122
- }
1124
+ const fn gpiox < const P : char > ( ) -> * const crate :: pac:: gpioa:: RegisterBlock {
1125
+ match P {
1126
+ 'A' => crate :: pac:: GPIOA :: ptr ( ) ,
1127
+ 'B' => crate :: pac:: GPIOB :: ptr ( ) as _ ,
1128
+ 'C' => crate :: pac:: GPIOC :: ptr ( ) as _ ,
1129
+ 'D' => crate :: pac:: GPIOD :: ptr ( ) as _ ,
1130
+ 'E' => crate :: pac:: GPIOE :: ptr ( ) as _ ,
1131
+ #[ cfg( any( feature = "xl" , feature = "high" ) ) ]
1132
+ 'F' => crate :: pac:: GPIOF :: ptr ( ) as _ ,
1133
+ #[ cfg( any( feature = "xl" , feature = "high" ) ) ]
1134
+ 'G' => crate :: pac:: GPIOG :: ptr ( ) as _ ,
1135
+ _ => unreachable ! ( ) ,
1123
1136
}
1124
1137
}
0 commit comments