diff options
| author | Dario Nieuwenhuis <[email protected]> | 2023-07-31 10:28:05 +0000 |
|---|---|---|
| committer | GitHub <[email protected]> | 2023-07-31 10:28:05 +0000 |
| commit | 2568c714c80267503554bcf090a2dbcd80b41e08 (patch) | |
| tree | 10725ca58f3b846c24b11ebf9414c2326bfbaff0 /tests | |
| parent | eff2d71f28b44ad71256cba645ce25e56ab88f9c (diff) | |
| parent | 83ab8e057a759ac76c8cddeec1ebdc28c4516b2b (diff) | |
Merge pull request #1687 from chemicstry/bxcan_timestamp
stm32/can: implement proper RX timestamps
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/stm32/src/bin/can.rs | 27 |
1 files changed, 22 insertions, 5 deletions
diff --git a/tests/stm32/src/bin/can.rs b/tests/stm32/src/bin/can.rs index 8bdd3c24f..8737ca8ee 100644 --- a/tests/stm32/src/bin/can.rs +++ b/tests/stm32/src/bin/can.rs | |||
| @@ -7,6 +7,7 @@ | |||
| 7 | #[path = "../common.rs"] | 7 | #[path = "../common.rs"] |
| 8 | mod common; | 8 | mod common; |
| 9 | use common::*; | 9 | use common::*; |
| 10 | use defmt::assert; | ||
| 10 | use embassy_executor::Spawner; | 11 | use embassy_executor::Spawner; |
| 11 | use embassy_stm32::bind_interrupts; | 12 | use embassy_stm32::bind_interrupts; |
| 12 | use embassy_stm32::can::bxcan::filter::Mask32; | 13 | use embassy_stm32::can::bxcan::filter::Mask32; |
| @@ -14,6 +15,7 @@ use embassy_stm32::can::bxcan::{Fifo, Frame, StandardId}; | |||
| 14 | use embassy_stm32::can::{Can, Rx0InterruptHandler, Rx1InterruptHandler, SceInterruptHandler, TxInterruptHandler}; | 15 | use embassy_stm32::can::{Can, Rx0InterruptHandler, Rx1InterruptHandler, SceInterruptHandler, TxInterruptHandler}; |
| 15 | use embassy_stm32::gpio::{Input, Pull}; | 16 | use embassy_stm32::gpio::{Input, Pull}; |
| 16 | use embassy_stm32::peripherals::CAN1; | 17 | use embassy_stm32::peripherals::CAN1; |
| 18 | use embassy_time::{Duration, Instant}; | ||
| 17 | use {defmt_rtt as _, panic_probe as _}; | 19 | use {defmt_rtt as _, panic_probe as _}; |
| 18 | 20 | ||
| 19 | bind_interrupts!(struct Irqs { | 21 | bind_interrupts!(struct Irqs { |
| @@ -62,13 +64,28 @@ async fn main(_spawner: Spawner) { | |||
| 62 | let tx_frame = Frame::new_data(unwrap!(StandardId::new(i as _)), [i]); | 64 | let tx_frame = Frame::new_data(unwrap!(StandardId::new(i as _)), [i]); |
| 63 | 65 | ||
| 64 | info!("Transmitting frame..."); | 66 | info!("Transmitting frame..."); |
| 67 | let tx_ts = Instant::now(); | ||
| 65 | can.write(&tx_frame).await; | 68 | can.write(&tx_frame).await; |
| 66 | 69 | ||
| 67 | info!("Receiving frame..."); | 70 | let envelope = can.read().await.unwrap(); |
| 68 | let (time, rx_frame) = can.read().await.unwrap(); | 71 | info!("Frame received!"); |
| 69 | 72 | ||
| 70 | info!("loopback time {}", time); | 73 | info!("loopback time {}", envelope.ts); |
| 71 | info!("loopback frame {=u8}", rx_frame.data().unwrap()[0]); | 74 | info!("loopback frame {=u8}", envelope.frame.data().unwrap()[0]); |
| 75 | |||
| 76 | let latency = envelope.ts.saturating_duration_since(tx_ts); | ||
| 77 | info!("loopback latency {} us", latency.as_micros()); | ||
| 78 | |||
| 79 | // Theoretical minimum latency is 55us, actual is usually ~80us | ||
| 80 | const MIN_LATENCY: Duration = Duration::from_micros(50); | ||
| 81 | const MAX_LATENCY: Duration = Duration::from_micros(150); | ||
| 82 | assert!( | ||
| 83 | MIN_LATENCY < latency && latency < MAX_LATENCY, | ||
| 84 | "{} < {} < {}", | ||
| 85 | MIN_LATENCY, | ||
| 86 | latency, | ||
| 87 | MAX_LATENCY | ||
| 88 | ); | ||
| 72 | 89 | ||
| 73 | i += 1; | 90 | i += 1; |
| 74 | if i > 10 { | 91 | if i > 10 { |
