From 62ab0bf2e75c560aa255ab51aab1f5ebf591ba97 Mon Sep 17 00:00:00 2001 From: chemicstry Date: Tue, 25 Jul 2023 12:07:09 +0300 Subject: stm32/can: implement proper RX timestamps --- tests/stm32/src/bin/can.rs | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) (limited to 'tests') diff --git a/tests/stm32/src/bin/can.rs b/tests/stm32/src/bin/can.rs index 8bdd3c24f..93253ab84 100644 --- a/tests/stm32/src/bin/can.rs +++ b/tests/stm32/src/bin/can.rs @@ -7,6 +7,7 @@ #[path = "../common.rs"] mod common; use common::*; +use defmt::assert; use embassy_executor::Spawner; use embassy_stm32::bind_interrupts; use embassy_stm32::can::bxcan::filter::Mask32; @@ -14,6 +15,7 @@ use embassy_stm32::can::bxcan::{Fifo, Frame, StandardId}; use embassy_stm32::can::{Can, Rx0InterruptHandler, Rx1InterruptHandler, SceInterruptHandler, TxInterruptHandler}; use embassy_stm32::gpio::{Input, Pull}; use embassy_stm32::peripherals::CAN1; +use embassy_time::{Duration, Instant}; use {defmt_rtt as _, panic_probe as _}; bind_interrupts!(struct Irqs { @@ -62,13 +64,18 @@ async fn main(_spawner: Spawner) { let tx_frame = Frame::new_data(unwrap!(StandardId::new(i as _)), [i]); info!("Transmitting frame..."); + let tx_ts = Instant::now(); can.write(&tx_frame).await; info!("Receiving frame..."); - let (time, rx_frame) = can.read().await.unwrap(); + let envelope = can.read().await.unwrap(); - info!("loopback time {}", time); - info!("loopback frame {=u8}", rx_frame.data().unwrap()[0]); + info!("loopback time {}", envelope.ts); + info!("loopback frame {=u8}", envelope.frame.data().unwrap()[0]); + + // Theoretical minimum latency is 55us, actual is usually ~80us + let latency = envelope.ts.saturating_duration_since(tx_ts); + assert!(Duration::from_micros(50) < latency && latency < Duration::from_micros(100)); i += 1; if i > 10 { -- cgit From 83ab8e057a759ac76c8cddeec1ebdc28c4516b2b Mon Sep 17 00:00:00 2001 From: chemicstry Date: Mon, 31 Jul 2023 13:24:10 +0300 Subject: stm32/can: Fix latency measurement in tests --- tests/stm32/src/bin/can.rs | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) (limited to 'tests') diff --git a/tests/stm32/src/bin/can.rs b/tests/stm32/src/bin/can.rs index 93253ab84..8737ca8ee 100644 --- a/tests/stm32/src/bin/can.rs +++ b/tests/stm32/src/bin/can.rs @@ -67,15 +67,25 @@ async fn main(_spawner: Spawner) { let tx_ts = Instant::now(); can.write(&tx_frame).await; - info!("Receiving frame..."); let envelope = can.read().await.unwrap(); + info!("Frame received!"); info!("loopback time {}", envelope.ts); info!("loopback frame {=u8}", envelope.frame.data().unwrap()[0]); - // Theoretical minimum latency is 55us, actual is usually ~80us let latency = envelope.ts.saturating_duration_since(tx_ts); - assert!(Duration::from_micros(50) < latency && latency < Duration::from_micros(100)); + info!("loopback latency {} us", latency.as_micros()); + + // Theoretical minimum latency is 55us, actual is usually ~80us + const MIN_LATENCY: Duration = Duration::from_micros(50); + const MAX_LATENCY: Duration = Duration::from_micros(150); + assert!( + MIN_LATENCY < latency && latency < MAX_LATENCY, + "{} < {} < {}", + MIN_LATENCY, + latency, + MAX_LATENCY + ); i += 1; if i > 10 { -- cgit