Raspberry Pi Pico Guide
Raspberry Pi Pico Guide
Raspberry Pi Pico Guide
Tobi Wealth
Copyright 2021 © Tobi Wealth
Another nice thing about the pico is that it has more speed, more
memory and it is a dual core micro controller and best of all, it is a
32 bit arm controller compared to the 8 bit of the arduino nano.
1
CHAPTER ONE: QUICK PICO SETUP
First run this code sudo apt install git if you don’t
have Git installed.
$ chmod +x pico_setup.sh
$ pico-setup/pico_setup.sh
2
● Download the pico-sdk, pico-examples, pico-
extras, and pico-playground repositories
● Creates the paths for the following;
Pico_SDk_path, Pico_Examples_Path, and
Pico_Playground_Path inside your `/. Bashrc.
● Prototype the blink and hello_world examples in
Pico_examples/build/blink and pico-
examples/build/hello-world.
● Download the build protocol. Once you are done,
copy it to /usr/local/bin.
● Download and build picoprobe
● Download and compile OpenOCD – this is to
create support for debugging.
● Download and install Visual Studio Code.
● Install the necessary Visual Studio Code
extensions.
$ sudo reboot
3
Open the Visual Studio Code in the “Programming”
menu using the instructions from Section 6.2
4
Chapter 2: The Pico SDK
Important:
$ cd ~/
$ mkdir pico
$ cd pico
$ cd pico-sdk
$ cd ..
WARNING
6
2.2. Install the Toolchain
7
Chapter 3: Blinking a LED in C
8
$ cd pico-examples
$ mkdir build
$ cd build
$ export PICO_SDK_PATH=../../pico-sdk
$ cmake ..
PICO_SDK_PATH is /home/pi/pico/pico-sdk
Note
9
There is an area prepared by CMake for the pico-
examples tree. It is this0place that you will build its
example applications0by typing make. Although we
are only changing0directory into the blink directory
as we are only0building blink before going ahead
with typing make
Tip
Invoking makewith -j4 will run four make0jobs in parallel to speed it up. A
Raspberry Pi 4 has 4 cores so-j4is a0reasonable number.
$ cd blink
10
● Blink uf2 that can be superimposed into the
RP2040 USB Mass Storage Device.
Note
Loading code via the USB Mass Storage0method is great if you know
your program is going to work first0time, but if you are developing
anything new it is likely you will0want to debug it.
So you can also load your software on0to RP2040 using the Serial Wire
Debug interface, see Chapter 5. As well0as loading software this allows
you to; set breakpoints, inspect0variables, and inspect memory contents.
11
3.2.1. From the Desktop
fig 1
12
The only way to manually mount0the mass storage
device is when you0are logged in via ssh:
$ dmesg | tail
$ ls /mnt/pico/
INDEX.HTM INFO_UF2.TXT
Note
Removing power from the board does0not remove the code. When the
board is reattached to power the0code you have just loaded will begin
running again. If you want to0remove the code from the board, and
upload new code, press and0hold the BOOT SEL switch when applying
power to put the board0into Mass Storage mode.
14
• The SWD interface is being exposed in most
boards, (Chapter 5) and this can reset the board
and have the code loaded without you pressing any
button.
15
down, and then reset the0board in some way. You
can do this by0unplugging and plugging0the USB
connector, or adding
16
Chapter 4: Saying “Hello World” in C
17
https://github.com/raspberrypi/pico
sdk/tree/master/src/boards/include/boards/pico.h. The0Pico0SDK
defaults to a board name of0Raspberry Pi Pico if no other board is
specified.
18
1. Allow printf output through USB CDC (USB
serial).
2. Disallow printf output through UART.
19
4.2. Create “Hello World”
22
There you go! Hello, world! Is printed boldly on the
console.
Tip
23
hardware to be enabled?” check out the picture
below to see what happens.
24
Pin 8) 2)
25
Once you toggle the power to Raspberry Pi Pico,
Hello world stares at you from the console’s screen.
Tip
26
Connecting the 5V pin on Raspberry Pi to the
Raspberry Pi Pico VBUS pin is practical but not
recommended.
27
Chapter 5: Debugging with SWD
Important
28
5.2. Installing OpenOCD
Note
$ cd ~/pico
$ cd openocd
$ ./bootstrap
$ make -j4
Install gdb-multiarch
30
$ sudo apt install gdb-multiarch
Warning
If you see an error likeInfo : DAP init failedthen your Raspberry Pi Pico is either
powered off, wired incorrectly, or has signal integrity issues. Try different
GPIO jumper cables.
$ cd ~/pico/pico-examples/build/hello_world
$ gdb-multiarch hello_world.elf
31
Leave this OpenOCD terminal open. First, open
another terminal where you attach a gdb extension
to OpenOCD. Go to the “Hello World” example
earlier and introduce gdb into the command line.
Tip
(gdb) load
32
Transfer rate: 31 KB/sec, 2664 bytes/write.
If any error shows up that is the0same Error finishing flash operation or Error
erasing flash with vFlashErase packet in GDB as0you attempt to load the
binary onto the0Raspberry Pi Pico0via OpenOCD then there is likely poor
signal integrity between the0Raspberry Pi and the Raspberry Pi Pico.
33
To leave the gdb mode, run the line of code below:
34
Chapter 6: Using Visual Studio Code
Important
35
Use the .deb package on the command line .cd to a
folder path the file was downloaded, install with
dpkg -i<downloaded file name. deb>
$ export PICO_SDK_PATH=/home/pi/pico/pico-sdk
$ code
36
There is a popup close to the base of the vscode
window. If you do not see it, that means CMake
Tip
If you miss the popups that close0after a few seconds, configure the
compiler through the “No0Kit Selected” in the blue bottom of the VScode.
37
Do one of these things; click on the “Build” button
to build the examples in the pico-examples folder
just below the blue bottom bar, or tap where you
see “[all]” in the blue bottom bar.
Tip
38
The projects below have been created just like what
we did from the command line the other time
among other things
39
6.3. Debugging a Project
$ cd ~/pico/pico-examples
$ mkdir .vscode
$ cp ide/vscode/launch-raspberrypi-swd.json .vscode/launch.json
$ cp ide/vscode/launch-raspberrypi-swd.json .vscode/settings.jso
1{
2 "version": "0.2.0",
3 "configurations": [
4 {
40
6 "cwd": "${workspaceRoot}",
7 "executable": "${command:cmake.launchTargetPath}",
8 "request": "launch",
9 "type": "cortex-debug",
10 "servertype": "openocd",
12 "gdbPath" : "gdb-multiarch",
13 "device": "RP2040",
14 "configFiles": [
15 "interface/raspberrypi-swd.cfg",
16 "target/rp2040.cfg"
17 ],
18 "svdFile": "$
{env:PICO_SDK_PATH}/src/rp2040/hardware_regs/rp2040.svd",
19 "runToMain": true,
21 "postRestartCommands": [
22 "break main",
23 "continue"
24 ]
41
25 }
26 ]
27 }
Note
Pi Pico
Ensure that the example "Hello World" code has been as a Debug binary (CMAKE_BUILD_TYPE=Debug).
Important
42
Now that the code is loaded and displayed on your
Raspberry Pi Pico, the source code for “Hello World”
should also be displayed on the upper pane of the
window. The code runs and go to the first
breakpoint which is enabled by the runToMain
directive found int the Launch.json file. Tap the
small blue arrow or the play button above the
source code window to Continue (F5) and get the
code working.
43
Tip
44
Chapter 7: Creating your own Project
45
1. The line of code above enables serial output via
the USB.
2. The second one allows serial output via the
UART.
$ cp ../pico-sdk/external/pico_sdk_import.cmake .
46
Here is what everything looks like
$ mkdir build
$ cd build
$ export PICO_SDK_PATH=../../pico-sdk
$ cmake ..
$ make
47
USB drive
Note
48
Note
49
Use the CMAKE_BUILD_TYPE=Debug to create a
debug version of your project.
$ cd ~/pico/test
$ rmdirbuild
$ mkdirbuild
$ cd build
$ export PICO_SDK_PATH=../../pico-sdk
$ cmake -DCMAKE_BUILD_TYPE=Debug ..
$ make
$ openocd -f interface/raspberrypi-swd.cfg -f
target/rp2040.cfg
50
Leave the OpenOCD terminal open. Open another
terminal window to implement the gdb-multiarch
function with the command line below:
$ cd ~/pico/test/build
$ gdb-multiarch test.elf
(gdb) load
Allow it run.
(gdb) continue
$ cd pico-project-generator
$ ./pico_project.py --gui
52
From here, include specific features to your project
by identifying them from the check boxes on the
GUI. The build system automatically includes the
right code to the project you are creating, including
a basic example of the code describing how the
feature can be used. There are several options
available that also describes functionality such as
the one below:
53
Console Options Description
54
library features
that0by default are in
the0build, for
example, 0UART
support and HW
dividers.
55
caution.
Overwrite Project If a
project0already
exists in the
specified folder,
0overwrite it with
the0new project.
This will overwrite
any changes
you0may have
made.
56
appropriate Visual
Studio0Code project
files.
$ export PICO_SDK_PATH="/home/pi/pico/pico-
sdk"
$ ./pico_project.py --feature spi --feature
i2c --project vscode test
57
One feature that let you know that your -project
option feature is successful is the creation of
.vscode/launch.json, .vscode/c_cpp_properties.json,
and .vscode/settings.json files apart from the
normal CMake project files.
$ cd test/build
$ cmake ..
$ make
58
Chapter 8: Building on other
platforms
https://raw.githubusercontent.com/Homebrew/install/master
/install.sh)"
59
Install the toolchain
60
8.1.3. Building with CMake Tools
{ "cmake.environment": {
"PICO_SDK_PATH":"../../pico-sdk" }, }
61
After that, scroll down to
{ "cmake.environment": {
"PICO_SDK_PATH": "../../pico-sdk" },
"C_Cpp.default.configurationProvider": "ms-vscode.cmake-tools" }
62
Soon you will receive a prompt as the project loads
requesting to select a compiler. Tap “GCC for arm-
none-eabi” for your compiler.
63
Drag-and-drop the uf2 binary on the RP2040 board
directly once your computer is linked to it with a
USB.
64
You may have to check the manufacturer’s website
for FTDI Chip Drivers if you are not using a recent
version of macOS such as Catalina, where the
drivers load up by itself automatically. To connect
the serial port once you are not running the
platform on the latest software, use a Terminal
program such as Serial or something similar. Don’t
forget that Serial includes driver support.
65
Once you install the building code for RP2040, you
realize that it is similar to other platforms.
66
Figure 14. ensure that you register the path to the
compiler when installing the required tools to your
Windows machine as an environment variable to be
accessed directly from the command line. Tick the
box to register the path to the ARM compiler during
installation to identify it as an environment variable
inside the Windows with the prompt displayed.
67
Wait for the prompt to install the C++ build tools
during the Build Tools for Visual Studio installation.
68
select “Install for all users” from “Advanced
Features.”
Note
You may have to make a symbolic link0so that the Makefile can find
Python 3. To do so typecmdin the Run0Window next to the Windows Menu
to open a Developer Command0Prompt Window but select "Run as
administrator" in the right0hand pane to open the window with
administrative privileges. Then0navigate toC:\Program Files\Python37and make a
symlink.
69
It is very important to tick the checkbox to allow Git
to be used from third-party tools, although you can
disallow this feature when you are not comfortable
with it. During Git installation, check the “Checkout
as is, commit as-is”, click the “Use Windows’ default
console window.” Also check the “Enable
experimental support for pseudo consoles” during
installation.
C:\Users\pico\Downloads> cd pico-sdk
70
C:\Users\pico\Downloads\pico-sdk> git submodule update --init
C:\Users\pico\Downloads\pico-sdk> cd ..
C:\Users\pico\Downloads> cd pico-examples
C:\Users\pico\Downloads\pico-examples> cd build
71
C:\Users\pico\Downloads\pico-examples\build> cmake -G "NMake
Makefiles" ..
C:\Users\pico\Downloads\pico-examples\build> nmake
C:> code
72
This line is entered once a prompt requesting it is
displayed. The environment variables in the right
order are opened in the Visual Studio Code allowing
the toolchain to be configured well.
If you start Visual Studio code by0clicking0on its desktop icon, or directly
from the Start Menu then0the build0environment will not be correctly
configured. Although this can be0done manually0later in the CMake Tools
Settings, the easiest way to0configure the VisualStudio Code environment
is just to0open it from a0Developer CommandPrompt Window where
these environmental0variables0are already set.
Warning
73
Return to the File menu and select the “Open
Folder” towards pico-examples repo. Tap “Okay”
and wait for a prompt requesting the configuration
of your project. Once it appears, click “GCC for arm-
none-eabi” as the compiler.
74
Tap the “Build” button, the one with the cog wheel
in the blue bar. It creates a directory for your
projects, runs CMake and clones the project
examples, including “Hello World”.
75
8.2.5. Flashing and Running “Hello World”
C:> chgport
COM4 = \Device\ProlificSerial10
COM5 = \Device\VCP0
77
If the drivers are not available, download and install
PuTTY. Run and choose “Serial”, enter 115,200 as
the baud rate when you tap the “Speed” box. Also
tick the serial port for your UART converter.
If you have multiple serial devices0and0can’t figure out which one is your
UART to USB serial converter, try0unplugging0your cable, and
runningchgportagain to see which0COM0port disappears.
Note
78
Chapter 9: Using other Integrated
Development Environments
79
9.1.1. Setting up Eclipse for Pico on a Linux
machine
Requirements
Note
Other 64-bit Linux distributions can0also be used but are untested, for
example, Ubuntu for Raspberry Pi. 0Please follow the usual procedure for
installing an operating0system image on to your SD card.
Enable_uart=1
80
9.1.1.1. Installing Eclipse and Eclipse plugins
./eclipse
81
9.1.1.2. Using Pico-examples
82
the menu on the file can now be used to load your
new project files into Eclipse.
9.1.1.3. Building
9.1.1.4. OpenOCD
83
is a GDB interface to Eclipse on OpenOCD, and it is
there that the debugging is done.
84
The configuration put in place the information that
you need to identify the code that you will run,
parameters, the debugger, source paths and SVD
information.
…/pico-examples-eclipse/<name of example
folder>/<optional name of example
subfolder>/executable.elf
…/pico-examples-eclipse/blink/blink.elf
86
9.1.1.5.2. Setting up the debugger
-f interface/raspberrypi-swd.cfg -f target/rp2040.cfg
87
9.1.1.5.3. setting the SVD plugin up
88
···/pico-sdk/src/rp2040/hardware_regs/rp2040.svd
89
9.2. Using CLion
90
9.2.1. Setting up CLion
91
9.2.1.1.1. Configuring CMake Profiles
92
Create several CMake profiles with their unique
settings, and perhaps include Release build by
pressing the + button, and input the
PICO_SDK_PATH once more. Press the copy button
two towards the right to fix the name and settings
of the build profile.
93
9.2.1.1.2. Running a build.
94
Use the drop-down selector to choose the target
you wish to build and a CMake profile to use which
is Debug or Release.
95
Which is interesting, because build time per binary
configuration of libraries is one ubiquitous feature of
the Pico SDK.
96
of them are compatible with the Pico SDK. You will
need a few things to ensure that your IDE and
Raspberry Pi Pico go together:
● CMake integration
● Allowing GDB support with other remote
settings.
● SVD is not necessary, although with it, reading
peripheral status becomes easier.
● Extra ARM embedded development plugin –
with these plugins available, support becomes
easier.
97
After installation, clone the openocd-svd git
repository.
$ cd ~/pico
$ cd ~/pico/test
$ gdb-multiarch test.elf
(gdb)0 load
(gdb) 0continue
98
Once openocd and gdb are running, open a third
terminal window where you launch the openocd-svd
directing it to the SVD file in the Pico SDK.
99
100
Appendix A: Using Picoprobe
101
A.1. Build OpenOCD
$ cd ~/pico
$ cd openocd
$ ./bootstrap
$ ./configure --enable-picoprobe①
$ make -j4
A.1.1. Linux
A.1.2. Windows
pacman -Syu
pacman -Su
103
Install the necessary dependencies to complete the
update
104
$ git clone https://github.com/raspberrypi/openocd.git --branch picoprobe
--depth=1
$ cd openocd
$ make -j4
A.1.3. Mac
105
Install brew if it is required
https://raw.githubusercontent.com/Homebr
ew/install/master/install.sh)"
Install dependencies
$ cd ~/pico
$ git clone https://github.com/raspberrypi/openocd.git --
branch picoprobe --depth=1
$ cd openocd
$ export PATH="/usr/local/opt/texinfo/bin:$PATH"①
$ ./bootstrap
$ make -j4
$ cd openocd
$ export PATH="/usr/local/opt/texinfo/bin:$PATH"①
$ ./bootstrap
107
$ ./configure --enable-picoprobe --disable-werror②
$ make -j4
Picoprobe Wiring
108
Pico A GP3 -> Pico B SWDIO
Also, if you make use of the UART wires, you will be able
to communicate to any other UART serial device, like the
boot console on a Raspberry Pi.
Important
One slight caveat on the above image is that if Pico0B is a0USB Host then
you’d0want to hook VBUS up to0VBUS so it can0provide 5V instead of
VSYS to VSYS.
109
A.4. Install Picoprobe driver (only needed on
Windows)
110
A.5. Using Picoprobe’s UART
A.5.1. Linux
A.5.2. Windows
111
Click PuTTTY. Choose Serial below connection type.
Type the name of your COM port and change its
speed’s number to 115200.
112
Click Open to begin the serial console as you are
getting ready to run your very first application.
A.5.3. Mac
113
Appendix B: Using Picotool
$ cd picotool
Important:
When you want to build a a picotool on macOS you can decide to install
libusb with Homebrew,
But if you build via Microsoft Windows, you will need to download and install
a Windows binary of libusb right from the libusb.info site.
$ mkdir build
114
$ cd build
$ export PICO_SDK_PATH=~/pico/pico-sdk
$ cmake ../
$ make
Note
115
Note
Important
116
B.3.1. Display information
117
For instance, connect your Raspberry Pi Pico to your
computer using the Mass Storage Mode – one way
to do this is to long-press the BOOTSEL button
before plugging the USB. After that, open a
Terminal window to type
Program Information
name: hello_world
Alternatively,
119
For example
120
B.4. Binary Information
● Program name
● Program description
● Program version string
● Program build date
● Program URL
● Program end address.
● Program features – this is a list created from
individual strings in the binary. It is displayed
according to its channel or connectivity in the
SDK.
121
● Build attributes are concerned with similar
data in the strings that are pertinent to the
binary like the Debug Build.
B.4.2. Pins
File sprite_demo.elf:
16: HSync
17: VSync
122
19: Pixel Clock
File sprite_demo.elf:
16: HSync
17: VSync
20: UART1 TX
21: UART1 RX
123
Both pin numbers and the UART function are stored
and later parsed as their real names on picotool.
Use the bi_decl_if_func_used to include the binary
information that contains the function called.
bi_decl_if_func_used(bi_pin_mask_with_name(0x1f<<
(PICO_SCANVIDEO_COLOR_PIN_BASE+
PICO_SCANVIDEO_DPI_PIXEL_RSHIFT),"Red 0-4"));
Details
#include "pico/binary_info.h"
124
There are also the ones that use underlying macros,
e.g.
#definebi_program_url(url)BI_STRING(BINARY_INFO_TAG_RASPBERRY_PI,BINA
RY_INFO_ID_RP_PROGRAM_URL,url)
An example is below,
125
If it is queried with picotool,
126
B.4.5. Setting common fields from CMake
Note
127
128