How to use Raspberry PI zero as a JTAG server?
When we program/debug an embedded target, most of the time the target device would be within a distance which is comparable to the length of a USB cable. A direct connection to the development PC via USB provides both simple and low cost access to your target embedded controller, but what if the target is located far enough so we are not able to connect via USB port? This type of use cases urge us to use a GDB server to program/debug our target device. Fortunately developers of OpenOCD have already been developing this open-source JTAG agent by considering the remote access.
OpenOCD (Open On Chip Debug) is an open source JTAG/SWD agent software which supports a wide variety of debug adapters including, but not limited to ft232, ulink, jlink, and cmsis-dap hardware interfaces. OpenOCD creates a gdbserver along with the telnet interface in the host environment in order to debug and program the target MCUs.
In this document, I will discuss the openocd installation into Raspberry Pi zero wireless along with the integration to IAR EW. Before you start you need to configure your RPI zero wireless for WI-FI connection. I am not going into the details of WI-FI configuration of RPI, there are plenty amount of documents available online.
Let’s go into details of setup procedure of OpenOCD into Raspberry PI zero from its source code by logging in to your RPI via SSH,
1: Openocd installation into Raspberry PI zero wireless
1.1- installing dependencies
pi@raspberrypi:~$ mkdir sandbox && cd sandbox
pi@raspberrypi:~/sandbox $ sudo apt-get install git
pi@raspberrypi:~/sandbox $ sudo apt-get install libtool
pi@raspberrypi:~/sandbox $ sudo apt-get install autotools-dev
pi@raspberrypi:~/sandbox $ sudo apt-get install automake
pi@raspberrypi:~/sandbox $ sudo apt-get install libusb-1.0-0-dev
pi@raspberrypi:~/sandbox $ sudo apt-get install libhidapi-dev
1.2- obtaining source, and build
pi@raspberrypi:~/sandbox $ git clone https://git.code.sf.net/p/openocd/code openocd-code
pi@raspberrypi:~/sandbox $ cd openocd-code
pi@raspberrypi:~/sandbox/openocd-code $ ./bootstrap
pi@raspberrypi:~/sandbox/openocd-code $ ./configure –enable-ftdi –enable-stlink –enable-ti-icdi –enable-ulink –enable-ft232r –enable-vsllink –enable-jlink \
–enable-bcm2835gpio –enable-imx_gpio –enable-cmsis-dap-v2 –enable-cmsis-dap
pi@raspberrypi:~/sandbox/openocd-code $ time make #this will take about 55 minutes.
pi@raspberrypi:~/sandbox/openocd-code $ sudo make install
pi@raspberrypi:~/sandbox/openocd-code $ touch remote.cfg*
pi@raspberrypi:~/sandbox/openocd-code $ echo “bindto 0.0.0.0” >> remote.cfg
1.3- executing openocd as a root user so usb ports can be accessed, you should see the output as below
pi@raspberrypi:~/sandbox/openocd-code $ sudo openocd -f remote.cfg -f interface/stlink-v2.cfg -f target/stm32f1x.cfg
Open On-Chip Debugger 0.11.0-rc2+dev-00006-gf68ade529-dirty (2021-02-08-08:29)
Licensed under GNU GPL v2
For bug reports, read
WARNING: interface/stlink-v2.cfg is deprecated, please switch to interface/stlink.cfg
Info : auto-selecting first available session transport “hla_swd”. To override use ‘transport select ‘.
Info : The selected transport took over low-level target control. The results might differ compared to plain JTAG/SWD
Info : Listening on port 6666 for tcl connections
Info : Listening on port 4444 for telnet connections
Info : clock speed 1000 kHz
Info : STLINK V2J34S7 (API v2) VID:PID 0483:3748
Info : Target voltage: 6.031014
Info : stm32f1x.cpu: hardware has 6 breakpoints, 4 watchpoints
Info : starting gdb server for stm32f1x.cpu on 3333
Info : Listening on port 3333 for gdb connections
*: remote.cfg is necessary to access the socket of openocd from external IPs, otherwise only loopback device is available so you cannot connect remotely
2. Configuring IAR Embedded workbench for gdb access
You can see the actual setup in Fig.2. As it can be seen in the setup, a generic USB cable has been used in order to provide 5 volts power to Raspberry PI Zero. Any power bank which is able to supply 5 volts can also be used to power up the system without tethering. You need to use a USB OTG cable in order to connect your OpenOCD compliant JTAG/SWD adapter your Raspberry PI Zero. USB OTG cable has necessary signaling and power lines to run the JTAG/SWD adapter.
I tried to explain OpenOCD installation details for Raspberry PI Zero as a firmware developer point of view. You can also use a regular Raspberry PI with the same purpose. I hope this article helps you solve tough debugging issues with remote embedded targets.