Skip to content

Commit 804d20c

Browse files
committed
test: Add deticated overrun test
1 parent 96317d5 commit 804d20c

File tree

2 files changed

+24
-10
lines changed

2 files changed

+24
-10
lines changed

src/serial.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -786,7 +786,6 @@ where
786786
}
787787
}
788788

789-
790789
/// Configuring the UART to match each received character,
791790
/// with the configured one.
792791
///

testsuite/tests/uart.rs

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use hal::prelude::*;
1111
use hal::serial::Serial;
1212
use hal::serial::{
1313
config::{Config, Parity, StopBits},
14-
Error,
14+
Error, Event,
1515
};
1616
use hal::time::rate::Baud;
1717
use hal::{
@@ -36,7 +36,7 @@ struct State {
3636

3737
const BAUD_FAST: Baud = Baud(115_200);
3838
const BAUD_SLOW: Baud = Baud(57_600);
39-
const TEST_MSG: [u8; 8] = [0xD, 0xE, 0xA, 0xD, 0xB, 0xE, 0xE, 0xF];
39+
const TEST_MSG: [u8; 8] = [0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 0xaa, 0xbb];
4040

4141
fn test_test_msg_loopback(state: &mut State, config: impl Into<Config>) {
4242
let (usart, pins) = unwrap!(state.serial1.take()).free();
@@ -123,21 +123,36 @@ mod tests {
123123
#[test]
124124
fn send_receive_split(state: &mut super::State) {
125125
let (mut tx, mut rx) = unwrap!(state.serial1.take()).split();
126+
126127
for i in IntoIter::new(TEST_MSG) {
127128
defmt::unwrap!(nb::block!(tx.write(i)));
128129
let c = unwrap!(nb::block!(rx.read()));
129130
assert_eq!(c, i);
130131
}
131132

132-
// now provoke an overrun
133-
// send 5 u8 bytes, which do not fit in the 32 bit buffer
134-
for i in &TEST_MSG[..5] {
135-
defmt::unwrap!(nb::block!(tx.write(*i)));
136-
}
137-
let c = nb::block!(rx.read());
133+
state.serial1 = Some(Serial::join(tx, rx));
134+
}
135+
136+
#[test]
137+
fn test_overrun(state: &mut super::State) {
138+
let (usart, pins) = unwrap!(state.serial1.take()).free();
139+
let mut serial = Serial::new(usart, pins, 115200.Bd(), state.clocks, &mut state.apb2);
140+
// Provoke an overrun
141+
unwrap!(serial.bwrite_all(&TEST_MSG));
142+
143+
// Very important, to have a fix blocking point.
144+
// Waiting for the transfer to be finished - this implementation is
145+
// now independent of the choosen baudrate.
146+
unwrap!(serial.bflush());
147+
let c = nb::block!(serial.read());
138148
assert!(matches!(c, Err(Error::Overrun)));
149+
// Ensure that the receiver data reigster is empty.
150+
// Another nb::block!(serial.read()) should block forever.
151+
while serial.is_busy() {}
152+
assert!(!serial.is_event_triggered(Event::ReceiveDataRegisterNotEmpty));
153+
serial.clear_events();
139154

140-
state.serial1 = Some(Serial::join(tx, rx));
155+
state.serial1 = Some(serial);
141156
}
142157

143158
#[test]

0 commit comments

Comments
 (0)