2023-06-22 17:18:55 +02:00
|
|
|
#![no_std]
|
|
|
|
#![no_main]
|
|
|
|
|
2024-03-05 20:51:05 +10:00
|
|
|
use core::num::{NonZeroU16, NonZeroU8};
|
|
|
|
|
2023-06-22 17:18:55 +02:00
|
|
|
use defmt::*;
|
|
|
|
use embassy_executor::Spawner;
|
2024-03-05 20:51:05 +10:00
|
|
|
use embassy_stm32::can::filter::Mask32;
|
2023-07-03 22:57:33 +02:00
|
|
|
use embassy_stm32::can::{
|
2024-03-05 20:51:05 +10:00
|
|
|
Can, CanTx, Fifo, Frame, Rx0InterruptHandler, Rx1InterruptHandler, SceInterruptHandler, StandardId,
|
|
|
|
TxInterruptHandler,
|
2023-07-03 22:57:33 +02:00
|
|
|
};
|
2023-06-22 17:18:55 +02:00
|
|
|
use embassy_stm32::gpio::{Input, Pull};
|
|
|
|
use embassy_stm32::peripherals::CAN3;
|
2024-03-05 20:51:05 +10:00
|
|
|
use embassy_stm32::{bind_interrupts, can};
|
2023-12-21 08:50:54 +01:00
|
|
|
use static_cell::StaticCell;
|
2023-06-22 17:18:55 +02:00
|
|
|
use {defmt_rtt as _, panic_probe as _};
|
|
|
|
|
|
|
|
bind_interrupts!(struct Irqs {
|
|
|
|
CAN3_RX0 => Rx0InterruptHandler<CAN3>;
|
|
|
|
CAN3_RX1 => Rx1InterruptHandler<CAN3>;
|
|
|
|
CAN3_SCE => SceInterruptHandler<CAN3>;
|
|
|
|
CAN3_TX => TxInterruptHandler<CAN3>;
|
|
|
|
});
|
|
|
|
|
|
|
|
#[embassy_executor::task]
|
2024-03-05 20:51:05 +10:00
|
|
|
pub async fn send_can_message(tx: &'static mut CanTx<'static, CAN3>) {
|
2023-06-22 17:18:55 +02:00
|
|
|
loop {
|
2024-03-12 21:19:06 +10:00
|
|
|
let frame = Frame::new_data(unwrap!(StandardId::new(0 as _)), &[0]).unwrap();
|
2023-06-22 17:18:55 +02:00
|
|
|
tx.write(&frame).await;
|
2023-10-15 00:57:25 +01:00
|
|
|
embassy_time::Timer::after_secs(1).await;
|
2023-06-22 17:18:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[embassy_executor::main]
|
|
|
|
async fn main(spawner: Spawner) {
|
|
|
|
info!("Hello World!");
|
|
|
|
|
|
|
|
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.PA15, Pull::Up);
|
|
|
|
core::mem::forget(rx_pin);
|
|
|
|
|
2023-12-21 08:50:54 +01:00
|
|
|
static CAN: StaticCell<Can<'static, CAN3>> = StaticCell::new();
|
|
|
|
let can = CAN.init(Can::new(p.CAN3, p.PA8, p.PA15, Irqs));
|
2024-04-02 01:29:52 +02:00
|
|
|
can.modify_filters().enable_bank(0, Fifo::Fifo0, Mask32::accept_all());
|
2023-06-22 17:18:55 +02:00
|
|
|
|
2024-04-02 01:29:52 +02:00
|
|
|
can.modify_config()
|
2024-03-05 20:51:05 +10:00
|
|
|
.set_bit_timing(can::util::NominalBitTiming {
|
|
|
|
prescaler: NonZeroU16::new(2).unwrap(),
|
|
|
|
seg1: NonZeroU8::new(13).unwrap(),
|
|
|
|
seg2: NonZeroU8::new(2).unwrap(),
|
|
|
|
sync_jump_width: NonZeroU8::new(1).unwrap(),
|
|
|
|
}) // http://www.bittiming.can-wiki.info/
|
2024-04-02 01:29:52 +02:00
|
|
|
.set_loopback(true);
|
|
|
|
|
|
|
|
can.enable().await;
|
2023-06-22 17:18:55 +02:00
|
|
|
|
2023-07-03 23:48:07 +02:00
|
|
|
let (tx, mut rx) = can.split();
|
2023-06-22 17:47:58 +02:00
|
|
|
|
2024-03-05 20:51:05 +10:00
|
|
|
static CAN_TX: StaticCell<CanTx<'static, CAN3>> = StaticCell::new();
|
2023-12-21 08:50:54 +01:00
|
|
|
let tx = CAN_TX.init(tx);
|
2023-07-03 23:48:07 +02:00
|
|
|
spawner.spawn(send_can_message(tx)).unwrap();
|
2023-06-22 17:18:55 +02:00
|
|
|
|
|
|
|
loop {
|
2023-07-25 12:07:09 +03:00
|
|
|
let envelope = rx.read().await.unwrap();
|
|
|
|
println!("Received: {:?}", envelope);
|
2023-06-22 17:18:55 +02:00
|
|
|
}
|
|
|
|
}
|