Blink an LED on STM32 With Rust and stm32f4xx-hal
Build a bare-metal Rust blinky for an STM32F411 Nucleo board with stm32f4xx-hal, from Cargo.toml and memory.x to the toggle loop and the flash command.

Blinking an LED is the smallest program that proves a whole toolchain: the compiler targets the right core, the linker places code where the chip boots from, the debug probe reaches the flash, and the pin really changes state. This post builds it for a NUCLEO-F411RE board, whose user LED is on pin PA5, using Rust 1.85, stm32f4xx-hal 0.21 and cortex-m-rt 0.7. It assumes you know what #![no_std] removes and starts from an empty directory.
The board matters because the pin does. On a NUCLEO-F411RE the green LED LD2 is wired to PA5 according to the ST user manual for the Nucleo-64 boards (UM1724). Other boards use other pins, and that one number is the only part of the code that changes between them.
Install the target and the flash tool
The STM32F411 has a Cortex-M4F core, so the right Rust target is thumbv7em-none-eabihf: Thumb-2 with the extended DSP instructions and a hardware floating-point ABI.
rustup target add thumbv7em-none-eabihf
cargo install probe-rs-tools --lockedprobe-rs-tools provides the probe-rs command that flashes and runs firmware over the on-board ST-LINK. The Nucleo's USB connector reaches that debugger directly, so no extra hardware is needed.
The project layout
Create the project and four small files.
cargo new --bin blinky
cd blinky
mkdir .cargoCargo.toml lists the dependencies. stm32f4xx-hal needs a feature that names the exact chip, because the register layout differs by family member.
[package]
name = "blinky"
version = "0.1.0"
edition = "2021"
[dependencies]
cortex-m = "0.7"
cortex-m-rt = "0.7"
panic-halt = "1.0"
[dependencies.stm32f4xx-hal]
version = "0.21"
features = ["stm32f411"]
[profile.release]
opt-level = "s"
debug = true
lto = truedebug = true costs nothing on the chip, because debug info stays in the ELF file on the host and is never flashed. It lets the debugger show source lines later.
The linker needs to know where flash and RAM are. The F411RE has 512 KiB of flash at 0x08000000 and 128 KiB of RAM at 0x20000000, from the datasheet's memory map. cortex-m-rt reads a file called memory.x in the project root:
MEMORY
{
FLASH : ORIGIN = 0x08000000, LENGTH = 512K
RAM : ORIGIN = 0x20000000, LENGTH = 128K
}The Cargo configuration selects the target and tells cargo run to flash through probe-rs:
[build]
target = "thumbv7em-none-eabihf"
[target.thumbv7em-none-eabihf]
runner = "probe-rs run --chip STM32F411RETx"
rustflags = ["-C", "link-arg=-Tlink.x"]link.x is the linker script that cortex-m-rt generates; it includes your memory.x. Forgetting that flag produces a binary with no vector table, and the chip hard faults at reset.
The program
#![no_std]
#![no_main]
use cortex_m_rt::entry;
use panic_halt as _;
use stm32f4xx_hal::{pac, prelude::*};
#[entry]
fn main() -> ! {
let dp = pac::Peripherals::take().unwrap();
let cp = cortex_m::Peripherals::take().unwrap();
let rcc = dp.RCC.constrain();
let clocks = rcc.cfgr.sysclk(48.MHz()).freeze();
let gpioa = dp.GPIOA.split();
let mut led = gpioa.pa5.into_push_pull_output();
let mut delay = cp.SYST.delay(&clocks);
loop {
led.toggle();
delay.delay_ms(500u32);
}
}Read it top to bottom, because each line is one idea.
pac::Peripherals::take() returns the singleton that owns every peripheral register block. It returns Some once and None afterwards, so no two parts of the program can both believe they own the GPIO. That is Rust's ownership rule applied to hardware, and it is why the HAL's pin types are safe to hand around.
cortex_m::Peripherals::take() is the same idea for the core's own peripherals (SysTick, NVIC), which live outside the chip vendor's register map.
dp.RCC.constrain() turns the raw reset and clock control registers into a builder. sysclk(48.MHz()).freeze() writes the clock tree: the F411 boots on its 16 MHz internal oscillator, and this asks the HAL to reach 48 MHz through the PLL. freeze() returns a Clocks value that records the result, and the type exists to prove to other code that the clocks are final. Anything that needs a frequency, such as a delay, takes &clocks.
dp.GPIOA.split() enables the GPIOA clock in RCC and returns one field per pin. A pin starts in a reset-state type; into_push_pull_output() consumes it and returns a pin whose type says "output". After that call the compiler will not let you read the pin as an input, and the previous pin value is gone.
cp.SYST.delay(&clocks) builds a blocking delay from the SysTick timer, using the clock frequency to convert milliseconds to ticks. led.toggle() flips the output data register bit for PA5.
Build and run
cargo run --releaseCargo builds, then hands the ELF to the runner, which erases and writes flash and starts the core. A successful run prints something like:
Compiling blinky v0.1.0
Finished `release` profile [optimized + debuginfo] target(s) in 11.20s
Running `probe-rs run --chip STM32F411RETx target/thumbv7em-none-eabihf/release/blinky`
Erasing ✔ 100% [####################] 16.00 KiB @ 62.47 KiB/s (took 0s)
Programming ✔ 100% [####################] 1.50 KiB @ 22.47 KiB/s (took 0s)
Finished in 0.34sThe sizes and times are what one board printed and will differ on yours. After "Finished", LD2 blinks once per second: 500 ms on, 500 ms off, because each toggle flips the state and the loop sleeps half a second between them. probe-rs run keeps attached and will print panic output or logs if the program emits any, and ends on Ctrl-C.
Three things that go wrong
The LED stays dark and the chip looks halted. The most common cause is a missing memory.x or missing -Tlink.x, which leaves the reset vector wrong. Check with arm-none-eabi-objdump -h or cargo size that .vector_table sits at 0x08000000.
**error: no matching package named stm32f4xx-hal or a wall of unresolved pac:: items.** The chip feature is missing. With no stm32f411 feature the HAL cannot select a peripheral set.
The blink is slower or faster than expected. The delay uses the frequency you froze. If you remove the sysclk call the HAL runs at the 16 MHz default, and the delay adjusts, so the blink time stays right. The delay is only wrong if you build Clocks incorrectly by hand. A visible drift usually means the program is running from an unexpected clock source.
A debugging aid: reading led.is_set_high() after a toggle tells you the state the program believes, which separates a software problem from a wiring or board problem.
Why a HAL and not raw registers
You can write the same program against the PAC alone: set a bit in RCC.ahb1enr, set a field in GPIOA.moder, toggle a bit in GPIOA.odr. It is a dozen lines of unsafe-free but opaque register writes. The HAL buys you type states and a safe clock path; the cost is a dependency and a compile time that grows with the number of chip variants. The ecosystem layering, from PAC through HAL to board crate, is covered in PACs vs HALs vs BSPs, and a more portable approach through the embedded-hal traits is in writing a portable sensor driver.
When this does not apply
If your board is a different STM32 family (an L4, a G0, an H7), the crate name and feature change, from stm32f4xx-hal to stm32l4xx-hal and so on, and the pin and clock API differ in detail. If you want to avoid the busy-wait entirely, delay_ms blocks the core, which is fine for a blinky and wrong for firmware that has to do other work; the async alternative is in Embassy on STM32.


