Skip to content

Commit

Permalink
Fix Uart::flush_async
Browse files Browse the repository at this point in the history
  • Loading branch information
bugadani committed Feb 27, 2025
1 parent 1050ce5 commit 6e5ae05
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
1 change: 1 addition & 0 deletions esp-hal/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed

- Full-duplex SPI works when mixed with half-duplex SPI (#3176)
- `Uart::flush_async` should no longer return prematurely (#3186)

### Removed

Expand Down
2 changes: 1 addition & 1 deletion esp-hal/src/uart.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1935,7 +1935,7 @@ impl UartTx<'_, Async> {
///
/// This function is cancellation safe.
pub async fn flush_async(&mut self) -> Result<(), TxError> {
if self.tx_fifo_count() > 0 {
while self.tx_fifo_count() > 0 {
UartTxFuture::new(self.uart.reborrow(), TxEvent::Done).await;
}

Expand Down
19 changes: 19 additions & 0 deletions hil-test/tests/uart_tx_rx_async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,25 @@ mod tests {
}
}

#[test]
async fn flush_async_does_not_return_prematurely(mut ctx: Context) {
// Force TX_DONE to be set
ctx.tx.write_async(&[0u8; 10]).await.unwrap();

let mut read = [0u8; 10];
ctx.rx.read_exact_async(&mut read).await.unwrap();

// The flush should not return until the data is actually sent, regardless of
// previous TX_DONE status.
ctx.tx.write_async(&[1u8; 10]).await.unwrap();
ctx.tx.flush_async().await.unwrap();

let read_count = ctx.rx.read_buffered(&mut read).unwrap();

assert_eq!(read_count, 10);
assert_eq!(&read, &[1u8; 10]);
}

#[test]
async fn rx_overflow_is_detected(mut ctx: Context) {
let mut to_send: &[u8] = &[0; 250];
Expand Down

0 comments on commit 6e5ae05

Please sign in to comment.