Raspberry Pi RP2040
The Raspberry Pi RP2040 is a microcontroller developed by Raspberry Pi. The MCU is an ARM Cortex M0+ based chip with a number of common peripherals paired with it. The chip also has an interesting Programmable IO subsystem. In this current era of chip shortage, this chip has proven to be the most available.
In this article, I take you through setting up your toolchain in order to write software for this chip.
It must be noted that the official documentation for doing this is well written.
Prerequisites
In my previous article, I showed how to setup an ARM toolchain. This is one basic prerequite for setting up your system for RP2040 development.
Another required installation is the CMake build tool since the PICO-SDK depends on it.
CMake Installation
If you do not have a C++ compiler installed, install one.
The GNU C++ compiler can be installed as follows:
1 | sudo dnf install g++ |
To install CMake run:
1 | sudo dnf install cmake |
Raspberry Pi Pico C/C++ SDK
Clone Pi Pico SDK:
Clone the SDK by running:
1 | git clone https://github.com/raspberrypi/pico-sdk.git ~/opt/pico-sdk |
The SDK will be cloned into the ~/opt/pico-sdk
directory in this example.
There are a couple of submodules that need to be cloned into the SDK directory to help with USB and wireless communication for the boards that have it.
Change directory into the ~/opt/pico-sdk
directory and run the following:
1 | git submodule update --init |
Copy pico_sdk_import.cmake from the SDK into your project directory
Test Pico SDK installation
Create a directory for the project(you can call it any name).
Create a file named
main.c
with the following content.1
2
3
4
5
6
7
8
9
int main() {
setup_default_uart();
printf("This works!\n");
return 0;
}In your project directory add a
CMakeLists.txt
file with the following initial contents:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24cmake_minimum_required(VERSION 3.13)
# initialize the SDK based on PICO_SDK_PATH
# note: this must happen before project()
set(PICO_SDK_PATH ~/opt/pico-sdk) #Set the path to the location of the cloned SDK
include(pico_sdk_import.cmake)
set(MY_TARGET test_pico) # name of build target
project(my_project)
# initialize the Raspberry Pi Pico SDK
pico_sdk_init()
# rest of your project configs
add_executable(${MY_TARGET}
main.c
)
# Add pico_stdlib library which aggregates commonly used features
target_link_libraries(${MY_TARGET} pico_stdlib)
# create map/bin/hex/uf2 file in addition to ELF.
pico_add_extra_outputs(${MY_TARGET})Create a directory inside the project directory with the name
build
.Inside a terminal, change into the build directory, thus:
1
cd ./build
Run the
cmake
command inside thebuild
directory to create your makefile as follows:1
cmake ..
Run
make
in the build:1
make test_pico
You should now have a file named
test_pico.elf
and some other formats includingtest_pico.uf2
.
This confirms that the SDK setup was successful. Next, we have to get theelf
file or theuf2
file into the flash paired with the RP2040.
Flash the executable onto the RP2040’s flash storage
UF2
One way of doing this is by copying the uf2
file onto the RP2040 device when it shows up as a mass storage device on the computer.
Depending on the board you have, you could trigger this in different ways.
For the Pi Pico board, press the BOOTSEL
button while inserting the USB cable into your computer.
Once the device shows up, copy the uf2
file and paste it inside the mass storage device. If this is done successfully, the device will reset and start to run your program.
This approach is the simplest way of getting code onto your device.
Conclusion
At this point, we have gone from compiler to binary running on the RP2040. We have shown that the Pico SDK works by compiling code and loading it onto the Pi Pico.
However, there is another way of going about flashing code onto the microcontroller board. This approach is somewhat hassle free with regards to removing and inserting the USB cable of your board. It also allows us to debug our programs by stepping through our code.
In my next article, I will endeavour to show this approach too.
PermaLink:
https://raynayx.com/2022/06/20/piPicoSetup/