diff options
| author | xoviat <[email protected]> | 2025-12-09 19:57:25 -0600 |
|---|---|---|
| committer | xoviat <[email protected]> | 2025-12-09 19:57:25 -0600 |
| commit | 972f87a661639549cdda39afaa4a3ee5f72bd2f0 (patch) | |
| tree | c7d48889811b4fbf08530c9f3831fec8f40e4846 /tests/stm32/src | |
| parent | 4f322f4a03d336e90d530045255f46cce93e6252 (diff) | |
tests/stm32: make usart more noise-immune
Diffstat (limited to 'tests/stm32/src')
| -rw-r--r-- | tests/stm32/src/bin/usart.rs | 44 |
1 files changed, 32 insertions, 12 deletions
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; | |||
| 6 | use common::*; | 6 | use common::*; |
| 7 | use defmt::{assert, assert_eq, unreachable}; | 7 | use defmt::{assert, assert_eq, unreachable}; |
| 8 | use embassy_executor::Spawner; | 8 | use embassy_executor::Spawner; |
| 9 | use embassy_stm32::mode::Blocking; | ||
| 9 | use embassy_stm32::usart::{Config, ConfigError, Error, Uart}; | 10 | use embassy_stm32::usart::{Config, ConfigError, Error, Uart}; |
| 10 | use embassy_time::{Duration, Instant, block_for}; | 11 | use embassy_time::{Duration, Instant, block_for}; |
| 11 | 12 | ||
| @@ -24,22 +25,41 @@ async fn main(_spawner: Spawner) { | |||
| 24 | let config = Config::default(); | 25 | let config = Config::default(); |
| 25 | let mut usart = Uart::new_blocking(usart.reborrow(), rx.reborrow(), tx.reborrow(), config).unwrap(); | 26 | let mut usart = Uart::new_blocking(usart.reborrow(), rx.reborrow(), tx.reborrow(), config).unwrap(); |
| 26 | 27 | ||
| 27 | // We can't send too many bytes, they have to fit in the FIFO. | 28 | let test_usart = async |usart: &mut Uart<'_, Blocking>| -> Result<(), Error> { |
| 28 | // This is because we aren't sending+receiving at the same time. | 29 | // We can't send too many bytes, they have to fit in the FIFO. |
| 30 | // This is because we aren't sending+receiving at the same time. | ||
| 29 | 31 | ||
| 30 | let data = [0xC0, 0xDE]; | 32 | let data = [0xC0, 0xDE]; |
| 31 | usart.blocking_write(&data).unwrap(); | 33 | usart.blocking_write(&data)?; |
| 32 | 34 | ||
| 33 | let mut buf = [0; 2]; | 35 | let mut buf = [0; 2]; |
| 34 | usart.blocking_read(&mut buf).unwrap(); | 36 | usart.blocking_read(&mut buf)?; |
| 35 | assert_eq!(buf, data); | 37 | assert_eq!(buf, data); |
| 36 | 38 | ||
| 37 | // Test flush doesn't hang. | 39 | // Test flush doesn't hang. |
| 38 | usart.blocking_write(&data).unwrap(); | 40 | usart.blocking_write(&data)?; |
| 39 | usart.blocking_flush().unwrap(); | 41 | usart.blocking_flush()?; |
| 40 | 42 | ||
| 41 | // Test flush doesn't hang if there's nothing to flush | 43 | // Test flush doesn't hang if there's nothing to flush |
| 42 | usart.blocking_flush().unwrap(); | 44 | usart.blocking_flush()?; |
| 45 | |||
| 46 | Ok(()) | ||
| 47 | }; | ||
| 48 | |||
| 49 | let mut is_ok = false; | ||
| 50 | for _ in 0..3 { | ||
| 51 | match test_usart(&mut usart).await { | ||
| 52 | Ok(()) => is_ok = true, | ||
| 53 | Err(Error::Noise) => is_ok = false, | ||
| 54 | Err(e) => defmt::panic!("{}", e), | ||
| 55 | } | ||
| 56 | |||
| 57 | if is_ok { | ||
| 58 | break; | ||
| 59 | } | ||
| 60 | } | ||
| 61 | |||
| 62 | assert!(is_ok); | ||
| 43 | } | 63 | } |
| 44 | 64 | ||
| 45 | // Test error handling with with an overflow error | 65 | // Test error handling with with an overflow error |
