2023-05-30 21:14:25 -05:00
|
|
|
#![no_std]
|
|
|
|
#![no_main]
|
|
|
|
|
|
|
|
// required-features: can
|
|
|
|
|
2023-06-19 16:11:01 -05:00
|
|
|
#[path = "../common.rs"]
|
|
|
|
mod common;
|
|
|
|
use common::*;
|
2023-05-30 21:14:25 -05:00
|
|
|
use embassy_executor::Spawner;
|
|
|
|
use embassy_stm32::bind_interrupts;
|
2024-04-02 01:29:52 +02:00
|
|
|
use embassy_stm32::can::filter::Mask32;
|
2024-05-26 19:54:46 +10:00
|
|
|
use embassy_stm32::can::{Fifo, Rx0InterruptHandler, Rx1InterruptHandler, SceInterruptHandler, TxInterruptHandler};
|
2023-05-30 21:14:25 -05:00
|
|
|
use embassy_stm32::gpio::{Input, Pull};
|
|
|
|
use embassy_stm32::peripherals::CAN1;
|
2024-03-27 19:43:19 +10:00
|
|
|
use embassy_time::Duration;
|
2023-05-30 21:14:25 -05:00
|
|
|
use {defmt_rtt as _, panic_probe as _};
|
|
|
|
|
2024-03-27 19:43:19 +10:00
|
|
|
mod can_common;
|
|
|
|
use can_common::*;
|
|
|
|
|
2024-05-26 19:54:46 +10:00
|
|
|
type Can<'d> = embassy_stm32::can::Can<'d, embassy_stm32::peripherals::CAN1>;
|
2024-05-30 21:23:12 +10:00
|
|
|
type CanTx<'d> = embassy_stm32::can::CanTx<'d>;
|
|
|
|
type CanRx<'d> = embassy_stm32::can::CanRx<'d>;
|
2024-05-26 19:54:46 +10:00
|
|
|
|
2023-05-30 21:14:25 -05:00
|
|
|
bind_interrupts!(struct Irqs {
|
|
|
|
CAN1_RX0 => Rx0InterruptHandler<CAN1>;
|
|
|
|
CAN1_RX1 => Rx1InterruptHandler<CAN1>;
|
|
|
|
CAN1_SCE => SceInterruptHandler<CAN1>;
|
|
|
|
CAN1_TX => TxInterruptHandler<CAN1>;
|
|
|
|
});
|
|
|
|
|
|
|
|
#[embassy_executor::main]
|
|
|
|
async fn main(_spawner: Spawner) {
|
2023-11-27 00:36:04 +01:00
|
|
|
let p = embassy_stm32::init(config());
|
2023-05-30 21:14:25 -05:00
|
|
|
info!("Hello World!");
|
|
|
|
|
2024-03-27 19:43:19 +10:00
|
|
|
let options = TestOptions {
|
|
|
|
max_latency: Duration::from_micros(1200),
|
|
|
|
max_buffered: 2,
|
|
|
|
};
|
|
|
|
|
2023-11-27 00:36:04 +01:00
|
|
|
let can = peri!(p, CAN);
|
|
|
|
let tx = peri!(p, CAN_TX);
|
|
|
|
let mut rx = peri!(p, CAN_RX);
|
2023-05-30 21:14:25 -05:00
|
|
|
|
|
|
|
// 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.
|
2023-11-27 00:36:04 +01:00
|
|
|
let rx_pin = Input::new(&mut rx, Pull::Up);
|
2023-05-30 21:14:25 -05:00
|
|
|
core::mem::forget(rx_pin);
|
|
|
|
|
2023-11-27 00:36:04 +01:00
|
|
|
let mut can = Can::new(can, rx, tx, Irqs);
|
2023-05-30 21:14:25 -05:00
|
|
|
|
|
|
|
info!("Configuring can...");
|
|
|
|
|
2024-04-02 01:29:52 +02:00
|
|
|
can.modify_filters().enable_bank(0, Fifo::Fifo0, Mask32::accept_all());
|
2023-05-30 21:14:25 -05:00
|
|
|
|
2024-04-02 01:29:52 +02:00
|
|
|
can.modify_config()
|
2023-05-30 21:14:25 -05:00
|
|
|
.set_loopback(true) // Receive own frames
|
|
|
|
.set_silent(true)
|
|
|
|
// .set_bit_timing(0x001c0003)
|
2024-04-02 01:29:52 +02:00
|
|
|
.set_bitrate(1_000_000);
|
|
|
|
|
|
|
|
can.enable().await;
|
2023-05-30 21:14:25 -05:00
|
|
|
|
|
|
|
info!("Can configured");
|
|
|
|
|
2024-03-27 19:43:19 +10:00
|
|
|
run_can_tests(&mut can, &options).await;
|
2023-05-30 21:14:25 -05:00
|
|
|
|
2024-03-27 19:43:19 +10:00
|
|
|
// Test again with a split
|
|
|
|
let (mut tx, mut rx) = can.split();
|
|
|
|
run_split_can_tests(&mut tx, &mut rx, &options).await;
|
2023-05-30 21:14:25 -05:00
|
|
|
|
|
|
|
info!("Test OK");
|
2024-03-27 19:43:19 +10:00
|
|
|
|
2023-05-30 21:14:25 -05:00
|
|
|
cortex_m::asm::bkpt();
|
|
|
|
}
|