Skip to content

Commit 24e923d

Browse files
committed
UTx/URx by default
1 parent 10f36d4 commit 24e923d

File tree

1 file changed

+53
-144
lines changed

1 file changed

+53
-144
lines changed

src/serial.rs

Lines changed: 53 additions & 144 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
1919
use core::fmt;
2020
use core::marker::PhantomData;
21-
use core::ops::{Deref, DerefMut};
2221

2322
use crate::rcc;
2423
use nb::block;
@@ -187,32 +186,11 @@ pub type NoRx = NoPin;
187186

188187
/// Serial abstraction
189188
pub struct Serial<USART: Instance, WORD = u8> {
190-
usart: USART,
191-
pins: (USART::TxPin, USART::RxPin),
192189
tx: Tx<USART, WORD>,
193190
rx: Rx<USART, WORD>,
194191
}
195192

196-
/// Serial receiver
197-
pub struct Rx<USART, WORD = u8> {
198-
_usart: PhantomData<USART>,
199-
_word: PhantomData<WORD>,
200-
}
201-
202-
/// Serial transmitter
203-
pub struct Tx<USART, WORD = u8> {
204-
_usart: PhantomData<USART>,
205-
_word: PhantomData<WORD>,
206-
}
207-
208193
impl<USART: Instance, WORD> Rx<USART, WORD> {
209-
fn new() -> Self {
210-
Self {
211-
_usart: PhantomData,
212-
_word: PhantomData,
213-
}
214-
}
215-
216194
/// Start listening for an rx not empty interrupt event
217195
///
218196
/// Note, you will also have to enable the corresponding interrupt
@@ -240,6 +218,18 @@ impl<USART: Instance, WORD> Rx<USART, WORD> {
240218
}
241219
}
242220

221+
impl<USART: Instance> Rx<USART, u8> {
222+
fn with_u16_data(self) -> Rx<USART, u16> {
223+
Rx::new(self.pin)
224+
}
225+
}
226+
227+
impl<USART: Instance> Rx<USART, u16> {
228+
fn with_u8_data(self) -> Rx<USART, u8> {
229+
Rx::new(self.pin)
230+
}
231+
}
232+
243233
/// Trait for [`Rx`] interrupt handling.
244234
pub trait RxISR {
245235
/// Return true if the line idle status is set
@@ -273,13 +263,6 @@ impl<USART: Instance, WORD> RxISR for Rx<USART, WORD> {
273263
}
274264

275265
impl<USART: Instance, WORD> Tx<USART, WORD> {
276-
fn new() -> Self {
277-
Self {
278-
_usart: PhantomData,
279-
_word: PhantomData,
280-
}
281-
}
282-
283266
/// Start listening for a tx empty interrupt event
284267
///
285268
/// Note, you will also have to enable the corresponding interrupt
@@ -294,6 +277,18 @@ impl<USART: Instance, WORD> Tx<USART, WORD> {
294277
}
295278
}
296279

280+
impl<USART: Instance> Tx<USART, u8> {
281+
fn with_u16_data(self) -> Tx<USART, u16> {
282+
Tx::new(self.usart, self.pin)
283+
}
284+
}
285+
286+
impl<USART: Instance> Tx<USART, u16> {
287+
fn with_u8_data(self) -> Tx<USART, u8> {
288+
Tx::new(self.usart, self.pin)
289+
}
290+
}
291+
297292
/// Trait for [`Tx`] interrupt handling.
298293
pub trait TxISR {
299294
/// Return true if the tx register is empty (and can accept data)
@@ -336,118 +331,42 @@ impl<USART: Instance, WORD> AsMut<Rx<USART, WORD>> for Serial<USART, WORD> {
336331
}
337332

338333
/// Serial receiver containing RX pin
339-
pub struct URx<USART: Instance, WORD = u8> {
340-
inner: Rx<USART, WORD>,
334+
pub struct Rx<USART: Instance, WORD = u8> {
335+
_word: PhantomData<(USART, WORD)>,
341336
pin: USART::RxPin,
342337
}
343338

344339
/// Serial transmitter containing TX pin
345-
pub struct UTx<USART: Instance, WORD = u8> {
346-
inner: Tx<USART, WORD>,
340+
pub struct Tx<USART: Instance, WORD = u8> {
341+
_word: PhantomData<WORD>,
347342
usart: USART,
348343
pin: USART::TxPin,
349344
}
350345

351-
impl<USART: Instance, WORD> URx<USART, WORD> {
346+
impl<USART: Instance, WORD> Rx<USART, WORD> {
352347
fn new(pin: USART::RxPin) -> Self {
353348
Self {
354-
inner: Rx::new(),
349+
_word: PhantomData,
355350
pin,
356351
}
357352
}
358353

359-
pub fn erase(self) -> Rx<USART, WORD> {
360-
Rx::new()
361-
}
362-
363-
pub fn join<TX>(self, tx: UTx<USART, WORD>) -> Serial<USART, WORD> {
364-
Serial {
365-
usart: tx.usart,
366-
pins: (tx.pin, self.pin),
367-
tx: tx.inner,
368-
rx: self.inner,
369-
}
354+
pub fn join<TX>(self, tx: Tx<USART, WORD>) -> Serial<USART, WORD> {
355+
Serial { tx, rx: self }
370356
}
371357
}
372358

373-
impl<USART: Instance, WORD> UTx<USART, WORD> {
359+
impl<USART: Instance, WORD> Tx<USART, WORD> {
374360
fn new(usart: USART, pin: USART::TxPin) -> Self {
375361
Self {
376-
inner: Tx::new(),
362+
_word: PhantomData,
377363
usart,
378364
pin,
379365
}
380366
}
381367

382-
pub fn erase(self) -> Tx<USART, WORD> {
383-
Tx::new()
384-
}
385-
386-
pub fn join(self, rx: URx<USART, WORD>) -> Serial<USART, WORD> {
387-
Serial {
388-
usart: self.usart,
389-
pins: (self.pin, rx.pin),
390-
tx: self.inner,
391-
rx: rx.inner,
392-
}
393-
}
394-
}
395-
396-
impl<USART: Instance, WORD> AsRef<Tx<USART, WORD>> for UTx<USART, WORD> {
397-
#[inline(always)]
398-
fn as_ref(&self) -> &Tx<USART, WORD> {
399-
&self.inner
400-
}
401-
}
402-
403-
impl<USART: Instance, WORD> Deref for UTx<USART, WORD> {
404-
type Target = Tx<USART, WORD>;
405-
#[inline(always)]
406-
fn deref(&self) -> &Self::Target {
407-
&self.inner
408-
}
409-
}
410-
411-
impl<USART: Instance, WORD> AsRef<Rx<USART, WORD>> for URx<USART, WORD> {
412-
#[inline(always)]
413-
fn as_ref(&self) -> &Rx<USART, WORD> {
414-
&self.inner
415-
}
416-
}
417-
418-
impl<USART: Instance, WORD> Deref for URx<USART, WORD> {
419-
type Target = Rx<USART, WORD>;
420-
#[inline(always)]
421-
fn deref(&self) -> &Self::Target {
422-
&self.inner
423-
}
424-
}
425-
426-
impl<USART: Instance, WORD> AsMut<Tx<USART, WORD>> for UTx<USART, WORD> {
427-
#[inline(always)]
428-
fn as_mut(&mut self) -> &mut Tx<USART, WORD> {
429-
&mut self.inner
430-
}
431-
}
432-
433-
impl<USART: Instance, WORD> DerefMut for UTx<USART, WORD> {
434-
#[inline(always)]
435-
fn deref_mut(&mut self) -> &mut Self::Target {
436-
&mut self.inner
437-
}
438-
}
439-
440-
impl<USART: Instance, WORD> AsMut<Rx<USART, WORD>> for URx<USART, WORD> {
441-
#[inline(always)]
442-
fn as_mut(&mut self) -> &mut Rx<USART, WORD> {
443-
&mut self.inner
444-
}
445-
}
446-
447-
impl<USART: Instance, WORD> DerefMut for URx<USART, WORD> {
448-
#[inline(always)]
449-
fn deref_mut(&mut self) -> &mut Self::Target {
450-
&mut self.inner
368+
pub fn join(self, rx: Rx<USART, WORD>) -> Serial<USART, WORD> {
369+
Serial { tx: self, rx }
451370
}
452371
}
453372

@@ -628,13 +547,9 @@ impl<USART: Instance, WORD> Serial<USART, WORD> {
628547
DmaConfig::None => {}
629548
}
630549

631-
let pins = (pins.0.into(), pins.1.into());
632-
633550
Ok(Serial {
634-
usart,
635-
pins,
636-
tx: Tx::new(),
637-
rx: Rx::new(),
551+
tx: Tx::new(usart, pins.0.into()),
552+
rx: Rx::new(pins.1.into()),
638553
}
639554
.config_stop(config))
640555
}
@@ -645,8 +560,8 @@ impl<USART: Instance, WORD> Serial<USART, WORD> {
645560
RX: TryFrom<USART::RxPin, Error = E>,
646561
{
647562
Ok((
648-
self.usart,
649-
(self.pins.0.try_into()?, self.pins.1.try_into()?),
563+
self.tx.usart,
564+
(self.tx.pin.try_into()?, self.rx.pin.try_into()?),
650565
))
651566
}
652567
}
@@ -730,22 +645,14 @@ impl<USART: Instance, WORD> TxISR for Serial<USART, WORD> {
730645
}
731646
}
732647

733-
impl<USART: Instance, WORD> Serial<USART, WORD> {
734-
pub fn split_nondestructive(self) -> (UTx<USART, WORD>, URx<USART, WORD>) {
735-
(UTx::new(self.usart, self.pins.0), URx::new(self.pins.1))
736-
}
737-
}
738-
739648
impl<USART: Instance> Serial<USART, u8> {
740649
/// Converts this Serial into a version that can read and write `u16` values instead of `u8`s
741650
///
742651
/// This can be used with a word length of 9 bits.
743652
pub fn with_u16_data(self) -> Serial<USART, u16> {
744653
Serial {
745-
usart: self.usart,
746-
pins: self.pins,
747-
tx: Tx::new(),
748-
rx: Rx::new(),
654+
tx: self.tx.with_u16_data(),
655+
rx: self.rx.with_u16_data(),
749656
}
750657
}
751658
}
@@ -756,10 +663,8 @@ impl<USART: Instance> Serial<USART, u16> {
756663
/// This can be used with a word length of 8 bits.
757664
pub fn with_u8_data(self) -> Serial<USART, u8> {
758665
Serial {
759-
usart: self.usart,
760-
pins: self.pins,
761-
tx: Tx::new(),
762-
rx: Rx::new(),
666+
tx: self.tx.with_u8_data(),
667+
rx: self.rx.with_u8_data(),
763668
}
764669
}
765670
}
@@ -784,7 +689,7 @@ unsafe impl<USART: Instance> PeriAddress for Tx<USART, u8> {
784689

785690
impl<USART: Instance, WORD> Serial<USART, WORD> {
786691
fn config_stop(self, config: config::Config) -> Self {
787-
self.usart.set_stopbits(config.stopbits);
692+
self.tx.usart.set_stopbits(config.stopbits);
788693
self
789694
}
790695
}
@@ -831,7 +736,7 @@ pub trait Instance: crate::Sealed + rcc::Enable + rcc::Reset + rcc::BusClock {
831736
}
832737

833738
macro_rules! halUsart {
834-
($USART:ty, $usart:ident ,$Serial:ident, $Tx:ident, $Rx:ident) => {
739+
($USART:ty, $usart:ident, $Serial:ident, $Tx:ident, $Rx:ident) => {
835740
pub type $Serial<WORD = u8> = Serial<$USART, WORD>;
836741
pub type $Tx<WORD = u8> = Tx<$USART, WORD>;
837742
pub type $Rx<WORD = u8> = Rx<$USART, WORD>;
@@ -949,7 +854,11 @@ impl<USART: Instance> fmt::Write for Tx<USART> {
949854
impl<USART: Instance> Rx<USART, u8> {
950855
fn read(&mut self) -> nb::Result<u8, Error> {
951856
// Delegate to the Read<u16> implementation, then truncate to 8 bits
952-
Rx::<USART, u16>::new().read().map(|word16| word16 as u8)
857+
unsafe {
858+
(&mut *(self as *mut Self as *mut Rx<USART, u16>))
859+
.read()
860+
.map(|word16| word16 as u8)
861+
}
953862
}
954863
}
955864

@@ -987,12 +896,12 @@ impl<USART: Instance> Rx<USART, u16> {
987896
impl<USART: Instance> Tx<USART, u8> {
988897
fn write(&mut self, word: u8) -> nb::Result<(), Error> {
989898
// Delegate to u16 version
990-
Tx::<USART, u16>::new().write(u16::from(word))
899+
unsafe { (&mut *(self as *mut Self as *mut Tx<USART, u16>)).write(u16::from(word)) }
991900
}
992901

993902
fn flush(&mut self) -> nb::Result<(), Error> {
994903
// Delegate to u16 version
995-
Tx::<USART, u16>::new().flush()
904+
unsafe { (&mut *(self as *mut Self as *mut Tx<USART, u16>)).flush() }
996905
}
997906

998907
fn bwrite_all(&mut self, bytes: &[u8]) -> Result<(), Error> {

0 commit comments

Comments
 (0)