@@ -102,18 +102,19 @@ pub trait PinExt {
102
102
pub struct Alternate < const A : u8 , Otype = PushPull > ( PhantomData < Otype > ) ;
103
103
104
104
/// Input mode (type state)
105
- pub struct Input < MODE = Floating > {
106
- _mode : PhantomData < MODE > ,
107
- }
108
-
109
- /// Floating input (type state)
110
- pub struct Floating ;
111
-
112
- /// Pulled down input (type state)
113
- pub struct PullDown ;
105
+ pub struct Input ;
114
106
115
- /// Pulled up input (type state)
116
- pub struct PullUp ;
107
+ /// Pull setting for an input.
108
+ #[ derive( Debug , Eq , PartialEq ) ]
109
+ #[ cfg_attr( feature = "defmt" , derive( defmt:: Format ) ) ]
110
+ pub enum Pull {
111
+ /// Floating
112
+ None = 0 ,
113
+ /// Pulled up
114
+ Up = 1 ,
115
+ /// Pulled down
116
+ Down = 2 ,
117
+ }
117
118
118
119
/// Open drain input or output (type state)
119
120
pub struct OpenDrain ;
@@ -131,6 +132,10 @@ pub struct Analog;
131
132
132
133
pub type Debugger = Alternate < 0 , PushPull > ;
133
134
135
+ impl sealed:: Active for Input { }
136
+ impl < Otype > sealed:: Active for Output < Otype > { }
137
+ impl < const A : u8 , Otype > sealed:: Active for Alternate < A , Otype > { }
138
+
134
139
/// GPIO Pin speed selection
135
140
#[ cfg_attr( feature = "defmt" , derive( defmt:: Format ) ) ]
136
141
#[ derive( Debug , PartialEq , Eq , Clone , Copy ) ]
@@ -152,11 +157,13 @@ pub enum Edge {
152
157
mod sealed {
153
158
/// Marker trait that show if `ExtiPin` can be implemented
154
159
pub trait Interruptable { }
160
+ /// Marker trait for active pin modes
161
+ pub trait Active { }
155
162
}
156
163
157
164
use sealed:: Interruptable ;
158
165
impl < MODE > Interruptable for Output < MODE > { }
159
- impl < MODE > Interruptable for Input < MODE > { }
166
+ impl Interruptable for Input { }
160
167
161
168
/// External Interrupt Pin
162
169
pub trait ExtiPin {
@@ -262,7 +269,7 @@ where
262
269
/// - `MODE` is one of the pin modes (see [Modes](crate::gpio#modes) section).
263
270
/// - `P` is port name: `A` for GPIOA, `B` for GPIOB, etc.
264
271
/// - `N` is pin number: from `0` to `15`.
265
- pub struct Pin < const P : char , const N : u8 , MODE = Input < Floating > > {
272
+ pub struct Pin < const P : char , const N : u8 , MODE = Input > {
266
273
_mode : PhantomData < MODE > ,
267
274
}
268
275
impl < const P : char , const N : u8 , MODE > Pin < P , N , MODE > {
@@ -317,34 +324,6 @@ impl<const P: char, const N: u8, MODE> Pin<P, N, Output<MODE>> {
317
324
}
318
325
}
319
326
320
- impl < const P : char , const N : u8 > Pin < P , N , Output < OpenDrain > > {
321
- /// Enables / disables the internal pull up
322
- pub fn internal_pull_up ( self , on : bool ) -> Self {
323
- let offset = 2 * { N } ;
324
- let value = if on { 0b01 } else { 0b00 } ;
325
- unsafe {
326
- ( * Gpio :: < P > :: ptr ( ) )
327
- . pupdr
328
- . modify ( |r, w| w. bits ( ( r. bits ( ) & !( 0b11 << offset) ) | ( value << offset) ) )
329
- } ;
330
-
331
- self
332
- }
333
-
334
- /// Enables / disables the internal pull down
335
- pub fn internal_pull_down ( self , on : bool ) -> Self {
336
- let offset = 2 * { N } ;
337
- let value = if on { 0b10 } else { 0b00 } ;
338
- unsafe {
339
- ( * Gpio :: < P > :: ptr ( ) )
340
- . pupdr
341
- . modify ( |r, w| w. bits ( ( r. bits ( ) & !( 0b11 << offset) ) | ( value << offset) ) )
342
- } ;
343
-
344
- self
345
- }
346
- }
347
-
348
327
impl < const P : char , const N : u8 , const A : u8 > Pin < P , N , Alternate < A , PushPull > > {
349
328
/// Set pin speed
350
329
pub fn set_speed ( self , speed : Speed ) -> Self {
@@ -358,11 +337,13 @@ impl<const P: char, const N: u8, const A: u8> Pin<P, N, Alternate<A, PushPull>>
358
337
359
338
self
360
339
}
340
+ }
361
341
362
- /// Enables / disables the internal pull up
363
- pub fn internal_pull_up ( self , on : bool ) -> Self {
342
+ impl < const P : char , const N : u8 , MODE > Pin < P , N , MODE > {
343
+ /// Set the internal pull-up and pull-down resistor
344
+ fn _internal_resistor ( self , resistor : Pull ) -> Self {
364
345
let offset = 2 * { N } ;
365
- let value = if on { 0b01 } else { 0b00 } ;
346
+ let value = resistor as u32 ;
366
347
unsafe {
367
348
( * Gpio :: < P > :: ptr ( ) )
368
349
. pupdr
@@ -371,32 +352,33 @@ impl<const P: char, const N: u8, const A: u8> Pin<P, N, Alternate<A, PushPull>>
371
352
372
353
self
373
354
}
355
+ }
374
356
375
- /// Enables / disables the internal pull down
376
- pub fn internal_pull_down ( self , on : bool ) -> Self {
377
- let offset = 2 * { N } ;
378
- let value = if on { 0b10 } else { 0b00 } ;
379
- unsafe {
380
- ( * Gpio :: < P > :: ptr ( ) )
381
- . pupdr
382
- . modify ( |r, w| w. bits ( ( r. bits ( ) & !( 0b11 << offset) ) | ( value << offset) ) )
383
- } ;
384
-
385
- self
357
+ impl < const P : char , const N : u8 , MODE > Pin < P , N , MODE >
358
+ where
359
+ MODE : sealed:: Active ,
360
+ {
361
+ /// Set the internal pull-up and pull-down resistor
362
+ pub fn internal_resistor ( self , resistor : Pull ) -> Self {
363
+ self . _internal_resistor ( resistor)
386
364
}
387
- }
388
365
389
- impl < const P : char , const N : u8 , const A : u8 > Pin < P , N , Alternate < A , PushPull > > {
390
- /// Turns pin alternate configuration pin into open drain
391
- pub fn set_open_drain ( self ) -> Pin < P , N , Alternate < A , OpenDrain > > {
392
- let offset = { N } ;
393
- unsafe {
394
- ( * Gpio :: < P > :: ptr ( ) )
395
- . otyper
396
- . modify ( |r, w| w. bits ( r. bits ( ) | ( 1 << offset) ) )
397
- } ;
366
+ /// Enables / disables the internal pull up
367
+ pub fn internal_pull_up ( self , on : bool ) -> Self {
368
+ if on {
369
+ self . internal_resistor ( Pull :: Up )
370
+ } else {
371
+ self . internal_resistor ( Pull :: None )
372
+ }
373
+ }
398
374
399
- Pin :: new ( )
375
+ /// Enables / disables the internal pull down
376
+ pub fn internal_pull_down ( self , on : bool ) -> Self {
377
+ if on {
378
+ self . internal_resistor ( Pull :: Down )
379
+ } else {
380
+ self . internal_resistor ( Pull :: None )
381
+ }
400
382
}
401
383
}
402
384
@@ -512,7 +494,7 @@ impl<const P: char, const N: u8> Pin<P, N, Output<OpenDrain>> {
512
494
}
513
495
}
514
496
515
- impl < const P : char , const N : u8 , MODE > Pin < P , N , Input < MODE > > {
497
+ impl < const P : char , const N : u8 > Pin < P , N , Input > {
516
498
#[ inline( always) ]
517
499
pub fn is_high ( & self ) -> bool {
518
500
!self . is_low ( )
@@ -533,7 +515,7 @@ macro_rules! gpio {
533
515
use crate :: pac:: { $GPIOX, RCC } ;
534
516
use crate :: rcc:: { Enable , Reset } ;
535
517
use super :: {
536
- Floating , Input ,
518
+ Input ,
537
519
} ;
538
520
539
521
/// GPIO parts
@@ -567,7 +549,7 @@ macro_rules! gpio {
567
549
pub type $PXn<MODE > = super :: PEPin <$port_id, MODE >;
568
550
569
551
$(
570
- pub type $PXi<MODE = Input < Floating > > = super :: Pin <$port_id, $i, MODE >;
552
+ pub type $PXi<MODE = Input > = super :: Pin <$port_id, $i, MODE >;
571
553
) +
572
554
573
555
}
0 commit comments