init basic examples and setup

This commit is contained in:
2026-05-25 18:16:02 +02:00
commit 8028dfd5fc
28 changed files with 2150 additions and 0 deletions

23
.cargo/config.toml Normal file
View File

@@ -0,0 +1,23 @@
[build]
target = "thumbv7m-none-eabi"
[target.thumbv7m-none-eabi]
linker = "flip-link"
rustflags = [
"-C",
"link-arg=-Tlink.x",
"-C",
"link-arg=-Tdefmt.x",
]
[target.thumbv6m-none-eabi]
linker = "flip-link"
rustflags = [
"-C",
"link-arg=-Tlink.x",
"-C",
"link-arg=-Tdefmt.x",
]
[env]
DEFMT_LOG = "info"

7
.gitignore vendored Normal file
View File

@@ -0,0 +1,7 @@
target/
result
.direnv/
.DS_Store
*.log
Embed.local.*
.embed.local.*

77
AGENTS.md Normal file
View File

@@ -0,0 +1,77 @@
# AGENTS
## Map
- Root has workspace files.
- `examples/` has one crate per demo.
- `docs/hardware/` has board and wiring notes.
- `memory.x` is shared.
- `justfile` is source of truth for commands.
## Rules
- Read before edit.
- Keep fixes small.
- No broad refactor.
- No dependency upgrade unless asked.
- Keep examples independent.
- Keep demo reliability first.
- Use simple code.
- No hidden IDE steps.
- No fake hardware claims.
## Preserve
- `nix develop`
- `just fmt`
- `just check`
- `just build`
## Hardware
- Blue Pill first.
- `thumbv7m-none-eabi` first.
- Do not mix STM32F0 with STM32F103.
- If hardware detail is unclear:
- write the assumption
- add a TODO
- keep build green
## Style
- Caveman style.
- Short names.
- Short comments.
- Explain embedded idea.
- Do not explain obvious Rust syntax.
- No heap.
- No dynamic dispatch unless teaching needs it.
- No async before Embassy examples.
- `unsafe` only if required and justified.
## Commands
- `just build`
- `just build-blinky`
- `just build-timer`
- `just build-button`
- `just build-embassy-blinky`
- `just build-embassy-button`
- `just flash-blinky`
- `just flash-timer`
- `just flash-button`
- `just flash-embassy-blinky`
- `just flash-embassy-button`
- `just run-blinky`
- `just run-embassy-blinky`
- `just fmt`
- `just clippy`
- `just check`
- `just clean`
## Do Not
- Do not invent board output.
- Do not claim flash or debug worked on hardware unless tested.
- Do not hide chip mismatches.
- Do not add abstractions for one use.

1276
Cargo.lock generated Normal file

File diff suppressed because it is too large Load Diff

24
Cargo.toml Normal file
View File

@@ -0,0 +1,24 @@
[workspace]
members = [
"examples/blinky-basic",
"examples/blinky-timer",
"examples/button-input",
"examples/embassy-blinky",
"examples/embassy-button",
]
resolver = "2"
[workspace.package]
edition = "2024"
[workspace.dependencies]
cortex-m = { version = "=0.7.7", features = ["critical-section-single-core"] }
cortex-m-rt = "=0.7.5"
defmt = "=1.1.0"
defmt-rtt = "=1.2.0"
embassy-executor = "=0.10.0"
embassy-stm32 = "=0.6.0"
embassy-time = "=0.5.1"
nb = "=1.1.0"
panic-probe = { version = "=1.0.0", features = ["print-defmt"] }
stm32f1xx-hal = "=0.11.0"

149
README.md Normal file
View File

@@ -0,0 +1,149 @@
# Rust on STM32
## Hardware Baseline
- Default board: STM32F103C8T6 Blue Pill, Cortex-M3
- Default target: `thumbv7m-none-eabi`
- Default `probe-rs` chip name: `STM32F103C8`
- Default probe: ST-Link v2
- Also supported in docs: Rusty Probe and other `probe-rs` compatible probes
- Onboard LED: usually `PC13`, active-low on most Blue Pill boards
See [blue-pill.md](docs/hardware/blue-pill.md) for wiring notes.
## Important Target Note
- Blue Pill `STM32F103` is Cortex-M3 and uses `thumbv7m-none-eabi`.
- STM32F0 is Cortex-M0 and uses `thumbv6m-none-eabi`.
- Do not mix them.
- This repo leaves room for STM32F0 later, but the default path is STM32F103 only.
## Repo Layout
- [Cargo.toml](Cargo.toml) - workspace root and pinned shared dependencies
- [flake.nix](flake.nix) - reproducible development shell
- [.cargo/config.toml](.cargo/config.toml) - default target and linker settings
- [memory.x](memory.x) - conservative Blue Pill memory map
- [justfile](justfile) - build, flash, run, and lint commands
- [examples/README.md](examples/README.md) - example index
## Nix Setup
Use Nix flakes. No global `rustup`. No global `cargo install`.
```bash
nix develop
```
The dev shell provides:
- pinned Rust toolchain
- `cargo`, `rustc`, `rustfmt`, `clippy`
- `rust-src`
- targets for `thumbv7m-none-eabi` and `thumbv6m-none-eabi`
- `probe-rs`
- `flip-link`
- `cargo-binutils`
- `just`
## First Commands
```bash
nix develop
just build
just flash-blinky
just run-blinky
```
If your chip name differs from the default, override it:
```bash
PROBE_RS_CHIP=STM32F103C8 just flash-blinky
```
If you need to inspect available names:
```bash
probe-rs chip list | rg STM32F103
```
## Examples
- `blinky-basic` - plain `no_std` blink with a simple delay
- `blinky-timer` - periodic blink driven by a timer abstraction
- `button-input` - poll a button on `PA0`, mirror state on the LED, log transitions
- `embassy-blinky` - minimal async blink with Embassy
- `embassy-button` - minimal Embassy polling loop for a button on `PA0`
The button examples assume a simple external button:
- one side to `PA0`
- one side to `3V3`
- internal pull-down enabled in firmware
- common ground still required for the probe
## ST-Link Setup
Typical SWD wiring:
- `SWDIO` -> `SWDIO`
- `SWCLK` -> `SWCLK`
- `GND` -> `GND`
- `3V3` -> `3V3` reference
Keep `BOOT0` low for normal flash-and-run.
## Rusty Probe / probe-rs Note
- `probe-rs` is the primary flash, run, and debug path in this repo.
- Rusty Probe works if the host sees it as a `probe-rs` compatible probe.
- Start with `probe-rs list` to confirm the probe is visible.
## Memory Note
Many Blue Pill boards have 64K or 128K flash despite markings.
This repo uses a conservative 64K flash map in [memory.x](memory.x).
## Troubleshooting
### Probe not found
- Run `probe-rs list`.
- Check USB cable and power.
- Check `SWDIO`, `SWCLK`, `GND`, and `3V3`.
### Linux udev permissions
- You may need udev rules for ST-Link or your probe.
- Install the vendor or distro rules, then replug the probe.
### Wrong chip selected
- Run `probe-rs chip list | rg STM32F103`.
- Override the default with `PROBE_RS_CHIP=...`.
### No RTT or defmt output
- Use `just run-blinky` or `just run-embassy-blinky`.
- Confirm the firmware did not crash early.
- Confirm the probe stays attached.
- Confirm `BOOT0` is low.
### Blue Pill LED looks inverted
- That is expected on most boards.
- `PC13` is usually active-low, so driving it low turns the LED on.
## Safety
- Check board voltage before connecting anything.
- Check `SWDIO`, `SWCLK`, `GND`, and `3V3` wiring before flashing.
- Do not assume a random button or LED pinout without checking the board.
## Contribution
- Keep examples independent.
- Keep demo reliability first.
- Keep `nix develop`, `just fmt`, `just check`, and `just build` working.
- Document hardware assumptions when they matter.
- Do not claim hardware-tested behavior unless it was actually tested on hardware.

View File

@@ -0,0 +1,28 @@
# Blue Pill Notes
## Baseline
- Board: STM32F103C8T6 Blue Pill
- Core: Cortex-M3
- Default target: `thumbv7m-none-eabi`
## LED
- Most Blue Pill boards wire the onboard LED to `PC13`
- It is usually active-low
- `set_low()` usually turns it on
- `set_high()` usually turns it off
## Button Assumption
- The repo button examples use `PA0`
- Assumed wiring:
- button between `PA0` and `3V3`
- firmware enables internal pull-down
## Probe Wiring
- `SWDIO` -> `SWDIO`
- `SWCLK` -> `SWCLK`
- `GND` -> `GND`
- `3V3` -> `3V3` reference

38
examples/README.md Normal file
View File

@@ -0,0 +1,38 @@
# Examples
Each example is its own Cargo package.
## Packages
- `blinky-basic` - simple delay, simple LED ownership story
- `blinky-timer` - periodic blink with a timer abstraction
- `button-input` - poll `PA0`, mirror state to `PC13`, log transitions
- `embassy-blinky` - minimal Embassy executor and async blink
- `embassy-button` - minimal Embassy polling loop for `PA0`
## Build One Example
```bash
just build-blinky
just build-timer
just build-button
just build-embassy-blinky
just build-embassy-button
```
## Flash One Example
```bash
just flash-blinky
just flash-timer
just flash-button
just flash-embassy-blinky
just flash-embassy-button
```
## Run With Logs
```bash
just run-blinky
just run-embassy-blinky
```

View File

@@ -0,0 +1,14 @@
[package]
name = "blinky-basic"
version = "0.1.0"
edition = "2024"
publish = false
build = "build.rs"
[dependencies]
cortex-m.workspace = true
cortex-m-rt.workspace = true
defmt.workspace = true
defmt-rtt.workspace = true
panic-probe.workspace = true
stm32f1xx-hal = { workspace = true, features = ["medium", "stm32f103"] }

View File

@@ -0,0 +1,9 @@
fn main() {
println!("cargo:rerun-if-changed=../../memory.x");
println!(
"cargo:rustc-link-search={}",
std::path::PathBuf::from(std::env::var("CARGO_MANIFEST_DIR").unwrap())
.join("../..")
.display()
);
}

View File

@@ -0,0 +1,37 @@
#![deny(unsafe_code)]
#![no_std]
#![no_main]
use cortex_m_rt::entry;
use defmt::info;
use defmt_rtt as _;
use panic_probe as _;
use stm32f1xx_hal::{pac, prelude::*};
const ON_MS: u16 = 500;
const OFF_MS: u16 = 500;
#[entry]
fn main() -> ! {
let dp = pac::Peripherals::take().unwrap();
let cp = cortex_m::Peripherals::take().unwrap();
let mut rcc = dp.RCC.constrain();
let mut gpioc = dp.GPIOC.split(&mut rcc);
// Splitting the GPIO block gives this function ownership of the LED pin.
let mut led = gpioc.pc13.into_push_pull_output(&mut gpioc.crh);
let mut delay = cp.SYST.delay(&rcc.clocks);
info!("blinky-basic: PC13 LED is assumed active-low");
loop {
led.set_low();
info!("led on");
delay.delay_ms(ON_MS);
led.set_high();
info!("led off");
delay.delay_ms(OFF_MS);
}
}

View File

@@ -0,0 +1,15 @@
[package]
name = "blinky-timer"
version = "0.1.0"
edition = "2024"
publish = false
build = "build.rs"
[dependencies]
cortex-m.workspace = true
cortex-m-rt.workspace = true
defmt.workspace = true
defmt-rtt.workspace = true
nb.workspace = true
panic-probe.workspace = true
stm32f1xx-hal = { workspace = true, features = ["medium", "stm32f103"] }

View File

@@ -0,0 +1,9 @@
fn main() {
println!("cargo:rerun-if-changed=../../memory.x");
println!(
"cargo:rustc-link-search={}",
std::path::PathBuf::from(std::env::var("CARGO_MANIFEST_DIR").unwrap())
.join("../..")
.display()
);
}

View File

@@ -0,0 +1,35 @@
#![deny(unsafe_code)]
#![no_std]
#![no_main]
use cortex_m_rt::entry;
use defmt::info;
use defmt_rtt as _;
use nb::block;
use panic_probe as _;
use stm32f1xx_hal::{pac, prelude::*, timer::Timer};
const BLINK_HZ: u32 = 2;
#[entry]
fn main() -> ! {
let dp = pac::Peripherals::take().unwrap();
let cp = cortex_m::Peripherals::take().unwrap();
let mut rcc = dp.RCC.constrain();
let mut gpioc = dp.GPIOC.split(&mut rcc);
let mut led = gpioc.pc13.into_push_pull_output(&mut gpioc.crh);
// A timer gives stable periodic work without burning cycles in a delay loop.
let mut timer = Timer::syst(cp.SYST, &rcc.clocks).counter_hz();
timer.start(BLINK_HZ.Hz()).unwrap();
led.set_high();
info!("blinky-timer: {} Hz toggle loop", BLINK_HZ);
loop {
block!(timer.wait()).unwrap();
led.toggle();
info!("tick");
}
}

View File

@@ -0,0 +1,14 @@
[package]
name = "button-input"
version = "0.1.0"
edition = "2024"
publish = false
build = "build.rs"
[dependencies]
cortex-m.workspace = true
cortex-m-rt.workspace = true
defmt.workspace = true
defmt-rtt.workspace = true
panic-probe.workspace = true
stm32f1xx-hal = { workspace = true, features = ["medium", "stm32f103"] }

View File

@@ -0,0 +1,9 @@
fn main() {
println!("cargo:rerun-if-changed=../../memory.x");
println!(
"cargo:rustc-link-search={}",
std::path::PathBuf::from(std::env::var("CARGO_MANIFEST_DIR").unwrap())
.join("../..")
.display()
);
}

View File

@@ -0,0 +1,47 @@
#![deny(unsafe_code)]
#![no_std]
#![no_main]
use cortex_m_rt::entry;
use defmt::info;
use defmt_rtt as _;
use panic_probe as _;
use stm32f1xx_hal::{pac, prelude::*};
const POLL_MS: u8 = 25;
#[entry]
fn main() -> ! {
let dp = pac::Peripherals::take().unwrap();
let cp = cortex_m::Peripherals::take().unwrap();
let mut rcc = dp.RCC.constrain();
let mut gpioa = dp.GPIOA.split(&mut rcc);
let mut gpioc = dp.GPIOC.split(&mut rcc);
let button = gpioa.pa0.into_pull_down_input(&mut gpioa.crl);
let mut led = gpioc.pc13.into_push_pull_output(&mut gpioc.crh);
let mut delay = cp.SYST.delay(&rcc.clocks);
let mut was_pressed = button.is_high();
led.set_high();
info!("button-input: PA0 button to 3V3 with internal pull-down");
loop {
let pressed = button.is_high();
if pressed != was_pressed {
was_pressed = pressed;
if pressed {
led.set_low();
info!("button pressed");
} else {
led.set_high();
info!("button released");
}
}
delay.delay_ms(POLL_MS);
}
}

View File

@@ -0,0 +1,15 @@
[package]
name = "embassy-blinky"
version = "0.1.0"
edition = "2024"
publish = false
build = "build.rs"
[dependencies]
cortex-m-rt.workspace = true
defmt.workspace = true
defmt-rtt.workspace = true
embassy-executor = { workspace = true, features = ["defmt", "executor-thread", "platform-cortex-m"] }
embassy-stm32 = { workspace = true, features = ["defmt", "stm32f103c8", "time-driver-tim2"] }
embassy-time.workspace = true
panic-probe.workspace = true

View File

@@ -0,0 +1,9 @@
fn main() {
println!("cargo:rerun-if-changed=../../memory.x");
println!(
"cargo:rustc-link-search={}",
std::path::PathBuf::from(std::env::var("CARGO_MANIFEST_DIR").unwrap())
.join("../..")
.display()
);
}

View File

@@ -0,0 +1,28 @@
#![deny(unsafe_code)]
#![no_std]
#![no_main]
use defmt::info;
use defmt_rtt as _;
use embassy_executor::Spawner;
use embassy_stm32::gpio::{Level, Output, Speed};
use embassy_time::Timer;
use panic_probe as _;
#[embassy_executor::main]
async fn main(_spawner: Spawner) {
let p = embassy_stm32::init(Default::default());
let mut led = Output::new(p.PC13, Level::High, Speed::Low);
info!("embassy-blinky: async blink on PC13");
loop {
led.set_low();
info!("led on");
Timer::after_millis(500).await;
led.set_high();
info!("led off");
Timer::after_millis(500).await;
}
}

View File

@@ -0,0 +1,15 @@
[package]
name = "embassy-button"
version = "0.1.0"
edition = "2024"
publish = false
build = "build.rs"
[dependencies]
cortex-m-rt.workspace = true
defmt.workspace = true
defmt-rtt.workspace = true
embassy-executor = { workspace = true, features = ["defmt", "executor-thread", "platform-cortex-m"] }
embassy-stm32 = { workspace = true, features = ["defmt", "stm32f103c8", "time-driver-tim2"] }
embassy-time.workspace = true
panic-probe.workspace = true

View File

@@ -0,0 +1,9 @@
fn main() {
println!("cargo:rerun-if-changed=../../memory.x");
println!(
"cargo:rustc-link-search={}",
std::path::PathBuf::from(std::env::var("CARGO_MANIFEST_DIR").unwrap())
.join("../..")
.display()
);
}

View File

@@ -0,0 +1,38 @@
#![deny(unsafe_code)]
#![no_std]
#![no_main]
use defmt::info;
use defmt_rtt as _;
use embassy_executor::Spawner;
use embassy_stm32::gpio::{Input, Level, Output, Pull, Speed};
use embassy_time::Timer;
use panic_probe as _;
#[embassy_executor::main]
async fn main(_spawner: Spawner) {
let p = embassy_stm32::init(Default::default());
let button = Input::new(p.PA0, Pull::Down);
let mut led = Output::new(p.PC13, Level::High, Speed::Low);
let mut was_pressed = button.is_high();
info!("embassy-button: polling PA0 every 25 ms");
loop {
let pressed = button.is_high();
if pressed != was_pressed {
was_pressed = pressed;
if pressed {
led.set_low();
info!("button pressed");
} else {
led.set_high();
info!("button released");
}
}
Timer::after_millis(25).await;
}
}

96
flake.lock generated Normal file
View File

@@ -0,0 +1,96 @@
{
"nodes": {
"flake-utils": {
"inputs": {
"systems": "systems"
},
"locked": {
"lastModified": 1731533236,
"narHash": "sha256-l0KFg5HjrsfsO/JpG+r7fRrqm12kzFHyUHqHCVpMMbI=",
"owner": "numtide",
"repo": "flake-utils",
"rev": "11707dc2f618dd54ca8739b309ec4fc024de578b",
"type": "github"
},
"original": {
"owner": "numtide",
"repo": "flake-utils",
"type": "github"
}
},
"nixpkgs": {
"locked": {
"lastModified": 1767313136,
"narHash": "sha256-16KkgfdYqjaeRGBaYsNrhPRRENs0qzkQVUooNHtoy2w=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "ac62194c3917d5f474c1a844b6fd6da2db95077d",
"type": "github"
},
"original": {
"owner": "NixOS",
"ref": "nixos-25.05",
"repo": "nixpkgs",
"type": "github"
}
},
"nixpkgs_2": {
"locked": {
"lastModified": 1744536153,
"narHash": "sha256-awS2zRgF4uTwrOKwwiJcByDzDOdo3Q1rPZbiHQg/N38=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "18dd725c29603f582cf1900e0d25f9f1063dbf11",
"type": "github"
},
"original": {
"owner": "NixOS",
"ref": "nixpkgs-unstable",
"repo": "nixpkgs",
"type": "github"
}
},
"root": {
"inputs": {
"flake-utils": "flake-utils",
"nixpkgs": "nixpkgs",
"rust-overlay": "rust-overlay"
}
},
"rust-overlay": {
"inputs": {
"nixpkgs": "nixpkgs_2"
},
"locked": {
"lastModified": 1779679233,
"narHash": "sha256-qSIAAfK66X6waos6alIYxVze1ZU3C/WPp7NlN4ooP54=",
"owner": "oxalica",
"repo": "rust-overlay",
"rev": "47ab6c7b3c6a68beac60067490240efa32ae344c",
"type": "github"
},
"original": {
"owner": "oxalica",
"repo": "rust-overlay",
"type": "github"
}
},
"systems": {
"locked": {
"lastModified": 1681028828,
"narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=",
"owner": "nix-systems",
"repo": "default",
"rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e",
"type": "github"
},
"original": {
"owner": "nix-systems",
"repo": "default",
"type": "github"
}
}
},
"root": "root",
"version": 7
}

55
flake.nix Normal file
View File

@@ -0,0 +1,55 @@
{
description = "Rust on STM32";
inputs = {
flake-utils.url = "github:numtide/flake-utils";
nixpkgs.url = "github:NixOS/nixpkgs/nixos-25.05";
rust-overlay.url = "github:oxalica/rust-overlay";
};
outputs = {
self,
flake-utils,
nixpkgs,
rust-overlay,
}:
flake-utils.lib.eachDefaultSystem (system: let
pkgs = import nixpkgs {
inherit system;
overlays = [(import rust-overlay)];
};
rustToolchain =
pkgs.rust-bin.stable."1.95.0".default.override {
extensions = [
"clippy"
"llvm-tools-preview"
"rust-src"
"rustfmt"
];
targets = [
"thumbv7m-none-eabi"
"thumbv6m-none-eabi"
];
};
in {
devShells.default = pkgs.mkShell {
packages = [
rustToolchain
pkgs.cargo-binutils
pkgs.flip-link
pkgs.just
pkgs.llvmPackages.bintools
pkgs.probe-rs
];
RUST_SRC_PATH = "${rustToolchain}/lib/rustlib/src/rust/library";
shellHook = ''
echo "Rust on STM32 dev shell"
echo "Default target: thumbv7m-none-eabi"
echo "Flash and run commands use PROBE_RS_CHIP if you need to override the default."
'';
};
});
}

62
justfile Normal file
View File

@@ -0,0 +1,62 @@
set shell := ["bash", "-eu", "-o", "pipefail", "-c"]
target := "thumbv7m-none-eabi"
chip := env_var_or_default("PROBE_RS_CHIP", "STM32F103C8")
build:
cargo build --workspace
build-blinky:
cargo build -p blinky-basic
build-timer:
cargo build -p blinky-timer
build-button:
cargo build -p button-input
build-embassy-blinky:
cargo build -p embassy-blinky
build-embassy-button:
cargo build -p embassy-button
flash-blinky:
cargo build -p blinky-basic
probe-rs download --chip {{chip}} target/{{target}}/debug/blinky-basic
flash-timer:
cargo build -p blinky-timer
probe-rs download --chip {{chip}} target/{{target}}/debug/blinky-timer
flash-button:
cargo build -p button-input
probe-rs download --chip {{chip}} target/{{target}}/debug/button-input
flash-embassy-blinky:
cargo build -p embassy-blinky
probe-rs download --chip {{chip}} target/{{target}}/debug/embassy-blinky
flash-embassy-button:
cargo build -p embassy-button
probe-rs download --chip {{chip}} target/{{target}}/debug/embassy-button
run-blinky:
cargo build -p blinky-basic
probe-rs run --chip {{chip}} target/{{target}}/debug/blinky-basic
run-embassy-blinky:
cargo build -p embassy-blinky
probe-rs run --chip {{chip}} target/{{target}}/debug/embassy-blinky
fmt:
cargo fmt --all
clippy:
cargo clippy --workspace --bins -- -D warnings
check:
cargo check --workspace
clean:
cargo clean

7
memory.x Normal file
View File

@@ -0,0 +1,7 @@
/* Conservative Blue Pill memory map.
Some boards expose 128K flash, but this repo stays within 64K. */
MEMORY
{
FLASH : ORIGIN = 0x08000000, LENGTH = 64K
RAM : ORIGIN = 0x20000000, LENGTH = 20K
}

5
rust-toolchain.toml Normal file
View File

@@ -0,0 +1,5 @@
[toolchain]
channel = "1.95.0"
components = ["clippy", "rustfmt", "rust-src", "llvm-tools-preview"]
targets = ["thumbv7m-none-eabi", "thumbv6m-none-eabi"]
profile = "minimal"