From 972f87a661639549cdda39afaa4a3ee5f72bd2f0 Mon Sep 17 00:00:00 2001 From: xoviat Date: Tue, 9 Dec 2025 19:57:25 -0600 Subject: tests/stm32: make usart more noise-immune --- tests/stm32/src/bin/usart.rs | 44 ++++++++++++++++++++++++++++++++------------ 1 file changed, 32 insertions(+), 12 deletions(-) (limited to 'tests') diff --git a/tests/stm32/src/bin/usart.rs b/tests/stm32/src/bin/usart.rs index 0b98d3eeb..ef7efe96a 100644 --- a/tests/stm32/src/bin/usart.rs +++ b/tests/stm32/src/bin/usart.rs @@ -6,6 +6,7 @@ mod common; use common::*; use defmt::{assert, assert_eq, unreachable}; use embassy_executor::Spawner; +use embassy_stm32::mode::Blocking; use embassy_stm32::usart::{Config, ConfigError, Error, Uart}; use embassy_time::{Duration, Instant, block_for}; @@ -24,22 +25,41 @@ async fn main(_spawner: Spawner) { let config = Config::default(); let mut usart = Uart::new_blocking(usart.reborrow(), rx.reborrow(), tx.reborrow(), config).unwrap(); - // We can't send too many bytes, they have to fit in the FIFO. - // This is because we aren't sending+receiving at the same time. + let test_usart = async |usart: &mut Uart<'_, Blocking>| -> Result<(), Error> { + // We can't send too many bytes, they have to fit in the FIFO. + // This is because we aren't sending+receiving at the same time. - let data = [0xC0, 0xDE]; - usart.blocking_write(&data).unwrap(); + let data = [0xC0, 0xDE]; + usart.blocking_write(&data)?; - let mut buf = [0; 2]; - usart.blocking_read(&mut buf).unwrap(); - assert_eq!(buf, data); + let mut buf = [0; 2]; + usart.blocking_read(&mut buf)?; + assert_eq!(buf, data); - // Test flush doesn't hang. - usart.blocking_write(&data).unwrap(); - usart.blocking_flush().unwrap(); + // Test flush doesn't hang. + usart.blocking_write(&data)?; + usart.blocking_flush()?; - // Test flush doesn't hang if there's nothing to flush - usart.blocking_flush().unwrap(); + // Test flush doesn't hang if there's nothing to flush + usart.blocking_flush()?; + + Ok(()) + }; + + let mut is_ok = false; + for _ in 0..3 { + match test_usart(&mut usart).await { + Ok(()) => is_ok = true, + Err(Error::Noise) => is_ok = false, + Err(e) => defmt::panic!("{}", e), + } + + if is_ok { + break; + } + } + + assert!(is_ok); } // Test error handling with with an overflow error -- cgit