From d5c12e27e6af894189ac50f96548b2f15917685d Mon Sep 17 00:00:00 2001 From: Sebastian Date: Sun, 20 Nov 2022 20:58:52 +0100 Subject: [PATCH] Initial commit --- .cargo/config.toml | 19 +++++++++++ .gitignore | 2 ++ Cargo.toml | 78 +++++++++++++++++++++++++++++++++++++++++++ memory.x | 6 ++++ src/main.rs | 83 ++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 188 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/main.rs diff --git a/.cargo/config.toml b/.cargo/config.toml new file mode 100644 index 0000000..ef756db --- /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 STM32F103C6" +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..e59acf2 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,78 @@ +[package] +authors = ["LongHairedHacker "] +name = "rtic-bluepill-blinky" +edition = "2018" +version = "0.1.0" + +[dependencies] +cortex-m = "0.7.6" +cortex-m-rt = "0.7.2" +cortex-m-rtic = "1.1.3" +defmt = "0.3.2" +defmt-rtt = "0.3.2" +panic-probe = { version = "0.3.0", features = ["print-defmt"] } +stm32f1xx-hal = { version = "0.9.0", features = ["stm32f103", "rt"] } +embedded-hal = {version = "0.2.3"} +systick-monotonic = "1.0.0" + + +[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 = 'z' # <- +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..c2c8cf6 --- /dev/null +++ b/memory.x @@ -0,0 +1,6 @@ +/* Fake bluepill using STM32F103C6T6 */ +MEMORY +{ + FLASH : ORIGIN = 0x08000000, LENGTH = 32K + RAM : ORIGIN = 0x20000000, LENGTH = 10K +} diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..211913d --- /dev/null +++ b/src/main.rs @@ -0,0 +1,83 @@ +#![deny(unsafe_code)] +#![no_main] +#![no_std] + +use defmt_rtt as _; // global logger +use panic_probe as _; +use rtic::app; +use stm32f1xx_hal::gpio::PinState; +use stm32f1xx_hal::gpio::{gpioc::PC13, Output, PushPull}; +use stm32f1xx_hal::prelude::*; +use systick_monotonic::Systick; + +// 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() +} + +#[app(device = stm32f1xx_hal::pac, peripherals = true, dispatchers = [TIM3])] +mod app { + use super::*; + + #[shared] + struct Shared {} + + #[local] + struct Local { + led: PC13>, + state: bool, + } + + #[monotonic(binds = SysTick, default = true)] + type MonoTimer = Systick<1000>; + + #[init] + fn init(cx: init::Context) -> (Shared, Local, init::Monotonics) { + // Setup clocks + let mut flash = cx.device.FLASH.constrain(); + let rcc = cx.device.RCC.constrain(); + + defmt::info!("init"); + + let clocks = rcc + .cfgr + .use_hse(8.MHz()) + .sysclk(36.MHz()) + .pclk1(36.MHz()) + .freeze(&mut flash.acr); + + let mono = Systick::new(cx.core.SYST, clocks.pclk1().to_Hz()); + + // Setup LED + let mut gpioc = cx.device.GPIOC.split(); + let led = gpioc + .pc13 + .into_push_pull_output_with_state(&mut gpioc.crh, PinState::Low); + + // Schedule the blinking task + blink::spawn_after(1.secs().into()).unwrap(); + + ( + Shared {}, + Local { led, state: false }, + init::Monotonics(mono), + ) + } + + #[task(local = [led, state])] + fn blink(cx: blink::Context) { + if *cx.local.state { + defmt::info!("blink"); + cx.local.led.set_high(); + *cx.local.state = false; + } else { + defmt::info!("blonk"); + cx.local.led.set_low(); + *cx.local.state = true; + } + + blink::spawn_after(1.secs().into()).unwrap(); + } +}