Embedded Rust

Bare-Metal STM32 in Rust: No HAL, No Embassy — and It Actually Runs Under QEMU

The same QEMU, the same boards, the same toolchain — but with no HAL in the firmware, there is nothing to wait on. This three-crate program prints immediately on both of QEMU's STM32 machines, loops about 750 million counter increments a second, and fits in 1,332 bytes of flash image. Get the flash base wrong and it faults before printing a single line.

Written November 8, 2026 · 12 min read

By the end of this guide
  • The firmware
  • The two memory maps
  • What it costs
  • The runs: it prints on both machines

What this guide is

This guide is the inverse of the companion guide on this site about Embassy on STM32. There, a real Embassy program compiles cleanly and then stops at embassy_stm32::init() under QEMU, waiting on STM32 clock bits the emulated boards do not implement, and its timer never ticks. Here, the firmware has no HAL, no device crate, and no peripheral registers to wait on — three crates, one loop, one counter — and it prints immediately on both machines used here. The difference is not the emulator. It is the dependency tree.

The numbers in this guide are the fresh ones. The 2026-11-07 figures are quoted alongside as a cross-check wherever they differ, and no number is carried forward without re-execution.

The reference for the runtime used here is the cortex-m-rt documentation, version 0.7.5. It states that the crate populates the vector table so the device can boot, initializes static variables before the entry point, and provides the #[entry] attribute to declare that entry point. It also states that memory.x must define at least two regions, named FLASH and RAM, and that the stack is placed at the end of the RAM region unless _stack_start says otherwise. The QEMU Arm system emulator documentation states that a board model must be chosen with the machine option because there is no default, that firmware written for one machine will not run at all on another, and it lists the Stellaris boards and the STM32 boards QEMU ships. The program below follows those two sources, with the versions the box actually resolved.

The firmware

Three crates, pinned exactly. No HAL, no Embassy, no device crate, no allocator.

TOML
[package]
name = "stm32bare"
version = "0.1.0"
edition = "2021"

[dependencies]
cortex-m-rt = "=0.7.7"
cortex-m-semihosting = "=0.5.0"
panic-halt = "=0.2.0"

[profile.release]
opt-level = "z"
lto = true

The program prints twice, then counts to one million, prints a line, and starts over, forever. The counter is the whole workload: one line of output per million increments, so the print count measures the loop rate directly.

Rust
#![no_std]
#![no_main]

use cortex_m_rt::entry;
use cortex_m_semihosting::hprintln;
use panic_halt as _;

#[entry]
fn main() -> ! {
    hprintln!("bare-metal-ok");
    hprintln!("no-hal no-embassy");
    let mut n: u32 = 0;
    loop {
        n = n.wrapping_add(1);
        if n == 1_000_000 {
            hprintln!("loop-1m");
            n = 0;
        }
    }
}

Two details in that listing deserve a note. First, hprintln! returns nothing in cortex-m-semihosting 0.5.0, so there is no .unwrap() and no ? on it — adding one fails the build. Second, panic-halt supplies the mandatory panic handler by looping forever; a bare-metal program has no runtime to unwind into, so the handler is a crate, not a function you write.

The runtime needs a linker script, and version 0.7 of cortex-m-rt does not pass one itself. The build script copies the project's memory.x into the build output directory and tells the linker to use the runtime's link.x with it. This is the same fix the Embassy probe used a day earlier.

Rust
use std::env;
use std::fs::File;
use std::io::Write;
use std::path::PathBuf;

fn main() {
    let out = &PathBuf::from(env::var_os("OUT_DIR").unwrap());
    File::create(out.join("memory.x"))
        .unwrap()
        .write_all(include_bytes!("memory.x"))
        .unwrap();
    println!("cargo:rustc-link-search={}", out.display());
    println!("cargo:rustc-link-arg=-Tlink.x");
    println!("cargo:rerun-if-changed=memory.x");
}

The target is set once, in the project configuration, so no --target flag is needed on any build command.

TOML
[build]
target = "thumbv7m-none-eabi"

The two memory maps

This is the load-bearing part of the guide, and the part most setup guides get wrong by omission: the flash base must match the machine. The two machines used here disagree about where flash lives. The first, lm3s6965evb, is a Stellaris Cortex-M3 board with 256 KB of flash at address zero and 64 KB of RAM. The second, stm32vldiscovery, is a real STM32 part with 128 KB of flash at 0x08000000 and only 8 KB of RAM. The RAM size matters too: the first attempt at the second board used 20 KB of RAM, which put the initial stack pointer past the end of the board's real RAM. The working map uses 8 KB.

For the lm3s6965evb board:

Output
MEMORY
{
  FLASH : ORIGIN = 0x00000000, LENGTH = 256K
  RAM : ORIGIN = 0x20000000, LENGTH = 64K
}

For the stm32vldiscovery board:

Output
MEMORY
{
  FLASH : ORIGIN = 0x08000000, LENGTH = 128K
  RAM : ORIGIN = 0x20000000, LENGTH = 8K
}

Everything else in the project is identical between the two builds. Only memory.x changes, and the rebuild after swapping it takes about 0.17 seconds.

What it costs

The cold release build returns 0 in 3.18 seconds, locking 24 packages, and the same figure came back on the 2026-11-07 probe to the hundredth of a second. The debug build returns 0 in 3.02 seconds. Both builds link with rust-lld alone: there is no arm-none-eabi-gcc, and none was needed, despite the toolchain guides that list it as a requirement.

Build Time ELF size
Release, cold 3.18 seconds 133,520 bytes
Debug 3.02 seconds 931,568 bytes
Release, incremental after a memory.x swap 0.16–0.18 seconds 133,520 bytes

The section table for the release binary, from size -A -d, is short enough to quote in full: the vector table takes 1,024 bytes, the text section 264, read-only data 44, data 0, and BSS 8 — 1,340 bytes loadable, 1,531 in total including the non-loadable sections. The raw flash image, extracted with llvm-objcopy -O binary, is 1,332 bytes, the same for both geometries. The entry point address confirms which geometry was linked: 0x401 for the lm3s6965evb build, 0x8000401 for the stm32vldiscovery build.

For scale, the debug ELF is 931,568 bytes against the Embassy app's debug ELF of 2,154,572 bytes, measured with the same profile and the same toolchain — 43% of the size, for a program that runs where the Embassy app hangs. Most of the 931,568 bytes is debug information; the release ELF is 133,520 bytes, and the flash image is 1,332.

One toolchain wrinkle: llvm-objcopy ships inside the Rust toolchain directory but is not on the shell path, and rustup run stable llvm-objcopy fails because there is no proxy for it. The working form is the full path:

Command
/home/user/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/bin/llvm-objcopy -O binary target/thumbv7m-none-eabi/release/stm32bare /tmp/stm32bare-lm3s.bin

The runs: it prints on both machines

The run command is the same for both boards, with only the machine name changing. Semihosting carries the prints to the terminal, and timeout stops QEMU because the firmware loops forever by design — a return code of 124 here means the timeout stopped a working program, not that anything crashed.

Output
timeout 10 qemu-system-arm -machine lm3s6965evb -cpu cortex-m3 -nographic -semihosting -kernel target/thumbv7m-none-eabi/release/stm32bare

The first lines of the log, verbatim:

Output
Timer with period zero, disabling
bare-metal-ok
no-hal no-embassy
loop-1m
loop-1m

The Timer with period zero, disabling line is the lm3s6965evb machine's own timer, not the firmware — it appears before any firmware output on every run of that board, and it does not appear on the stm32vldiscovery runs. The two startup lines print exactly once each, and then the loop lines follow until the timeout kills QEMU.

Machine Time Lines Rate
lm3s6965evb 10.012 seconds 7,691 ≈768
lm3s6965evb 5.006 seconds 3,827 ≈765
stm32vldiscovery 5.010 seconds 3,815 ≈762

Each loop-1m line is one million counter increments, so the rate is also ≈730–770 million increments a second under QEMU's translation. The range, not a single figure, is the claim: the 2026-11-07 probe measured 7,518 lines in 10.010 seconds (≈751 per second) and 3,663 in 5 seconds (≈733 per second), so five timed runs across two days span ≈733–769 prints a second. The shorter runs sit a few percent lower because QEMU's startup takes a larger share of a 5-second window. The two machines agree with each other within the same few percent, which is the point: the firmware runs at the same speed on the real STM32 geometry as on the lm3s board.

The fault matrix: a wrong flash base faults, silently

The central table of this guide is the one you hit by accident. Swap the geometries — build with the STM32 flash base and run on the lm3s board, or the reverse — and the firmware prints nothing at all. QEMU aborts with the same fatal line in both directions:

Output
qemu: fatal: Lockup: can't escalate 3 to HardFault (current priority -1)
Flash base in memory.x Machine Result
0x00000000 (lm3s) lm3s6965evb Return code 124, prints (7,691 lines in 10 seconds)
0x08000000 (STM32) stm32vldiscovery Return code 124, prints (3,815 lines in 5 seconds)
0x08000000 (STM32) lm3s6965evb Return code 134, zero output, lockup
0x00000000 (lm3s) stm32vldiscovery Return code 134, zero output, lockup

Both fault runs were executed fresh for this guide, with their own log files — the 2026-11-07 probe overwrote its first fault log in place with a later successful run, so its fault text was verbatim from the transcript rather than from a kept file. The fresh runs confirm it exactly: the wrong-base run on lm3s6965evb aborts after 1,827 ms with return code 134 and zero output lines, and the wrong-base run on stm32vldiscovery aborts after 2,786 ms with return code 134 and zero output lines.

The lockup line is byte-identical to the diagnostic the Embassy probe documented for a missing vector table, because from the CPU's point of view that is what it is: the vector table is loaded where the machine has no flash, so the reset fetch faults, the fault handler faults, and the core locks up. The diagnostic to teach is therefore a matching rule, not a debugging session: match the memory.x flash origin to the machine — 0x00000000 for lm3s, 0x08000000 for STM32 — and a HardFault lockup with zero output means the geometry is wrong, not that QEMU is broken.

What this guide does not cover

No STM32 hardware was involved in any run. Everything here is QEMU emulation, and no on-device behavior is claimed — the same honesty line as the Embassy guide. A real STM32F103C8 board is a different machine from both QEMU boards: QEMU ships no F103 board, and the stm32vldiscovery board is an STM32F100 part. Running this firmware on real hardware needs a debug probe and a flash tool, neither of which was exercised here, and the semihosting prints would need a debugger or emulator on the other end — they are not something to flash onto a bare board and expect to see.

No peripheral is touched. There is no GPIO, no clock configuration, no timer, and no interrupt handler in this program, by design: the absence of peripherals is exactly why it runs. The moment the firmware waits on a peripheral register, it inherits the emulation limits the Embassy guide documents. The next step from here is a peripheral access crate for the chip in hand, not a bigger loop.

Checklist

  • Before building, check the target matches the core: the boards here are Cortex-M3, so the target is thumbv7m-none-eabi, and the memory.x flash origin must match the machine you will run on.
  • Before trusting a link, check the ELF has a vector table and the entry point matches the geometry: 0x401 for the lm3s build, 0x8000401 for the STM32 build.
  • Before running under QEMU, check the extracted QEMU's library path is set: without LD_LIBRARY_PATH pointed at the extracted libraries, QEMU fails before it starts with a libfdt.so.1 load error and return code 127.
  • Before reading a lockup, check the geometry first: a HardFault lockup with zero output means the flash base is wrong for the machine, not that the firmware is broken.
  • Before calling hprintln! a result, remember it returns nothing: no unwrap, no question mark.
  • Before concluding a run crashed, check the return code: 124 after the prints means the timeout stopped a looping program, and 134 with zero output means the core locked up at reset.
  • Before flashing anything onto real hardware, remember this was QEMU only: no board run was executed, and no board output is claimed.

Sources

  • cortex-m-rt documentation, version 0.7.5 — startup code and minimal runtime for Cortex-M; populates the vector table; initializes statics before the entry point; #[entry] declares the entry point; memory.x must define FLASH and RAM regions; the stack sits at the end of RAM unless _stack_start says otherwise; the worked example uses flash at 0x08000000 with 64K and RAM at 0x20000000 with 20K for the STM32F103C8T6.
  • cortex-m-rt, version 0.7.7 — the version this guide pins, released 2026-09-24; the documentation page defaults to 0.7.5, so the pinned version is newer than the page most readers land on.
  • QEMU Arm system emulator documentation — a board model must be chosen with the machine option because there is no default; firmware written for one machine will not run at all on another; the board list includes the Stellaris boards (lm3s6965evb, lm3s811evb) and the STM32 boards (netduino2, netduinoplus2, olimex-stm32-h405, stm32vldiscovery).
  • Rust Embassy on STM32: What Compiles, What Runs, and the QEMU Gap — the companion guide on this site: the Embassy app compiles and the executor-only firmware prints, but the full app hangs at embassy_stm32::init() under QEMU with zero timer ticks; its debug ELF is 2,154,572 bytes, against 931,568 here.
  • Rust on ARM Cortex-M: no_std, memory.x and PAC vs HAL — the nearest existing guide: it covers the same target triple, the same memory.x layout, and the same lm3s6965evb board, with size tables of its own — but its QEMU run is explicitly untested ("QEMU was not installed on the machine used for this article, so the QEMU run described below is untested"), its register code is build-only, and it runs no timed loop and no fault matrix. This guide is the executed half of that article.

Keep Exploring