Merge pull request #4365 from qwerty19106/stm32_impl_nb_write

stm32: Fix impl embedded_hal_nb::serial::Write for usart::UartTx
This commit is contained in:
Dario Nieuwenhuis 2025-07-03 14:24:43 +00:00 committed by GitHub
commit 4af2d9adc4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -550,6 +550,20 @@ impl<'d, M: Mode> UartTx<'d, M> {
reconfigure(self.info, self.kernel_clock, config)
}
/// Write a single u8 if there is tx empty, otherwise return WouldBlock
pub(crate) fn nb_write(&mut self, byte: u8) -> Result<(), nb::Error<Error>> {
let r = self.info.regs;
let sr = sr(r).read();
if sr.txe() {
unsafe {
tdr(r).write_volatile(byte);
}
Ok(())
} else {
Err(nb::Error::WouldBlock)
}
}
/// Perform a blocking UART write
pub fn blocking_write(&mut self, buffer: &[u8]) -> Result<(), Error> {
let r = self.info.regs;
@ -1864,7 +1878,7 @@ impl<'d, M: Mode> embedded_hal_nb::serial::Read for UartRx<'d, M> {
impl<'d, M: Mode> embedded_hal_nb::serial::Write for UartTx<'d, M> {
fn write(&mut self, char: u8) -> nb::Result<(), Self::Error> {
self.blocking_write(&[char]).map_err(nb::Error::Other)
self.nb_write(char)
}
fn flush(&mut self) -> nb::Result<(), Self::Error> {