zig-stm32-blink
zig-stm32-blink copied to clipboard
Use Zig to blink some LEDs
Zig STM32 Blink
Make LEDs blink on an STM32F4 Discovery board using only Zig (and a linker script).
See my blogpost for a more thorough explanation of what's going on.
Build
The code was tested with Zig 0.9.0.
To build the ELF file just run:
zig build
Flashing
The easiest way to flash the board is to install stlink
tools. Most Linux distributions should have them in their
repos, the build system will try to use the st-flash program.
The command to flash the board is:
zig build flash
After flashing the board you should see the 4 LEDs blinking in an alternating pattern.
Debugging
It's possible to use openocd and gdb-multiarch to debug the firmware
directly on the board.
In a terminal run:
openocd -f board/stm32f4discovery.cfg
Then from another terminal navigate to the directory containing the ELF output (i.e.
zig-out/bin) and run:
gdb-multiarch zig-stm32-blink.elf -ex "target remote :3333"
You can also manually flash the firmware inside gdb with:
load
Emulation using qemu
If you don't have an STM32F4 Discovery board or you just want to test the code locally, you can use xPack QEMU Arm.
Note: you have to comment out this line to make the code work in QEMU since it doesn't support the FPU coprocessor yet.
After that, you can emulate the board with:
qemu-system-gnuarmeclipse -machine STM32F4-Discovery -mcu STM32F407VG \
-kernel zig-out/bin/zig-stm32-blink.elf -gdb tcp::3333
You should see the blinking LEDs on the board image, and you can connect a gdb instance to it (see
the previous section).