embassy/examples/stm32f7/src/bin/can.rs

70 lines
2.2 KiB
Rust
Raw Normal View History

2023-06-22 17:18:55 +02:00
#![no_std]
#![no_main]
#![feature(type_alias_impl_trait)]
2023-07-03 22:57:33 +02:00
use core::borrow::{Borrow, BorrowMut};
2023-06-22 17:18:55 +02:00
use cortex_m_rt::entry;
use defmt::*;
use embassy_executor::Spawner;
use embassy_stm32::bind_interrupts;
use embassy_stm32::can::bxcan::filter::Mask32;
2023-07-03 22:57:33 +02:00
use embassy_stm32::can::bxcan::{Data, Fifo, Frame, StandardId};
use embassy_stm32::can::{
2023-07-03 23:48:07 +02:00
Can, CanTx, Rx0InterruptHandler, Rx1InterruptHandler, SceInterruptHandler, 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;
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]
pub async fn send_can_message(tx: &'static mut CanTx<'static, 'static, CAN3>) {
loop {
let frame = Frame::new_data(unwrap!(StandardId::new(0 as _)), [0]);
tx.write(&frame).await;
embassy_time::Timer::after(embassy_time::Duration::from_secs(1)).await;
}
}
#[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-07-03 23:48:07 +02:00
let can: &'static mut Can<'static, CAN3> = static_cell::make_static!(Can::new(p.CAN3, p.PA8, p.PA15, Irqs));
can.as_mut()
2023-07-03 22:57:33 +02:00
.modify_filters()
.enable_bank(0, Fifo::Fifo0, Mask32::accept_all());
2023-06-22 17:18:55 +02:00
2023-07-03 23:48:07 +02:00
can.as_mut()
2023-06-22 17:18:55 +02:00
.modify_config()
.set_bit_timing(0x001c0001) // http://www.bittiming.can-wiki.info/
.set_loopback(true)
2023-06-22 17:18:55 +02:00
.enable();
2023-07-03 23:48:07 +02:00
let (tx, mut rx) = can.split();
2023-07-03 23:48:07 +02:00
let tx: &'static mut CanTx<'static, 'static, CAN3> = static_cell::make_static!(tx);
spawner.spawn(send_can_message(tx)).unwrap();
2023-06-22 17:18:55 +02:00
loop {
let frame = rx.read().await.unwrap();
println!("Received: {:?}", frame);
}
}