Support overflow detection for more than one ring-period

This commit is contained in:
Rasmus Melchior Jacobsen
2023-04-27 10:48:38 +02:00
committed by Dario Nieuwenhuis
parent 4ea6662e55
commit fc268df6f5
5 changed files with 216 additions and 181 deletions

View File

@ -56,6 +56,7 @@ mod board {
}
const ONE_BYTE_DURATION_US: u32 = 9_000_000 / 115200;
const DMA_BUF_SIZE: usize = 64;
#[embassy_executor::main]
async fn main(spawner: Spawner) {
@ -114,7 +115,7 @@ async fn main(spawner: Spawner) {
let usart = Uart::new(usart, rx, tx, irq, tx_dma, rx_dma, config);
let (tx, rx) = usart.split();
static mut DMA_BUF: [u8; 64] = [0; 64];
static mut DMA_BUF: [u8; DMA_BUF_SIZE] = [0; DMA_BUF_SIZE];
let dma_buf = unsafe { DMA_BUF.as_mut() };
let rx = rx.into_ring_buffered(dma_buf);
@ -159,7 +160,14 @@ async fn receive_task(mut rx: RingBufferedUartRx<'static, board::Uart, board::Rx
loop {
let mut buf = [0; 100];
let max_len = 1 + (rng.next_u32() as usize % (buf.len() - 1));
let received = rx.read(&mut buf[..max_len]).await.unwrap();
let received = match rx.read(&mut buf[..max_len]).await {
Ok(r) => r,
Err(e) => {
error!("Test fail! read error: {:?}", e);
cortex_m::asm::bkpt();
return;
}
};
if expected.is_none() {
info!("Test started");
@ -176,8 +184,11 @@ async fn receive_task(mut rx: RingBufferedUartRx<'static, board::Uart, board::Rx
}
if received < max_len {
let byte_count = rng.next_u32() % 64;
Timer::after(Duration::from_micros((byte_count * ONE_BYTE_DURATION_US) as _)).await;
let byte_count = rng.next_u32() % (DMA_BUF_SIZE as u32);
let random_delay_us = (byte_count * ONE_BYTE_DURATION_US) as u64;
if random_delay_us > 200 {
Timer::after(Duration::from_micros(random_delay_us - 200)).await;
}
}
i += 1;

View File

@ -1,18 +1,19 @@
use std::path::Path;
use std::time::Duration;
use std::{env, io, thread};
use std::{env, io, process, thread};
use rand::random;
use serial::SerialPort;
pub fn main() {
if let Some(port_name) = env::args().nth(1) {
let sleep = env::args().position(|x| x == "--sleep").is_some();
let idles = env::args().position(|x| x == "--idles").is_some();
println!("Saturating port {:?} with 115200 8N1", port_name);
println!("Sleep: {}", sleep);
println!("Idles: {}", idles);
println!("Process ID: {}", process::id());
let mut port = serial::open(&port_name).unwrap();
if saturate(&mut port, sleep).is_err() {
if saturate(&mut port, idles).is_err() {
eprintln!("Unable to saturate port");
}
} else {
@ -23,7 +24,7 @@ pub fn main() {
}
}
fn saturate<T: SerialPort>(port: &mut T, sleep: bool) -> io::Result<()> {
fn saturate<T: SerialPort>(port: &mut T, idles: bool) -> io::Result<()> {
port.reconfigure(&|settings| {
settings.set_baud_rate(serial::Baud115200)?;
settings.set_char_size(serial::Bits8);
@ -39,7 +40,7 @@ fn saturate<T: SerialPort>(port: &mut T, sleep: bool) -> io::Result<()> {
port.write_all(&buf)?;
if sleep {
if idles {
let micros = (random::<usize>() % 1000) as u64;
println!("Sleeping {}us", micros);
port.flush().unwrap();
@ -49,4 +50,4 @@ fn saturate<T: SerialPort>(port: &mut T, sleep: bool) -> io::Result<()> {
written += len;
println!("Written: {}", written);
}
}
}