56
56
57
57
use core:: marker:: PhantomData ;
58
58
59
+ use crate :: pac;
59
60
pub mod alt;
60
61
mod convert;
61
62
pub use convert:: PinMode ;
@@ -115,7 +116,7 @@ pub struct Alternate<const A: u8, Otype = PushPull>(PhantomData<Otype>);
115
116
pub struct Input ;
116
117
117
118
/// Pull setting for an input.
118
- #[ derive( Debug , Eq , PartialEq ) ]
119
+ #[ derive( Clone , Copy , Debug , Eq , PartialEq ) ]
119
120
#[ cfg_attr( feature = "defmt" , derive( defmt:: Format ) ) ]
120
121
pub enum Pull {
121
122
/// Floating
@@ -126,6 +127,16 @@ pub enum Pull {
126
127
Down = 2 ,
127
128
}
128
129
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
+
129
140
/// Open drain input or output (type state)
130
141
#[ derive( Debug , Default ) ]
131
142
#[ cfg_attr( feature = "defmt" , derive( defmt:: Format ) ) ]
@@ -195,6 +206,17 @@ pub enum Speed {
195
206
VeryHigh = 3 ,
196
207
}
197
208
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
+
198
220
/// GPIO interrupt trigger edge selection
199
221
#[ cfg_attr( feature = "defmt" , derive( defmt:: Format ) ) ]
200
222
#[ derive( Debug , PartialEq , Eq , Clone , Copy ) ]
@@ -318,13 +340,9 @@ where
318
340
{
319
341
/// Set pin speed
320
342
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 ( ) ) ) ;
328
346
}
329
347
330
348
/// Set pin speed
@@ -350,13 +368,9 @@ where
350
368
{
351
369
/// Set the internal pull-up and pull-down resistor
352
370
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 ( ) ) ) ;
360
374
}
361
375
362
376
/// 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> {
435
449
#[ inline( always) ]
436
450
fn _set_high ( & mut self ) {
437
451
// 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 ( ) )
439
454
}
440
455
#[ inline( always) ]
441
456
fn _set_low ( & mut self ) {
442
457
// 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 ( ) )
444
460
}
445
461
#[ inline( always) ]
446
462
fn _is_set_low ( & self ) -> bool {
447
463
// 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 ( )
449
466
}
450
467
#[ inline( always) ]
451
468
fn _is_low ( & self ) -> bool {
452
469
// 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 ( )
454
472
}
455
473
}
456
474
0 commit comments