From 88d4b0c00d5164f2fe6307bacce74887b3f8d4da Mon Sep 17 00:00:00 2001 From: Dario Nieuwenhuis Date: Sat, 27 Nov 2021 02:21:53 +0100 Subject: stm32: add stm32g4 support. --- examples/stm32g0/Cargo.toml | 2 +- examples/stm32g4/.cargo/config.toml | 6 ++++++ examples/stm32g4/Cargo.toml | 22 ++++++++++++++++++++++ examples/stm32g4/build.rs | 5 +++++ examples/stm32g4/src/bin/blinky.rs | 29 +++++++++++++++++++++++++++++ examples/stm32g4/src/bin/button.rs | 27 +++++++++++++++++++++++++++ examples/stm32g4/src/bin/button_exti.rs | 29 +++++++++++++++++++++++++++++ examples/stm32g4/src/example_common.rs | 17 +++++++++++++++++ 8 files changed, 136 insertions(+), 1 deletion(-) create mode 100644 examples/stm32g4/.cargo/config.toml create mode 100644 examples/stm32g4/Cargo.toml create mode 100644 examples/stm32g4/build.rs create mode 100644 examples/stm32g4/src/bin/blinky.rs create mode 100644 examples/stm32g4/src/bin/button.rs create mode 100644 examples/stm32g4/src/bin/button_exti.rs create mode 100644 examples/stm32g4/src/example_common.rs (limited to 'examples') diff --git a/examples/stm32g0/Cargo.toml b/examples/stm32g0/Cargo.toml index 203cdad99..731116c32 100644 --- a/examples/stm32g0/Cargo.toml +++ b/examples/stm32g0/Cargo.toml @@ -8,7 +8,7 @@ resolver = "2" [dependencies] embassy = { version = "0.1.0", path = "../../embassy", features = ["defmt"] } embassy-traits = { version = "0.1.0", path = "../../embassy-traits", features = ["defmt"] } -embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["defmt", "time-driver-tim2", "stm32g071rb", "unstable-pac"] } +embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["defmt", "time-driver-tim2", "stm32g071rb", "memory-x", "unstable-pac"] } embassy-hal-common = {version = "0.1.0", path = "../../embassy-hal-common" } defmt = "0.3" diff --git a/examples/stm32g4/.cargo/config.toml b/examples/stm32g4/.cargo/config.toml new file mode 100644 index 000000000..62003c2a4 --- /dev/null +++ b/examples/stm32g4/.cargo/config.toml @@ -0,0 +1,6 @@ +[target.'cfg(all(target_arch = "arm", target_os = "none"))'] +# replace STM32G071C8Rx with your chip as listed in `probe-run --list-chips` +runner = "probe-run --chip STM32G484VETx" + +[build] +target = "thumbv7em-none-eabi" diff --git a/examples/stm32g4/Cargo.toml b/examples/stm32g4/Cargo.toml new file mode 100644 index 000000000..0f9d77f5e --- /dev/null +++ b/examples/stm32g4/Cargo.toml @@ -0,0 +1,22 @@ +[package] +authors = ["Dario Nieuwenhuis ", "Ben Gamari "] +edition = "2018" +name = "embassy-stm32g4-examples" +version = "0.1.0" +resolver = "2" + +[dependencies] +embassy = { version = "0.1.0", path = "../../embassy", features = ["defmt"] } +embassy-traits = { version = "0.1.0", path = "../../embassy-traits", features = ["defmt"] } +embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["defmt", "time-driver-tim2", "stm32g491re", "memory-x", "unstable-pac"] } +embassy-hal-common = {version = "0.1.0", path = "../../embassy-hal-common" } + +defmt = "0.3" +defmt-rtt = "0.3" + +cortex-m = "0.7.3" +cortex-m-rt = "0.7.0" +embedded-hal = "0.2.6" +panic-probe = { version = "0.3", features = ["print-defmt"] } +futures = { version = "0.3.17", default-features = false, features = ["async-await"] } +heapless = { version = "0.7.5", default-features = false } diff --git a/examples/stm32g4/build.rs b/examples/stm32g4/build.rs new file mode 100644 index 000000000..8cd32d7ed --- /dev/null +++ b/examples/stm32g4/build.rs @@ -0,0 +1,5 @@ +fn main() { + println!("cargo:rustc-link-arg-bins=--nmagic"); + println!("cargo:rustc-link-arg-bins=-Tlink.x"); + println!("cargo:rustc-link-arg-bins=-Tdefmt.x"); +} diff --git a/examples/stm32g4/src/bin/blinky.rs b/examples/stm32g4/src/bin/blinky.rs new file mode 100644 index 000000000..a43922a63 --- /dev/null +++ b/examples/stm32g4/src/bin/blinky.rs @@ -0,0 +1,29 @@ +#![no_std] +#![no_main] +#![feature(type_alias_impl_trait)] + +#[path = "../example_common.rs"] +mod example_common; +use embassy::executor::Spawner; +use embassy::time::{Duration, Timer}; +use embassy_stm32::gpio::{Level, Output, Speed}; +use embassy_stm32::Peripherals; +use embedded_hal::digital::v2::OutputPin; +use example_common::*; + +#[embassy::main] +async fn main(_spawner: Spawner, p: Peripherals) { + info!("Hello World!"); + + let mut led = Output::new(p.PA5, Level::High, Speed::Low); + + loop { + info!("high"); + unwrap!(led.set_high()); + Timer::after(Duration::from_millis(300)).await; + + info!("low"); + unwrap!(led.set_low()); + Timer::after(Duration::from_millis(300)).await; + } +} diff --git a/examples/stm32g4/src/bin/button.rs b/examples/stm32g4/src/bin/button.rs new file mode 100644 index 000000000..f0a4c8745 --- /dev/null +++ b/examples/stm32g4/src/bin/button.rs @@ -0,0 +1,27 @@ +#![no_std] +#![no_main] +#![feature(type_alias_impl_trait)] + +#[path = "../example_common.rs"] +mod example_common; +use cortex_m_rt::entry; +use embassy_stm32::gpio::{Input, Pull}; +use embedded_hal::digital::v2::InputPin; +use example_common::*; + +#[entry] +fn main() -> ! { + info!("Hello World!"); + + let p = embassy_stm32::init(Default::default()); + + let button = Input::new(p.PC13, Pull::Down); + + loop { + if unwrap!(button.is_high()) { + info!("high"); + } else { + info!("low"); + } + } +} diff --git a/examples/stm32g4/src/bin/button_exti.rs b/examples/stm32g4/src/bin/button_exti.rs new file mode 100644 index 000000000..2c4318d64 --- /dev/null +++ b/examples/stm32g4/src/bin/button_exti.rs @@ -0,0 +1,29 @@ +#![no_std] +#![no_main] +#![feature(type_alias_impl_trait)] + +#[path = "../example_common.rs"] +mod example_common; +use embassy::executor::Spawner; +use embassy_stm32::exti::ExtiInput; +use embassy_stm32::gpio::{Input, Pull}; +use embassy_stm32::Peripherals; +use embassy_traits::gpio::{WaitForFallingEdge, WaitForRisingEdge}; +use example_common::*; + +#[embassy::main] +async fn main(_spawner: Spawner, p: Peripherals) { + info!("Hello World!"); + + let button = Input::new(p.PC13, Pull::Down); + let mut button = ExtiInput::new(button, p.EXTI13); + + info!("Press the USER button..."); + + loop { + button.wait_for_rising_edge().await; + info!("Pressed!"); + button.wait_for_falling_edge().await; + info!("Released!"); + } +} diff --git a/examples/stm32g4/src/example_common.rs b/examples/stm32g4/src/example_common.rs new file mode 100644 index 000000000..54d633837 --- /dev/null +++ b/examples/stm32g4/src/example_common.rs @@ -0,0 +1,17 @@ +#![macro_use] + +use defmt_rtt as _; // global logger +use panic_probe as _; + +pub use defmt::*; + +use core::sync::atomic::{AtomicUsize, Ordering}; + +defmt::timestamp! {"{=u64}", { + static COUNT: AtomicUsize = AtomicUsize::new(0); + // NOTE(no-CAS) `timestamps` runs with interrupts disabled + let n = COUNT.load(Ordering::Relaxed); + COUNT.store(n + 1, Ordering::Relaxed); + n as u64 + } +} -- cgit