diff options
| author | Corey Schuhen <[email protected]> | 2024-01-25 17:10:25 +1000 |
|---|---|---|
| committer | Corey Schuhen <[email protected]> | 2024-01-31 05:40:05 +1000 |
| commit | 1698f4dbc3b5f4e561c3edd20246af63c22da507 (patch) | |
| tree | 460771503272bd12d19b18c443f3606a141d5f9c /examples/stm32g4 | |
| parent | 1de78d049017b7d21946a2e64d94c990f1bf4e7e (diff) | |
Add FDCAN examples for STM32G4, STM32H5 and STM32H7
Fix examples
Fix examples
Fix examples.
Diffstat (limited to 'examples/stm32g4')
| -rw-r--r-- | examples/stm32g4/Cargo.toml | 2 | ||||
| -rw-r--r-- | examples/stm32g4/src/bin/can.rs | 56 |
2 files changed, 57 insertions, 1 deletions
diff --git a/examples/stm32g4/Cargo.toml b/examples/stm32g4/Cargo.toml index b87f461b4..895ad3e7c 100644 --- a/examples/stm32g4/Cargo.toml +++ b/examples/stm32g4/Cargo.toml | |||
| @@ -6,7 +6,7 @@ license = "MIT OR Apache-2.0" | |||
| 6 | 6 | ||
| 7 | [dependencies] | 7 | [dependencies] |
| 8 | # Change stm32g491re to your chip name, if necessary. | 8 | # Change stm32g491re to your chip name, if necessary. |
| 9 | embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = [ "defmt", "time-driver-any", "stm32g473re", "memory-x", "unstable-pac", "exti", "fdcan"] } | 9 | embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = [ "defmt", "time-driver-any", "stm32g491re", "memory-x", "unstable-pac", "exti"] } |
| 10 | embassy-sync = { version = "0.5.0", path = "../../embassy-sync", features = ["defmt"] } | 10 | embassy-sync = { version = "0.5.0", path = "../../embassy-sync", features = ["defmt"] } |
| 11 | embassy-executor = { version = "0.5.0", path = "../../embassy-executor", features = ["arch-cortex-m", "executor-thread", "defmt", "integrated-timers"] } | 11 | embassy-executor = { version = "0.5.0", path = "../../embassy-executor", features = ["arch-cortex-m", "executor-thread", "defmt", "integrated-timers"] } |
| 12 | embassy-time = { version = "0.3.0", path = "../../embassy-time", features = ["defmt", "defmt-timestamp-uptime", "tick-hz-32_768"] } | 12 | embassy-time = { version = "0.3.0", path = "../../embassy-time", features = ["defmt", "defmt-timestamp-uptime", "tick-hz-32_768"] } |
diff --git a/examples/stm32g4/src/bin/can.rs b/examples/stm32g4/src/bin/can.rs new file mode 100644 index 000000000..727921fba --- /dev/null +++ b/examples/stm32g4/src/bin/can.rs | |||
| @@ -0,0 +1,56 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | use defmt::*; | ||
| 4 | use embassy_executor::Spawner; | ||
| 5 | use embassy_stm32::peripherals::*; | ||
| 6 | use embassy_stm32::{bind_interrupts, can, Config}; | ||
| 7 | use embassy_time::Timer; | ||
| 8 | use {defmt_rtt as _, panic_probe as _}; | ||
| 9 | |||
| 10 | bind_interrupts!(struct Irqs { | ||
| 11 | FDCAN1_IT0 => can::IT0InterruptHandler<FDCAN1>; | ||
| 12 | FDCAN1_IT1 => can::IT1InterruptHandler<FDCAN1>; | ||
| 13 | }); | ||
| 14 | |||
| 15 | #[embassy_executor::main] | ||
| 16 | async fn main(_spawner: Spawner) { | ||
| 17 | let config = Config::default(); | ||
| 18 | |||
| 19 | let peripherals = embassy_stm32::init(config); | ||
| 20 | |||
| 21 | let mut can = can::Fdcan::new(peripherals.FDCAN1, peripherals.PA11, peripherals.PA12, Irqs); | ||
| 22 | |||
| 23 | // 250k bps | ||
| 24 | can.set_bitrate(250_000); | ||
| 25 | |||
| 26 | info!("Configured"); | ||
| 27 | |||
| 28 | //let mut can = can.into_external_loopback_mode(); | ||
| 29 | let mut can = can.into_normal_mode(); | ||
| 30 | |||
| 31 | let mut i = 0; | ||
| 32 | loop { | ||
| 33 | let frame = can::TxFrame::new( | ||
| 34 | can::TxFrameHeader { | ||
| 35 | len: 1, | ||
| 36 | frame_format: can::FrameFormat::Standard, | ||
| 37 | id: can::StandardId::new(0x123).unwrap().into(), | ||
| 38 | bit_rate_switching: false, | ||
| 39 | marker: None, | ||
| 40 | }, | ||
| 41 | &[i], | ||
| 42 | ) | ||
| 43 | .unwrap(); | ||
| 44 | info!("Writing frame"); | ||
| 45 | _ = can.write(&frame).await; | ||
| 46 | |||
| 47 | match can.read().await { | ||
| 48 | Ok(rx_frame) => info!("Rx: {}", rx_frame.data()[0]), | ||
| 49 | Err(_err) => error!("Error in frame"), | ||
| 50 | } | ||
| 51 | |||
| 52 | Timer::after_millis(250).await; | ||
| 53 | |||
| 54 | i += 1; | ||
| 55 | } | ||
| 56 | } | ||
