Skip to content

Commit 9bcc4a7

Browse files
committed
tx/rx constructors
1 parent 93f1d04 commit 9bcc4a7

File tree

5 files changed

+78
-19
lines changed

5 files changed

+78
-19
lines changed

examples/uart-dma-rx.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ fn main() -> ! {
4848
//.USART2
4949
.USART3
5050
.usart(
51-
(Some(tx), Some(rx)),
51+
(tx, rx),
5252
FullConfig::default()
5353
.baudrate(115200.bps())
5454
.receiver_timeout_us(1000), // Timeout after 1ms

examples/uart-dma-tx.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ fn main() -> ! {
3939

4040
info!("Init UART");
4141
let gpioa = dp.GPIOA.split(&mut rcc);
42-
let tx = Some(gpioa.pa2.into_alternate());
43-
let rx = Some(gpioa.pa3);
42+
let tx = gpioa.pa2.into_alternate();
43+
let rx = gpioa.pa3;
4444
let mut usart = dp.USART2.usart((tx, rx), 115200.bps(), &mut rcc).unwrap();
4545

4646
let mut delay_syst = cp.SYST.delay(&rcc.clocks);

examples/uart-fifo.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ fn main() -> ! {
3636
let mut usart = dp
3737
.USART2
3838
.usart(
39-
(Some(tx), Some(rx)),
39+
(tx, rx),
4040
FullConfig::default()
4141
.baudrate(115200.bps())
4242
.fifo_enable()

examples/uart.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ fn main() -> ! {
4848
let rx = gpioc.pc11.into_alternate();
4949
let mut usart = dp
5050
.USART3
51-
.usart((Some(tx), Some(rx)), FullConfig::default(), &mut rcc)
51+
.usart((tx, rx), FullConfig::default(), &mut rcc)
5252
.unwrap();
5353

5454
writeln!(usart, "Hello USART3, yay!!\r\n").unwrap();

src/serial/usart.rs

Lines changed: 73 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -120,17 +120,24 @@ pub struct DMA;
120120

121121
#[allow(non_upper_case_globals)]
122122
pub trait SerialExt<Config>: Sized + Instance {
123-
const NoTx: Option<Self::Tx<PushPull>> = None;
124-
const NoRx: Option<Self::Rx<PushPull>> = None;
125123
fn usart<Otype>(
126124
self,
127-
pins: (
128-
Option<impl Into<Self::Tx<Otype>>>,
129-
Option<impl Into<Self::Rx<PushPull>>>,
130-
),
125+
pins: (impl Into<Self::Tx<Otype>>, impl Into<Self::Rx<PushPull>>),
131126
config: impl Into<Config>,
132127
rcc: &mut Rcc,
133128
) -> Result<Serial<Self, Otype>, InvalidConfig>;
129+
fn tx<Otype>(
130+
self,
131+
tx: impl Into<Self::Tx<Otype>>,
132+
config: impl Into<Config>,
133+
rcc: &mut Rcc,
134+
) -> Result<Tx<Self, NoDMA, Otype>, InvalidConfig>;
135+
fn rx(
136+
self,
137+
rx: impl Into<Self::Rx<PushPull>>,
138+
config: impl Into<Config>,
139+
rcc: &mut Rcc,
140+
) -> Result<Rx<Self, NoDMA>, InvalidConfig>;
134141
}
135142

136143
impl<USART: Instance, Otype> fmt::Write for Serial<USART, Otype>
@@ -501,19 +508,45 @@ macro_rules! uart_lp {
501508
impl SerialExt<LowPowerConfig> for $USARTX {
502509
fn usart<Otype>(
503510
self,
504-
pins: (
505-
Option<impl Into<Self::Tx<Otype>>>,
506-
Option<impl Into<Self::Rx<PushPull>>>,
507-
),
511+
pins: (impl Into<Self::Tx<Otype>>, impl Into<Self::Rx<PushPull>>),
508512
config: impl Into<LowPowerConfig>,
509513
rcc: &mut Rcc,
510514
) -> Result<Serial<Self, Otype>, InvalidConfig> {
511515
Serial::$usartX(self, pins, config, rcc)
512516
}
517+
fn tx<Otype>(
518+
self,
519+
tx: impl Into<Self::Tx<Otype>>,
520+
config: impl Into<LowPowerConfig>,
521+
rcc: &mut Rcc,
522+
) -> Result<Tx<Self, NoDMA, Otype>, InvalidConfig> {
523+
Serial::<Self, _>::_new(self, (Some(tx), None::<Self::Rx<PushPull>>), config, rcc)
524+
.map(|s| s.split().0)
525+
}
526+
fn rx(
527+
self,
528+
rx: impl Into<Self::Rx<PushPull>>,
529+
config: impl Into<LowPowerConfig>,
530+
rcc: &mut Rcc,
531+
) -> Result<Rx<Self, NoDMA>, InvalidConfig> {
532+
Serial::<Self, _>::_new(self, (None::<Self::Tx<PushPull>>, Some(rx)), config, rcc)
533+
.map(|s| s.split().1)
534+
}
513535
}
514536

515537
impl<Otype> Serial<$USARTX, Otype> {
516538
pub fn $usartX(
539+
usart: $USARTX,
540+
pins: (
541+
impl Into<<$USARTX as CommonPins>::Tx<Otype>>,
542+
impl Into<<$USARTX as CommonPins>::Rx<PushPull>>,
543+
),
544+
config: impl Into<LowPowerConfig>,
545+
rcc: &mut Rcc,
546+
) -> Result<Self, InvalidConfig> {
547+
Self::_new(usart, (Some(pins.0), Some(pins.1)), config, rcc)
548+
}
549+
fn _new(
517550
usart: $USARTX,
518551
pins: (
519552
Option<impl Into<<$USARTX as CommonPins>::Tx<Otype>>>,
@@ -629,19 +662,45 @@ macro_rules! uart_full {
629662
impl SerialExt<FullConfig> for $USARTX {
630663
fn usart<Otype>(
631664
self,
632-
pins: (
633-
Option<impl Into<Self::Tx<Otype>>>,
634-
Option<impl Into<Self::Rx<PushPull>>>,
635-
),
665+
pins: (impl Into<Self::Tx<Otype>>, impl Into<Self::Rx<PushPull>>),
636666
config: impl Into<FullConfig>,
637667
rcc: &mut Rcc,
638668
) -> Result<Serial<Self, Otype>, InvalidConfig> {
639669
Serial::$usartX(self, pins, config, rcc)
640670
}
671+
fn tx<Otype>(
672+
self,
673+
tx: impl Into<Self::Tx<Otype>>,
674+
config: impl Into<FullConfig>,
675+
rcc: &mut Rcc,
676+
) -> Result<Tx<Self, NoDMA, Otype>, InvalidConfig> {
677+
Serial::<Self, _>::_new(self, (Some(tx), None::<Self::Rx<PushPull>>), config, rcc)
678+
.map(|s| s.split().0)
679+
}
680+
fn rx(
681+
self,
682+
rx: impl Into<Self::Rx<PushPull>>,
683+
config: impl Into<FullConfig>,
684+
rcc: &mut Rcc,
685+
) -> Result<Rx<Self, NoDMA>, InvalidConfig> {
686+
Serial::<Self, _>::_new(self, (None::<Self::Tx<PushPull>>, Some(rx)), config, rcc)
687+
.map(|s| s.split().1)
688+
}
641689
}
642690

643691
impl<Otype> Serial<$USARTX, Otype> {
644692
pub fn $usartX(
693+
usart: $USARTX,
694+
pins: (
695+
impl Into<<$USARTX as CommonPins>::Tx<Otype>>,
696+
impl Into<<$USARTX as CommonPins>::Rx<PushPull>>,
697+
),
698+
config: impl Into<FullConfig>,
699+
rcc: &mut Rcc,
700+
) -> Result<Self, InvalidConfig> {
701+
Self::_new(usart, (Some(pins.0), Some(pins.1)), config, rcc)
702+
}
703+
fn _new(
645704
usart: $USARTX,
646705
pins: (
647706
Option<impl Into<<$USARTX as CommonPins>::Tx<Otype>>>,

0 commit comments

Comments
 (0)