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