From 5b588a4e07178388f3fcb8b77527f0219717ff10 Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Wed, 10 Dec 2025 12:20:54 +0100 Subject: [PATCH] improve async TX API for UART --- .../examples/embassy/src/bin/async-uart-tx.rs | 5 ++-- .../examples/embassy/src/bin/async-uart-tx.rs | 5 ++-- vorago-shared-hal/src/uart/tx_asynch.rs | 24 +++++++++++++++++-- 3 files changed, 26 insertions(+), 8 deletions(-) diff --git a/va108xx/examples/embassy/src/bin/async-uart-tx.rs b/va108xx/examples/embassy/src/bin/async-uart-tx.rs index 4b70584..7c28f8a 100644 --- a/va108xx/examples/embassy/src/bin/async-uart-tx.rs +++ b/va108xx/examples/embassy/src/bin/async-uart-tx.rs @@ -15,7 +15,6 @@ use embassy_example as _; use embassy_executor::Spawner; use embassy_time::{Duration, Instant, Ticker}; -use embedded_io_async::Write; use va108xx_hal::{ gpio::{Output, PinState}, pac::{self, interrupt}, @@ -70,8 +69,8 @@ async fn main(_spawner: Spawner) { led0.toggle(); led1.toggle(); led2.toggle(); - let _written = async_tx - .write(STR_LIST[idx].as_bytes()) + async_tx + .write_all(STR_LIST[idx].as_bytes()) .await .expect("writing failed"); idx += 1; diff --git a/va416xx/examples/embassy/src/bin/async-uart-tx.rs b/va416xx/examples/embassy/src/bin/async-uart-tx.rs index 124ee7c..075ca99 100644 --- a/va416xx/examples/embassy/src/bin/async-uart-tx.rs +++ b/va416xx/examples/embassy/src/bin/async-uart-tx.rs @@ -19,7 +19,6 @@ use defmt_rtt as _; use embassy_example::EXTCLK_FREQ; use embassy_executor::Spawner; use embassy_time::{Duration, Instant, Ticker}; -use embedded_io_async::Write; use va416xx_hal::{ clock::ClockConfigurator, gpio::{Output, PinState}, @@ -69,8 +68,8 @@ async fn main(_spawner: Spawner) { loop { defmt::println!("Current time: {}", Instant::now().as_secs()); led.toggle(); - let _written = async_tx - .write(STR_LIST[idx].as_bytes()) + async_tx + .write_all(STR_LIST[idx].as_bytes()) .await .expect("writing failed"); idx += 1; diff --git a/vorago-shared-hal/src/uart/tx_asynch.rs b/vorago-shared-hal/src/uart/tx_asynch.rs index 2cde8d2..4725779 100644 --- a/vorago-shared-hal/src/uart/tx_asynch.rs +++ b/vorago-shared-hal/src/uart/tx_asynch.rs @@ -177,6 +177,27 @@ impl TxAsync { Self(tx) } + /// Write a buffer asynchronously. + /// + /// This implementation is not side effect free, and a started future might have already + /// written part of the passed buffer. + pub async fn write(&mut self, buf: &[u8]) -> Result { + let fut = unsafe { TxFuture::new(&mut self.0, buf) }; + fut.await + } + + /// Write an entire buffer into this writer. + /// + /// This function calls `write()` in a loop until exactly `buf.len()` bytes have + /// been written, waiting if needed. + /// + /// This function is not side-effect-free on cancel (AKA "cancel-safe"), i.e. if you cancel (drop) a returned + /// future that hasn't completed yet, some bytes might have already been written. + pub async fn write_all(&mut self, buf: &[u8]) -> Result<(), TxOverrunError> { + let fut = ::write_all(self, buf); + fut.await + } + pub fn release(self) -> Tx { self.0 } @@ -203,8 +224,7 @@ impl Write for TxAsync { /// This implementation is not side effect free, and a started future might have already /// written part of the passed buffer. async fn write(&mut self, buf: &[u8]) -> Result { - let fut = unsafe { TxFuture::new(&mut self.0, buf) }; - fut.await + self.write(buf).await } async fn flush(&mut self) -> Result<(), Self::Error> {