@@ -45,6 +45,11 @@ pub struct Alternate<AF, MODE>
4545 _mode : PhantomData < MODE > ,
4646}
4747
48+ pub enum State {
49+ High ,
50+ Low ,
51+ }
52+
4853/// Alternate function 0 (type state)
4954pub struct AF0 ;
5055
@@ -106,8 +111,10 @@ macro_rules! gpio {
106111
107112 use crate :: rcc:: AHB2 ;
108113 use super :: {
109- Alternate , AF4 , AF5 , AF6 , AF7 , AF8 , AF9 , Floating , GpioExt , Input , OpenDrain , Output ,
110- PullDown , PullUp , PushPull ,
114+ Alternate ,
115+ AF1 , AF4 , AF5 , AF6 , AF7 , AF8 , AF9 ,
116+ Floating , GpioExt , Input , OpenDrain , Output ,
117+ PullDown , PullUp , PushPull , State ,
111118 } ;
112119
113120 /// GPIO parts
@@ -231,6 +238,29 @@ macro_rules! gpio {
231238 }
232239
233240 impl <MODE > $PXi<MODE > {
241+ /// Configures the pin to serve as alternate function 1 (AF1)
242+ pub fn into_af1(
243+ self ,
244+ moder: & mut MODER ,
245+ afr: & mut $AFR,
246+ ) -> $PXi<Alternate <AF1 , MODE >> {
247+ let offset = 2 * $i;
248+
249+ // alternate function mode
250+ let mode = 0b10 ;
251+ moder. moder( ) . modify( |r, w| unsafe {
252+ w. bits( ( r. bits( ) & !( 0b11 << offset) ) | ( mode << offset) )
253+ } ) ;
254+
255+ let af = 1 ;
256+ let offset = 4 * ( $i % 8 ) ;
257+ afr. afr( ) . modify( |r, w| unsafe {
258+ w. bits( ( r. bits( ) & !( 0b1111 << offset) ) | ( af << offset) )
259+ } ) ;
260+
261+ $PXi { _mode: PhantomData }
262+ }
263+
234264 /// Configures the pin to serve as alternate function 4 (AF4)
235265 pub fn into_af4(
236266 self ,
@@ -458,11 +488,33 @@ macro_rules! gpio {
458488 }
459489
460490 /// Configures the pin to operate as an push pull output pin
491+ /// Initial state will be low
461492 pub fn into_push_pull_output(
462493 self ,
463494 moder: & mut MODER ,
464495 otyper: & mut OTYPER ,
465496 ) -> $PXi<Output <PushPull >> {
497+ self . into_push_pull_output_with_state( moder, otyper, State :: Low )
498+ }
499+
500+ /// Configures the pin to operate as an push pull output pin
501+ /// Initial state can be chosen to be high or low
502+ pub fn into_push_pull_output_with_state(
503+ self ,
504+ moder: & mut MODER ,
505+ otyper: & mut OTYPER ,
506+ initial_state: State ,
507+ ) -> $PXi<Output <PushPull >> {
508+ let mut res = $PXi { _mode: PhantomData } ;
509+
510+ // set pin high/low before activating, to prevent
511+ // spurious signals (e.g. LED flash)
512+ // TODO: I still see a flash of LED using this order
513+ match initial_state {
514+ State :: High => res. set_high( ) ,
515+ State :: Low => res. set_low( ) ,
516+ }
517+
466518 let offset = 2 * $i;
467519
468520 // general purpose output mode
@@ -476,7 +528,7 @@ macro_rules! gpio {
476528 . otyper( )
477529 . modify( |r, w| unsafe { w. bits( r. bits( ) & !( 0b1 << $i) ) } ) ;
478530
479- $PXi { _mode : PhantomData }
531+ res
480532 }
481533
482534 /// Configures the pin to operate as an touch sample
0 commit comments