Skip to content

Commit 28596b4

Browse files
committed
generic into_af_otype
1 parent 14cc52a commit 28596b4

File tree

1 file changed

+40
-8
lines changed

1 file changed

+40
-8
lines changed

src/gpio.rs

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -663,6 +663,44 @@ where
663663
}
664664
}
665665

666+
impl<Gpio, Index, Mode> Pin<Gpio, Index, Mode>
667+
where
668+
Gpio: marker::GpioStatic,
669+
Index: marker::Index,
670+
{
671+
/// Configures the pin to operate as an alternate function push-pull output pin
672+
pub fn into_af_push_pull<const A: u8>(
673+
self,
674+
moder: &mut Gpio::MODER,
675+
otyper: &mut Gpio::OTYPER,
676+
afr: &mut <Self as marker::IntoAf<A>>::AFR,
677+
) -> Pin<Gpio, Index, Alternate<PushPull, A>>
678+
where
679+
Self: marker::IntoAf<A>,
680+
{
681+
moder.alternate(self.index.index());
682+
otyper.push_pull(self.index.index());
683+
afr.afx(self.index.index(), A);
684+
self.into_mode()
685+
}
686+
687+
/// Configures the pin to operate as an alternate function open-drain output pin
688+
pub fn into_af_open_drain<const A: u8>(
689+
self,
690+
moder: &mut Gpio::MODER,
691+
otyper: &mut Gpio::OTYPER,
692+
afr: &mut <Self as marker::IntoAf<A>>::AFR,
693+
) -> Pin<Gpio, Index, Alternate<OpenDrain, A>>
694+
where
695+
Self: marker::IntoAf<A>,
696+
{
697+
moder.alternate(self.index.index());
698+
otyper.open_drain(self.index.index());
699+
afr.afx(self.index.index(), A);
700+
self.into_mode()
701+
}
702+
}
703+
666704
macro_rules! af {
667705
($i:literal, $AFi:ident, $into_afi_push_pull:ident, $into_afi_open_drain:ident) => {
668706
paste::paste! {
@@ -683,10 +721,7 @@ macro_rules! af {
683721
otyper: &mut Gpio::OTYPER,
684722
afr: &mut <Self as marker::IntoAf<$i>>::AFR,
685723
) -> Pin<Gpio, Index, $AFi<PushPull>> {
686-
moder.alternate(self.index.index());
687-
otyper.push_pull(self.index.index());
688-
afr.afx(self.index.index(), $i);
689-
self.into_mode()
724+
self.into_af_push_pull::<$i>(moder, otyper, afr)
690725
}
691726

692727
/// Configures the pin to operate as an alternate function open-drain output pin
@@ -696,10 +731,7 @@ macro_rules! af {
696731
otyper: &mut Gpio::OTYPER,
697732
afr: &mut <Self as marker::IntoAf<$i>>::AFR,
698733
) -> Pin<Gpio, Index, $AFi<OpenDrain>> {
699-
moder.alternate(self.index.index());
700-
otyper.open_drain(self.index.index());
701-
afr.afx(self.index.index(), $i);
702-
self.into_mode()
734+
self.into_af_open_drain::<$i>(moder, otyper, afr)
703735
}
704736
}
705737
};

0 commit comments

Comments
 (0)