aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorRasmus Melchior Jacobsen <[email protected]>2023-04-27 10:48:38 +0200
committerDario Nieuwenhuis <[email protected]>2023-05-01 22:42:36 +0200
commitfc268df6f56661d5f43450c7a03850044ae8e136 (patch)
treeb48b4543442552b46aceba5bd7f930997d67cd57 /tests
parent4ea6662e55f32ff90b564c1c611f5ac4e0d8ab1c (diff)
Support overflow detection for more than one ring-period
Diffstat (limited to 'tests')
-rw-r--r--tests/stm32/src/bin/usart_rx_ringbuffered.rs19
-rw-r--r--tests/utils/src/bin/saturate_serial.rs15
2 files changed, 23 insertions, 11 deletions
diff --git a/tests/stm32/src/bin/usart_rx_ringbuffered.rs b/tests/stm32/src/bin/usart_rx_ringbuffered.rs
index 3ea8bfb7b..48dc25b0e 100644
--- a/tests/stm32/src/bin/usart_rx_ringbuffered.rs
+++ b/tests/stm32/src/bin/usart_rx_ringbuffered.rs
@@ -56,6 +56,7 @@ mod board {
56} 56}
57 57
58const ONE_BYTE_DURATION_US: u32 = 9_000_000 / 115200; 58const ONE_BYTE_DURATION_US: u32 = 9_000_000 / 115200;
59const DMA_BUF_SIZE: usize = 64;
59 60
60#[embassy_executor::main] 61#[embassy_executor::main]
61async fn main(spawner: Spawner) { 62async fn main(spawner: Spawner) {
@@ -114,7 +115,7 @@ async fn main(spawner: Spawner) {
114 115
115 let usart = Uart::new(usart, rx, tx, irq, tx_dma, rx_dma, config); 116 let usart = Uart::new(usart, rx, tx, irq, tx_dma, rx_dma, config);
116 let (tx, rx) = usart.split(); 117 let (tx, rx) = usart.split();
117 static mut DMA_BUF: [u8; 64] = [0; 64]; 118 static mut DMA_BUF: [u8; DMA_BUF_SIZE] = [0; DMA_BUF_SIZE];
118 let dma_buf = unsafe { DMA_BUF.as_mut() }; 119 let dma_buf = unsafe { DMA_BUF.as_mut() };
119 let rx = rx.into_ring_buffered(dma_buf); 120 let rx = rx.into_ring_buffered(dma_buf);
120 121
@@ -159,7 +160,14 @@ async fn receive_task(mut rx: RingBufferedUartRx<'static, board::Uart, board::Rx
159 loop { 160 loop {
160 let mut buf = [0; 100]; 161 let mut buf = [0; 100];
161 let max_len = 1 + (rng.next_u32() as usize % (buf.len() - 1)); 162 let max_len = 1 + (rng.next_u32() as usize % (buf.len() - 1));
162 let received = rx.read(&mut buf[..max_len]).await.unwrap(); 163 let received = match rx.read(&mut buf[..max_len]).await {
164 Ok(r) => r,
165 Err(e) => {
166 error!("Test fail! read error: {:?}", e);
167 cortex_m::asm::bkpt();
168 return;
169 }
170 };
163 171
164 if expected.is_none() { 172 if expected.is_none() {
165 info!("Test started"); 173 info!("Test started");
@@ -176,8 +184,11 @@ async fn receive_task(mut rx: RingBufferedUartRx<'static, board::Uart, board::Rx
176 } 184 }
177 185
178 if received < max_len { 186 if received < max_len {
179 let byte_count = rng.next_u32() % 64; 187 let byte_count = rng.next_u32() % (DMA_BUF_SIZE as u32);
180 Timer::after(Duration::from_micros((byte_count * ONE_BYTE_DURATION_US) as _)).await; 188 let random_delay_us = (byte_count * ONE_BYTE_DURATION_US) as u64;
189 if random_delay_us > 200 {
190 Timer::after(Duration::from_micros(random_delay_us - 200)).await;
191 }
181 } 192 }
182 193
183 i += 1; 194 i += 1;
diff --git a/tests/utils/src/bin/saturate_serial.rs b/tests/utils/src/bin/saturate_serial.rs
index 28480516d..18ca12fb7 100644
--- a/tests/utils/src/bin/saturate_serial.rs
+++ b/tests/utils/src/bin/saturate_serial.rs
@@ -1,18 +1,19 @@
1use std::path::Path; 1use std::path::Path;
2use std::time::Duration; 2use std::time::Duration;
3use std::{env, io, thread}; 3use std::{env, io, process, thread};
4 4
5use rand::random; 5use rand::random;
6use serial::SerialPort; 6use serial::SerialPort;
7 7
8pub fn main() { 8pub fn main() {
9 if let Some(port_name) = env::args().nth(1) { 9 if let Some(port_name) = env::args().nth(1) {
10 let sleep = env::args().position(|x| x == "--sleep").is_some(); 10 let idles = env::args().position(|x| x == "--idles").is_some();
11 11
12 println!("Saturating port {:?} with 115200 8N1", port_name); 12 println!("Saturating port {:?} with 115200 8N1", port_name);
13 println!("Sleep: {}", sleep); 13 println!("Idles: {}", idles);
14 println!("Process ID: {}", process::id());
14 let mut port = serial::open(&port_name).unwrap(); 15 let mut port = serial::open(&port_name).unwrap();
15 if saturate(&mut port, sleep).is_err() { 16 if saturate(&mut port, idles).is_err() {
16 eprintln!("Unable to saturate port"); 17 eprintln!("Unable to saturate port");
17 } 18 }
18 } else { 19 } else {
@@ -23,7 +24,7 @@ pub fn main() {
23 } 24 }
24} 25}
25 26
26fn saturate<T: SerialPort>(port: &mut T, sleep: bool) -> io::Result<()> { 27fn saturate<T: SerialPort>(port: &mut T, idles: bool) -> io::Result<()> {
27 port.reconfigure(&|settings| { 28 port.reconfigure(&|settings| {
28 settings.set_baud_rate(serial::Baud115200)?; 29 settings.set_baud_rate(serial::Baud115200)?;
29 settings.set_char_size(serial::Bits8); 30 settings.set_char_size(serial::Bits8);
@@ -39,7 +40,7 @@ fn saturate<T: SerialPort>(port: &mut T, sleep: bool) -> io::Result<()> {
39 40
40 port.write_all(&buf)?; 41 port.write_all(&buf)?;
41 42
42 if sleep { 43 if idles {
43 let micros = (random::<usize>() % 1000) as u64; 44 let micros = (random::<usize>() % 1000) as u64;
44 println!("Sleeping {}us", micros); 45 println!("Sleeping {}us", micros);
45 port.flush().unwrap(); 46 port.flush().unwrap();
@@ -49,4 +50,4 @@ fn saturate<T: SerialPort>(port: &mut T, sleep: bool) -> io::Result<()> {
49 written += len; 50 written += len;
50 println!("Written: {}", written); 51 println!("Written: {}", written);
51 } 52 }
52} \ No newline at end of file 53}