From 136507e87aac8d8937019eca2130fa3fda97d176 Mon Sep 17 00:00:00 2001 From: Andrey Zgarbul Date: Sat, 29 Nov 2025 15:07:31 +0300 Subject: [PATCH] remove WORD from Serial --- CHANGELOG.md | 5 + examples/rtic-usart-shell-ssd1306.rs | 3 +- examples/rtic-usart-shell.rs | 3 +- examples/serial-9bit.rs | 8 +- src/prelude.rs | 4 - src/serial.rs | 173 ++++++++---------------- src/serial/hal_02.rs | 61 ++++++--- src/serial/hal_1.rs | 58 ++++---- src/serial/uart_impls.rs | 195 +++++++++++++++++++++++---- 9 files changed, 309 insertions(+), 201 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 832d370c..419cdc56 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ## [Unreleased] +### Changed + + - `Serial` implements `Write` and `Read` for `WORD` simultaneously as `u8` and `u16` + - Add inherent impl of `read`/`write` methods on `Serial` + ## [v0.23.0] - 2025-09-22 - Implement `embedded_hal::i2c::I2c` for `I2cMasterDma` [#838] diff --git a/examples/rtic-usart-shell-ssd1306.rs b/examples/rtic-usart-shell-ssd1306.rs index 018a0a0a..b3de00e3 100644 --- a/examples/rtic-usart-shell-ssd1306.rs +++ b/examples/rtic-usart-shell-ssd1306.rs @@ -110,8 +110,7 @@ mod usart_shell { Config::default().baudrate(115_200.bps()).wordlength_8(), &mut rcc, ) - .unwrap() - .with_u8_data(); + .unwrap(); serial.listen(serial::Event::RxNotEmpty); // ushell let autocomplete = StaticAutocomplete(["clear", "help", "off", "on", "status"]); diff --git a/examples/rtic-usart-shell.rs b/examples/rtic-usart-shell.rs index 59ffe59d..16b1cf47 100644 --- a/examples/rtic-usart-shell.rs +++ b/examples/rtic-usart-shell.rs @@ -77,8 +77,7 @@ mod usart_shell { Config::default().baudrate(115_200.bps()).wordlength_8(), &mut rcc, ) - .unwrap() - .with_u8_data(); + .unwrap(); serial.listen(serial::Event::RxNotEmpty); // ushell let autocomplete = StaticAutocomplete(["clear", "help", "off", "on", "status"]); diff --git a/examples/serial-9bit.rs b/examples/serial-9bit.rs index 0612395f..5f5873e9 100644 --- a/examples/serial-9bit.rs +++ b/examples/serial-9bit.rs @@ -67,9 +67,7 @@ fn main() -> ! { Config::default().baudrate(9600.bps()).wordlength_9(), &mut rcc, ) - .unwrap() - // Make this Serial object use u16s instead of u8s - .with_u16_data(); + .unwrap(); let (mut tx, mut rx) = serial.split(); @@ -77,9 +75,9 @@ fn main() -> ! { loop { for value in nine_bit_integers.clone() { - block!(tx.write(value)).unwrap(); + block!(tx.write_u16(value)).unwrap(); // Receive what we just sent - let received: u16 = block!(rx.read()).unwrap(); + let received: u16 = block!(rx.read_u16()).unwrap(); // Update LEDs to display what was received if ((received >> 5) & 1) == 1 { diff --git a/src/prelude.rs b/src/prelude.rs index d3722cca..1b808d5f 100644 --- a/src/prelude.rs +++ b/src/prelude.rs @@ -38,12 +38,8 @@ //! ``` pub use embedded_hal::delay::DelayNs as _; pub use embedded_hal_02::adc::OneShot as _embedded_hal_adc_OneShot; -pub use embedded_hal_02::blocking::serial::Write as _embedded_hal_blocking_serial_Write; -pub use embedded_hal_02::Capture as _embedded_hal_Capture; pub use embedded_hal_02::Pwm as _embedded_hal_Pwm; pub use embedded_hal_02::Qei as _embedded_hal_Qei; -pub use embedded_hal_nb::serial::Read as _embedded_hal_serial_nb_Read; -pub use embedded_hal_nb::serial::Write as _embedded_hal_serial_nb_Write; pub use fugit::ExtU32 as _fugit_ExtU32; pub use fugit::RateExtU32 as _fugit_RateExtU32; diff --git a/src/serial.rs b/src/serial.rs index 4d8f4517..e2408dc9 100644 --- a/src/serial.rs +++ b/src/serial.rs @@ -15,7 +15,6 @@ //! implementations for 9-bit words. use core::fmt; -use core::marker::PhantomData; use enumflags2::BitFlags; #[allow(unused)] @@ -195,49 +194,47 @@ pub trait TxListen { } /// Serial abstraction -pub struct Serial { - tx: Tx, - rx: Rx, +pub struct Serial { + tx: Tx, + rx: Rx, } /// Serial receiver containing RX pin -pub struct Rx { - _word: PhantomData, +pub struct Rx { usart: USART, pin: Option>, } /// Serial transmitter containing TX pin -pub struct Tx { - _word: PhantomData, +pub struct Tx { usart: USART, pin: Option>, } pub trait SerialExt: Sized + Instance { - fn serial( + fn serial( self, pins: (impl Into>, impl Into>), config: impl Into, rcc: &mut Rcc, - ) -> Result, config::InvalidConfig>; + ) -> Result, config::InvalidConfig>; - fn tx( + fn tx( self, tx_pin: impl Into>, config: impl Into, rcc: &mut Rcc, - ) -> Result, config::InvalidConfig>; + ) -> Result, config::InvalidConfig>; - fn rx( + fn rx( self, rx_pin: impl Into>, config: impl Into, rcc: &mut Rcc, - ) -> Result, config::InvalidConfig>; + ) -> Result, config::InvalidConfig>; } -impl Serial { +impl Serial { pub fn new( uart: USART, pins: ( @@ -368,8 +365,8 @@ fn calculate_brr(pclk_freq: u32, baud: u32) -> Result<(bool, u32), config::Inval } } -impl Serial { - pub fn split(self) -> (Tx, Rx) { +impl Serial { + pub fn split(self) -> (Tx, Rx) { (self.tx, self.rx) } @@ -386,9 +383,9 @@ impl Serial { macro_rules! halUsart { ($USART:ty, $Serial:ident, $Rx:ident, $Tx:ident) => { - pub type $Serial = Serial<$USART, WORD>; - pub type $Tx = Tx<$USART, WORD>; - pub type $Rx = Rx<$USART, WORD>; + pub type $Serial = Serial<$USART>; + pub type $Tx = Tx<$USART>; + pub type $Rx = Rx<$USART>; impl Instance for $USART {} }; @@ -405,9 +402,9 @@ halUsart! { pac::USART3, Serial3, Rx3, Tx3 } #[cfg(feature = "uart4")] macro_rules! halUart { ($UART:ty, $Serial:ident, $Rx:ident, $Tx:ident) => { - pub type $Serial = Serial<$UART, WORD>; - pub type $Tx = Tx<$UART, WORD>; - pub type $Rx = Rx<$UART, WORD>; + pub type $Serial = Serial<$UART>; + pub type $Tx = Tx<$UART>; + pub type $Rx = Rx<$UART>; impl Instance for $UART {} }; @@ -426,113 +423,57 @@ halUart! { pac::UART9, Serial9, Rx9, Tx9 } #[cfg(feature = "uart10")] halUart! { pac::UART10, Serial10, Rx10, Tx10 } -impl Rx { - pub(crate) fn with_u16_data(self) -> Rx { - Rx::new(self.usart, self.pin) - } -} - -impl Rx { - pub(crate) fn with_u8_data(self) -> Rx { - Rx::new(self.usart, self.pin) - } -} - -impl Tx { - pub(crate) fn with_u16_data(self) -> Tx { - Tx::new(self.usart, self.pin) - } -} - -impl Tx { - pub(crate) fn with_u8_data(self) -> Tx { - Tx::new(self.usart, self.pin) - } -} - -impl Rx { +impl Rx { pub(crate) fn new(usart: UART, pin: Option>) -> Self { - Self { - _word: PhantomData, - usart, - pin, - } + Self { usart, pin } } - pub fn join(self, tx: Tx) -> Serial { + pub fn join(self, tx: Tx) -> Serial { Serial { tx, rx: self } } } -impl Tx { +impl Tx { pub(crate) fn new(usart: UART, pin: Option>) -> Self { - Self { - _word: PhantomData, - usart, - pin, - } + Self { usart, pin } } - pub fn join(self, rx: Rx) -> Serial { + pub fn join(self, rx: Rx) -> Serial { Serial { tx: self, rx } } } -impl AsRef> for Serial { +impl AsRef> for Serial { #[inline(always)] - fn as_ref(&self) -> &Tx { + fn as_ref(&self) -> &Tx { &self.tx } } -impl AsRef> for Serial { +impl AsRef> for Serial { #[inline(always)] - fn as_ref(&self) -> &Rx { + fn as_ref(&self) -> &Rx { &self.rx } } -impl AsMut> for Serial { +impl AsMut> for Serial { #[inline(always)] - fn as_mut(&mut self) -> &mut Tx { + fn as_mut(&mut self) -> &mut Tx { &mut self.tx } } -impl AsMut> for Serial { +impl AsMut> for Serial { #[inline(always)] - fn as_mut(&mut self) -> &mut Rx { + fn as_mut(&mut self) -> &mut Rx { &mut self.rx } } -impl Serial { - /// Converts this Serial into a version that can read and write `u16` values instead of `u8`s - /// - /// This can be used with a word length of 9 bits. - pub fn with_u16_data(self) -> Serial { - Serial { - tx: self.tx.with_u16_data(), - rx: self.rx.with_u16_data(), - } - } -} - -impl Serial { - /// Converts this Serial into a version that can read and write `u8` values instead of `u16`s - /// - /// This can be used with a word length of 8 bits. - pub fn with_u8_data(self) -> Serial { - Serial { - tx: self.tx.with_u8_data(), - rx: self.rx.with_u8_data(), - } - } -} - -impl RxISR for Serial +impl RxISR for Serial where - Rx: RxISR, + Rx: RxISR, { fn is_idle(&self) -> bool { self.rx.is_idle() @@ -548,7 +489,7 @@ where } } -impl RxISR for Rx { +impl RxISR for Rx { fn is_idle(&self) -> bool { self.usart.is_idle() } @@ -563,22 +504,22 @@ impl RxISR for Rx { } } -impl TxISR for Serial +impl TxISR for Serial where - Tx: TxISR, + Tx: TxISR, { fn is_tx_empty(&self) -> bool { self.tx.is_tx_empty() } } -impl TxISR for Tx { +impl TxISR for Tx { fn is_tx_empty(&self) -> bool { self.usart.is_tx_empty() } } -impl RxListen for Rx { +impl RxListen for Rx { fn listen(&mut self) { self.usart.listen_rxne() } @@ -596,7 +537,7 @@ impl RxListen for Rx { } } -impl TxListen for Tx { +impl TxListen for Tx { fn listen(&mut self) { self.usart.listen_txe() } @@ -606,7 +547,7 @@ impl TxListen for Tx { } } -impl crate::ClearFlags for Serial { +impl crate::ClearFlags for Serial { type Flag = CFlag; #[inline(always)] @@ -615,7 +556,7 @@ impl crate::ClearFlags for Serial { } } -impl crate::ReadFlags for Serial { +impl crate::ReadFlags for Serial { type Flag = Flag; #[inline(always)] @@ -624,7 +565,7 @@ impl crate::ReadFlags for Serial { } } -impl crate::Listen for Serial { +impl crate::Listen for Serial { type Event = Event; #[inline(always)] @@ -663,39 +604,39 @@ impl fmt::Write for Tx { } impl SerialExt for UART { - fn serial( + fn serial( self, pins: (impl Into>, impl Into>), config: impl Into, rcc: &mut Rcc, - ) -> Result, config::InvalidConfig> { + ) -> Result, config::InvalidConfig> { Serial::new(self, pins, config, rcc) } - fn tx( + fn tx( self, tx_pin: impl Into>, config: impl Into, rcc: &mut Rcc, - ) -> Result, config::InvalidConfig> { + ) -> Result, config::InvalidConfig> { Serial::tx(self, tx_pin, config, rcc) } - fn rx( + fn rx( self, rx_pin: impl Into>, config: impl Into, rcc: &mut Rcc, - ) -> Result, config::InvalidConfig> { + ) -> Result, config::InvalidConfig> { Serial::rx(self, rx_pin, config, rcc) } } -impl Serial { +impl Serial { pub fn tx( usart: UART, tx_pin: impl Into>, config: impl Into, rcc: &mut Rcc, - ) -> Result, config::InvalidConfig> { + ) -> Result, config::InvalidConfig> { Self::_new( usart, (Some(tx_pin), None::>), @@ -706,13 +647,13 @@ impl Serial { } } -impl Serial { +impl Serial { pub fn rx( usart: UART, rx_pin: impl Into>, config: impl Into, rcc: &mut Rcc, - ) -> Result, config::InvalidConfig> { + ) -> Result, config::InvalidConfig> { Self::_new( usart, (None::>, Some(rx_pin)), @@ -723,7 +664,7 @@ impl Serial { } } -unsafe impl PeriAddress for Rx { +unsafe impl PeriAddress for Rx { #[inline(always)] fn address(&self) -> u32 { self.usart.peri_address() @@ -739,7 +680,7 @@ where { } -unsafe impl PeriAddress for Tx { +unsafe impl PeriAddress for Tx { #[inline(always)] fn address(&self) -> u32 { self.usart.peri_address() diff --git a/src/serial/hal_02.rs b/src/serial/hal_02.rs index 2776ee1f..7861cd22 100644 --- a/src/serial/hal_02.rs +++ b/src/serial/hal_02.rs @@ -4,20 +4,22 @@ mod nb { use super::super::{Error, Instance, Rx, Serial, Tx}; use embedded_hal_02::serial::{Read, Write}; - impl Read for Serial + impl Read for Serial where - Rx: Read, + Rx: Read, { type Error = Error; + #[inline(always)] fn read(&mut self) -> nb::Result { - self.rx.read() + Read::read(&mut self.rx) } } - impl Read for Rx { + impl Read for Rx { type Error = Error; + #[inline(always)] fn read(&mut self) -> nb::Result { self.usart.read_u8() } @@ -28,35 +30,40 @@ mod nb { /// If the UART/USART was configured with `WordLength::DataBits9`, the returned value will contain /// 9 received data bits and all other bits set to zero. Otherwise, the returned value will contain /// 8 received data bits and all other bits set to zero. - impl Read for Rx { + impl Read for Rx { type Error = Error; + #[inline(always)] fn read(&mut self) -> nb::Result { self.usart.read_u16() } } - impl Write for Serial + impl Write for Serial where - Tx: Write, + Tx: Write, { type Error = Error; + #[inline(always)] fn flush(&mut self) -> nb::Result<(), Self::Error> { - self.tx.flush() + Write::flush(&mut self.tx) } - fn write(&mut self, byte: WORD) -> nb::Result<(), Self::Error> { - self.tx.write(byte) + #[inline(always)] + fn write(&mut self, word: WORD) -> nb::Result<(), Self::Error> { + Write::write(&mut self.tx, word) } } - impl Write for Tx { + impl Write for Tx { type Error = Error; + #[inline(always)] fn write(&mut self, word: u8) -> nb::Result<(), Self::Error> { self.usart.write_u8(word) } + #[inline(always)] fn flush(&mut self) -> nb::Result<(), Self::Error> { self.usart.flush() } @@ -67,13 +74,15 @@ mod nb { /// If the UART/USART was configured with `WordLength::DataBits9`, the 9 least significant bits will /// be transmitted and the other 7 bits will be ignored. Otherwise, the 8 least significant bits /// will be transmitted and the other 8 bits will be ignored. - impl Write for Tx { + impl Write for Tx { type Error = Error; + #[inline(always)] fn write(&mut self, word: u16) -> nb::Result<(), Self::Error> { self.usart.write_u16(word) } + #[inline(always)] fn flush(&mut self) -> nb::Result<(), Self::Error> { self.usart.flush() } @@ -88,60 +97,68 @@ mod blocking { use super::super::{Error, Instance, Serial, Tx}; use embedded_hal_02::blocking::serial::Write; - impl Write for Tx { + impl Write for Tx { type Error = Error; + #[inline(always)] fn bwrite_all(&mut self, bytes: &[u8]) -> Result<(), Self::Error> { self.usart.bwrite_all_u8(bytes) } + #[inline(always)] fn bflush(&mut self) -> Result<(), Self::Error> { self.usart.bflush() } } - impl Write for Serial + impl Write for Serial where - Tx: Write, + Tx: Write, { type Error = Error; + #[inline(always)] fn bwrite_all(&mut self, bytes: &[u8]) -> Result<(), Self::Error> { - self.tx.bwrite_all(bytes) + Write::bwrite_all(&mut self.tx, bytes) } + #[inline(always)] fn bflush(&mut self) -> Result<(), Self::Error> { - self.tx.bflush() + Write::bflush(&mut self.tx) } } - impl Write for Tx + impl Write for Tx where USART: Deref::RB>, { type Error = Error; + #[inline(always)] fn bwrite_all(&mut self, slice: &[u16]) -> Result<(), Self::Error> { self.usart.bwrite_all_u16(slice) } + #[inline(always)] fn bflush(&mut self) -> Result<(), Self::Error> { self.usart.bflush() } } - impl Write for Serial + impl Write for Serial where - Tx: Write, + Tx: Write, { type Error = Error; + #[inline(always)] fn bwrite_all(&mut self, bytes: &[u16]) -> Result<(), Self::Error> { - self.tx.bwrite_all(bytes) + Write::bwrite_all(&mut self.tx, bytes) } + #[inline(always)] fn bflush(&mut self) -> Result<(), Self::Error> { - self.tx.bflush() + Write::bflush(&mut self.tx) } } } diff --git a/src/serial/hal_1.rs b/src/serial/hal_1.rs index ae608075..40fc0687 100644 --- a/src/serial/hal_1.rs +++ b/src/serial/hal_1.rs @@ -16,26 +16,28 @@ mod nb { } } - impl embedded_hal_nb::serial::ErrorType for Serial { + impl embedded_hal_nb::serial::ErrorType for Serial { type Error = Error; } - impl embedded_hal_nb::serial::ErrorType for Rx { + impl embedded_hal_nb::serial::ErrorType for Rx { type Error = Error; } - impl embedded_hal_nb::serial::ErrorType for Tx { + impl embedded_hal_nb::serial::ErrorType for Tx { type Error = Error; } - impl Read for Serial + impl Read for Serial where - Rx: Read, + Rx: Read, { + #[inline(always)] fn read(&mut self) -> nb::Result { - self.rx.read() + Read::read(&mut self.rx) } } - impl Read for Rx { + impl Read for Rx { + #[inline(always)] fn read(&mut self) -> nb::Result { self.usart.read_u8() } @@ -46,29 +48,34 @@ mod nb { /// If the UART/USART was configured with `WordLength::DataBits9`, the returned value will contain /// 9 received data bits and all other bits set to zero. Otherwise, the returned value will contain /// 8 received data bits and all other bits set to zero. - impl Read for Rx { + impl Read for Rx { + #[inline(always)] fn read(&mut self) -> nb::Result { self.usart.read_u16() } } - impl Write for Serial + impl Write for Serial where - Tx: Write, + Tx: Write, { + #[inline(always)] fn flush(&mut self) -> nb::Result<(), Self::Error> { - self.tx.flush() + Write::flush(&mut self.tx) } - fn write(&mut self, byte: WORD) -> nb::Result<(), Self::Error> { - self.tx.write(byte) + #[inline(always)] + fn write(&mut self, word: WORD) -> nb::Result<(), Self::Error> { + Write::write(&mut self.tx, word) } } - impl Write for Tx { + impl Write for Tx { + #[inline(always)] fn write(&mut self, word: u8) -> nb::Result<(), Self::Error> { self.usart.write_u8(word) } + #[inline(always)] fn flush(&mut self) -> nb::Result<(), Self::Error> { self.usart.flush() } @@ -79,11 +86,13 @@ mod nb { /// If the UART/USART was configured with `WordLength::DataBits9`, the 9 least significant bits will /// be transmitted and the other 7 bits will be ignored. Otherwise, the 8 least significant bits /// will be transmitted and the other 8 bits will be ignored. - impl Write for Tx { + impl Write for Tx { + #[inline(always)] fn write(&mut self, word: u16) -> nb::Result<(), Self::Error> { self.usart.write_u16(word) } + #[inline(always)] fn flush(&mut self) -> nb::Result<(), Self::Error> { self.usart.flush() } @@ -103,19 +112,19 @@ mod io { } } - impl embedded_io::ErrorType for Serial { + impl embedded_io::ErrorType for Serial { type Error = Error; } - impl embedded_io::ErrorType for Tx { + impl embedded_io::ErrorType for Tx { type Error = Error; } - impl embedded_io::ErrorType for Rx { + impl embedded_io::ErrorType for Rx { type Error = Error; } - impl Write for Tx { + impl Write for Tx { fn write(&mut self, bytes: &[u8]) -> Result { let mut i = 0; for byte in bytes.iter() { @@ -134,22 +143,25 @@ mod io { Ok(i) } + #[inline(always)] fn flush(&mut self) -> Result<(), Self::Error> { self.usart.bflush()?; Ok(()) } } - impl Write for Serial + impl Write for Serial where - Tx: Write, + Tx: Write, { + #[inline(always)] fn write(&mut self, bytes: &[u8]) -> Result { - self.tx.write(bytes) + Write::write(&mut self.tx, bytes) } + #[inline(always)] fn flush(&mut self) -> Result<(), Self::Error> { - self.tx.flush() + Write::flush(&mut self.tx) } } } diff --git a/src/serial/uart_impls.rs b/src/serial/uart_impls.rs index e1eb6963..de987876 100644 --- a/src/serial/uart_impls.rs +++ b/src/serial/uart_impls.rs @@ -12,36 +12,38 @@ pub trait RegisterBlockImpl: UartRB { // NOTE(unsafe) atomic read with no side effects let sr = self.sr().read(); - // Any error requires the dr to be read to clear - if sr.pe().bit_is_set() - || sr.fe().bit_is_set() - || sr.nf().bit_is_set() - || sr.ore().bit_is_set() - { - self.dr().read(); - } - - Err(if sr.pe().bit_is_set() { - Error::Parity.into() + // Check for any errors + let err = if sr.pe().bit_is_set() { + Some(Error::Parity) } else if sr.fe().bit_is_set() { - Error::FrameFormat.into() + Some(Error::FrameFormat) } else if sr.nf().bit_is_set() { - Error::Noise.into() + Some(Error::Noise) } else if sr.ore().bit_is_set() { - Error::Overrun.into() - } else if sr.rxne().bit_is_set() { - // NOTE(unsafe) atomic read from stateless register - return Ok(self.dr().read().dr().bits()); + Some(Error::Overrun) } else { - nb::Error::WouldBlock - }) + None + }; + + if let Some(err) = err { + // Some error occurred. In order to clear that error flag, you have to + // do a read from the sr register followed by a read from the dr register. + let _ = self.sr().read(); + let _ = self.dr().read(); + Err(err.into()) + } else { + // Check if a byte is available + if sr.rxne().bit_is_set() { + // Read the received byte + Ok(self.dr().read().dr().bits()) + } else { + Err(nb::Error::WouldBlock) + } + } } fn write_u16(&self, word: u16) -> nb::Result<(), Error> { - // NOTE(unsafe) atomic read with no side effects - let sr = self.sr().read(); - - if sr.txe().bit_is_set() { + if self.sr().read().txe().bit_is_set() { // NOTE(unsafe) atomic write to stateless register self.dr().write(|w| w.dr().set(word)); Ok(()) @@ -63,10 +65,7 @@ pub trait RegisterBlockImpl: UartRB { } fn flush(&self) -> nb::Result<(), Error> { - // NOTE(unsafe) atomic read with no side effects - let sr = self.sr().read(); - - if sr.tc().bit_is_set() { + if self.sr().read().tc().bit_is_set() { Ok(()) } else { Err(nb::Error::WouldBlock) @@ -273,3 +272,145 @@ impl RegisterBlockImpl for crate::pac::uart4::RegisterBlock { } fn configure_irda(&self, _irda: IrdaMode, _pclk_freq: u32) {} } + +impl super::Tx { + /// Writes 9-bit words to the UART/USART + /// + /// If the UART/USART was configured with `WordLength::Bits9`, the 9 least significant bits will + /// be transmitted and the other 7 bits will be ignored. Otherwise, the 8 least significant bits + /// will be transmitted and the other 8 bits will be ignored. + #[inline(always)] + pub fn write_u16(&mut self, word: u16) -> nb::Result<(), Error> { + self.usart.write_u16(word) + } + #[inline(always)] + pub fn write_u8(&mut self, word: u8) -> nb::Result<(), Error> { + self.usart.write_u8(word) + } + #[inline(always)] + pub fn write(&mut self, word: u8) -> nb::Result<(), Error> { + self.usart.write_u8(word) + } + #[inline(always)] + pub fn bwrite_all_u16(&mut self, buffer: &[u16]) -> Result<(), Error> { + self.usart.bwrite_all_u16(buffer) + } + #[inline(always)] + pub fn bwrite_all_u8(&mut self, buffer: &[u8]) -> Result<(), Error> { + self.usart.bwrite_all_u8(buffer) + } + #[inline(always)] + pub fn bwrite_all(&mut self, buffer: &[u8]) -> Result<(), Error> { + self.usart.bwrite_all_u8(buffer) + } + #[inline(always)] + pub fn flush(&mut self) -> nb::Result<(), Error> { + self.usart.flush() + } + #[inline(always)] + pub fn bflush(&mut self) -> Result<(), Error> { + self.usart.bflush() + } +} + +impl super::Rx { + /// Reads 9-bit words from the UART/USART + /// + /// If the UART/USART was configured with `WordLength::Bits9`, the returned value will contain + /// 9 received data bits and all other bits set to zero. Otherwise, the returned value will contain + /// 8 received data bits and all other bits set to zero. + #[inline(always)] + pub fn read_u16(&mut self) -> nb::Result { + self.usart.read_u16() + } + #[inline(always)] + pub fn read_u8(&mut self) -> nb::Result { + self.usart.read_u8() + } + #[inline(always)] + pub fn read(&mut self) -> nb::Result { + self.usart.read_u8() + } + #[inline(always)] + pub fn bread_all_u8(&mut self, buffer: &mut [u8]) -> Result<(), Error> { + self.usart.bread_all_u8(buffer) + } + #[inline(always)] + pub fn bread_all(&mut self, buffer: &mut [u8]) -> Result<(), Error> { + self.usart.bread_all_u8(buffer) + } + #[inline(always)] + pub fn bread_all_u16(&mut self, buffer: &mut [u16]) -> Result<(), Error> { + self.usart.bread_all_u16(buffer) + } +} + +impl super::Serial { + /// Writes 9-bit words to the UART/USART + /// + /// If the UART/USART was configured with `WordLength::Bits9`, the 9 least significant bits will + /// be transmitted and the other 7 bits will be ignored. Otherwise, the 8 least significant bits + /// will be transmitted and the other 8 bits will be ignored. + #[inline(always)] + pub fn write_u16(&mut self, word: u16) -> nb::Result<(), Error> { + self.tx.write_u16(word) + } + #[inline(always)] + pub fn write_u8(&mut self, word: u8) -> nb::Result<(), Error> { + self.tx.write_u8(word) + } + #[inline(always)] + pub fn write(&mut self, word: u8) -> nb::Result<(), Error> { + self.tx.write_u8(word) + } + #[inline(always)] + pub fn bwrite_all_u16(&mut self, buffer: &[u16]) -> Result<(), Error> { + self.tx.bwrite_all_u16(buffer) + } + #[inline(always)] + pub fn bwrite_all_u8(&mut self, buffer: &[u8]) -> Result<(), Error> { + self.tx.bwrite_all_u8(buffer) + } + #[inline(always)] + pub fn bwrite_all(&mut self, buffer: &[u8]) -> Result<(), Error> { + self.tx.bwrite_all_u8(buffer) + } + #[inline(always)] + pub fn flush(&mut self) -> nb::Result<(), Error> { + self.tx.flush() + } + #[inline(always)] + pub fn bflush(&mut self) -> Result<(), Error> { + self.tx.bflush() + } + + /// Reads 9-bit words from the UART/USART + /// + /// If the UART/USART was configured with `WordLength::Bits9`, the returned value will contain + /// 9 received data bits and all other bits set to zero. Otherwise, the returned value will contain + /// 8 received data bits and all other bits set to zero. + #[inline(always)] + pub fn read_u16(&mut self) -> nb::Result { + self.rx.read_u16() + } + #[inline(always)] + pub fn read_u8(&mut self) -> nb::Result { + self.rx.read_u8() + } + #[inline(always)] + pub fn read(&mut self) -> nb::Result { + self.rx.read_u8() + } + #[inline(always)] + pub fn bread_all_u8(&mut self, buffer: &mut [u8]) -> Result<(), Error> { + self.rx.bread_all_u8(buffer) + } + #[inline(always)] + pub fn bread_all(&mut self, buffer: &mut [u8]) -> Result<(), Error> { + self.rx.bread_all_u8(buffer) + } + #[inline(always)] + pub fn bread_all_u16(&mut self, buffer: &mut [u16]) -> Result<(), Error> { + self.rx.bread_all_u16(buffer) + } +}