diff options
| author | Dario Nieuwenhuis <[email protected]> | 2023-12-11 11:36:47 +0000 |
|---|---|---|
| committer | GitHub <[email protected]> | 2023-12-11 11:36:47 +0000 |
| commit | 14f41a71b6ea9dedb4ee5b9c741fe10575772c7d (patch) | |
| tree | 4bee74836e4ae8cd6f6668ec2bc51a9a6def7302 | |
| parent | 2d2bd679ee2d3247c94dd66fa8ec40ffa07090ab (diff) | |
| parent | 3626deecaab1ed5b2f2c97ef49f6dbf058ad068a (diff) | |
Merge pull request #2276 from cschuhen/stm32f1-can-example
Add example for using CAN with STM32F103 (BluePill) with a real CAN
| -rw-r--r-- | examples/stm32f1/src/bin/can.rs | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/examples/stm32f1/src/bin/can.rs b/examples/stm32f1/src/bin/can.rs new file mode 100644 index 000000000..a5ce819d4 --- /dev/null +++ b/examples/stm32f1/src/bin/can.rs | |||
| @@ -0,0 +1,67 @@ | |||
| 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::can::bxcan::filter::Mask32; | ||
| 8 | use embassy_stm32::can::bxcan::{Fifo, Frame, Id, StandardId}; | ||
| 9 | use embassy_stm32::can::{Can, Rx0InterruptHandler, Rx1InterruptHandler, SceInterruptHandler, TxInterruptHandler}; | ||
| 10 | use embassy_stm32::peripherals::CAN; | ||
| 11 | use embassy_stm32::{bind_interrupts, Config}; | ||
| 12 | use {defmt_rtt as _, panic_probe as _}; | ||
| 13 | |||
| 14 | bind_interrupts!(struct Irqs { | ||
| 15 | USB_LP_CAN1_RX0 => Rx0InterruptHandler<CAN>; | ||
| 16 | CAN1_RX1 => Rx1InterruptHandler<CAN>; | ||
| 17 | CAN1_SCE => SceInterruptHandler<CAN>; | ||
| 18 | USB_HP_CAN1_TX => TxInterruptHandler<CAN>; | ||
| 19 | }); | ||
| 20 | |||
| 21 | // This example is configured to work with real CAN transceivers on B8/B9. | ||
| 22 | // See other examples for loopback. | ||
| 23 | |||
| 24 | #[embassy_executor::main] | ||
| 25 | async fn main(_spawner: Spawner) { | ||
| 26 | let p = embassy_stm32::init(Config::default()); | ||
| 27 | |||
| 28 | // Set alternate pin mapping to B8/B9 | ||
| 29 | embassy_stm32::pac::AFIO.mapr().modify(|w| w.set_can1_remap(2)); | ||
| 30 | |||
| 31 | let mut can = Can::new(p.CAN, p.PB8, p.PB9, Irqs); | ||
| 32 | |||
| 33 | can.as_mut() | ||
| 34 | .modify_filters() | ||
| 35 | .enable_bank(0, Fifo::Fifo0, Mask32::accept_all()); | ||
| 36 | |||
| 37 | can.as_mut() | ||
| 38 | .modify_config() | ||
| 39 | .set_loopback(false) | ||
| 40 | .set_silent(false) | ||
| 41 | .leave_disabled(); | ||
| 42 | |||
| 43 | can.set_bitrate(250_000); | ||
| 44 | |||
| 45 | can.enable().await; | ||
| 46 | |||
| 47 | let mut i: u8 = 0; | ||
| 48 | loop { | ||
| 49 | let tx_frame = Frame::new_data(unwrap!(StandardId::new(i as _)), [i]); | ||
| 50 | can.write(&tx_frame).await; | ||
| 51 | |||
| 52 | match can.read().await { | ||
| 53 | Ok(env) => match env.frame.id() { | ||
| 54 | Id::Extended(id) => { | ||
| 55 | defmt::println!("Extended Frame id={:x}", id.as_raw()); | ||
| 56 | } | ||
| 57 | Id::Standard(id) => { | ||
| 58 | defmt::println!("Standard Frame id={:x}", id.as_raw()); | ||
| 59 | } | ||
| 60 | }, | ||
| 61 | Err(err) => { | ||
| 62 | defmt::println!("Error {}", err); | ||
| 63 | } | ||
| 64 | } | ||
| 65 | i += 1; | ||
| 66 | } | ||
| 67 | } | ||
