2021-08-06 00:08:24 +02:00
|
|
|
#![no_std]
|
|
|
|
#![no_main]
|
|
|
|
#![feature(type_alias_impl_trait)]
|
|
|
|
|
2022-04-02 04:35:06 +02:00
|
|
|
use defmt::*;
|
2023-07-04 00:21:08 +02:00
|
|
|
use embassy_executor::Spawner;
|
2023-06-19 23:05:59 +02:00
|
|
|
use embassy_stm32::bind_interrupts;
|
2022-02-28 22:42:45 +01:00
|
|
|
use embassy_stm32::can::bxcan::filter::Mask32;
|
2022-06-23 01:01:23 +02:00
|
|
|
use embassy_stm32::can::bxcan::{Fifo, Frame, StandardId};
|
2023-06-19 23:05:59 +02:00
|
|
|
use embassy_stm32::can::{Can, Rx0InterruptHandler, Rx1InterruptHandler, SceInterruptHandler, TxInterruptHandler};
|
2021-08-09 15:59:05 +02:00
|
|
|
use embassy_stm32::gpio::{Input, Pull};
|
2023-06-19 23:05:59 +02:00
|
|
|
use embassy_stm32::peripherals::CAN1;
|
2023-07-25 11:07:09 +02:00
|
|
|
use embassy_time::Instant;
|
2022-06-12 22:15:44 +02:00
|
|
|
use {defmt_rtt as _, panic_probe as _};
|
2021-08-06 00:08:24 +02:00
|
|
|
|
2023-06-19 23:05:59 +02:00
|
|
|
bind_interrupts!(struct Irqs {
|
|
|
|
CAN1_RX0 => Rx0InterruptHandler<CAN1>;
|
|
|
|
CAN1_RX1 => Rx1InterruptHandler<CAN1>;
|
|
|
|
CAN1_SCE => SceInterruptHandler<CAN1>;
|
|
|
|
CAN1_TX => TxInterruptHandler<CAN1>;
|
|
|
|
});
|
|
|
|
|
2023-07-04 00:21:08 +02:00
|
|
|
#[embassy_executor::main]
|
|
|
|
async fn main(_spawner: Spawner) {
|
2021-08-06 00:08:24 +02:00
|
|
|
info!("Hello World!");
|
|
|
|
|
2021-08-09 15:59:05 +02:00
|
|
|
let mut p = embassy_stm32::init(Default::default());
|
|
|
|
|
|
|
|
// The next two lines are a workaround for testing without transceiver.
|
|
|
|
// To synchronise to the bus the RX input needs to see a high level.
|
|
|
|
// Use `mem::forget()` to release the borrow on the pin but keep the
|
|
|
|
// pull-up resistor enabled.
|
|
|
|
let rx_pin = Input::new(&mut p.PA11, Pull::Up);
|
|
|
|
core::mem::forget(rx_pin);
|
2021-08-06 00:08:24 +02:00
|
|
|
|
2023-06-19 23:05:59 +02:00
|
|
|
let mut can = Can::new(p.CAN1, p.PA11, p.PA12, Irqs);
|
2021-08-06 00:08:24 +02:00
|
|
|
|
2023-07-04 00:21:08 +02:00
|
|
|
can.as_mut()
|
|
|
|
.modify_filters()
|
|
|
|
.enable_bank(0, Fifo::Fifo0, Mask32::accept_all());
|
2021-11-15 18:00:26 +01:00
|
|
|
|
2023-07-04 00:21:08 +02:00
|
|
|
can.as_mut()
|
|
|
|
.modify_config()
|
2021-08-09 15:59:05 +02:00
|
|
|
.set_loopback(true) // Receive own frames
|
2021-11-15 18:00:26 +01:00
|
|
|
.set_silent(true)
|
2023-07-24 16:46:03 +02:00
|
|
|
.leave_disabled();
|
|
|
|
|
|
|
|
can.set_bitrate(1_000_000);
|
|
|
|
|
|
|
|
can.enable().await;
|
2021-08-06 00:08:24 +02:00
|
|
|
|
|
|
|
let mut i: u8 = 0;
|
|
|
|
loop {
|
|
|
|
let tx_frame = Frame::new_data(unwrap!(StandardId::new(i as _)), [i]);
|
2023-07-25 11:07:09 +02:00
|
|
|
let tx_ts = Instant::now();
|
2023-07-04 00:21:08 +02:00
|
|
|
can.write(&tx_frame).await;
|
2023-07-25 11:07:09 +02:00
|
|
|
|
|
|
|
let envelope = can.read().await.unwrap();
|
|
|
|
|
|
|
|
// We can measure loopback latency by using receive timestamp in the `Envelope`.
|
|
|
|
// Our frame is ~55 bits long (exlcuding bit stuffing), so at 1mbps loopback delay is at least 55 us.
|
|
|
|
// When measured with `tick-hz-1_000_000` actual latency is 80~83 us, giving a combined hardware and software
|
|
|
|
// overhead of ~25 us. Note that CPU frequency can greatly affect the result.
|
|
|
|
let latency = envelope.ts.saturating_duration_since(tx_ts);
|
|
|
|
|
|
|
|
info!(
|
|
|
|
"loopback frame {=u8}, latency: {} us",
|
|
|
|
unwrap!(envelope.frame.data())[0],
|
|
|
|
latency.as_micros()
|
|
|
|
);
|
2021-08-06 00:08:24 +02:00
|
|
|
i += 1;
|
|
|
|
}
|
|
|
|
}
|