aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/stm32/src/bin/usart.rs42
1 files changed, 26 insertions, 16 deletions
diff --git a/tests/stm32/src/bin/usart.rs b/tests/stm32/src/bin/usart.rs
index fece2fb3e..74a81b4ec 100644
--- a/tests/stm32/src/bin/usart.rs
+++ b/tests/stm32/src/bin/usart.rs
@@ -5,11 +5,11 @@
5mod common; 5mod common;
6 6
7use common::*; 7use common::*;
8use defmt::assert_eq; 8use defmt::{assert, assert_eq, unreachable};
9use embassy_executor::Spawner; 9use embassy_executor::Spawner;
10use embassy_stm32::dma::NoDma; 10use embassy_stm32::dma::NoDma;
11use embassy_stm32::usart::{Config, Error, Uart}; 11use embassy_stm32::usart::{Config, ConfigError, Error, Uart};
12use embassy_time::{Duration, Instant}; 12use embassy_time::{block_for, Duration, Instant};
13 13
14#[embassy_executor::main] 14#[embassy_executor::main]
15async fn main(_spawner: Spawner) { 15async fn main(_spawner: Spawner) {
@@ -44,10 +44,16 @@ async fn main(_spawner: Spawner) {
44 let mut usart = Uart::new(&mut usart, &mut rx, &mut tx, irq, NoDma, NoDma, config).unwrap(); 44 let mut usart = Uart::new(&mut usart, &mut rx, &mut tx, irq, NoDma, NoDma, config).unwrap();
45 45
46 // Send enough bytes to fill the RX FIFOs off all USART versions. 46 // Send enough bytes to fill the RX FIFOs off all USART versions.
47 let data = [0xC0, 0xDE, 0x12, 0x23, 0x34]; 47 let data = [0; 64];
48 usart.blocking_write(&data).unwrap(); 48 usart.blocking_write(&data).unwrap();
49 usart.blocking_flush().unwrap(); 49 usart.blocking_flush().unwrap();
50 50
51 // USART can still take up to 1 bit time (?) to receive the last byte
52 // that we just flushed, so wait a bit.
53 // otherwise, we might clear the overrun flag from an *earlier* byte and
54 // it gets set again when receiving the last byte is done.
55 block_for(Duration::from_millis(1));
56
51 // The error should be reported first. 57 // The error should be reported first.
52 let mut buf = [0; 1]; 58 let mut buf = [0; 1];
53 let err = usart.blocking_read(&mut buf); 59 let err = usart.blocking_read(&mut buf);
@@ -60,22 +66,25 @@ async fn main(_spawner: Spawner) {
60 66
61 // Test that baudrate divider is calculated correctly. 67 // Test that baudrate divider is calculated correctly.
62 // Do it by comparing the time it takes to send a known number of bytes. 68 // Do it by comparing the time it takes to send a known number of bytes.
63 for baudrate in [ 69 for baudrate in [300, 9600, 115200, 250_000, 337_934, 1_000_000, 2_000_000] {
64 300,
65 9600,
66 115200,
67 250_000,
68 337_934,
69 #[cfg(not(feature = "stm32f103c8"))]
70 1_000_000,
71 #[cfg(not(feature = "stm32f103c8"))]
72 2_000_000,
73 ] {
74 info!("testing baudrate {}", baudrate); 70 info!("testing baudrate {}", baudrate);
75 71
76 let mut config = Config::default(); 72 let mut config = Config::default();
77 config.baudrate = baudrate; 73 config.baudrate = baudrate;
78 let mut usart = Uart::new(&mut usart, &mut rx, &mut tx, irq, NoDma, NoDma, config).unwrap(); 74 let mut usart = match Uart::new(&mut usart, &mut rx, &mut tx, irq, NoDma, NoDma, config) {
75 Ok(x) => x,
76 Err(ConfigError::BaudrateTooHigh) => {
77 info!("baudrate too high");
78 assert!(baudrate >= 1_000_000);
79 continue;
80 }
81 Err(ConfigError::BaudrateTooLow) => {
82 info!("baudrate too low");
83 assert!(baudrate <= 300);
84 continue;
85 }
86 Err(_) => unreachable!(),
87 };
79 88
80 let n = (baudrate as usize / 100).max(64); 89 let n = (baudrate as usize / 100).max(64);
81 90
@@ -83,6 +92,7 @@ async fn main(_spawner: Spawner) {
83 for _ in 0..n { 92 for _ in 0..n {
84 usart.blocking_write(&[0x00]).unwrap(); 93 usart.blocking_write(&[0x00]).unwrap();
85 } 94 }
95 usart.blocking_flush().unwrap();
86 let dur = Instant::now() - start; 96 let dur = Instant::now() - start;
87 let want_dur = Duration::from_micros(n as u64 * 10 * 1_000_000 / (baudrate as u64)); 97 let want_dur = Duration::from_micros(n as u64 * 10 * 1_000_000 / (baudrate as u64));
88 let fuzz = want_dur / 5; 98 let fuzz = want_dur / 5;