improve async TX API for UART #6

Merged
muellerr merged 1 commits from improve-tx-async-api into main 2025-12-10 12:22:45 +01:00
3 changed files with 26 additions and 8 deletions

View File

@@ -15,7 +15,6 @@ use embassy_example as _;
use embassy_executor::Spawner; use embassy_executor::Spawner;
use embassy_time::{Duration, Instant, Ticker}; use embassy_time::{Duration, Instant, Ticker};
use embedded_io_async::Write;
use va108xx_hal::{ use va108xx_hal::{
gpio::{Output, PinState}, gpio::{Output, PinState},
pac::{self, interrupt}, pac::{self, interrupt},
@@ -70,8 +69,8 @@ async fn main(_spawner: Spawner) {
led0.toggle(); led0.toggle();
led1.toggle(); led1.toggle();
led2.toggle(); led2.toggle();
let _written = async_tx async_tx
.write(STR_LIST[idx].as_bytes()) .write_all(STR_LIST[idx].as_bytes())
.await .await
.expect("writing failed"); .expect("writing failed");
idx += 1; idx += 1;

View File

@@ -19,7 +19,6 @@ use defmt_rtt as _;
use embassy_example::EXTCLK_FREQ; use embassy_example::EXTCLK_FREQ;
use embassy_executor::Spawner; use embassy_executor::Spawner;
use embassy_time::{Duration, Instant, Ticker}; use embassy_time::{Duration, Instant, Ticker};
use embedded_io_async::Write;
use va416xx_hal::{ use va416xx_hal::{
clock::ClockConfigurator, clock::ClockConfigurator,
gpio::{Output, PinState}, gpio::{Output, PinState},
@@ -69,8 +68,8 @@ async fn main(_spawner: Spawner) {
loop { loop {
defmt::println!("Current time: {}", Instant::now().as_secs()); defmt::println!("Current time: {}", Instant::now().as_secs());
led.toggle(); led.toggle();
let _written = async_tx async_tx
.write(STR_LIST[idx].as_bytes()) .write_all(STR_LIST[idx].as_bytes())
.await .await
.expect("writing failed"); .expect("writing failed");
idx += 1; idx += 1;

View File

@@ -177,6 +177,27 @@ impl TxAsync {
Self(tx) 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<usize, TxOverrunError> {
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 = <Self as embedded_io_async::Write>::write_all(self, buf);
fut.await
}
pub fn release(self) -> Tx { pub fn release(self) -> Tx {
self.0 self.0
} }
@@ -203,8 +224,7 @@ impl Write for TxAsync {
/// This implementation is not side effect free, and a started future might have already /// This implementation is not side effect free, and a started future might have already
/// written part of the passed buffer. /// written part of the passed buffer.
async fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error> { async fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error> {
let fut = unsafe { TxFuture::new(&mut self.0, buf) }; self.write(buf).await
fut.await
} }
async fn flush(&mut self) -> Result<(), Self::Error> { async fn flush(&mut self) -> Result<(), Self::Error> {