diff options
| author | Fabian Wolter <[email protected]> | 2025-08-14 19:41:00 +0200 |
|---|---|---|
| committer | Fabian Wolter <[email protected]> | 2025-08-14 19:41:00 +0200 |
| commit | 4c556dcbf23c0024766e78d5839ce3f717bbc0e9 (patch) | |
| tree | 17c28db6910cca7fa013da124bc1074710caef37 /tests/stm32/src/can_common.rs | |
| parent | f689cd10000d3c5d608907d52c686abcc6c0971f (diff) | |
stm32 tests: move can_common out of the bin folder
Diffstat (limited to 'tests/stm32/src/can_common.rs')
| -rw-r--r-- | tests/stm32/src/can_common.rs | 109 |
1 files changed, 109 insertions, 0 deletions
diff --git a/tests/stm32/src/can_common.rs b/tests/stm32/src/can_common.rs new file mode 100644 index 000000000..4e1740ad5 --- /dev/null +++ b/tests/stm32/src/can_common.rs | |||
| @@ -0,0 +1,109 @@ | |||
| 1 | use defmt::{assert, *}; | ||
| 2 | use embassy_stm32::can; | ||
| 3 | use embassy_time::{Duration, Instant}; | ||
| 4 | |||
| 5 | #[derive(Clone, Copy, Debug)] | ||
| 6 | pub struct TestOptions { | ||
| 7 | pub max_latency: Duration, | ||
| 8 | pub max_buffered: u8, | ||
| 9 | } | ||
| 10 | |||
| 11 | pub async fn run_can_tests<'d>(can: &mut can::Can<'d>, options: &TestOptions) { | ||
| 12 | //pub async fn run_can_tests<'d, T: can::Instance>(can: &mut can::Can<'d, T>, options: &TestOptions) { | ||
| 13 | let mut i: u8 = 0; | ||
| 14 | loop { | ||
| 15 | //let tx_frame = can::frame::Frame::new_standard(0x123, &[i, 0x12 as u8, 0x34 as u8, 0x56 as u8, 0x78 as u8, 0x9A as u8, 0xBC as u8 ]).unwrap(); | ||
| 16 | let tx_frame = can::frame::Frame::new_standard(0x123, &[i; 1]).unwrap(); | ||
| 17 | |||
| 18 | //info!("Transmitting frame..."); | ||
| 19 | let tx_ts = Instant::now(); | ||
| 20 | can.write(&tx_frame).await; | ||
| 21 | |||
| 22 | let (frame, timestamp) = can.read().await.unwrap().parts(); | ||
| 23 | //info!("Frame received!"); | ||
| 24 | |||
| 25 | // Check data. | ||
| 26 | assert!(i == frame.data()[0], "{} == {}", i, frame.data()[0]); | ||
| 27 | |||
| 28 | //info!("loopback time {}", timestamp); | ||
| 29 | //info!("loopback frame {=u8}", frame.data()[0]); | ||
| 30 | let latency = timestamp.saturating_duration_since(tx_ts); | ||
| 31 | info!("loopback latency {} us", latency.as_micros()); | ||
| 32 | |||
| 33 | // Theoretical minimum latency is 55us, actual is usually ~80us | ||
| 34 | const MIN_LATENCY: Duration = Duration::from_micros(50); | ||
| 35 | // Was failing at 150 but we are not getting a real time stamp. I'm not | ||
| 36 | // sure if there are other delays | ||
| 37 | assert!( | ||
| 38 | MIN_LATENCY <= latency && latency <= options.max_latency, | ||
| 39 | "{} <= {} <= {}", | ||
| 40 | MIN_LATENCY, | ||
| 41 | latency, | ||
| 42 | options.max_latency | ||
| 43 | ); | ||
| 44 | |||
| 45 | i += 1; | ||
| 46 | if i > 5 { | ||
| 47 | break; | ||
| 48 | } | ||
| 49 | } | ||
| 50 | |||
| 51 | // Below here, check that we can receive from both FIFO0 and FIFO1 | ||
| 52 | // Above we configured FIFO1 for extended ID packets. There are only 3 slots | ||
| 53 | // in each FIFO so make sure we write enough to fill them both up before reading. | ||
| 54 | for i in 0..options.max_buffered { | ||
| 55 | // Try filling up the RX FIFO0 buffers | ||
| 56 | //let tx_frame = if 0 != (i & 0x01) { | ||
| 57 | let tx_frame = if i < options.max_buffered / 2 { | ||
| 58 | info!("Transmitting standard frame {}", i); | ||
| 59 | can::frame::Frame::new_standard(0x123, &[i; 1]).unwrap() | ||
| 60 | } else { | ||
| 61 | info!("Transmitting extended frame {}", i); | ||
| 62 | can::frame::Frame::new_extended(0x1232344, &[i; 1]).unwrap() | ||
| 63 | }; | ||
| 64 | can.write(&tx_frame).await; | ||
| 65 | } | ||
| 66 | |||
| 67 | // Try and receive all 6 packets | ||
| 68 | for _i in 0..options.max_buffered { | ||
| 69 | let (frame, _ts) = can.read().await.unwrap().parts(); | ||
| 70 | match frame.id() { | ||
| 71 | embedded_can::Id::Extended(_id) => { | ||
| 72 | info!("Extended received! {}", frame.data()[0]); | ||
| 73 | //info!("Extended received! {:x} {} {}", id.as_raw(), frame.data()[0], i); | ||
| 74 | } | ||
| 75 | embedded_can::Id::Standard(_id) => { | ||
| 76 | info!("Standard received! {}", frame.data()[0]); | ||
| 77 | //info!("Standard received! {:x} {} {}", id.as_raw(), frame.data()[0], i); | ||
| 78 | } | ||
| 79 | } | ||
| 80 | } | ||
| 81 | } | ||
| 82 | |||
| 83 | pub async fn run_split_can_tests<'d>(tx: &mut can::CanTx<'d>, rx: &mut can::CanRx<'d>, options: &TestOptions) { | ||
| 84 | for i in 0..options.max_buffered { | ||
| 85 | // Try filling up the RX FIFO0 buffers | ||
| 86 | //let tx_frame = if 0 != (i & 0x01) { | ||
| 87 | let tx_frame = if i < options.max_buffered / 2 { | ||
| 88 | info!("Transmitting standard frame {}", i); | ||
| 89 | can::frame::Frame::new_standard(0x123, &[i; 1]).unwrap() | ||
| 90 | } else { | ||
| 91 | info!("Transmitting extended frame {}", i); | ||
| 92 | can::frame::Frame::new_extended(0x1232344, &[i; 1]).unwrap() | ||
| 93 | }; | ||
| 94 | tx.write(&tx_frame).await; | ||
| 95 | } | ||
| 96 | |||
| 97 | // Try and receive all 6 packets | ||
| 98 | for _i in 0..options.max_buffered { | ||
| 99 | let (frame, _ts) = rx.read().await.unwrap().parts(); | ||
| 100 | match frame.id() { | ||
| 101 | embedded_can::Id::Extended(_id) => { | ||
| 102 | info!("Extended received! {}", frame.data()[0]); | ||
| 103 | } | ||
| 104 | embedded_can::Id::Standard(_id) => { | ||
| 105 | info!("Standard received! {}", frame.data()[0]); | ||
| 106 | } | ||
| 107 | } | ||
| 108 | } | ||
| 109 | } | ||
