Skip to content

Instantly share code, notes, and snippets.

@mildmojo
Last active October 23, 2024 17:33
Show Gist options
  • Save mildmojo/f8cd9170e3c619ff99e16f16a0ceda1d to your computer and use it in GitHub Desktop.
Save mildmojo/f8cd9170e3c619ff99e16f16a0ceda1d to your computer and use it in GitHub Desktop.
PlatformIO config: Arduino for Raspberry Pi Pico (RP2040) with Programmable IO (PIO) support

PlatformIO: Arduino for Raspberry Pi Pico (RP2040) with Programmable IO (PIO) support

This is a set of files that sets up an environment that will compile an Arduino project and assemble Programmable IO (PIO) scripts into C++ header files for inclusion in Arduino source files.

I made these notes in October 2024 after spending a lot of time researching how to make PlatformIO, Arduino, and PIO work together.

(Not yet fully tested.)

Prerequisites

File layout

The files in this gist would be arranged on disk as follows (main.cpp and my-pio.pio would be your project code, and are not included here):

my-cool-project/
  Dockerfile
  platformio.ini
  scripts/
    pioasm-build-sh-wrapper.py
    pioasm-build.sh
  src/
    main.cpp
    my-pio.pio

Prep

You can either (re)build the docker image ahead of time, or let it happen as part of the PlatformIO build (see "Usage" below).

docker build -t pioasm .

If you wanted to run pioasm against your source files manually, you could do this in the root of your project (assuming your source is in the src/ dir):

docker run --rm -v $(pwd):/code pioasm /code/src/my-pio.pio /code/src/my-pio.pio.h

Usage

Use the PlatformIO CLI to compile and upload your project to a Raspberry Pi Pico or other RP2040-based board:

pio run -e rpi -t upload

Alternative to Docker

Instead of dockerizing pioasm, you could compile and install it locally on your apt-based Linux system:

sudo apt install build-essentials cmake gcc-arm-none-eabi libnewlib-arm-none-eabi libstdc++-arm-none-eabi-newlib

git clone --branch $RELEASE_TAG --depth 1 https://github.com/raspberrypi/pico-sdk.git /tmp/pico-sdk

cd /tmp/pico-sdk/tools/pioasm && \
  cmake . && \
  make && \
  cp pioasm ~/.local/bin ### Copy to somewhere in your $PATH

Then you could either run it manually on your *.pio files or modify pioasm-build.sh to remove the docker bits and call the pioasm command directly.

# scripts/pioasm-build-sh-wrapper.py
#
# Python shim to run the real build script.
# PlatformIO has an undocumented requirement that `extra_scripts` be Python scripts.
import os
os.system('bash -c ./scripts/pioasm-build-all.sh')
#!/bin/bash
# scripts/pioasm-build.sh
PICO_SDK_TAG=${PICO_SDK_TAG:-1.5.1}
# Make sure docker is installed.
if ! type "docker" > /dev/null; then
echo "Please install docker to use dockerized pioasm."
exit 1
fi
# Make sure docker is running.
if ! docker info > /dev/null 2>&1; then
echo "Docker engine not running. Please start docker and try again."
exit 1
fi
# Build the pioasm docker image if it doesn't exist.
if [ -z "$(docker images -q pioasm 2> /dev/null)" ]; then
docker build --build-arg "PICO_SDK_TAG=$PICO_SDK_TAG" -t pioasm .
fi
# Run pioasm to assemble every *.pio file in the source tree.
for srcfile in $(find . -type f -name *.pio); do
echo "Building $srcfile with pioasm..."
docker run --rm -v $(pwd):/src pioasm /src/$srcfile /src/${srcfile}.h
done

PlatformIO: Arduino for Raspberry Pi Pico (RP2040) with Programmable IO (PIO) support

This is a set of files that sets up an environment that will compile an Arduino project and assemble Programmable IO (PIO) scripts into C++ header files for inclusion in Arduino source files.

I made these notes in October 2024 after spending a lot of time researching how to make PlatformIO, Arduino, and PIO work together.

(Not yet fully tested.)

Prerequisites

File layout

The files in this gist would be arranged on disk as follows (main.cpp and my-pio.pio would be your project code, and are not included here):

my-cool-project/
  Dockerfile
  platformio.ini
  scripts/
    pioasm-build-sh-wrapper.py
    pioasm-build.sh
  src/
    main.cpp
    my-pio.pio

Prep

You can either (re)build the docker image ahead of time, or let it happen as part of the PlatformIO build (see "Usage" below).

docker build -t pioasm .

If you wanted to run pioasm against your source files manually, you could do this in the root of your project (assuming your source is in the src/ dir):

docker run --rm -v $(pwd):/code pioasm /code/src/my-pio.pio /code/src/my-pio.pio.h

Usage

Use the PlatformIO CLI to compile and upload your project to a Raspberry Pi Pico or other RP2040-based board:

pio run -e rpi -t upload

Alternative to Docker

Instead of dockerizing pioasm, you could compile and install it locally on your apt-based Linux system:

sudo apt install build-essentials cmake gcc-arm-none-eabi libnewlib-arm-none-eabi libstdc++-arm-none-eabi-newlib

git clone --branch $RELEASE_TAG --depth 1 https://github.com/raspberrypi/pico-sdk.git /tmp/pico-sdk

cd /tmp/pico-sdk/tools/pioasm && \
  cmake . && \
  make && \
  cp pioasm ~/.local/bin ### Copy to somewhere in your $PATH

Then you could either run it manually on your *.pio files or modify pioasm-build.sh to remove the docker bits and call the pioasm command directly.

# Based on: https://arduino-pico.readthedocs.io/en/latest/platformio.html#current-state-of-development
[platformio]
[env:rpi]
framework = arduino
# Update this integration with: pio pkg update -g -p https://github.com/maxgerhardt/platform-raspberrypi.git
platform = https://github.com/maxgerhardt/platform-raspberrypi.git
board = pico
board_build.core = earlephilhower
# Compile *.pio files using docker + pioasm from pico-sdk
extra_scripts = pre:scripts/pioasm-build-sh-wrapper.py
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment