Skip to content

Commit f446600

Browse files
committed
option pin
1 parent e8a239e commit f446600

File tree

5 files changed

+37
-23
lines changed

5 files changed

+37
-23
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-
(tx, rx),
51+
(Some(tx), Some(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 = gpioa.pa2.into_alternate();
43-
let rx = gpioa.pa3;
42+
let tx = Some(gpioa.pa2.into_alternate());
43+
let rx = Some(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-
(tx, rx),
39+
(Some(tx), Some(rx)),
4040
FullConfig::default()
4141
.baudrate(115200.bps())
4242
.fifo_enable()

examples/uart.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,22 +33,22 @@ fn main() -> ! {
3333
let rx = gpioa.pa3.into_alternate();
3434
let mut usart = dp
3535
.USART2
36-
.usart(tx, rx, FullConfig::default(), &mut rcc)
36+
.usart(((Some(tx), Some(rx)), FullConfig::default(), &mut rcc)
3737
.unwrap();*/
3838
/*let gpioc = dp.GPIOC.split(&mut rcc);
3939
let tx = gpioc.pc4.into_alternate();
4040
let rx = gpioc.pc5.into_alternate();
4141
let mut usart = dp
4242
.USART1
43-
.usart((tx, rx), FullConfig::default(), &mut rcc)
43+
.usart(((Some(tx), Some(rx)), FullConfig::default(), &mut rcc)
4444
.unwrap();*/
4545

4646
let gpioc = dp.GPIOC.split(&mut rcc);
4747
let tx = gpioc.pc10.into_alternate();
4848
let rx = gpioc.pc11.into_alternate();
4949
let mut usart = dp
5050
.USART3
51-
.usart((tx, rx), FullConfig::default(), &mut rcc)
51+
.usart((Some(tx), Some(rx)), FullConfig::default(), &mut rcc)
5252
.unwrap();
5353

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

src/serial/usart.rs

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -93,14 +93,14 @@ pub trait Instance: crate::Sealed + crate::Ptr + Enable + Reset + CommonPins {}
9393

9494
/// Serial receiver
9595
pub struct Rx<USART: Instance, Dma> {
96-
pin: USART::Rx<PushPull>,
96+
pin: Option<USART::Rx<PushPull>>,
9797
usart: USART,
9898
_dma: PhantomData<Dma>,
9999
}
100100

101101
/// Serial transmitter
102102
pub struct Tx<USART: Instance, Dma, Otype = PushPull> {
103-
pin: USART::Tx<Otype>,
103+
pin: Option<USART::Tx<Otype>>,
104104
usart: USART,
105105
_dma: PhantomData<Dma>,
106106
}
@@ -118,10 +118,16 @@ pub struct NoDMA;
118118
#[derive(Debug)]
119119
pub struct DMA;
120120

121+
#[allow(non_upper_case_globals)]
121122
pub trait SerialExt<Config>: Sized + Instance {
123+
const NoTx: Option<Self::Tx<PushPull>> = None;
124+
const NoRx: Option<Self::Rx<PushPull>> = None;
122125
fn usart<Otype>(
123126
self,
124-
pins: (impl Into<Self::Tx<Otype>>, impl Into<Self::Rx<PushPull>>),
127+
pins: (
128+
Option<impl Into<Self::Tx<Otype>>>,
129+
Option<impl Into<Self::Rx<PushPull>>>,
130+
),
125131
config: impl Into<Config>,
126132
rcc: &mut Rcc,
127133
) -> Result<Serial<Self, Otype>, InvalidConfig>;
@@ -461,15 +467,17 @@ macro_rules! uart_shared {
461467
self,
462468
) -> (
463469
$USARTX,
464-
<$USARTX as CommonPins>::Tx<Otype>,
465-
<$USARTX as CommonPins>::Rx<PushPull>,
470+
(
471+
Option<<$USARTX as CommonPins>::Tx<Otype>>,
472+
Option<<$USARTX as CommonPins>::Rx<PushPull>>,
473+
),
466474
) {
467475
// Disable the UART as well as its clock.
468476
self.tx.usart.cr1().modify(|_, w| w.ue().clear_bit());
469477
unsafe {
470478
$USARTX::disable_unchecked();
471479
}
472-
(self.tx.usart, self.tx.pin, self.rx.pin)
480+
(self.tx.usart, (self.tx.pin, self.rx.pin))
473481
}
474482
}
475483

@@ -506,7 +514,10 @@ macro_rules! uart_lp {
506514
impl SerialExt<LowPowerConfig> for $USARTX {
507515
fn usart<Otype>(
508516
self,
509-
pins: (impl Into<Self::Tx<Otype>>, impl Into<Self::Rx<PushPull>>),
517+
pins: (
518+
Option<impl Into<Self::Tx<Otype>>>,
519+
Option<impl Into<Self::Rx<PushPull>>>,
520+
),
510521
config: impl Into<LowPowerConfig>,
511522
rcc: &mut Rcc,
512523
) -> Result<Serial<Self, Otype>, InvalidConfig> {
@@ -518,8 +529,8 @@ macro_rules! uart_lp {
518529
pub fn $usartX(
519530
usart: $USARTX,
520531
pins: (
521-
impl Into<<$USARTX as CommonPins>::Tx<Otype>>,
522-
impl Into<<$USARTX as CommonPins>::Rx<PushPull>>,
532+
Option<impl Into<<$USARTX as CommonPins>::Tx<Otype>>>,
533+
Option<impl Into<<$USARTX as CommonPins>::Rx<PushPull>>>,
523534
),
524535
config: impl Into<LowPowerConfig>,
525536
rcc: &mut Rcc,
@@ -574,12 +585,12 @@ macro_rules! uart_lp {
574585

575586
Ok(Serial {
576587
tx: Tx {
577-
pin: pins.0.into(),
588+
pin: pins.0.map(Into::into),
578589
usart,
579590
_dma: PhantomData,
580591
},
581592
rx: Rx {
582-
pin: pins.1.into(),
593+
pin: pins.1.map(Into::into),
583594
usart: unsafe { $USARTX::steal() },
584595
_dma: PhantomData,
585596
},
@@ -631,7 +642,10 @@ macro_rules! uart_full {
631642
impl SerialExt<FullConfig> for $USARTX {
632643
fn usart<Otype>(
633644
self,
634-
pins: (impl Into<Self::Tx<Otype>>, impl Into<Self::Rx<PushPull>>),
645+
pins: (
646+
Option<impl Into<Self::Tx<Otype>>>,
647+
Option<impl Into<Self::Rx<PushPull>>>,
648+
),
635649
config: impl Into<FullConfig>,
636650
rcc: &mut Rcc,
637651
) -> Result<Serial<Self, Otype>, InvalidConfig> {
@@ -643,8 +657,8 @@ macro_rules! uart_full {
643657
pub fn $usartX(
644658
usart: $USARTX,
645659
pins: (
646-
impl Into<<$USARTX as CommonPins>::Tx<Otype>>,
647-
impl Into<<$USARTX as CommonPins>::Rx<PushPull>>,
660+
Option<impl Into<<$USARTX as CommonPins>::Tx<Otype>>>,
661+
Option<impl Into<<$USARTX as CommonPins>::Rx<PushPull>>>,
648662
),
649663
config: impl Into<FullConfig>,
650664
rcc: &mut Rcc,
@@ -706,12 +720,12 @@ macro_rules! uart_full {
706720

707721
Ok(Serial {
708722
tx: Tx {
709-
pin: pins.0.into(),
723+
pin: pins.0.map(Into::into),
710724
usart,
711725
_dma: PhantomData,
712726
},
713727
rx: Rx {
714-
pin: pins.1.into(),
728+
pin: pins.1.map(Into::into),
715729
usart: unsafe { $USARTX::steal() },
716730
_dma: PhantomData,
717731
},

0 commit comments

Comments
 (0)