How to Use Rust for ARM Cortex-M Development

Compile Rust for ARM Cortex-M by adding the thumbv7m-none-eabi target, using no_std, and linking with the cortex-m crate.

Use the thumbv7m-none-eabi target triple with the no_std attribute to compile Rust code for ARM Cortex-M microcontrollers.

#![no_std]
#![no_main]

use panic_halt as _;

#[no_mangle]
pub extern "C" fn _start() -> ! {
    loop {}
}
  1. Add the Cortex-M target to your toolchain. rustup target add thumbv7m-none-eabi

  2. Create a new binary crate with the no_std attribute. cargo new --bin cortex-m-app && cd cortex-m-app

  3. Add the cortex-m and panic-halt dependencies to your Cargo.toml. cargo add cortex-m panic-halt

  4. Compile the project for the specific target architecture. cargo build --target thumbv7m-none-eabi

  5. Convert the resulting binary into a flashable ELF file. objcopy --output-target=elf32-littlerm target/thumbv7m-none-eabi/debug/cortex-m-app cortex-m-app.elf

  6. Flash the ELF file to your microcontroller using a debugger like OpenOCD. `openocd -f interface/stlink.cfg -f target/stm32f1x.cfg -c "program cortex-m-app.elf verify reset exit"