{"id":419,"date":"2021-07-07T19:36:37","date_gmt":"2021-07-07T19:36:37","guid":{"rendered":"https:\/\/andrejacobs.org\/?p=419"},"modified":"2022-04-11T20:22:59","modified_gmt":"2022-04-11T20:22:59","slug":"develop-and-debug-raspberry-pi-pico-on-macos-big-sur","status":"publish","type":"post","link":"https:\/\/andrejacobs.org\/electronics\/develop-and-debug-raspberry-pi-pico-on-macos-big-sur\/","title":{"rendered":"Develop and Debug Raspberry Pi Pico on macOS Big Sur"},"content":{"rendered":"\n
I ❤️ the Raspberry Pi Pico and think this is the best microcontroller since the AVR! My history with microcontrollers go back to the late 90s using PIC16s followed by AVR ATtiny\/mega (Arduino etc.) as I dabble with Electronics from time to time.<\/p>\n
I don’t think it has ever been this easy to use a microcontroller as it is with the Raspberry Pi Pico. Trust me back in the late 90s you had to etch your own PCB and build a PIC programmer if you were on a budget. Now for \u00a33 you can buy an incredibly powerful microcontroller and program it just by plugging it into a USB port. 🤯<\/p>\n
In this guide I will show you how I have setup my Mac running macOS Big Sur and is able to build and debug C\/C++ code on a Raspberry Pi Pico. The reference material I used can be found at the bottom of this guide.<\/p>\n
I prefer to use homebrew<\/a> as my package manager on macOS, doesn’t everyone nowadays?<\/p>\n <\/p>\n OpenOCD (On Chip Debugging) is used to translate commands from gdb (debugger) to SWD (debugging interface provided by the ARM chip) and can also be used to flash a new program on the Pico without having to do the unplug + BOOTSEL tango.<\/p>\n NOTE:<\/strong> I will be using <\/p>\n You can download a precompiled uf2 of picoprobe, however I will be building from source.<\/p>\n See Appendix A: Using Picoprobe from the official getting started guide for the wiring diagram.<\/p>\n (Image from the official guide)<\/p>\n <\/p>\n You don’t need this if you are only going to be using Visual Studio Code for development and debugging. However I wanted to confirm that everything is working from as lower level (away from IDEs) as possible.<\/p>\n <\/p>\n <\/p>\n <\/p>\n <\/p>\n <\/p>\n <\/p>\n <\/p>\n <\/p>\n <\/p>\n <\/p>\n <\/p>\n Congratulations! 🥳 🎉 You can now build, run and debug a Raspberry Pi Pico from you Mac (hopefully).<\/p>\n\n
$ brew doctor\n$ brew update\n$ brew upgrade\n<\/code><\/pre>\n
\n
$ brew install cmake\n$ brew tap ArmMbed\/homebrew-formulae\n$ brew install arm-none-eabi-gcc\n<\/code><\/pre>\n
\n
\n
# cd into a directory where you want to store the files\n$ git clone -b master https:\/\/github.com\/raspberrypi\/pico-sdk.git\n$ cd pico-sdk\n$ git submodule update --init\n\n# Get the example code\n$ cd ..\n$ git clone -b master https:\/\/github.com\/raspberrypi\/pico-examples.git\n<\/code><\/pre>\n
Compiling and running Blink<\/h2>\n
\n
$ cd pico-examples\n$ mkdir build\n$ cd build\n\n# Set the path to where the SDK can be found\n$ export PICO_SDK_PATH=..\/..\/pico-sdk\n\n# Get the build directory ready for cmake\n$ cmake ..\n<\/code><\/pre>\n
\n
$ cd blink\n$ make -j4\n...\n[100%] Linking CXX executable blink.elf\n[100%] Built target blink\n\n# You should now have a blink.uf2 file inside the build\/blink directory\n<\/code><\/pre>\n
\n
blink.uf2<\/code> over to the attached drive named
RPI-RP2<\/code>.<\/li>\n
Installing OpenOCD<\/h2>\n
picoprobe<\/code> since I am on a Mac. This requires having one Pico running the picoprobe program that is connected to the actual Pico to be debugged. See Appendix A from the official guide.<\/p>\n
\n
$ brew install libtool automake libusb wget pkg-config gcc texinfo\n...\n# Note this output\nIf you need to have texinfo first in your PATH, run:\n echo 'export PATH="\/usr\/local\/opt\/texinfo\/bin:$PATH"' >> ~\/.zshrc\n\n$ export PATH="\/usr\/local\/opt\/texinfo\/bin:$PATH"\n<\/code><\/pre>\n
\n
$ git clone https:\/\/github.com\/raspberrypi\/openocd.git --branch picoprobe --depth=1\n$ cd openocd\n$ .\/bootstrap\n$ .\/configure --enable-picoprobe --disable-werror\n$ make -j4\n$ make install\n<\/code><\/pre>\n
\n
$ openocd\nOpen On-Chip Debugger 0.10.0+dev-g18b4c35-dirty (2021-07-07-09:58)\n...\nError: Debug Adapter has to be specified, see "adapter driver" command\n...\n<\/code><\/pre>\n
Installing picoprobe<\/h2>\n
picoprobe<\/code> is used to connect the Mac via USB to the Raspberry Pi Pico that will then be attached to the Pico that is being debugged. If you are using a Raspberry Pi, then you can just connect via the GPIO pins and not need the extra Pico in the mix.<\/p>\n
\n
picoprobe<\/code> from source.<\/li>\n<\/ul>\n
# You might need to set the PICO_SDK_PATH again if you closed the terminal at some point\n# export PICO_SDK_PATH=..\/..\/pico-sdk\n\n$ git clone https:\/\/github.com\/raspberrypi\/picoprobe.git\n$ cd picoprobe\n$ mkdir build\n$ cd build\n$ cmake ..\n$ make -j4\n...\n[100%] Linking CXX executable picoprobe.elf\n[100%] Built target picoprobe\n<\/code><\/pre>\n
\n
picoprobe.uf2<\/code> inside the build directory.<\/li>\n
picoprobe.uf2<\/code> file onto the drive.<\/li>\n<\/ul>\n
Wire up the two Picos<\/h2>\n
Installing gdb and doing a test debug (optional)<\/h2>\n
\n
arm-none-eabi-gdb<\/code> installed from the toolchain steps.<\/li>\n<\/ul>\n
$ which arm-none-eabi-gdb\n\/usr\/local\/bin\/arm-none-eabi-gdb\n<\/code><\/pre>\n
\n
$ openocd -f interface\/picoprobe.cfg -f target\/rp2040.cfg -s tcl\n...\nInfo : rp2040.core0: hardware has 4 breakpoints, 2 watchpoints\nInfo : rp2040.core1: hardware has 4 breakpoints, 2 watchpoints\nInfo : starting gdb server for rp2040.core0 on 3333\nInfo : Listening on port 3333 for gdb connections\n<\/code><\/pre>\n
\n
\n
# Open another terminal, since OpenOCD is running in one already\n$ cd <YOUR_PATH>\/pico-examples\/\n$ rm -rf build\n$ mkdir build\n$ cd build\n$ export PICO_SDK_PATH=..\/..\/pico-sdk\n$ cmake -DCMAKE_BUILD_TYPE=Debug ..\n$ cd hello_world\/serial\n$ make -j4\n...\n[100%] Linking CXX executable hello_serial.elf\n[100%] Built target hello_serial\n<\/code><\/pre>\n
\n
hello_serial.elf<\/code> inside the build\/hello_world\/serial directory.<\/li>\n
$ arm-none-eabi-gdb hello_serial.elf\n<\/code><\/pre>\n
\n
(gdb) target remote localhost:3333\n(gdb) load\n<\/code><\/pre>\n
\n
(gdb) monitor reset init\n(gdb) b main\n...\nBreakpoint 1 at 0x1000030e: file ...\/pico-examples\/hello_world\/serial\/hello_serial.c, line 11.\n\n(gdb) continue\n...\ntarget halted due to debug-request, current mode: Thread\nxPSR: 0x01000000 pc: 0x00000178 msp: 0x20041f00\n\nThread 1 hit Breakpoint 1, main ()\n at ...\/pico-examples\/hello_world\/serial\/hello_serial.c:11\n11\t stdio_init_all();\n<\/code><\/pre>\n
\n
Setup Visual Studio Code for development and debugging<\/h2>\n
\n
PICO_SDK_PATH<\/code> to your shell’s path since Visual Studio Code will need this.<\/li>\n
\u2318\u21e7X<\/code>.<\/li>\n<\/ul>\n
\n
\n
\n
\n
\n
\n
\n
$ cd <YOUR_PATH>\/pico-examples\n$ mkdir .vscode\n# The example repo comes with example settings\n$ cp ide\/vscode\/launch-raspberrypi-swd.json .vscode\/launch.json\n$ cp ide\/vscode\/settings.json .vscode\/settings.json\n<\/code><\/pre>\n
\n
.vscode\/launch.json<\/code> and make the following changes.<\/li>\n<\/ul>\n
"gdbPath" : "arm-none-eabi-gdb",\n"configFiles": [\n\t"interface\/picoprobe.cfg",\n\t"target\/rp2040.cfg"\n],\n<\/code><\/pre>\n
\n
.vscode\/launch.json<\/code> I am using.<\/li>\n<\/ul>\n
{\n "version": "0.2.0",\n "configurations": [\n {\n "name": "Pico Debug",\n "cwd": "${workspaceRoot}",\n "executable": "${command:cmake.launchTargetPath}",\n "request": "launch",\n "type": "cortex-debug",\n "servertype": "openocd",\n "gdbPath" : "arm-none-eabi-gdb",\n "device": "RP2040",\n "configFiles": [\n "interface\/picoprobe.cfg",\n "target\/rp2040.cfg"\n ],\n "svdFile": "${env:PICO_SDK_PATH}\/src\/rp2040\/hardware_regs\/rp2040.svd",\n "runToMain": true,\n "postRestartCommands": [\n "break main",\n "continue"\n ]\n }\n ]\n}\n<\/code><\/pre>\n
\n
\n
Wrapping up<\/h2>\n
References<\/h2>\n