diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 2035c96dc..118143daa 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -88,6 +88,8 @@ jobs: target: thumbv6m-none-eabi - package: examples/stm32wb55 target: thumbv7em-none-eabihf + - package: examples/stm32f0 + target: thumbv6m-none-eabi steps: - uses: actions/checkout@v2 diff --git a/examples/stm32f0/.cargo/config.toml b/examples/stm32f0/.cargo/config.toml new file mode 100644 index 000000000..7506fa91b --- /dev/null +++ b/examples/stm32f0/.cargo/config.toml @@ -0,0 +1,10 @@ +[target.thumbv6m-none-eabi] +runner = 'probe-run --chip STM32F030F4Px' +rustflags = [ + # LLD (shipped with the Rust toolchain) is used as the default linker + "-C", "link-arg=-Tlink.x", + "-C", "link-arg=-Tdefmt.x", +] + +[build] +target = "thumbv6m-none-eabi" diff --git a/examples/stm32f0/Cargo.toml b/examples/stm32f0/Cargo.toml new file mode 100644 index 000000000..cc2c3f2b7 --- /dev/null +++ b/examples/stm32f0/Cargo.toml @@ -0,0 +1,29 @@ +[package] +name = "embassy-stm32f0-examples" +version = "0.1.0" +authors = ["Thales Fragoso "] +edition = "2018" +resolver = "2" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +cortex-m = { version = "0.7.1", features = ["inline-asm"] } +cortex-m-rt = "0.6.13" +defmt = "0.2.0" +defmt-rtt = "0.2.0" +panic-probe = { version = "0.2.0" } +rtt-target = { version = "0.3", features = ["cortex-m"] } +embassy = { path = "../../embassy", features = ["defmt"] } +embassy-stm32 = { path = "../../embassy-stm32", features = ["defmt", "stm32f030f4"] } + +[features] +default = [ + "defmt-default", +] +defmt-default = [] +defmt-trace = [] +defmt-debug = [] +defmt-info = [] +defmt-warn = [] +defmt-error = [] diff --git a/examples/stm32f0/build.rs b/examples/stm32f0/build.rs new file mode 100644 index 000000000..d534cc3df --- /dev/null +++ b/examples/stm32f0/build.rs @@ -0,0 +1,31 @@ +//! This build script copies the `memory.x` file from the crate root into +//! a directory where the linker can always find it at build time. +//! For many projects this is optional, as the linker always searches the +//! project root directory -- wherever `Cargo.toml` is. However, if you +//! are using a workspace or have a more complicated build setup, this +//! build script becomes required. Additionally, by requesting that +//! Cargo re-run the build script whenever `memory.x` is changed, +//! updating `memory.x` ensures a rebuild of the application with the +//! new memory settings. + +use std::env; +use std::fs::File; +use std::io::Write; +use std::path::PathBuf; + +fn main() { + // Put `memory.x` in our output directory and ensure it's + // on the linker search path. + let out = &PathBuf::from(env::var_os("OUT_DIR").unwrap()); + File::create(out.join("memory.x")) + .unwrap() + .write_all(include_bytes!("memory.x")) + .unwrap(); + println!("cargo:rustc-link-search={}", out.display()); + + // By default, Cargo will re-run a build script whenever + // any file in the project changes. By specifying `memory.x` + // here, we ensure the build script is only re-run when + // `memory.x` is changed. + println!("cargo:rerun-if-changed=memory.x"); +} diff --git a/examples/stm32f0/memory.x b/examples/stm32f0/memory.x new file mode 100644 index 000000000..3bddaed41 --- /dev/null +++ b/examples/stm32f0/memory.x @@ -0,0 +1,6 @@ +MEMORY +{ + FLASH : ORIGIN = 0x08000000, LENGTH = 16K + /* DTCM */ + RAM : ORIGIN = 0x20000000, LENGTH = 4K +} diff --git a/examples/stm32f0/src/bin/hello.rs b/examples/stm32f0/src/bin/hello.rs new file mode 100644 index 000000000..a78b9892f --- /dev/null +++ b/examples/stm32f0/src/bin/hello.rs @@ -0,0 +1,23 @@ +#![no_std] +#![no_main] +#![feature(min_type_alias_impl_trait)] +#![feature(impl_trait_in_bindings)] +#![feature(type_alias_impl_trait)] +#![allow(incomplete_features)] + +use defmt::info; + +use embassy::executor::Spawner; +use embassy::time::{Duration, Timer}; +use embassy_stm32::Peripherals; + +#[path = "../example_common.rs"] +mod example_common; + +#[embassy::main] +async fn main(_spawner: Spawner, _p: Peripherals) -> ! { + loop { + Timer::after(Duration::from_secs(1)).await; + info!("Hello"); + } +} diff --git a/examples/stm32f0/src/example_common.rs b/examples/stm32f0/src/example_common.rs new file mode 100644 index 000000000..54d633837 --- /dev/null +++ b/examples/stm32f0/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 + } +}