From 94dce3b2e290dda9a915ec35690be04ce6c9785c Mon Sep 17 00:00:00 2001 From: LongHairedHacker Date: Sun, 11 Apr 2021 15:55:46 +0200 Subject: [PATCH] Adjusted template for STM32F103 --- .cargo/config.toml | 19 ++++++++++ .gitignore | 2 ++ Cargo.toml | 79 +++++++++++++++++++++++++++++++++++++++++ memory.x | 6 ++++ src/bin/hello.rs | 11 ++++++ src/lib.rs | 30 ++++++++++++++++ testsuite/Cargo.toml | 36 +++++++++++++++++++ testsuite/tests/test.rs | 21 +++++++++++ 8 files changed, 204 insertions(+) create mode 100644 .cargo/config.toml create mode 100644 .gitignore create mode 100644 Cargo.toml create mode 100644 memory.x create mode 100644 src/bin/hello.rs create mode 100644 src/lib.rs create mode 100644 testsuite/Cargo.toml create mode 100644 testsuite/tests/test.rs diff --git a/.cargo/config.toml b/.cargo/config.toml new file mode 100644 index 0000000..bf8dd40 --- /dev/null +++ b/.cargo/config.toml @@ -0,0 +1,19 @@ +[target.'cfg(all(target_arch = "arm", target_os = "none"))'] +# TODO(2) replace `$CHIP` with your chip's name (see `probe-run --list-chips` output) +runner = "probe-run --chip STM32F103CB" +rustflags = [ + "-C", "linker=flip-link", + "-C", "link-arg=-Tlink.x", + "-C", "link-arg=-Tdefmt.x", + # This is needed if your flash or ram addresses are not aligned to 0x10000 in memory.x + # See https://github.com/rust-embedded/cortex-m-quickstart/pull/95 + "-C", "link-arg=--nmagic", +] + +[build] +target = "thumbv7m-none-eabi" # Cortex-M3 + + +[alias] +rb = "run --bin" +rrb = "run --release --bin" diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..96ef6c0 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/target +Cargo.lock diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..a1b79c2 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,79 @@ +[package] +# TODO(1) fix `authors` and `name` if you didn't use `cargo-generate` +authors = ["LongHairedHacker "] +name = "wspr-beacon" +edition = "2018" +version = "0.1.0" + +[workspace] +members = ["testsuite"] + +[dependencies] +cortex-m = "~0.7.1" +cortex-m-rt = "~0.6.13" +defmt = "~0.2.0" +defmt-rtt = "~0.2.0" +panic-probe = { version = "~0.2.0", features = ["print-defmt"] } +stm32f1xx-hal = { version = "~0.6.1", features = ["stm32f103", "rt"] } +embedded-hal = {version = "~0.2.3", feature = ["unproven"]} + +[features] +# set logging levels here +default = [ + "defmt-default", + # "dependency-a/defmt-trace", +] + +# do NOT modify these features +defmt-default = [] +defmt-trace = [] +defmt-debug = [] +defmt-info = [] +defmt-warn = [] +defmt-error = [] + +# cargo build/run +[profile.dev] +codegen-units = 1 +debug = 2 +debug-assertions = true # <- +incremental = false +opt-level = 3 # <- +overflow-checks = true # <- + +# cargo test +[profile.test] +codegen-units = 1 +debug = 2 +debug-assertions = true # <- +incremental = false +opt-level = 3 # <- +overflow-checks = true # <- + +# cargo build/run --release +[profile.release] +codegen-units = 1 +debug = 2 +debug-assertions = false # <- +incremental = false +lto = 'fat' +opt-level = 3 # <- +overflow-checks = false # <- + +# cargo test --release +[profile.bench] +codegen-units = 1 +debug = 2 +debug-assertions = false # <- +incremental = false +lto = 'fat' +opt-level = 3 # <- +overflow-checks = false # <- + +# uncomment this to switch from the crates.io version of defmt to its git version +# check app-template's README for instructions +# [patch.crates-io] +# defmt = { git = "https://github.com/knurling-rs/defmt", rev = "use defmt version reported by `probe-run --version`" } +# defmt-rtt = { git = "https://github.com/knurling-rs/defmt", rev = "use defmt version reported by `probe-run --version`" } +# defmt-test = { git = "https://github.com/knurling-rs/defmt", rev = "use defmt version reported by `probe-run --version`" } +# panic-probe = { git = "https://github.com/knurling-rs/defmt", rev = "use defmt version reported by `probe-run --version`" } diff --git a/memory.x b/memory.x new file mode 100644 index 0000000..71f245d --- /dev/null +++ b/memory.x @@ -0,0 +1,6 @@ +/* Linker script for the STM32F103C8T6 */ +MEMORY +{ + FLASH : ORIGIN = 0x08000000, LENGTH = 64K + RAM : ORIGIN = 0x20000000, LENGTH = 20K +} diff --git a/src/bin/hello.rs b/src/bin/hello.rs new file mode 100644 index 0000000..6ad0a7c --- /dev/null +++ b/src/bin/hello.rs @@ -0,0 +1,11 @@ +#![no_main] +#![no_std] + +use wspr_beacon as _; // global logger + panicking-behavior + memory layout + +#[cortex_m_rt::entry] +fn main() -> ! { + defmt::info!("Hello, world!"); + + wspr_beacon::exit() +} diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..97aff03 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,30 @@ +#![no_std] + +use core::sync::atomic::{AtomicUsize, Ordering}; + +use defmt_rtt as _; // global logger + +use panic_probe as _; +use stm32f1xx_hal as _; + +// same panicking *behavior* as `panic-probe` but doesn't print a panic message +// this prevents the panic message being printed *twice* when `defmt::panic` is invoked +#[defmt::panic_handler] +fn panic() -> ! { + cortex_m::asm::udf() +} + +static COUNT: AtomicUsize = AtomicUsize::new(0); +defmt::timestamp!("{=usize}", { + // NOTE(no-CAS) `timestamps` runs with interrupts disabled + let n = COUNT.load(Ordering::Relaxed); + COUNT.store(n + 1, Ordering::Relaxed); + n +}); + +/// Terminates the application and makes `probe-run` exit with exit-code = 0 +pub fn exit() -> ! { + loop { + cortex_m::asm::bkpt(); + } +} diff --git a/testsuite/Cargo.toml b/testsuite/Cargo.toml new file mode 100644 index 0000000..cd069fd --- /dev/null +++ b/testsuite/Cargo.toml @@ -0,0 +1,36 @@ +[package] +# TODO(1) fix `authors` if you didn't use `cargo-generate` +authors = ["LongHairedHacker "] +name = "testsuite" +publish = false +edition = "2018" +version = "0.1.0" + +[[test]] +name = "test" +harness = false + +[dependencies] +wspr-beacon = { path = ".." } +cortex-m = "0.7.1" +cortex-m-rt = "0.6.12" +defmt = "0.2.0" +defmt-rtt = "0.2.0" +defmt-test = "0.2.0" +panic-probe = { version = "0.2.0", features = ["print-defmt"] } + +[features] +# set logging levels here +default = [ + # in tests, enable all logs + "defmt-trace", + # "dependency-a/defmt-trace", +] + +# do NOT modify these features +defmt-default = [] +defmt-trace = [] +defmt-debug = [] +defmt-info = [] +defmt-warn = [] +defmt-error = [] diff --git a/testsuite/tests/test.rs b/testsuite/tests/test.rs new file mode 100644 index 0000000..3c35c4b --- /dev/null +++ b/testsuite/tests/test.rs @@ -0,0 +1,21 @@ +#![no_std] +#![no_main] + +use wspr_beacon as _; // memory layout + panic handler + +// See https://crates.io/crates/defmt-test/0.1.0 for more documentation (e.g. about the 'state' +// feature) +#[defmt_test::tests] +mod tests { + use defmt::{assert, assert_eq}; + + #[test] + fn assert_true() { + assert!(true) + } + + #[test] + fn assert_eq() { + assert_eq!(24, 42, "TODO: write actual tests") + } +}