cobaltowl

We'll cross that bridge when we find it

Cross compilation for the Raspberry Pico

29-08-2023


Pre-requisites

In order to cross-compile for the Raspberry Pico on Arch, some packages (GNU embedded ARM toolchain stuff) need to be installed:

sudo pacman -Syu arm-none-eabi-newlib arm-none-eabi-binutils
arm-none-eabi-gcc

Clone the Pico SDK

git clone https://github.com/raspberrypi/pico-sdk

Set PICO_SDK_PATH to the SDK location. I recommend exporting this variable in whatever shell script is executed on instantiation for your current setup, such as .zshrc, .bashrc or .profile. This can be done (after cd'ing to the SDK folder) with:

echo "export PICO_SDK_PATH=$PWD" >> LOCATION

Building

Create a new project folder, with an inner folder named build.

The fastest way is to use a tool called "Raspberry Pico Project Generator", which can be found at https://github.com/raspberrypi/pico-project-generator.git. Usage is described in the repository, however, I recommend swapping out the original include for include(\$ENV{PICO_SDK_PATH}pico_sdk_init.cmake), as that points directly to the SDK init cmake file, and therefore, you no longer need to copy the provided import file to every single project.

You can also modify the following file to better fit your project:

cmake_minimum_required(VERSION 3.13)

set(CMAKE_C_STANDARD 11)
set(CMAKE_CXX_STANDARD 17)
set(PICO_BOARD pico CACHE STRING "Board type")
include($ENV{PICO_SDK_PATH}pico_sdk_init.cmake)

project(<YOUR_PROJECT_NAME> C CXX ASM)

pico_sdk_init()
add_executable(<EXEC_NAME> <EXEC_FILE>.c )
pico_set_program_name(<EXEC_NAME> "A_NAME_HERE")
pico_set_program_version(<EXEC_NAME> "0.1")

# If you plan on using UART, feel free to swap this around
pico_enable_stdio_uart(<EXEC_NAME> 0)
pico_enable_stdio_usb(<EXEC_NAME> 1)
target_link_libraries(<EXEC_NAME> 
pico_stdlib)

# Add the standard include files to the build

target_include_directories(<EXEC_NAME> PRIVATE

${CMAKE_CURRENT_LIST_DIR}
${CMAKE_CURRENT_LIST_DIR}/.. # for our common lwipopts or any other standard includes, if required
)

pico_add_extra_outputs(<EXEC_NAME>)

If you want an IDE-agnostic solution, all you need to do is create a C file, and then cd build.

Configure the project with cmake ..

Build with make.

You will notice a few files have been created, however, the most important one is <EXEC_NAME>.uf2, which you must copy to the filesystem root of your Raspberry Pico.

Loading

To load your program, hold the BOOTSEL button while powering on your Pico. Once the Pico is recognised by your OS, mount it as USB storage, and copy the uf2 binary to it. The Pico will restart automatically, and you'll be able to see your program running almost instantly.

Communication

If you're still using USB stdio, you can communicate with your board using a tool such as minicom. Once minicom is installed, run minicom -b 115200 -o -D /dev/ttyACM0.

IDE Config (VSCode)

First, install some important extensions that will make debugging easier:

code \--install-extension marus25.cortex-debug\
code \--install-extension ms-vscode.cmake-tools\
code \--install-extension ms-vscode.cpptools

If you're running a "free" version of VSCode (free as in freedom), such as Codium, you cannot install cpptools from the extension store by default, as Codium uses a different extension repository. You can, however, download the extension's vsix file from https://github.com/microsoft/vscode-cpptools/releases/ and install it with code \--install-extension cpptools-linux.vsix.

Once all extensions are installed, you will notice that new options have been added to the bottom left menu. Click the second option (marked with a wrench), with the text "No Active Kit":

{width="12.831cm" height="6.93cm"}

Then, select the previously installed GNU embedded ARM toolkit. If that does not appear, click on "Scan for Kits":

{width="17cm" height="5.477cm"}Now, you should be able to click "Build" on the bottom left to build your binaries. However, you might want to change your target, which can also be done on the bottom left menu.