Table of Contents
Introduction
There exists software called OpenOCD that plays a fairly crucial role in the software development process. In brief, the software (which runs on a PC) connects to a microcontroller for Flash uploading and debugging purposes.
The purpose of this blog post is not to describe OpenOCD usage, the diagram below shows where OpenOCD is inserted as part of the overall programming and debug solution (if you're not using such a solution with your Pi Pico or other RP2040 microcontroller based board, it's highly recommended to explore it, because it allows for rapid programming and testing!).
This blog post is simply for one purpose: to explain how to resolve a problem when using OpenOCD with Pi Pico-like boards such as the (excellent) Xiao RP2040 .
What is the Problem?
The RP2040 microcontroller, like many other microcontrollers, uses an interface called JTAG for programming and debugging. There are two pins, labelled SWDIO and SWCLK (they might be abbreviated to SWD and SWC) which are used to transport JTAG. The interface is called Serial Wire Debug (SWD). On the Pi Pico board, you can see the interface at the opposite far end to the USB connector (some versions of the Pi Pico have the SWD interface pins elsewhere).
If you look in the diagram above, there are two Pi Pico boards. The purple one acts as a translator between USB and Serial Wire Debug. The pink one is the board that is being programmed or debugged.
That all works fine for Pi Pico boards. However, Jan Cumps discovered an interesting problem; some RP2040 boards cannot work in the pink location. OpenOCD throws an error:
Info : accepting 'gdb' connection on tcp/50000
Error: Unknown flash device (ID 0x00156085)
Error: auto_probe failed
The problem is, that the Xiao RP2040 happens to use a different Flash chip compared to the original Pi Pico board. The Xiao board is not unique, there will be plenty of other RP2040 boards, all with different Flash chips. The ‘official’ OpenOCD release from raspberrypi.com is actually quite ancient; they have not bothered to keep it up-to-date, it doesn’t affect their Pi Pico board, but it will affect every piece of hardware based on the RP2040 if it happens to use a Flash chip which isn’t encoded inside the OpenOCD software.
What is the Long Solution
There may be a possibility to skip the check with OpenOCD, but that’s not as good as simply updating OpenOCD to support the Flash chip. That’s what we did!
The old Pi Pico datasheet used to explain how to build new OpenOCD releases, but it’s no longer present in the most recent datasheet versions.
In brief, the steps for Windows users are:
(a) Install MSYS2 on your PC
(b) Open up an MSYS2 MINGW64 terminal
(c) Type:
pacman -Syu
(d) Close the terminal (it may have closed anyway) and re-open it and type:
pacman -Su
(e) Close the terminal, and then re-open it, but this time as Administrator
(f) Type the following:
pacman -S mingw-w64-x86_64-toolchain git make libtool pkg-config autoconf automake texinfo mingw-w64-x86_64-libusb
(g) Close the terminal, and then re-open (not as Administrator)
(h) Create a folder, and then inside that, type:
git clone https://github.com/raspberrypi/openocd.git --branch picoprobe --depth=1
(i) Go to the openocd/src/flash/nor folder
(j) Edit the spi.c file and add the Flash chip identifier to it. This is discussed further below!
(k) Go back to the openocd folder, and then type:
./bootstrap
./configure --enable-picoprobe --disable-werror
make -j4
After all that, openocd will be built.
Flash Chip Identifiers
The spi.c file in the openocd/src/flash/nor folder contains dozens of Flash chip identifiers, as shown here:
We decided to simply replace the old raspberrypi.org spi.c file with the newer spi.c file at the official openocd page plus the additional spi.c change here.
Short Solution
Download the pre-built OpenOCD. You'll need to extract it with 7-Zip software.
This pre-built version was bundled together and tested by Jan on both Xiao RP2040 boards, and Pi Pico ones. It should also work with many other RP2040 boards.
Summary
OpenOCD is necessary to use the Serial Wire Debug (SWD) programming and debugging solution with the PicoProbe, which is an RP2040-based USB-to-SWD adapter.
You may encounter Error: Unknown flash device messages when attempting via SWD to program some RP2040 boards such as the Xiao RP2040.
The problem is resolved by modifying the OpenOCD source code to add a Flash memory identifier. Since that process is quite involved, a ready-built OpenOCD release for Windows is available to download.
Thanks for reading.