Created
December 2, 2022 21:40
-
-
Save SpotlightKid/e3c44e3fc74c77749120041ea8b7f57d to your computer and use it in GitHub Desktop.
pico_setup.sh - Set up a development environment for the Raspberry Pi Pico
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# | |
# pico_setup.sh - Set up a development environment for the Raspberry Pi Pico | |
# | |
# See the "Getting started with Raspberry Pi Pico" guide for reference: | |
# | |
# https://www.raspberrypi.com/documentation/microcontrollers/c_sdk.html | |
# | |
# The script will: | |
# | |
# * Create a directory called pico in the current directory¹ | |
# * Install required dependencies. | |
# * Check out the pico-sdk, pico-examples, pico-extras, and pico-playground | |
# repositories from GitHub. | |
# * Define PICO_SDK_PATH, PICO_EXAMPLES_PATH, PICO_EXTRAS_PATH, and | |
# PICO_PLAYGROUND_PATH in your ~/.bashrc. | |
# * Build the blink and hello_world examples in pico-examples/build/blink | |
# and pico-examples/build/hello_world. | |
# * Download and build picotool (see Appendix B), and copy it to /usr/local/bin. | |
# * Download and build picoprobe (see Appendix A). | |
# * (Optionally) Download and compile OpenOCD (for debug support). | |
# * Download and install Visual Studio Code. | |
# * Install the required Visual Studio Code extensions (see Chapter 7 for more | |
# details). | |
# * (Optional) Configure the Raspberry Pi UART for use with Raspberry Pi Pico. | |
# | |
# ¹ A different name or path can be passed as the first command line argument. | |
# | |
# The script's behaviour can be modified in some way by setting certain | |
# environment variables: | |
# | |
# SKIP_INSTALL_DEPS=1 - Don't install dependencies, assume they're already satisfied | |
# SKIP_OPENOCD=1 - Don't build & install OpenOCD | |
# SKIP_VSCODE=1 - Don't install VSCode | |
# PREFIX=<path> - Install picotool and OpenOCD to $PREFIX instead of /usr/local | |
# | |
# This script has been adapted to work on Debian/Ubuntu based Linux distros | |
# (including Raspberry Pi OS) and Arch Linux based distros. | |
# Exit on error | |
set -e | |
# Clear env variables which may interfere with building | |
unset CFLAGS CXXFLAGS LDFLAGS | |
if grep -qEi '(ubuntu|debian|mint)' /etc/lsb-release; then | |
UPDATE_PACKAGES="sudo apt update" | |
INSTALL_PACKAGES="sudo apt install -y" | |
SDK_DEPS="cmake gcc-arm-none-eabi-bin gcc g++" | |
OPENOCD_DEPS="gdb-multiarch automake autoconf build-essential texinfo libtool libftdi-dev libusb-1.0-0-dev" | |
elif grep -qEi '(arch|manjaro)' /etc/lsb-release; then | |
UPDATE_PACKAGES="sudo pacman -Syu" | |
INSTALL_PACKAGES="sudo pacman -S --needed --noconfirm --quiet" | |
SDK_DEPS="base-devel cmake arm-none-eabi-gcc arm-none-eabi-binutils arm-none-eabi-gdb arm-none-eabi-newlib" | |
OPENOCD_DEPS="texinfo libtool libftdi libusb" | |
else | |
echo "Distribution not recognized. Aborting." >/dev/stderr | |
exit 1 | |
fi | |
if grep -q Raspberry /proc/cpuinfo; then | |
echo "Running on a Raspberry Pi" | |
else | |
echo "WARNING: Not running on a Raspberry Pi. Use at your own risk!" >/dev/stderr | |
fi | |
# Number of cores when running make | |
JNUM=$(nproc --ignore=1) | |
# Where will the output go? | |
OUTDIR="${1:-$(pwd)/pico}" | |
# Where will picotool and OpenOCD be installed? | |
PREFIX="${PREFIX:-/usr/local}" | |
# Install dependencies | |
GIT_DEPS="git" | |
VSCODE_DEPS="code" | |
UART_DEPS="minicom" | |
# Build full list of dependencies | |
DEPS="$GIT_DEPS $SDK_DEPS" | |
if [[ "$SKIP_OPENOCD" == 1 ]]; then | |
echo "Skipping OpenOCD (debug support)" | |
else | |
DEPS="$DEPS $OPENOCD_DEPS" | |
fi | |
if [[ "$SKIP_INSTALL_DEPS" != "1" ]]; then | |
echo "Installing Dependencies" | |
$UPDATE_PACKAGES | |
$INSTALL_PACKAGES $DEPS | |
fi | |
if [[ ! -d "$OUTDIR" ]]; then | |
echo "Creating $OUTDIR" | |
# Create pico directory to put everything in | |
mkdir "$OUTDIR" | |
fi | |
cd "$OUTDIR" | |
# Clone sw repos | |
GITHUB_PREFIX="https://github.com/raspberrypi/" | |
GITHUB_SUFFIX=".git" | |
SDK_BRANCH="master" | |
for REPO in sdk examples extras playground; do | |
DEST="$OUTDIR/pico-$REPO" | |
if [[ -d "$DEST" ]]; then | |
if [[ -d "$DEST/.git" ]]; then | |
cd "$DEST" | |
git co master && git pull | |
else | |
echo "Directory $DEST already exists and is not a Git checkout. Aborting." >/dev/stderr | |
exit 1 | |
fi | |
else | |
REPO_URL="${GITHUB_PREFIX}pico-${REPO}${GITHUB_SUFFIX}" | |
echo "Cloning $REPO_URL" | |
git clone -b $SDK_BRANCH "$REPO_URL" | |
# Any submodules | |
cd "$DEST" | |
git submodule update --init | |
cd "$OUTDIR" | |
fi | |
# Define PICO_SDK_PATH in ~/.bashrc | |
VARNAME="PICO_${REPO^^}_PATH" | |
export ${VARNAME}="$DEST" | |
done | |
if ! grep -q "^PICODIR=" ~/.bashrc; then | |
echo "Adding evironment variables to bashrc" | |
echo -e "\n# Raspberry Pico SDK" >> ~/.bashrc | |
echo "export PICODIR=\"$OUTDIR\"" >> ~/.bashrc | |
for REPO in sdk examples extras playground; do | |
VARNAME="PICO_${REPO^^}_PATH" | |
echo "Adding $VARNAME to ~/.bashrc" | |
echo "export $VARNAME=\"\$PICODIR/pico-$REPO\"" >> ~/.bashrc | |
done | |
echo "unset PICODIR" >> ~/.bashrc | |
fi | |
cd "$OUTDIR" | |
# Pick up new variables we just defined | |
source ~/.bashrc | |
# Build a couple of examples | |
echo "Building SDK examples..." | |
cd "$OUTDIR/pico-examples" | |
mkdir -p build | |
cd build | |
cmake ../ -DCMAKE_BUILD_TYPE=Debug | |
for e in blink hello_world; do | |
echo "Building $e..." | |
cd $e | |
make -j$JNUM | |
cd .. | |
done | |
cd "$OUTDIR" | |
# Picoprobe and picotool | |
for REPO in picoprobe picotool; do | |
echo "Building $REPO..." | |
DEST="$OUTDIR/$REPO" | |
if [[ -d "$DEST" ]]; then | |
if [[ -d "$DEST/.git" ]]; then | |
cd "$DEST" | |
git co master && git pull | |
else | |
echo "Directory $DEST already exists and is not a Git checkout. Aborting." >/dev/stderr | |
exit 1 | |
fi | |
else | |
REPO_URL="${GITHUB_PREFIX}${REPO}${GITHUB_SUFFIX}" | |
git clone "$REPO_URL" | |
cd "$DEST" | |
git submodule update --init | |
fi | |
# Build both | |
mkdir -p build | |
cd build | |
cmake ../ | |
make -j$JNUM | |
if [[ "$REPO" == "picotool" ]]; then | |
echo "Installing picotool to $PREFIX/bin/picotool" | |
sudo install -v -Dm755 picotool -t "$PREFIX"/bin | |
fi | |
cd "$OUTDIR" | |
done | |
if [[ -x "$PREFIX"/bin/openocd ]]; then | |
echo "openocd already installed so skipping" | |
SKIP_OPENOCD=1 | |
fi | |
if [[ "$SKIP_OPENOCD" == 1 ]]; then | |
echo "Won't build OpenOCD" | |
else | |
# Build OpenOCD | |
echo "Building OpenOCD..." | |
cd "$OUTDIR" | |
DEST="$OUTDIR/openocd" | |
# Should we include picoprobe support (which is a Pico acting as a debugger for another Pico) | |
INCLUDE_PICOPROBE=1 | |
OPENOCD_BRANCH="rp2040" | |
OPENOCD_CONFIGURE_ARGS="--enable-ftdi --enable-sysfsgpio --enable-bcm2835gpio" | |
if [[ "$INCLUDE_PICOPROBE" == 1 ]]; then | |
OPENOCD_CONFIGURE_ARGS="$OPENOCD_CONFIGURE_ARGS --enable-picoprobe" | |
fi | |
if [[ -d "$DEST" ]]; then | |
if [[ -d "$DEST/.git" ]]; then | |
cd "$DEST" | |
git co master && git pull | |
else | |
echo "Directory $DEST already exists and is not a Git checkout. Aborting." >/dev/stderr | |
exit 1 | |
fi | |
else | |
git clone "${GITHUB_PREFIX}openocd${GITHUB_SUFFIX}" -b "$OPENOCD_BRANCH" --depth=1 | |
cd openocd | |
fi | |
./bootstrap | |
./configure "--prefix=$PREFIX" $OPENOCD_CONFIGURE_ARGS | |
make -j$JNUM | |
sudo make install | |
fi | |
cd "$OUTDIR" | |
if [[ "$SKIP_VSCODE" == 1 ]]; then | |
echo "Skipping VSCODE" | |
else | |
echo "Installing VSCODE..." | |
$INSTALL_PACKAGES $VSCODE_DEPS | |
# Get extensions | |
# BUG: the extension ms-vscode.cpptools is not found by Open Source build | |
# of Visual Studio Code on Arch Linux | |
# See: https://github.com/microsoft/vscode-cpptools/issues/6266 | |
for EXT in marus25.cortex-debug ms-vscode.cmake-tools ms-vscode.cpptools; do | |
code --install-extension "$EXT" || \ | |
echo "Installation of VSCode extension $EXT failed. Please install it manually!" \ | |
>/dev/stderr | |
done | |
fi | |
# Enable UART | |
if [[ "$SKIP_UART" == 1 ]]; then | |
echo "Skipping uart configuration" | |
else | |
if [[ "$SKIP_INSTALL_DEPS" != "1" ]]; then | |
$INSTALL_PACKAGES $UART_DEPS | |
fi | |
if which raspi-config >&/dev/null; then | |
echo "Disabling Raspberry Linux serial console (UART) so we can use it for pico" | |
sudo raspi-config nonint do_serial 2 | |
echo "You must run sudo reboot to finish UART setup" | |
fi | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment