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 | |
| parent | 1de78d049017b7d21946a2e64d94c990f1bf4e7e (diff) | |
Add FDCAN examples for STM32G4, STM32H5 and STM32H7
Fix examples
Fix examples
Fix examples.
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/stm32g4/Cargo.toml | 2 | ||||
| -rw-r--r-- | examples/stm32g4/src/bin/can.rs | 56 | ||||
| -rw-r--r-- | examples/stm32h5/src/bin/can.rs | 74 | ||||
| -rw-r--r-- | examples/stm32h7/src/bin/can.rs | 74 |
4 files changed, 205 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 | } | ||
diff --git a/examples/stm32h5/src/bin/can.rs b/examples/stm32h5/src/bin/can.rs new file mode 100644 index 000000000..2906d1576 --- /dev/null +++ b/examples/stm32h5/src/bin/can.rs | |||
| @@ -0,0 +1,74 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | |||
| 4 | use defmt::*; | ||
| 5 | use embassy_executor::Spawner; | ||
| 6 | use embassy_stm32::peripherals::*; | ||
| 7 | use embassy_stm32::{bind_interrupts, can, rcc, Config}; | ||
| 8 | use embassy_time::Timer; | ||
| 9 | use {defmt_rtt as _, panic_probe as _}; | ||
| 10 | |||
| 11 | bind_interrupts!(struct Irqs { | ||
| 12 | FDCAN1_IT0 => can::IT0InterruptHandler<FDCAN1>; | ||
| 13 | FDCAN1_IT1 => can::IT1InterruptHandler<FDCAN1>; | ||
| 14 | }); | ||
| 15 | |||
| 16 | #[embassy_executor::main] | ||
| 17 | async fn main(_spawner: Spawner) { | ||
| 18 | let mut config = Config::default(); | ||
| 19 | |||
| 20 | // configure FDCAN to use PLL1_Q at 64 MHz | ||
| 21 | config.rcc.pll1 = Some(rcc::Pll { | ||
| 22 | source: rcc::PllSource::HSI, | ||
| 23 | prediv: rcc::PllPreDiv::DIV4, | ||
| 24 | mul: rcc::PllMul::MUL8, | ||
| 25 | divp: None, | ||
| 26 | divq: Some(rcc::PllDiv::DIV2), | ||
| 27 | divr: None, | ||
| 28 | }); | ||
| 29 | config.rcc.fdcan_clock_source = rcc::FdCanClockSource::PLL1_Q; | ||
| 30 | |||
| 31 | let peripherals = embassy_stm32::init(config); | ||
| 32 | |||
| 33 | let mut can = can::Fdcan::new(peripherals.FDCAN1, peripherals.PA11, peripherals.PA12, Irqs); | ||
| 34 | |||
| 35 | can.can.apply_config( | ||
| 36 | can::config::FdCanConfig::default().set_nominal_bit_timing(can::config::NominalBitTiming { | ||
| 37 | sync_jump_width: 1.try_into().unwrap(), | ||
| 38 | prescaler: 8.try_into().unwrap(), | ||
| 39 | seg1: 13.try_into().unwrap(), | ||
| 40 | seg2: 2.try_into().unwrap(), | ||
| 41 | }), | ||
| 42 | ); | ||
| 43 | |||
| 44 | info!("Configured"); | ||
| 45 | |||
| 46 | let mut can = can.into_external_loopback_mode(); | ||
| 47 | //let mut can = can.into_normal_mode(); | ||
| 48 | |||
| 49 | let mut i = 0; | ||
| 50 | loop { | ||
| 51 | let frame = can::TxFrame::new( | ||
| 52 | can::TxFrameHeader { | ||
| 53 | len: 1, | ||
| 54 | frame_format: can::FrameFormat::Standard, | ||
| 55 | id: can::StandardId::new(0x123).unwrap().into(), | ||
| 56 | bit_rate_switching: false, | ||
| 57 | marker: None, | ||
| 58 | }, | ||
| 59 | &[i], | ||
| 60 | ) | ||
| 61 | .unwrap(); | ||
| 62 | info!("Writing frame"); | ||
| 63 | _ = can.write(&frame).await; | ||
| 64 | |||
| 65 | match can.read().await { | ||
| 66 | Ok(rx_frame) => info!("Rx: {}", rx_frame.data()[0]), | ||
| 67 | Err(_err) => error!("Error in frame"), | ||
| 68 | } | ||
| 69 | |||
| 70 | Timer::after_millis(250).await; | ||
| 71 | |||
| 72 | i += 1; | ||
| 73 | } | ||
| 74 | } | ||
diff --git a/examples/stm32h7/src/bin/can.rs b/examples/stm32h7/src/bin/can.rs new file mode 100644 index 000000000..2906d1576 --- /dev/null +++ b/examples/stm32h7/src/bin/can.rs | |||
| @@ -0,0 +1,74 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | |||
| 4 | use defmt::*; | ||
| 5 | use embassy_executor::Spawner; | ||
| 6 | use embassy_stm32::peripherals::*; | ||
| 7 | use embassy_stm32::{bind_interrupts, can, rcc, Config}; | ||
| 8 | use embassy_time::Timer; | ||
| 9 | use {defmt_rtt as _, panic_probe as _}; | ||
| 10 | |||
| 11 | bind_interrupts!(struct Irqs { | ||
| 12 | FDCAN1_IT0 => can::IT0InterruptHandler<FDCAN1>; | ||
| 13 | FDCAN1_IT1 => can::IT1InterruptHandler<FDCAN1>; | ||
| 14 | }); | ||
| 15 | |||
| 16 | #[embassy_executor::main] | ||
| 17 | async fn main(_spawner: Spawner) { | ||
| 18 | let mut config = Config::default(); | ||
| 19 | |||
| 20 | // configure FDCAN to use PLL1_Q at 64 MHz | ||
| 21 | config.rcc.pll1 = Some(rcc::Pll { | ||
| 22 | source: rcc::PllSource::HSI, | ||
| 23 | prediv: rcc::PllPreDiv::DIV4, | ||
| 24 | mul: rcc::PllMul::MUL8, | ||
| 25 | divp: None, | ||
| 26 | divq: Some(rcc::PllDiv::DIV2), | ||
| 27 | divr: None, | ||
| 28 | }); | ||
| 29 | config.rcc.fdcan_clock_source = rcc::FdCanClockSource::PLL1_Q; | ||
| 30 | |||
| 31 | let peripherals = embassy_stm32::init(config); | ||
| 32 | |||
| 33 | let mut can = can::Fdcan::new(peripherals.FDCAN1, peripherals.PA11, peripherals.PA12, Irqs); | ||
| 34 | |||
| 35 | can.can.apply_config( | ||
| 36 | can::config::FdCanConfig::default().set_nominal_bit_timing(can::config::NominalBitTiming { | ||
| 37 | sync_jump_width: 1.try_into().unwrap(), | ||
| 38 | prescaler: 8.try_into().unwrap(), | ||
| 39 | seg1: 13.try_into().unwrap(), | ||
| 40 | seg2: 2.try_into().unwrap(), | ||
| 41 | }), | ||
| 42 | ); | ||
| 43 | |||
| 44 | info!("Configured"); | ||
| 45 | |||
| 46 | let mut can = can.into_external_loopback_mode(); | ||
| 47 | //let mut can = can.into_normal_mode(); | ||
| 48 | |||
| 49 | let mut i = 0; | ||
| 50 | loop { | ||
| 51 | let frame = can::TxFrame::new( | ||
| 52 | can::TxFrameHeader { | ||
| 53 | len: 1, | ||
| 54 | frame_format: can::FrameFormat::Standard, | ||
| 55 | id: can::StandardId::new(0x123).unwrap().into(), | ||
| 56 | bit_rate_switching: false, | ||
| 57 | marker: None, | ||
| 58 | }, | ||
| 59 | &[i], | ||
| 60 | ) | ||
| 61 | .unwrap(); | ||
| 62 | info!("Writing frame"); | ||
| 63 | _ = can.write(&frame).await; | ||
| 64 | |||
| 65 | match can.read().await { | ||
| 66 | Ok(rx_frame) => info!("Rx: {}", rx_frame.data()[0]), | ||
| 67 | Err(_err) => error!("Error in frame"), | ||
| 68 | } | ||
| 69 | |||
| 70 | Timer::after_millis(250).await; | ||
| 71 | |||
| 72 | i += 1; | ||
| 73 | } | ||
| 74 | } | ||
