https://github.com/rbino/zig-stm32-blink
Use Zig to blink some LEDs
https://github.com/rbino/zig-stm32-blink
embedded stm32 stm32f4-discovery zig
Last synced: 8 months ago
JSON representation
Use Zig to blink some LEDs
- Host: GitHub
- URL: https://github.com/rbino/zig-stm32-blink
- Owner: rbino
- License: mit
- Created: 2021-04-05T21:43:40.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2021-12-22T21:15:21.000Z (almost 4 years ago)
- Last Synced: 2025-03-18T12:50:58.066Z (8 months ago)
- Topics: embedded, stm32, stm32f4-discovery, zig
- Language: Zig
- Homepage:
- Size: 142 KB
- Stars: 61
- Watchers: 4
- Forks: 9
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Zig STM32 Blink
Make LEDs blink on an STM32F4 Discovery board using only Zig (and a linker script).
See [my blogpost](https://rbino.com/posts/zig-stm32-blink/) 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](https://github.com/stlink-org/stlink). 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`](http://openocd.org/) 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](https://xpack.github.io/qemu-arm/install/#manual-install).
*Note*: you have to comment out [this
line](https://github.com/rbino/zig-stm32-blink/blob/master/src/main.zig#L44) 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).