RP2040 and PI PICO Software Development Tools

The project software will be partially developed using multiple Raspberry PI Pico microcontroller boards. Subcomponents of the design will be breadboarded and tested before the initial prototype PCB is ordered. The Raspberry PI Debug Probe will be used to load and debug code.

The home page for the Raspberry PI RP2040 microcontroller is Raspberry PI RP2040 Microcontroller. Installation of the tools for use with the PI Pico are covered in “Getting started with Raspberry Pi PicoC/C++ development with Raspberry Pi Pico and other RP2040-based microcontroller boards“. Chapter 2 of this guide describes tool installation for Linux. Chapter 9 covers tool installation on Apple macOS and on Microsoft Windows. I installed the tools on Ubuntu 22.04 running under “Windows Subsystem for Linux” (wsl2).

Windows Subsystem for Linux (WSL)

Install WSL (version 2) from the Microsoft store. By default, the latest version of the Ubuntu distribution is installed. You may choose a different Linux distribution. More details of the WSL installation can be found at WSL Install. You must be running Windows 10 version 2004 and higher (Build 19041 and higher) or Windows 11 to install the latest version of WSL.

An advantage of WSL 2 is that allows Linux GUI applications. The procedure for installing and running Linux GUI applications is here. Run Linux GUI apps on the Windows Subsystem for Linux

Install the SDK and Toolchain

Install the SDK as described in section 2.1 of Chapter 2. Install the Toolchain following section 2.2. If installing on Ubuntu or Debian, pay attention to the NOTE at the end of this section. Update the SDK as described in section 2.3.

If you like follow on to Chapters 3 and 4 to verify the the example code builds and runs.

The C/C++ SDK is described in more detail in Raspberry Pi Pico C/C++ SDK – Libraries and tools for C/C++ development on RP2040 microcontrollers. Appendix D discusses board configuration. This information will be necessary to develop code to run on our custom board.

Raspberry PI Debug Probe

The Raspberry PI Debug Probe is a USB device that provides both a UART serial port and a standard Arm Serial Wire Debug (SWD) interface. It will be used both to flash code and to debug code via OpenOCD. This is the documentation for the Raspberry PI Debug Probe.

Install OpenOCD

To support debugging both cores, build OpenOCD from source code. Follow the instructions in the documentation starting after the first command in the Linux (and Raspberry Pi) section of the OpenOCD installation instructions.

When I ran the ./bootstrap command I got the following output on the terminal.

$ ./bootstrap
aclocal --warnings=all
configure.ac:32: error: Macro PKG_PROG_PKG_CONFIG is not available. It is usually defined in file pkg.m4 provided by package pkg-config.
configure.ac:32: the top level
autom4te: error: /usr/bin/m4 failed with exit status: 1
aclocal: error: autom4te failed with exit status: 1

I verified that pkg-config was not installed on my system using

$ dpkg -s pkg-config

Install pkg-config with the following commands.

$ sudo apt-get install -y pkg-config
$ sudo apt-get install -y pkg-config

After the successful installation of pkg-config, rerun the ./bootstrap command from the openocd directory. Continue with the installation as described in the documentation.

Install GDB

$ sudo apt install gdb-multiarch

Uploading Programs to the Pico

The Pico Debug Probes allows binary programs to be uploaded via the SWD port and OpenOCD. There is no need to push and hold the BOOTSEL button. Uploading new code becomes hands free. Change directory to the build directory of one of the pico examples; blink for example. Load the code with the debug probe as shown below. The connection fails. The problem is that the USB port is not connected to WSL2.

$ cd ~/pico/pico-examples/build/blink/
$ sudo openocd -f interface/cmsis-dap.cfg -f target/rp2040.cfg -c "adapter speed 5000" -c "program blink.elf verify reset exit"
Open On-Chip Debugger 0.12.0-g4d87f6d (2023-08-18-11:16)
Licensed under GNU GPL v2
For bug reports, read
        http://openocd.org/doc/doxygen/bugs.html
Info : Hardware thread awareness created
Info : Hardware thread awareness created
adapter speed: 5000 kHz

Error: unable to find a matching CMSIS-DAP device
** OpenOCD init failed **
shutdown command invoked

$

Open a windows PowerShell as Administrator, list the USB devices and attach the CMSIS-DAP interface device. Attaching the USB device failed.

This image has an empty alt attribute; its file name is attach_usb_fail.jpg

The usbip client must be installed on the Ubuntu distribution. Install using the commands.

sudo apt install linux-tools-virtual hwdata
sudo update-alternatives --install /usr/local/bin/usbip usbip `ls /usr/lib/linux-tools/*/usbip | tail -n1` 20

Attaching the USB port from the Windows PowerShell should now succeed.

sudo openocd -f interface/cmsis-dap.cfg -f target/rp2040.cfg -c "adapter speed 5000" -c "program blink.elf verify reset exit"
Open On-Chip Debugger 0.12.0-g4d87f6d (2023-08-18-11:16)
Licensed under GNU GPL v2
For bug reports, read
http://openocd.org/doc/doxygen/bugs.html
Info : Hardware thread awareness created
Info : Hardware thread awareness created
adapter speed: 5000 kHz

Info : Using CMSIS-DAPv2 interface with VID:PID=0x2e8a:0x000c, serial=E6614103E73B5D2F
Info : CMSIS-DAP: SWD supported
Info : CMSIS-DAP: Atomic commands supported
Info : CMSIS-DAP: Test domain timer supported
Info : CMSIS-DAP: FW Version = 2.0.0
Info : CMSIS-DAP: Interface Initialised (SWD)
Info : SWCLK/TCK = 0 SWDIO/TMS = 0 TDI = 0 TDO = 0 nTRST = 0 nRESET = 0
Info : CMSIS-DAP: Interface ready
Info : clock speed 5000 kHz
Info : SWD DPIDR 0x0bc12477, DLPIDR 0x00000001
Info : SWD DPIDR 0x0bc12477, DLPIDR 0x10000001
Info : [rp2040.core0] Cortex-M0+ r0p1 processor detected
Info : [rp2040.core0] target has 4 breakpoints, 2 watchpoints
Info : [rp2040.core1] Cortex-M0+ r0p1 processor detected
Info : [rp2040.core1] target has 4 breakpoints, 2 watchpoints
Info : starting gdb server for rp2040.core0 on 3333
Info : Listening on port 3333 for gdb connections
[rp2040.core0] halted due to debug-request, current mode: Thread
xPSR: 0xf1000000 pc: 0x000000ea msp: 0x20041f00
[rp2040.core1] halted due to debug-request, current mode: Thread
xPSR: 0xf1000000 pc: 0x000000ea msp: 0x20041f00
** Programming Started **
Info : Found flash device 'win w25q16jv' (ID 0x001540ef)
Info : RP2040 B0 Flash Probe: 2097152 bytes @0x10000000, in 32 sectors

Warn : Adding extra erase range, 0x10002200 .. 0x1000ffff
** Programming Finished **
** Verify Started **
** Verified OK **
** Resetting Target **
shutdown command invoked
$

Debug a Program with SWD

In the GDB debugger.

$ sudo apt install gdb-multiarch

Rebuild pico-examples with CMAKE_BUILD_TYPE=Debug. If you have already set PICO_SDK_PATH in your ~/.bashrc file skip the “export” command. You can check the value of PICO_SDK_PATH with

$ echo $PICO_SDK_PATH
$ cd ~/pico/pico-examples
$ rm -rf build
$ mkdir build
$ cd build
$ export PICO_SDK_PATH=../../pico-sdk
$ cmake -DCMAKE_BUILD_TYPE=Debug ..
$ cd blink
$ make

Attach OpenOCD via the SWD interface.

$ sudo openocd -f interface/cmsis-dap.cfg -f target/rp2040.cfg -c "adapter speed 5000"

The output should look something like this.

Open On-Chip Debugger 0.12.0-g4d87f6d (2023-08-18-11:16)
Licensed under GNU GPL v2
For bug reports, read
http://openocd.org/doc/doxygen/bugs.html
Info : Hardware thread awareness created
Info : Hardware thread awareness created
adapter speed: 5000 kHz

Info : Listening on port 6666 for tcl connections
Info : Listening on port 4444 for telnet connections
Info : Using CMSIS-DAPv2 interface with VID:PID=0x2e8a:0x000c, serial=E6614103E73B5D2F
Info : CMSIS-DAP: SWD supported
Info : CMSIS-DAP: Atomic commands supported
Info : CMSIS-DAP: Test domain timer supported
Info : CMSIS-DAP: FW Version = 2.0.0
Info : CMSIS-DAP: Interface Initialised (SWD)
Info : SWCLK/TCK = 0 SWDIO/TMS = 0 TDI = 0 TDO = 0 nTRST = 0 nRESET = 0
Info : CMSIS-DAP: Interface ready
Info : clock speed 5000 kHz
Info : SWD DPIDR 0x0bc12477, DLPIDR 0x00000001
Info : SWD DPIDR 0x0bc12477, DLPIDR 0x10000001
Info : [rp2040.core0] Cortex-M0+ r0p1 processor detected
Info : [rp2040.core0] target has 4 breakpoints, 2 watchpoints
Info : [rp2040.core1] Cortex-M0+ r0p1 processor detected
Info : [rp2040.core1] target has 4 breakpoints, 2 watchpoints
Info : starting gdb server for rp2040.core0 on 3333
Info : Listening on port 3333 for gdb connections

Open a second terminal window. You can do this with a Shift-click on the WSL2 icon in the taskbar. In the newly opened terminal, change directory containing the built binary. Start GDB.

$ cd ~/pico/pico-examples/build/blink/
$ gdb-multiarch blink.elf
GNU gdb (Ubuntu 12.1-0ubuntu1~22.04) 12.1
Copyright (C) 2022 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later http://gnu.org/licenses/gpl.html
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
https://www.gnu.org/software/gdb/bugs/.
Find the GDB manual and other documentation resources online at:
http://www.gnu.org/software/gdb/documentation/.

For help, type "help".
Type "apropos word" to search for commands related to "word"…
Reading symbols from blink.elf…
(gdb)

This starts the GDB command line debugger. You can get a text user interface to GDB using the -tui command line option.

$ gdb-multiarch -tui blin.exe

This gives you something like this.

A Graphical Interface to GDB

Microsoft Visual Studio Code (I couldn’t make this work.)

One potential graphical interface to GDB is Microsoft Visual Studio Code. can be used as a graphical user interface to GDB as well as a coding environment. Chapter 7 of “Getting Started with Raspberry PI PicoC++ development with Raspberry PI Pico and other RP2040-based microcontroller boards“, provides instruction for installing VSCode A guide to installing and using Visual Studio Code on Raspberry Pi OS. I am using Ubuntu as my development OS. This link provides instructions for installing VSCode on Ubuntu 22.04. Note, I was not able to make this install work.

If you follow this procedure begin with the following command to become the root user. This will be required to write to root owned directories.

$ sudo -i

I followed this procedure. When I finished and typed the command to verify the install, I got the following output.

# code --version --user-data-dir
To use Visual Studio Code with the Windows Subsystem for Linux, please install Visual Studio Code in Windows and uninstall the Linux version in WSL. You can then use the code command in a WSL terminal just as you would in a normal command prompt.
Do you want to continue anyway? [y/N] y

Following the se instructions, I uninstalled VSCode as follows.

# apt purge code
# apt autoremove

After installing VSCode in Windows from the Microsoft Store, I ran in from a Uduntu terminal as suggested. Here is the result.

$ code
/mnt/c/Users/glena/AppData/Local/Programs/Microsoft VS Code/bin/code: 61: /mnt/c/Users/glena/AppData/Local/Programs/Microsoft VS Code/Code.exe: Exec format error

One suggestion online to fix this issue was to edit the /etc/wsl.conf file in Ubuntu to disable systemd.

[boot]
systemd=false

This did not fix the problem for me. The error remained.

Install gdbgui

gdbgui is a browser based GUI for GDB. Therefore a browser, if not already present must be installed. I installed Google Chrome using the instructions from Microsoft’s WSL Gui-Apps Tutorial.

The recommended way to install gdbgui is to use pipx. I installed pipx as follows,

$ sudo apt update
$ sudo apt install pipx

and then used pipx to install gdbgui.

$ pipx install gdbgui
installed package gdbgui 0.15.1.0, installed using Python 3.10.12
These apps are now globally available
- gdbgui
⚠️ Note: '/home/glenafield/.local/bin' is not on your PATH environment variable. These apps will not be globally
accessible until your PATH is updated. Run pipx ensurepath to automatically add it, or manually modify your PATH
in your shell's config file (i.e. ~/.bashrc).
done! ✨ 🌟 ✨
$ pipx ensurepath
Success! Added /home/glenafield/.local/bin to the PATH environment variable.

Consider adding shell completions for pipx. Run 'pipx completions' for instructions.

You will need to open a new terminal or re-login for the PATH changes to take effect.

Otherwise pipx is ready to go! ✨ 🌟 ✨

Using gdbgui

Using Windows Powershell as administrator make sure the the Pico Probe USB port is attached is attached to WSL.

Attach OpenOCD via the SWD interface.

$ sudo openocd -f interface/cmsis-dap.cfg -f target/rp2040.cfg -c "adapter speed 5000"

Open a new Ubuntu terminal window. Shift-click on the WSL icon in the taskbar. Start Google Chrome in the background.

$ google-chrome&

Project – US-AFSRM and Drive

This is my first post in a while. Over the past three years haven’t had much spare time to work on this site. I had been working full-time for a company that makes high pole count permanent magnet synchronous motors (PMSM). While there I worked on motor cogging reduction/elimination using active control. This required a deeper dive into motor physics and developed an interest in various motor topologies. One type of motor I find interesting, is the switched reluctance motor (SRM). SRM’s have a simple, low-cost construction which doesn’t require permanent magnets. This allows reliable operation at high speed and high temperature. The challenge with SRMs is, high torque ripple, acoustic noise and vibration. My goal with this project is to build my own SRM, associated drive electronics, firmware and software which will allow experimentation with a variety of control methods.

There are many good references for switched reluctance motor. I find that my go-to reference is;

Switched Reluctance Motor Drives – Fundamentals to Applications, Ed. Berker Bilgen, James Weisheng Jiang &Ali Amadi, CRC Press, Taylor and Frances Group, Boca Raton Fl.

I purchased the Kindle version of the book from Amazon. I found the particular motor topology which I have selected in the Electric Power Applications journal of the Institution of Engineering and Technology (IET).

Hossein Torkaman , Aghil Ghaheri, Ali Keyhani, Axial flux switched reluctance machines: a comprehensive review of design and topologies, IET Electr. Power Appl., 2019, Vol. 13 Iss. 3, pp. 310-321

This paper refers to my selected topology as Single-Phase US-AFSRM. This is short for “UShaped stator”Axial Flex Synchronous Reluctance Machine” Note the use of “machine” rather than “motor”. This is done to reflect that these machines may be run as motors or generators. Also “synchronous” rather than “switched” to indicate the type of switching used. The initial concept is shown below. This is known as an out-runner motor. The motor’s rotor spins around an interior stator. This single puck (I’m Canadian eh!) is only a single phase motor. Multiple phases can be obtained by stacking multiple pucks. Alignment pins placed in the holes of the rotor shaft permit offsets for up to 12 independently aligned phases. A minimum of one puck is required for each phase. Generally more phases permit the generation not just more torque but smoother torque. The cost of this is adding drive electronics for each phase. Torque can be increased for a fixed number of phases by stacking multiple pucks for each phase at the same alignment and wiring their coils in parallel. Make sure that the power electronics are capable of driving the extra current at the lower resistance and inductance presented by parallel connected coils.

The system supports attaching drive electronics to each puck. A preliminary design is shown below. This is the PCB, which connects to the outside world. There is still some room on the board. I hadn’t planned on adding any power electronics to this PCB. My intent is to put the motor drive power components on a separate PCB(s). These could be swapped in and out depending on the type of motor or number of phases. I could add some bus capacitors for the motor power supply. Logic power comes from a separate 8 to 28 Volt DC supply. Another option is to add an AC/DC converter to this PCB to provide logic power from a single phase AC plugin. This is a prototype design. It may make more sense to add interface options in addition to USB and CAN-FD. WiFi, and wired Ethernet are being considered.

The microcontroller used on the board is the RP2040. It is extremely cheap (< $1US) and easy to get considering current supply-chain issues. Thankfully, these seem to be easing somewhat. The microcontroller does not need to be as powerful as some of the microcontrollers typically used in applications like this (for example STF32x) since computations will be spread across multiple microcontrollers; typically one RP2040 per puck. The clocks of the microcontrollers are synchronized and data is passed used multidrop low voltage differential signalling (M-LVDS). The M-LVDS network is connected with coaxial cable using U.FL connectors on the top an d bottom of each PCB. The programmable IO (PIO) of the RP2040 microcontroller allows passing data between PCBs with almost no load on the dual ARM M0+ processors. This PCB communicates with the outside world with a CAN-FD interface.

In coming posts I will describe some of the software development using breadboarded components including multiple Raspberry PI Pico evaluation boards.