summaryrefslogtreecommitdiff
path: root/examples/stm32f0
diff options
context:
space:
mode:
authorThales Fragoso <thales.fragosoz@gmail.com>2021-07-15 13:50:39 -0300
committerThales Fragoso <thales.fragosoz@gmail.com>2021-07-15 13:50:39 -0300
commit697f93ad423fb70aa134c8810237622642a301d6 (patch)
tree936020003205a563844b33203d0baddb2418e0eb /examples/stm32f0
parent2f08c7ced56f855f75bb73cb4775c6379958b3f2 (diff)
downloadembassy-697f93ad423fb70aa134c8810237622642a301d6.zip
stm32: Add F0 example
Diffstat (limited to 'examples/stm32f0')
-rw-r--r--examples/stm32f0/.cargo/config.toml10
-rw-r--r--examples/stm32f0/Cargo.toml29
-rw-r--r--examples/stm32f0/build.rs31
-rw-r--r--examples/stm32f0/memory.x6
-rw-r--r--examples/stm32f0/src/bin/hello.rs23
-rw-r--r--examples/stm32f0/src/example_common.rs17
6 files changed, 116 insertions, 0 deletions
diff --git a/examples/stm32f0/.cargo/config.toml b/examples/stm32f0/.cargo/config.toml
new file mode 100644
index 00000000..7506fa91
--- /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 00000000..cc2c3f2b
--- /dev/null
+++ b/examples/stm32f0/Cargo.toml
@@ -0,0 +1,29 @@
+[package]
+name = "embassy-stm32f0-examples"
+version = "0.1.0"
+authors = ["Thales Fragoso <thales.fragosoz@gmail.com>"]
+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 00000000..d534cc3d
--- /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 00000000..3bddaed4
--- /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 00000000..a78b9892
--- /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 00000000..54d63383
--- /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
+ }
+}