From e6d5d8430418680b03e9a9f9ebac7af58818a349 Mon Sep 17 00:00:00 2001 From: Sebastian Date: Sun, 7 Nov 2021 20:17:36 +0100 Subject: [PATCH] Initial commit --- .cargo/config.toml | 17 +++++++++++ .gitignore | 2 ++ Cargo.toml | 66 ++++++++++++++++++++++++++++++++++++++++ README.md | 48 +++++++++++++++++++++++++++++ cargo-generate.toml | 2 ++ memory.x | 6 ++++ src/application/mod.rs | 28 +++++++++++++++++ src/application/setup.rs | 31 +++++++++++++++++++ src/lib.rs | 26 ++++++++++++++++ 9 files changed, 226 insertions(+) create mode 100644 .cargo/config.toml create mode 100644 .gitignore create mode 100644 Cargo.toml create mode 100644 README.md create mode 100644 cargo-generate.toml create mode 100644 memory.x create mode 100644 src/application/mod.rs create mode 100644 src/application/setup.rs create mode 100644 src/lib.rs diff --git a/.cargo/config.toml b/.cargo/config.toml new file mode 100644 index 0000000..bba05c7 --- /dev/null +++ b/.cargo/config.toml @@ -0,0 +1,17 @@ +[target.'cfg(all(target_arch = "arm", target_os = "none"))'] +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..e69059e --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,66 @@ +[package] +authors = ["{{authors}}"] +name = "{{project-name}}" +edition = "2018" +version = "0.1.0" + +[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.7.0", features = ["stm32f103", "rt"] } +embedded-hal = {version = "0.2.6"} + +[features] +# set logging levels here +default = [ + "defmt-default", +] + +# 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 # <- diff --git a/README.md b/README.md new file mode 100644 index 0000000..a6153c3 --- /dev/null +++ b/README.md @@ -0,0 +1,48 @@ +# `stm32f103-template` + +> Quickly set up a [`probe-run`] + [`defmt`] + [`flip-link`] embedded project + +[`probe-run`]: https://crates.io/crates/probe-run +[`defmt`]: https://github.com/knurling-rs/defmt +[`flip-link`]: https://github.com/knurling-rs/flip-link + +## Dependencies + +#### 1. `flip-link`: + +```console +$ cargo install flip-link +``` + +#### 2. `probe-run`: + +``` console +$ # make sure to install v0.2.0 or later +$ cargo install probe-run +``` + +#### 3. [`cargo-generate`]: + +``` console +$ cargo install cargo-generate +``` + +[`cargo-generate`]: https://crates.io/crates/cargo-generate + + +## Setup + +#### 1. Initialize the project template + +``` console +$ cargo generate \ + --git ssh://git@gitea.zenerdio.de:2202/sebastian/stm32f103-template.git \ + --branch main \ + --name my-app +``` + +## License +- MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT) + +## Original Work +Based on the awsome [app-template](https://github.com/knurling-rs/app-template) by knurling-rs. diff --git a/cargo-generate.toml b/cargo-generate.toml new file mode 100644 index 0000000..0c72b6c --- /dev/null +++ b/cargo-generate.toml @@ -0,0 +1,2 @@ +[template] +exclude = ["README.md"] 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/application/mod.rs b/src/application/mod.rs new file mode 100644 index 0000000..0518bcf --- /dev/null +++ b/src/application/mod.rs @@ -0,0 +1,28 @@ +use cortex_m::prelude::*; +use embedded_hal::digital::v2::ToggleableOutputPin; +use stm32f1xx_hal::{ + delay::Delay, + gpio::{gpioc, Output, PushPull}, +}; + +mod setup; + +//use crate::exit; +pub use setup::setup; + +pub struct App { + delay: Delay, + board_led: gpioc::PC13>, +} + +impl App { + pub fn run(mut self) -> ! { + defmt::info!("Application Startup!"); + + loop { + self.board_led.toggle().unwrap(); + defmt::info!("Blink..."); + self.delay.delay_ms(500u16); + } + } +} diff --git a/src/application/setup.rs b/src/application/setup.rs new file mode 100644 index 0000000..bd12a64 --- /dev/null +++ b/src/application/setup.rs @@ -0,0 +1,31 @@ +use stm32f1xx_hal::{delay::Delay, prelude::*, stm32}; + +use crate::application::App; + +pub fn setup(cp: cortex_m::peripheral::Peripherals, dp: stm32::Peripherals) -> App { + // Take ownership over the raw flash and rcc devices and convert them into the corresponding + // HAL structs + let mut flash = dp.FLASH.constrain(); + + let mut rcc = dp.RCC.constrain(); + + // Freeze the configuration of all the clocks in the system and store the frozen frequencies in + // `clocks` + let clocks = rcc + .cfgr + .use_hse(8.mhz()) + .sysclk(72.mhz()) + .pclk1(36.mhz()) + .freeze(&mut flash.acr); + + defmt::info!("Clock Setup done"); + + // Acquire the GPIOC peripheral + let mut gpioc = dp.GPIOC.split(&mut rcc.apb2); + + let board_led = gpioc.pc13.into_push_pull_output(&mut gpioc.crh); + + let delay = Delay::new(cp.SYST, clocks); + + App { delay, board_led } +} diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..7952131 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,26 @@ +#![no_std] +use defmt_rtt as _; // global logger + +use panic_probe as _; +use stm32f1xx_hal as _; + +use core::sync::atomic::{AtomicU32, Ordering}; + +pub mod application; + +// 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: AtomicU32 = AtomicU32::new(0); +defmt::timestamp!("{=u32}", COUNT.fetch_add(1, Ordering::Relaxed)); + +/// Terminates the application and makes `probe-run` exit with exit-code = 0 +pub fn exit() -> ! { + loop { + cortex_m::asm::bkpt(); + } +}