5656
5757use core:: marker:: PhantomData ;
5858
59+ use crate :: pac;
5960pub mod alt;
6061mod convert;
6162pub use convert:: PinMode ;
@@ -115,7 +116,7 @@ pub struct Alternate<const A: u8, Otype = PushPull>(PhantomData<Otype>);
115116pub struct Input ;
116117
117118/// Pull setting for an input.
118- #[ derive( Debug , Eq , PartialEq ) ]
119+ #[ derive( Clone , Copy , Debug , Eq , PartialEq ) ]
119120#[ cfg_attr( feature = "defmt" , derive( defmt:: Format ) ) ]
120121pub enum Pull {
121122 /// Floating
@@ -126,6 +127,16 @@ pub enum Pull {
126127 Down = 2 ,
127128}
128129
130+ impl From < Pull > for pac:: gpioa:: pupdr:: PUPDR0 {
131+ fn from ( value : Pull ) -> Self {
132+ match value {
133+ Pull :: Down => Self :: PullDown ,
134+ Pull :: Up => Self :: PullUp ,
135+ Pull :: None => Self :: Floating ,
136+ }
137+ }
138+ }
139+
129140/// Open drain input or output (type state)
130141#[ derive( Debug , Default ) ]
131142#[ cfg_attr( feature = "defmt" , derive( defmt:: Format ) ) ]
@@ -195,6 +206,17 @@ pub enum Speed {
195206 VeryHigh = 3 ,
196207}
197208
209+ impl From < Speed > for pac:: gpioa:: ospeedr:: OSPEEDR0 {
210+ fn from ( value : Speed ) -> Self {
211+ match value {
212+ Speed :: Low => Self :: LowSpeed ,
213+ Speed :: Medium => Self :: MediumSpeed ,
214+ Speed :: High => Self :: HighSpeed ,
215+ Speed :: VeryHigh => Self :: VeryHighSpeed ,
216+ }
217+ }
218+ }
219+
198220/// GPIO interrupt trigger edge selection
199221#[ cfg_attr( feature = "defmt" , derive( defmt:: Format ) ) ]
200222#[ derive( Debug , PartialEq , Eq , Clone , Copy ) ]
@@ -318,13 +340,9 @@ where
318340{
319341 /// Set pin speed
320342 pub fn set_speed ( & mut self , speed : Speed ) {
321- let offset = 2 * { N } ;
322-
323- unsafe {
324- ( * gpiox :: < P > ( ) )
325- . ospeedr ( )
326- . modify ( |r, w| w. bits ( ( r. bits ( ) & !( 0b11 << offset) ) | ( ( speed as u32 ) << offset) ) ) ;
327- }
343+ unsafe { & ( * gpiox :: < P > ( ) ) }
344+ . ospeedr ( )
345+ . modify ( |_, w| w. ospeedr ( N ) . variant ( speed. into ( ) ) ) ;
328346 }
329347
330348 /// Set pin speed
@@ -350,13 +368,9 @@ where
350368{
351369 /// Set the internal pull-up and pull-down resistor
352370 pub fn set_internal_resistor ( & mut self , resistor : Pull ) {
353- let offset = 2 * { N } ;
354- let value = resistor as u32 ;
355- unsafe {
356- ( * gpiox :: < P > ( ) )
357- . pupdr ( )
358- . modify ( |r, w| w. bits ( ( r. bits ( ) & !( 0b11 << offset) ) | ( value << offset) ) ) ;
359- }
371+ unsafe { & ( * gpiox :: < P > ( ) ) }
372+ . pupdr ( )
373+ . modify ( |_, w| w. pupdr ( N ) . variant ( resistor. into ( ) ) ) ;
360374 }
361375
362376 /// Set the internal pull-up and pull-down resistor
@@ -435,22 +449,26 @@ impl<const P: char, const N: u8, MODE> Pin<P, N, MODE> {
435449 #[ inline( always) ]
436450 fn _set_high ( & mut self ) {
437451 // NOTE(unsafe) atomic write to a stateless register
438- unsafe { ( * gpiox :: < P > ( ) ) . bsrr ( ) . write ( |w| w. bits ( 1 << N ) ) }
452+ let gpio = unsafe { & ( * gpiox :: < P > ( ) ) } ;
453+ gpio. bsrr ( ) . write ( |w| w. bs ( N ) . set_bit ( ) )
439454 }
440455 #[ inline( always) ]
441456 fn _set_low ( & mut self ) {
442457 // NOTE(unsafe) atomic write to a stateless register
443- unsafe { ( * gpiox :: < P > ( ) ) . bsrr ( ) . write ( |w| w. bits ( 1 << ( 16 + N ) ) ) }
458+ let gpio = unsafe { & ( * gpiox :: < P > ( ) ) } ;
459+ gpio. bsrr ( ) . write ( |w| w. br ( N ) . set_bit ( ) )
444460 }
445461 #[ inline( always) ]
446462 fn _is_set_low ( & self ) -> bool {
447463 // NOTE(unsafe) atomic read with no side effects
448- unsafe { ( * gpiox :: < P > ( ) ) . odr ( ) . read ( ) . bits ( ) & ( 1 << N ) == 0 }
464+ let gpio = unsafe { & ( * gpiox :: < P > ( ) ) } ;
465+ gpio. odr ( ) . read ( ) . odr ( N ) . bit_is_clear ( )
449466 }
450467 #[ inline( always) ]
451468 fn _is_low ( & self ) -> bool {
452469 // NOTE(unsafe) atomic read with no side effects
453- unsafe { ( * gpiox :: < P > ( ) ) . idr ( ) . read ( ) . bits ( ) & ( 1 << N ) == 0 }
470+ let gpio = unsafe { & ( * gpiox :: < P > ( ) ) } ;
471+ gpio. idr ( ) . read ( ) . idr ( N ) . bit_is_clear ( )
454472 }
455473}
456474
0 commit comments