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