Skip to content

Commit 463be58

Browse files
Add serial single pin traits and implement all af pins
1 parent 34c7573 commit 463be58

File tree

1 file changed

+46
-38
lines changed

1 file changed

+46
-38
lines changed

src/serial.rs

Lines changed: 46 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -61,49 +61,55 @@ pub enum Error {
6161
_Extensible,
6262
}
6363

64-
pub trait Pins<USART> {}
64+
pub trait TxPin<USART> {}
65+
pub trait RxPin<USART> {}
6566

66-
#[cfg(any(feature = "stm32f030", feature = "stm32f042"))]
67-
impl Pins<stm32::USART1> for (gpioa::PA9<Alternate<AF1>>, gpioa::PA10<Alternate<AF1>>) {}
68-
#[cfg(any(feature = "stm32f030", feature = "stm32f042"))]
69-
impl Pins<stm32::USART1> for (gpiob::PB6<Alternate<AF0>>, gpiob::PB7<Alternate<AF0>>) {}
70-
#[cfg(any(feature = "stm32f030", feature = "stm32f042"))]
71-
impl Pins<stm32::USART1> for (gpioa::PA9<Alternate<AF1>>, gpiob::PB7<Alternate<AF0>>) {}
72-
#[cfg(any(feature = "stm32f030", feature = "stm32f042"))]
73-
impl Pins<stm32::USART1> for (gpiob::PB6<Alternate<AF0>>, gpioa::PA10<Alternate<AF1>>) {}
67+
macro_rules! usart_pins {
68+
($($USART:ident: ($tx:ty, $rx:ty),)+) => {
69+
$(
70+
impl TxPin<stm32::$USART> for $tx {}
71+
impl RxPin<stm32::$USART> for $rx {}
72+
)+
73+
}
74+
}
7475

76+
#[cfg(any(feature = "stm32f030", feature = "stm32f042"))]
77+
usart_pins! {
78+
USART1: (gpioa::PA9<Alternate<AF1>>, gpioa::PA10<Alternate<AF1>>),
79+
USART1: (gpiob::PB6<Alternate<AF0>>, gpiob::PB6<Alternate<AF0>>),
80+
}
7581
#[cfg(feature = "stm32f030x6")]
76-
impl Pins<stm32::USART1> for (gpioa::PA2<Alternate<AF1>>, gpioa::PA3<Alternate<AF1>>) {}
77-
78-
#[cfg(any(
79-
feature = "stm32f042",
80-
feature = "stm32f030x8",
81-
feature = "stm32f030xc",
82-
))]
83-
impl Pins<stm32::USART2> for (gpioa::PA2<Alternate<AF1>>, gpioa::PA3<Alternate<AF1>>) {}
84-
#[cfg(any(
85-
feature = "stm32f042",
86-
feature = "stm32f030x8",
87-
feature = "stm32f030xc",
88-
))]
89-
impl Pins<stm32::USART2> for (gpioa::PA2<Alternate<AF1>>, gpioa::PA15<Alternate<AF1>>) {}
90-
#[cfg(any(
91-
feature = "stm32f042",
92-
feature = "stm32f030x8",
93-
feature = "stm32f030xc",
94-
))]
95-
impl Pins<stm32::USART2> for (gpioa::PA14<Alternate<AF1>>, gpioa::PA15<Alternate<AF1>>) {}
82+
usart_pins! {
83+
USART1: (gpioa::PA2<Alternate<AF1>>, gpioa::PA3<Alternate<AF1>>),
84+
USART1: (gpioa::PA14<Alternate<AF1>>, gpioa::PA15<Alternate<AF1>>),
85+
}
9686
#[cfg(any(
9787
feature = "stm32f042",
9888
feature = "stm32f030x8",
9989
feature = "stm32f030xc",
10090
))]
101-
impl Pins<stm32::USART2> for (gpioa::PA14<Alternate<AF1>>, gpioa::PA3<Alternate<AF1>>) {}
91+
usart_pins! {
92+
USART2: (gpioa::PA2<Alternate<AF1>>, gpioa::PA3<Alternate<AF1>>),
93+
USART2: (gpioa::PA14<Alternate<AF1>>, gpioa::PA15<Alternate<AF1>>),
94+
}
95+
#[cfg(feature = "stm32f030xc")]
96+
usart_pins! {
97+
// TODO WTF look at this again, in the datasheet PB10 is both tx and rx
98+
// USART3: (gpiob::PB10, AF4, gpiob::PA11, AF4),
99+
USART3: (gpioc::PC4<Alternate<AF1>>, gpioc::PC5<Alternate<AF1>>),
100+
USART3: (gpioc::PC10<Alternate<AF1>>, gpioc::PC11<Alternate<AF1>>),
101+
USART4: (gpioa::PA0<Alternate<AF4>>, gpioa::PA1<Alternate<AF4>>),
102+
USART4: (gpioc::PC10<Alternate<AF0>>, gpioc::PC11<Alternate<AF0>>),
103+
USART5: (gpiob::PB3<Alternate<AF4>>, gpiob::PB4<Alternate<AF4>>),
104+
USART5: (gpioc::PC12<Alternate<AF2>>, gpiod::PD2<Alternate<AF2>>),
105+
USART6: (gpioa::PA4<Alternate<AF5>>, gpioa::PA5<Alternate<AF5>>),
106+
USART6: (gpioc::PC0<Alternate<AF2>>, gpioc::PC1<Alternate<AF2>>),
107+
}
102108

103109
/// Serial abstraction
104-
pub struct Serial<USART, PINS> {
110+
pub struct Serial<USART, TXPIN, RXPIN> {
105111
usart: USART,
106-
pins: PINS,
112+
pins: (TXPIN, RXPIN),
107113
}
108114

109115
/// Serial receiver
@@ -122,11 +128,12 @@ macro_rules! usart {
122128
($($USART:ident: ($usart:ident, $usartXen:ident, $apbenr:ident),)+) => {
123129
$(
124130
use crate::stm32::$USART;
125-
impl<PINS> Serial<$USART, PINS> {
131+
impl<TXPIN, RXPIN> Serial<$USART, TXPIN, RXPIN> {
126132
/// Creates a new serial instance
127-
pub fn $usart(usart: $USART, pins: PINS, baud_rate: Bps, clocks: Clocks) -> Self
133+
pub fn $usart(usart: $USART, pins: (TXPIN, RXPIN), baud_rate: Bps, clocks: Clocks) -> Self
128134
where
129-
PINS: Pins<$USART>,
135+
TXPIN: TxPin<$USART>,
136+
RXPIN: RxPin<$USART>,
130137
{
131138
// NOTE(unsafe) This executes only during initialisation
132139
let rcc = unsafe { &(*stm32::RCC::ptr()) };
@@ -240,10 +247,11 @@ where
240247
}
241248
}
242249

243-
impl<USART, PINS> Serial<USART, PINS>
250+
impl<USART, TXPIN, RXPIN> Serial<USART, TXPIN, RXPIN>
244251
where
245252
USART: Deref<Target = SerialRegisterBlock>,
246-
PINS: Pins<USART>,
253+
TXPIN: TxPin<USART>,
254+
RXPIN: RxPin<USART>,
247255
{
248256
/// Splits the UART Peripheral in a Tx and an Rx part
249257
/// This is required for sending/receiving
@@ -257,7 +265,7 @@ where
257265
},
258266
)
259267
}
260-
pub fn release(self) -> (USART, PINS) {
268+
pub fn release(self) -> (USART, (TXPIN, RXPIN)) {
261269
(self.usart, self.pins)
262270
}
263271
}

0 commit comments

Comments
 (0)