11use super :: * ;
22
3- pub use ErasedPin as AnyPin ;
4-
53/// Fully erased pin
64///
75/// `MODE` is one of the pin modes (see [Modes](crate::gpio#modes) section).
8- pub struct ErasedPin < MODE > {
6+ pub struct AnyPin < MODE > {
97 // Bits 0-3: Pin, Bits 4-7: Port
108 pin_port : u8 ,
119 _mode : PhantomData < MODE > ,
1210}
1311
14- impl < MODE > fmt:: Debug for ErasedPin < MODE > {
12+ impl < MODE > fmt:: Debug for AnyPin < MODE > {
1513 fn fmt ( & self , formatter : & mut fmt:: Formatter ) -> fmt:: Result {
1614 formatter. write_fmt ( format_args ! (
1715 "P({}{})<{}>" ,
@@ -23,7 +21,7 @@ impl<MODE> fmt::Debug for ErasedPin<MODE> {
2321}
2422
2523#[ cfg( feature = "defmt" ) ]
26- impl < MODE > defmt:: Format for ErasedPin < MODE > {
24+ impl < MODE > defmt:: Format for AnyPin < MODE > {
2725 fn format ( & self , f : defmt:: Formatter ) {
2826 defmt:: write!(
2927 f,
@@ -35,7 +33,7 @@ impl<MODE> defmt::Format for ErasedPin<MODE> {
3533 }
3634}
3735
38- impl < MODE > PinExt for ErasedPin < MODE > {
36+ impl < MODE > PinExt for AnyPin < MODE > {
3937 type Mode = MODE ;
4038
4139 #[ inline( always) ]
@@ -48,7 +46,7 @@ impl<MODE> PinExt for ErasedPin<MODE> {
4846 }
4947}
5048
51- impl < MODE > ErasedPin < MODE > {
49+ impl < MODE > AnyPin < MODE > {
5250 pub ( crate ) fn from_pin_port ( pin_port : u8 ) -> Self {
5351 Self {
5452 pin_port,
@@ -73,7 +71,7 @@ impl<MODE> ErasedPin<MODE> {
7371 }
7472
7573 #[ inline]
76- pub ( crate ) fn block ( & self ) -> & crate :: pac:: gpioa:: RegisterBlock {
74+ pub ( crate ) unsafe fn block ( & self ) -> * const crate :: pac:: gpioa:: RegisterBlock {
7775 // This function uses pointer arithmetic instead of branching to be more efficient
7876
7977 // The logic relies on the following assumptions:
@@ -86,29 +84,30 @@ impl<MODE> ErasedPin<MODE> {
8684 const GPIO_REGISTER_OFFSET : usize = 0x0400 ;
8785
8886 let offset = GPIO_REGISTER_OFFSET * self . port_id ( ) as usize ;
89- let block_ptr =
90- ( crate :: pac:: GPIOA :: ptr ( ) as usize + offset) as * const crate :: pac:: gpioa:: RegisterBlock ;
91-
92- unsafe { & * block_ptr }
87+ ( crate :: pac:: GPIOA :: ptr ( ) as usize + offset) as * const crate :: pac:: gpioa:: RegisterBlock
9388 }
9489}
9590
96- impl < MODE > ErasedPin < Output < MODE > > {
91+ impl < MODE > AnyPin < Output < MODE > > {
9792 /// Drives the pin high
9893 #[ inline( always) ]
9994 pub fn set_high ( & mut self ) {
10095 // NOTE(unsafe) atomic write to a stateless register
101- unsafe { self . block ( ) . bsrr ( ) . write ( |w| w. bits ( 1 << self . pin_id ( ) ) ) } ;
96+ unsafe {
97+ ( * self . block ( ) )
98+ . bsrr ( )
99+ . write ( |w| w. bs ( self . pin_id ( ) ) . set_bit ( ) )
100+ } ;
102101 }
103102
104103 /// Drives the pin low
105104 #[ inline( always) ]
106105 pub fn set_low ( & mut self ) {
107106 // NOTE(unsafe) atomic write to a stateless register
108107 unsafe {
109- self . block ( )
108+ ( * self . block ( ) )
110109 . bsrr ( )
111- . write ( |w| w. bits ( 1 << ( self . pin_id ( ) + 16 ) ) )
110+ . write ( |w| w. br ( self . pin_id ( ) ) . set_bit ( ) )
112111 } ;
113112 }
114113
@@ -140,7 +139,13 @@ impl<MODE> ErasedPin<Output<MODE>> {
140139 /// Is the pin in drive low mode?
141140 #[ inline( always) ]
142141 pub fn is_set_low ( & self ) -> bool {
143- self . block ( ) . odr ( ) . read ( ) . bits ( ) & ( 1 << self . pin_id ( ) ) == 0
142+ unsafe {
143+ ( * self . block ( ) )
144+ . odr ( )
145+ . read ( )
146+ . odr ( self . pin_id ( ) )
147+ . bit_is_clear ( )
148+ }
144149 }
145150
146151 /// Toggle pin output
@@ -154,7 +159,7 @@ impl<MODE> ErasedPin<Output<MODE>> {
154159 }
155160}
156161
157- impl < MODE > ErasedPin < MODE >
162+ impl < MODE > AnyPin < MODE >
158163where
159164 MODE : marker:: Readable ,
160165{
@@ -167,6 +172,12 @@ where
167172 /// Is the input pin low?
168173 #[ inline( always) ]
169174 pub fn is_low ( & self ) -> bool {
170- self . block ( ) . idr ( ) . read ( ) . bits ( ) & ( 1 << self . pin_id ( ) ) == 0
175+ unsafe {
176+ ( * self . block ( ) )
177+ . idr ( )
178+ . read ( )
179+ . idr ( self . pin_id ( ) )
180+ . bit_is_clear ( )
181+ }
171182 }
172183}
0 commit comments