项目作者: rbino

项目描述 :
Use Zig to blink some LEDs
高级语言: Zig
项目地址: git://github.com/rbino/zig-stm32-blink.git
创建时间: 2021-04-05T21:43:40Z
项目社区:https://github.com/rbino/zig-stm32-blink

开源协议:MIT License

下载


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:

  1. 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:

  1. 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:

  1. openocd -f board/stm32f4discovery.cfg

Then from another terminal navigate to the directory containing the ELF output (i.e.
zig-out/bin) and run:

  1. gdb-multiarch zig-stm32-blink.elf -ex "target remote :3333"

You can also manually flash the firmware inside gdb with:

  1. 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:

  1. qemu-system-gnuarmeclipse -machine STM32F4-Discovery -mcu STM32F407VG \
  2. -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).