Skip to content

mnerv/hellostm32

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

HelloSTM32

Learning how to use STM32 MCUs by setting up a build environment with Meson, downloading the ARM compiler, exploring the STM32CubeIDE directory structure, and modifying it to suit personal needs and style.

Development

Guide to setting up development tools: follow your operating-system–specific instructions first, then proceed with the Meson guide.

meson

Use meson to configure your build directory and ninja to compile:

meson setup build --cross-file toolchain/cross.ini
ninja -C build

Linux

Download arm toolchains for Linux.

mkdir -p .tools && cd .tools
wget "https://developer.arm.com/-/media/Files/downloads/gnu/14.2.rel1/binrel/arm-gnu-toolchain-14.2.rel1-x86_64-arm-none-eabi.tar.xz"
tar -xf arm-gnu-toolchain-14.2.rel1-x86_64-arm-none-eabi.tar.xz

Set environment in the current shell environment.

export PATH="$(pwd)/.tools/bin:$PATH"

Optional: Symlink compile_commands.json to root directory for clangd/ccls.

ln -sfn ./build/compile_commands.json .

Windows

Download arm toolchains for Windows.

mkdir .tools; cd .tools
Invoke-WebRequest `
  -Uri "https://developer.arm.com/-/media/Files/downloads/gnu/14.2.rel1/binrel/arm-gnu-toolchain-14.2.rel1-mingw-w64-x86_64-arm-none-eabi.zip" `
  -OutFile "arm-gnu-toolchain-14.2.rel1-mingw-w64-x86_64-arm-none-eabi.zip"
unzip arm-gnu-toolchain-14.2.rel1-mingw-w64-x86_64-arm-none-eabi.zip

Set environment in the current shell environment. The example below uses powershell.

$env:PATH = "$(pwd)/.tools/bin;$env:PATH"

Optional: Symlink compile_commands.json to root directory for clangd/ccls for windows. Requires admin.

New-Item -ItemType SymbolicLink -Path "compile_commands.json" -Target "./build/compile_commands.json"

Docker Container

Linux:

docker build --build-arg USER_ID=$(id -u) --build-arg GROUP_ID=$(id -g) -t hellostm32 .

Windows:

docker build -t hellostm32 .

Run container:

docker run -it --rm -v "$(pwd):/app" hellostm32

Flashing

You can flash your firmware onto the target MCU using various tools. Choose the one that matches your hardware/debug adapter.

stlink

Download stlink.

st-flash write build/firmware.bin 0x8000000

Windows

If you're downloading through the github release page make sure to copy the stlink directory under Program Files (x86) in the zip file to the real Program Files (x86).

And you'll also need to download libusb and put the dll either in the same path as stlink binaries or expose it in the system path environment.

OpenOCD

openocd -f interface/stlink.cfg -f target/stm32u5x.cfg \
        -c "program firmware.elf verify reset exit"

Debugging

OpenOCD

openocd -f interface/stlink.cfg -f target/stm32u5x.cfg
arm-none-eabi-gdb build/firmware.elf

gdb

(gdb) target remote localhost:3333
(gdb) load
(gdb) monitor reset init
(gdb) b main
(gdb) c

Segger J-Link

JLinkGDBServer -device STM32U545RET6 -if SWD -speed 4000
arm-none-eabi-gdb build/firmware.elf

Follow the same step as the openocd.

Resources

You'll need an account if you want to download anything from ST.

Tutorials

Hardware

AArch32 bare-metal target (arm-none-eabi)