diff options
| author | Philipp Scheff <[email protected]> | 2023-06-22 17:18:55 +0200 |
|---|---|---|
| committer | Philipp Scheff <[email protected]> | 2023-06-22 17:18:55 +0200 |
| commit | f47a148f51c7c0de52a1c202fefe6f863c669854 (patch) | |
| tree | 22ccb6bcfd96d9cfb05d1c53433ae2d59bc079c0 | |
| parent | 5ecf9ec7bcd785555563d8f5006fd24414bbc17f (diff) | |
add stm32f7 can example
| -rw-r--r-- | examples/stm32f7/src/bin/can.rs | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/examples/stm32f7/src/bin/can.rs b/examples/stm32f7/src/bin/can.rs new file mode 100644 index 000000000..cf027cc3e --- /dev/null +++ b/examples/stm32f7/src/bin/can.rs | |||
| @@ -0,0 +1,63 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | #![feature(type_alias_impl_trait)] | ||
| 4 | |||
| 5 | use core::borrow::BorrowMut; | ||
| 6 | use core::borrow::Borrow; | ||
| 7 | use cortex_m_rt::entry; | ||
| 8 | use defmt::*; | ||
| 9 | use embassy_executor::Spawner; | ||
| 10 | use embassy_stm32::bind_interrupts; | ||
| 11 | use embassy_stm32::can::bxcan::filter::Mask32; | ||
| 12 | use embassy_stm32::can::bxcan::{Fifo, Frame, StandardId, Data}; | ||
| 13 | use embassy_stm32::can::{Can,CanTx,CanRx, Rx0InterruptHandler, Rx1InterruptHandler, SceInterruptHandler, TxInterruptHandler}; | ||
| 14 | use embassy_stm32::gpio::{Input, Pull}; | ||
| 15 | use embassy_stm32::peripherals::CAN3; | ||
| 16 | use {defmt_rtt as _, panic_probe as _}; | ||
| 17 | |||
| 18 | bind_interrupts!(struct Irqs { | ||
| 19 | CAN3_RX0 => Rx0InterruptHandler<CAN3>; | ||
| 20 | CAN3_RX1 => Rx1InterruptHandler<CAN3>; | ||
| 21 | CAN3_SCE => SceInterruptHandler<CAN3>; | ||
| 22 | CAN3_TX => TxInterruptHandler<CAN3>; | ||
| 23 | }); | ||
| 24 | |||
| 25 | #[embassy_executor::task] | ||
| 26 | pub async fn send_can_message(tx: &'static mut CanTx<'static, 'static, CAN3>) { | ||
| 27 | loop { | ||
| 28 | let frame = Frame::new_data(unwrap!(StandardId::new(0 as _)), [0]); | ||
| 29 | tx.write(&frame).await; | ||
| 30 | embassy_time::Timer::after(embassy_time::Duration::from_secs(1)).await; | ||
| 31 | } | ||
| 32 | } | ||
| 33 | |||
| 34 | #[embassy_executor::main] | ||
| 35 | async fn main(spawner: Spawner) { | ||
| 36 | info!("Hello World!"); | ||
| 37 | |||
| 38 | let mut p = embassy_stm32::init(Default::default()); | ||
| 39 | |||
| 40 | // The next two lines are a workaround for testing without transceiver. | ||
| 41 | // To synchronise to the bus the RX input needs to see a high level. | ||
| 42 | // Use `mem::forget()` to release the borrow on the pin but keep the | ||
| 43 | // pull-up resistor enabled. | ||
| 44 | let rx_pin = Input::new(&mut p.PA15, Pull::Up); | ||
| 45 | core::mem::forget(rx_pin); | ||
| 46 | |||
| 47 | let CAN: &'static mut Can<'static,CAN3> = static_cell::make_static!(Can::new(p.CAN3, p.PA8, p.PA15, Irqs)); | ||
| 48 | CAN.can.borrow_mut().modify_filters().enable_bank(0, Fifo::Fifo0, Mask32::accept_all()); | ||
| 49 | |||
| 50 | CAN.can.borrow_mut() | ||
| 51 | .modify_config() | ||
| 52 | .set_bit_timing(0x001c0001) // http://www.bittiming.can-wiki.info/ | ||
| 53 | .enable(); | ||
| 54 | |||
| 55 | let (tx, mut rx) = CAN.split(); | ||
| 56 | let TX: &'static mut CanTx<'static, 'static, CAN3> = static_cell::make_static!(tx); | ||
| 57 | spawner.spawn(send_can_message(TX)).unwrap(); | ||
| 58 | |||
| 59 | loop { | ||
| 60 | let frame = rx.read().await.unwrap(); | ||
| 61 | println!("Received: {:?}", frame); | ||
| 62 | } | ||
| 63 | } | ||
