Skip to content

Commit 8e63159

Browse files
committed
Allow re-combining TX/RX pair to serial port.
1 parent 48e737f commit 8e63159

File tree

1 file changed

+59
-48
lines changed

1 file changed

+59
-48
lines changed

src/serial.rs

+59-48
Original file line numberDiff line numberDiff line change
@@ -206,20 +206,20 @@ impl From<Bps> for Config {
206206

207207
/// Serial abstraction
208208
pub struct Serial<USART, PINS> {
209-
usart: USART,
210-
pins: PINS,
211-
tx: Tx<USART>,
212-
rx: Rx<USART>,
209+
tx: Tx<USART, PINS>,
210+
rx: Rx<USART, PINS>,
213211
}
214212

215213
/// Serial receiver
216-
pub struct Rx<USART> {
214+
pub struct Rx<USART, PINS> {
217215
_usart: PhantomData<USART>,
216+
pins: PINS,
218217
}
219218

220219
/// Serial transmitter
221-
pub struct Tx<USART> {
222-
_usart: PhantomData<USART>,
220+
pub struct Tx<USART, PINS> {
221+
usart: USART,
222+
pins: PhantomData<PINS>,
223223
}
224224

225225
macro_rules! hal {
@@ -375,29 +375,33 @@ macro_rules! hal {
375375
.modify(|_, w| w.ue().set_bit().re().set_bit().te().set_bit());
376376

377377
Serial {
378-
usart, pins,
379-
tx: Tx { _usart: PhantomData },
380-
rx: Rx { _usart: PhantomData },
378+
tx: Tx { usart, pins: PhantomData },
379+
rx: Rx { _usart: PhantomData, pins },
381380
}
382381
}
383382

383+
#[inline]
384+
fn usart(&self) -> &pac::$USARTX {
385+
&self.tx.usart
386+
}
387+
384388
/// Starts listening for an interrupt event
385389
pub fn listen(&mut self, event: Event) {
386390
match event {
387391
Event::Rxne => {
388-
self.usart.cr1.modify(|_, w| w.rxneie().set_bit())
392+
self.usart().cr1.modify(|_, w| w.rxneie().set_bit())
389393
},
390394
Event::Txe => {
391-
self.usart.cr1.modify(|_, w| w.txeie().set_bit())
395+
self.usart().cr1.modify(|_, w| w.txeie().set_bit())
392396
},
393397
Event::Idle => {
394-
self.usart.cr1.modify(|_, w| w.idleie().set_bit())
398+
self.usart().cr1.modify(|_, w| w.idleie().set_bit())
395399
},
396400
Event::CharacterMatch => {
397-
self.usart.cr1.modify(|_, w| w.cmie().set_bit())
401+
self.usart().cr1.modify(|_, w| w.cmie().set_bit())
398402
},
399403
Event::ReceiverTimeout => {
400-
self.usart.cr1.modify(|_, w| w.rtoie().set_bit())
404+
self.usart().cr1.modify(|_, w| w.rtoie().set_bit())
401405
},
402406
}
403407
}
@@ -434,25 +438,25 @@ macro_rules! hal {
434438
pub fn unlisten(&mut self, event: Event) {
435439
match event {
436440
Event::Rxne => {
437-
self.usart.cr1.modify(|_, w| w.rxneie().clear_bit())
441+
self.usart().cr1.modify(|_, w| w.rxneie().clear_bit())
438442
},
439443
Event::Txe => {
440-
self.usart.cr1.modify(|_, w| w.txeie().clear_bit())
444+
self.usart().cr1.modify(|_, w| w.txeie().clear_bit())
441445
},
442446
Event::Idle => {
443-
self.usart.cr1.modify(|_, w| w.idleie().clear_bit())
447+
self.usart().cr1.modify(|_, w| w.idleie().clear_bit())
444448
},
445449
Event::CharacterMatch => {
446-
self.usart.cr1.modify(|_, w| w.cmie().clear_bit())
450+
self.usart().cr1.modify(|_, w| w.cmie().clear_bit())
447451
},
448452
Event::ReceiverTimeout => {
449-
self.usart.cr1.modify(|_, w| w.rtoie().clear_bit())
453+
self.usart().cr1.modify(|_, w| w.rtoie().clear_bit())
450454
},
451455
}
452456
}
453457

454458
/// Splits the `Serial` abstraction into a transmitter and a receiver half
455-
pub fn split(self) -> (Tx<pac::$USARTX>, Rx<pac::$USARTX>) {
459+
pub fn split(self) -> (Tx<pac::$USARTX, PINS>, Rx<pac::$USARTX, PINS>) {
456460
(
457461
self.tx,
458462
self.rx,
@@ -461,7 +465,7 @@ macro_rules! hal {
461465

462466
/// Frees the USART peripheral
463467
pub fn release(self) -> (pac::$USARTX, PINS) {
464-
(self.usart, self.pins)
468+
(self.tx.usart, self.rx.pins)
465469
}
466470
}
467471

@@ -473,7 +477,7 @@ macro_rules! hal {
473477
}
474478
}
475479

476-
impl serial::Read<u8> for Rx<pac::$USARTX> {
480+
impl<PINS> serial::Read<u8> for Rx<pac::$USARTX, PINS> {
477481
type Error = Error;
478482

479483
fn read(&mut self) -> nb::Result<u8, Error> {
@@ -505,7 +509,7 @@ macro_rules! hal {
505509
}
506510
}
507511

508-
impl serial::Write<u8> for Tx<pac::$USARTX> {
512+
impl<PINS> serial::Write<u8> for Tx<pac::$USARTX, PINS> {
509513
// NOTE(Void) See section "29.7 USART interrupts"; the only possible errors during
510514
// transmission are: clear to send (which is disabled in this case) errors and
511515
// framing errors (which only occur in SmartCard mode); neither of these apply to
@@ -540,23 +544,22 @@ macro_rules! hal {
540544
}
541545
}
542546

543-
impl embedded_hal::blocking::serial::write::Default<u8>
544-
for Tx<pac::$USARTX> {}
547+
impl<PINS> embedded_hal::blocking::serial::write::Default<u8> for Tx<pac::$USARTX, PINS> {}
545548

546-
pub type $rxdma = RxDma<Rx<pac::$USARTX>, $dmarxch>;
547-
pub type $txdma = TxDma<Tx<pac::$USARTX>, $dmatxch>;
549+
pub type $rxdma<PINS> = RxDma<Rx<pac::$USARTX, PINS>, $dmarxch>;
550+
pub type $txdma<PINS> = TxDma<Tx<pac::$USARTX, PINS>, $dmatxch>;
548551

549-
impl Receive for $rxdma {
552+
impl<PINS> Receive for $rxdma<PINS> {
550553
type RxChannel = $dmarxch;
551554
type TransmittedWord = u8;
552555
}
553556

554-
impl Transmit for $txdma {
557+
impl<PINS> Transmit for $txdma<PINS> {
555558
type TxChannel = $dmatxch;
556559
type ReceivedWord = u8;
557560
}
558561

559-
impl TransferPayload for $rxdma {
562+
impl<PINS> TransferPayload for $rxdma<PINS> {
560563
fn start(&mut self) {
561564
self.channel.start();
562565
}
@@ -565,7 +568,7 @@ macro_rules! hal {
565568
}
566569
}
567570

568-
impl TransferPayload for $txdma {
571+
impl<PINS> TransferPayload for $txdma<PINS> {
569572
fn start(&mut self) {
570573
self.channel.start();
571574
}
@@ -574,8 +577,8 @@ macro_rules! hal {
574577
}
575578
}
576579

577-
impl Rx<pac::$USARTX> {
578-
pub fn with_dma(self, channel: $dmarxch) -> $rxdma {
580+
impl<PINS> Rx<pac::$USARTX, PINS> {
581+
pub fn with_dma(self, channel: $dmarxch) -> $rxdma<PINS> {
579582
RxDma {
580583
payload: self,
581584
channel,
@@ -643,37 +646,37 @@ macro_rules! hal {
643646
}
644647
}
645648

646-
impl crate::dma::CharacterMatch for Rx<pac::$USARTX> {
649+
impl<PINS> crate::dma::CharacterMatch for Rx<pac::$USARTX, PINS> {
647650
/// Checks to see if the USART peripheral has detected an character match and
648651
/// clears the flag
649652
fn check_character_match(&mut self, clear: bool) -> bool {
650653
self.check_character_match(clear)
651654
}
652655
}
653656

654-
impl crate::dma::ReceiverTimeout for Rx<pac::$USARTX> {
657+
impl<PINS> crate::dma::ReceiverTimeout for Rx<pac::$USARTX, PINS> {
655658
fn check_receiver_timeout(&mut self, clear: bool) -> bool {
656659
self.is_receiver_timeout(clear)
657660
}
658661
}
659662

660-
impl crate::dma::OperationError<(), Error> for Rx<pac::$USARTX>{
663+
impl<PINS> crate::dma::OperationError<(), Error> for Rx<pac::$USARTX, PINS> {
661664
fn check_operation_error(&mut self) -> Result<(), Error> {
662665
self.check_for_error()
663666
}
664667
}
665668

666-
impl Tx<pac::$USARTX> {
667-
pub fn with_dma(self, channel: $dmatxch) -> $txdma {
669+
impl<PINS> Tx<pac::$USARTX, PINS> {
670+
pub fn with_dma(self, channel: $dmatxch) -> $txdma<PINS> {
668671
TxDma {
669672
payload: self,
670673
channel,
671674
}
672675
}
673676
}
674677

675-
impl $rxdma {
676-
pub fn split(mut self) -> (Rx<pac::$USARTX>, $dmarxch) {
678+
impl<PINS> $rxdma<PINS> {
679+
pub fn split(mut self) -> (Rx<pac::$USARTX, PINS>, $dmarxch) {
677680
self.stop();
678681
let RxDma {payload, channel} = self;
679682
(
@@ -683,8 +686,8 @@ macro_rules! hal {
683686
}
684687
}
685688

686-
impl $txdma {
687-
pub fn split(mut self) -> (Tx<pac::$USARTX>, $dmatxch) {
689+
impl<PINS> $txdma<PINS> {
690+
pub fn split(mut self) -> (Tx<pac::$USARTX, PINS>, $dmatxch) {
688691
self.stop();
689692
let TxDma {payload, channel} = self;
690693
(
@@ -694,7 +697,7 @@ macro_rules! hal {
694697
}
695698
}
696699

697-
impl<B> crate::dma::CircReadDma<B, u8> for $rxdma
700+
impl<B, PINS> crate::dma::CircReadDma<B, u8> for $rxdma<PINS>
698701
where
699702
&'static mut B: StaticWriteBuffer<Word = u8>,
700703
B: 'static,
@@ -746,7 +749,7 @@ macro_rules! hal {
746749
}
747750
}
748751

749-
impl $rxdma {
752+
impl<PINS> $rxdma<PINS> {
750753
/// Create a frame reader that can either react on the Character match interrupt or
751754
/// Transfer Complete from the DMA.
752755
pub fn frame_reader<BUFFER, const N: usize>(
@@ -796,7 +799,7 @@ macro_rules! hal {
796799
}
797800
}
798801

799-
impl $txdma {
802+
impl<PINS> $txdma<PINS> {
800803
/// Creates a new DMA frame sender
801804
pub fn frame_sender<BUFFER, const N: usize>(
802805
mut self,
@@ -836,6 +839,14 @@ macro_rules! hal {
836839
}
837840
}
838841

842+
impl<USART, PINS> From<(Tx<USART, PINS>, Rx<USART, PINS>)> for Serial<USART, PINS> {
843+
/// Convert a transmitter/receiver back to a combined serial port.
844+
fn from(txrx: (Tx<USART, PINS>, Rx<USART, PINS>)) -> Self {
845+
let (tx, rx) = txrx;
846+
Self { tx, rx }
847+
}
848+
}
849+
839850
hal! {
840851
USART1: (usart1, pclk2, tx: (TxDma1, dma1::C4, DmaInput::Usart1Tx), rx: (RxDma1, dma1::C5, DmaInput::Usart1Rx)),
841852
USART2: (usart2, pclk1, tx: (TxDma2, dma1::C7, DmaInput::Usart2Tx), rx: (RxDma2, dma1::C6, DmaInput::Usart2Rx)),
@@ -905,9 +916,9 @@ where
905916
}
906917
}
907918

908-
impl<USART> fmt::Write for Tx<USART>
919+
impl<USART, PINS> fmt::Write for Tx<USART, PINS>
909920
where
910-
Tx<USART>: crate::hal::serial::Write<u8>,
921+
Tx<USART, PINS>: crate::hal::serial::Write<u8>,
911922
{
912923
fn write_str(&mut self, s: &str) -> fmt::Result {
913924
let _ = s

0 commit comments

Comments
 (0)