diff options
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/stm32f4/src/bin/can.rs | 18 | ||||
| -rw-r--r-- | examples/stm32f7/src/bin/can.rs | 66 |
2 files changed, 76 insertions, 8 deletions
diff --git a/examples/stm32f4/src/bin/can.rs b/examples/stm32f4/src/bin/can.rs index da8955053..08bed88db 100644 --- a/examples/stm32f4/src/bin/can.rs +++ b/examples/stm32f4/src/bin/can.rs | |||
| @@ -2,8 +2,8 @@ | |||
| 2 | #![no_main] | 2 | #![no_main] |
| 3 | #![feature(type_alias_impl_trait)] | 3 | #![feature(type_alias_impl_trait)] |
| 4 | 4 | ||
| 5 | use cortex_m_rt::entry; | ||
| 6 | use defmt::*; | 5 | use defmt::*; |
| 6 | use embassy_executor::Spawner; | ||
| 7 | use embassy_stm32::bind_interrupts; | 7 | use embassy_stm32::bind_interrupts; |
| 8 | use embassy_stm32::can::bxcan::filter::Mask32; | 8 | use embassy_stm32::can::bxcan::filter::Mask32; |
| 9 | use embassy_stm32::can::bxcan::{Fifo, Frame, StandardId}; | 9 | use embassy_stm32::can::bxcan::{Fifo, Frame, StandardId}; |
| @@ -19,8 +19,8 @@ bind_interrupts!(struct Irqs { | |||
| 19 | CAN1_TX => TxInterruptHandler<CAN1>; | 19 | CAN1_TX => TxInterruptHandler<CAN1>; |
| 20 | }); | 20 | }); |
| 21 | 21 | ||
| 22 | #[entry] | 22 | #[embassy_executor::main] |
| 23 | fn main() -> ! { | 23 | async fn main(_spawner: Spawner) { |
| 24 | info!("Hello World!"); | 24 | info!("Hello World!"); |
| 25 | 25 | ||
| 26 | let mut p = embassy_stm32::init(Default::default()); | 26 | let mut p = embassy_stm32::init(Default::default()); |
| @@ -34,9 +34,12 @@ fn main() -> ! { | |||
| 34 | 34 | ||
| 35 | let mut can = Can::new(p.CAN1, p.PA11, p.PA12, Irqs); | 35 | let mut can = Can::new(p.CAN1, p.PA11, p.PA12, Irqs); |
| 36 | 36 | ||
| 37 | can.modify_filters().enable_bank(0, Fifo::Fifo0, Mask32::accept_all()); | 37 | can.as_mut() |
| 38 | .modify_filters() | ||
| 39 | .enable_bank(0, Fifo::Fifo0, Mask32::accept_all()); | ||
| 38 | 40 | ||
| 39 | can.modify_config() | 41 | can.as_mut() |
| 42 | .modify_config() | ||
| 40 | .set_bit_timing(0x001c0003) // http://www.bittiming.can-wiki.info/ | 43 | .set_bit_timing(0x001c0003) // http://www.bittiming.can-wiki.info/ |
| 41 | .set_loopback(true) // Receive own frames | 44 | .set_loopback(true) // Receive own frames |
| 42 | .set_silent(true) | 45 | .set_silent(true) |
| @@ -45,9 +48,8 @@ fn main() -> ! { | |||
| 45 | let mut i: u8 = 0; | 48 | let mut i: u8 = 0; |
| 46 | loop { | 49 | loop { |
| 47 | let tx_frame = Frame::new_data(unwrap!(StandardId::new(i as _)), [i]); | 50 | let tx_frame = Frame::new_data(unwrap!(StandardId::new(i as _)), [i]); |
| 48 | unwrap!(nb::block!(can.transmit(&tx_frame))); | 51 | can.write(&tx_frame).await; |
| 49 | while !can.is_transmitter_idle() {} | 52 | let (_, rx_frame) = can.read().await.unwrap(); |
| 50 | let rx_frame = unwrap!(nb::block!(can.receive())); | ||
| 51 | info!("loopback frame {=u8}", unwrap!(rx_frame.data())[0]); | 53 | info!("loopback frame {=u8}", unwrap!(rx_frame.data())[0]); |
| 52 | i += 1; | 54 | i += 1; |
| 53 | } | 55 | } |
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 | } | ||
