From a16fef21e1e8004a207a85cf542f3402213cac87 Mon Sep 17 00:00:00 2001 From: Joonas Javanainen Date: Sun, 27 Mar 2022 18:42:22 +0300 Subject: [PATCH] Add blinky example for STM32F2 Default configuration is for NUCLEO-F207ZG board --- examples/stm32f2/.cargo/config | 6 ++++++ examples/stm32f2/Cargo.toml | 21 +++++++++++++++++++ examples/stm32f2/build.rs | 5 +++++ examples/stm32f2/src/bin/blinky.rs | 29 ++++++++++++++++++++++++++ examples/stm32f2/src/example_common.rs | 19 +++++++++++++++++ 5 files changed, 80 insertions(+) create mode 100644 examples/stm32f2/.cargo/config create mode 100644 examples/stm32f2/Cargo.toml create mode 100644 examples/stm32f2/build.rs create mode 100644 examples/stm32f2/src/bin/blinky.rs create mode 100644 examples/stm32f2/src/example_common.rs diff --git a/examples/stm32f2/.cargo/config b/examples/stm32f2/.cargo/config new file mode 100644 index 00000000..30b6d190 --- /dev/null +++ b/examples/stm32f2/.cargo/config @@ -0,0 +1,6 @@ +[target.'cfg(all(target_arch = "arm", target_os = "none"))'] +# replace STM32F207ZGTx with your chip as listed in `probe-run --list-chips` +runner = "probe-run --chip STM32F207ZGTx" + +[build] +target = "thumbv7m-none-eabi" diff --git a/examples/stm32f2/Cargo.toml b/examples/stm32f2/Cargo.toml new file mode 100644 index 00000000..ee1d7ce2 --- /dev/null +++ b/examples/stm32f2/Cargo.toml @@ -0,0 +1,21 @@ +[package] +authors = ["Dario Nieuwenhuis "] +edition = "2018" +name = "embassy-stm32f2-examples" +version = "0.1.0" +resolver = "2" + +[dependencies] +embassy = { version = "0.1.0", path = "../../embassy", features = ["defmt"] } +embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["nightly", "defmt", "stm32f207zg", "unstable-pac", "memory-x", "time-driver-any", "exti"] } + +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 } +nb = "1.0.0" diff --git a/examples/stm32f2/build.rs b/examples/stm32f2/build.rs new file mode 100644 index 00000000..8cd32d7e --- /dev/null +++ b/examples/stm32f2/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/stm32f2/src/bin/blinky.rs b/examples/stm32f2/src/bin/blinky.rs new file mode 100644 index 00000000..637c2f4f --- /dev/null +++ b/examples/stm32f2/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 example_common::*; + +#[embassy::main] +async fn main(_spawner: Spawner, p: Peripherals) { + info!("Hello World!"); + + let mut led = Output::new(p.PB14, Level::High, Speed::Low); + + loop { + info!("high"); + led.set_high(); + Timer::after(Duration::from_millis(1000)).await; + + info!("low"); + led.set_low(); + Timer::after(Duration::from_millis(1000)).await; + } +} diff --git a/examples/stm32f2/src/example_common.rs b/examples/stm32f2/src/example_common.rs new file mode 100644 index 00000000..e1451703 --- /dev/null +++ b/examples/stm32f2/src/example_common.rs @@ -0,0 +1,19 @@ +#![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 + } +}