Skip to content

Commit 119aa3a

Browse files
committed
stm32/test: fix race condition in uart_dma.
1 parent 4167db3 commit 119aa3a

File tree

1 file changed

+18
-9
lines changed

1 file changed

+18
-9
lines changed

tests/stm32/src/bin/usart_dma.rs

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
mod example_common;
77
use defmt::assert_eq;
88
use embassy_executor::Spawner;
9+
use embassy_futures::join::join;
910
use embassy_stm32::interrupt;
1011
use embassy_stm32::usart::{Config, Uart};
1112
use example_common::*;
@@ -76,18 +77,26 @@ async fn main(_spawner: Spawner) {
7677
(p.PB6, p.PB7, p.USART1, interrupt::take!(USART1), p.DMA1_CH1, p.DMA1_CH2);
7778

7879
let config = Config::default();
79-
let mut usart = Uart::new(usart, rx, tx, irq, tx_dma, rx_dma, config);
80+
let usart = Uart::new(usart, rx, tx, irq, tx_dma, rx_dma, config);
8081

81-
// We can't send too many bytes, they have to fit in the FIFO.
82-
// This is because we aren't sending+receiving at the same time.
83-
// For whatever reason, blocking works with 2 bytes but DMA only with 1??
82+
const LEN: usize = 128;
83+
let mut tx_buf = [0; LEN];
84+
let mut rx_buf = [0; LEN];
85+
for i in 0..LEN {
86+
tx_buf[i] = i as u8;
87+
}
8488

85-
let data = [0x42];
86-
usart.write(&data).await.unwrap();
89+
let (mut tx, mut rx) = usart.split();
8790

88-
let mut buf = [0; 1];
89-
usart.read(&mut buf).await.unwrap();
90-
assert_eq!(buf, data);
91+
let tx_fut = async {
92+
tx.write(&tx_buf).await.unwrap();
93+
};
94+
let rx_fut = async {
95+
rx.read(&mut rx_buf).await.unwrap();
96+
};
97+
join(rx_fut, tx_fut).await;
98+
99+
assert_eq!(tx_buf, rx_buf);
91100

92101
info!("Test OK");
93102
cortex_m::asm::bkpt();

0 commit comments

Comments
 (0)