A simple rust blinky example on a stm32g031j6 microcontroller on the stm32g0316-disco development board. The board includes a st-link v2 programmer. The stm32g031j6 is a quite small 8pin mcu, which has only 8KB ram and 32KB flash.
rustup
- is recommended to install rust and its componentscargo install cargo-binutils
- forcargo size
andcargo objdump -- --disassemble
, etc…cargo install probe-rs-tools
- flash and debug using theprobe-rs project
cargo embed --release
This file specifies the memory layout of the stm32g031j6
mcu: 8KB sram starting at address 0x20000000
and 32KB of flash starting at address 0x08000000.
That layout assumes no bootloader, with a bootloader the FLASH ORIGIN needs to be moved to something like 0x08002000 for a 8KB bootloader. Not that I know of a bootloader for the stm32g031j6.
This file holds the cargo build configuration. It is setup to build using the thumbv6m-none-eabi (cortex-m0)
target by default. Also, it'll show the program size (using arm-none-eabi-size
) when
you run cargo run
.
Embed.toml
is configuration for the cargo embed tool.
See probe-run "Runs embedded programs just like native ones", just cargo install probe-run
away.
Change the runner
in .cargo/config
to: runner = 'arm-none-eabi-gdb'
, and
then in one terminal run:
cargo flash --chip stm32g031j6mx --gdb
and in another:
cargo run
Using stlink
stlink
(1.6.1 and up) can also be used to flash the mcu on this board. stlink can't flash
ELF files directly, so you need a binary dump of the program code, and you have to
specify the address of the flash (0x8000000
):
arm-none-eabi-objcopy -Obinary \
target/thumbv6m-none-eabi/debug/stm32g0-disco-rs \
target/thumbv6m-none-eabi/debug/stm32g0-disco-rs.bin
st-flash --debug write \
target/thumbv6m-none-eabi/debug/stm32g0-disco-rs.bin 0x8000000
- Discover the world of microcontrollers through Rust! - introductory book about rust on microcontrollers
- awesome embedded rust - Curated list of resources for Embedded and Low-level development in the Rust programming language
- libopencm3 miniblink - blinkies for any board supported. Useful for testing your toolchain and build environment