GD32 + libopencm3 + OpenOCD Debugging Notes

2018-05-27

/uploads/blog/2018/1527431127977-code_screenshot.png

I am using GD32 for some projects. While it's claimed to be register-level compatible with STM32, and it's possible to use ST's tools and libraries, this is certainly an violation of ST's license. One possible solution is to use GD provided tools and libraries, but then it would be troublesome if some day I want to migrate back to STM32. So I ended up using libopencm3.

The following are just my personal notes. Environment: Arch Linux.

Install dependencies

Install the following packages from the official software repo:

# pacman -S arm-none-eabi-gcc arm-none-eabi-newlib

Install the following from AUR:

Create a new project

$ mkdir project
$ cd project
$ git init .
$ git submodule add https://github.com/libopencm3/libopencm3
$ make -C libopencm3 -j5
$ wget -O libopencm3.rules.mk https://raw.githubusercontent.com/libopencm3/libopencm3-examples/master/examples/rules.mk
$ wget -O libopencm3.target.mk https://raw.githubusercontent.com/libopencm3/libopencm3-examples/master/examples/stm32/l1/Makefile.include
$ cp libopencm3/lib/stm32/f1/stm32f103x8.ld .

Modify the last line of libopencm3.target.mk to:

include ../libopencm3.rules.mk

Create a new src folder, copy an example from libopencm3-examples (.c and Makefile). Modify the Makefile as follows:

OPENCM3_DIR = ../libopencm3
LDSCRIPT = ../stm32f103x8.ld

include ../libopencm3.target.mk

Now make bin or make hex should generate the image.

Download using OpenOCD

Create a new configuration to use SWD:

$ cd /usr/share/openocd/scripts/interface
$ cp jlink.cfg jlink-swd.cfg
$ echo "transport select swd" > jlink-swd.cfg

Use OpenOCD to download:

openocd -f interface/jlink-swd.cfg -f target/stm32f1x.cfg
telnet localhost 4444
reset halt
flash write_image erase project.hex
reset