Build a Swift executable for the Raspberry Pi Pico without the Pico C SDK. Tested on macOS (13 and 14) and Linux (Ubuntu 22.04).
-
If you're on macOS, download and install the ARM Embedded LLVM Toolchain. Tested with v17.0.1, newer versions will probably work too. This isn’t necessary on Linux.
-
Download and install a current nightly Swift toolchain ("Trunk Development (main)"). Tested with 2024-01-04, newer versions will probably work too.
-
Install Python 3 if you don’t have it.
-
Install
elf2uf2-rs
andprobe-rs
(requires a working Rust installation):cargo install elf2uf2-rs --locked cargo install probe-rs --features cli
We’ll use these tools to create the UF2 file for the Pico and/or flash the ELF file to the Pico.
-
Edit
Makefile
to specify the paths toclang
,llvm-objcopy
, andswiftc
. These must point to the toolchains you just installed.
On Linux:
# Build only
# Creates a static library libApp.a
swift build --triple armv6m-none-none-eabi
# Build and link final executable App.elf
swift package --triple armv6m-none-none-eabi link
On macOS:
# Download a version of LLVM that includes ld.lld, the linker that Swift wants to use for ELF files.
brew install llvm
# Place `ld.lld` in your PATH:
ln -s /opt/homebrew/opt/llvm/bin/ld.lld ~/bin/ld.lld
# Build and link
swift package --triple armv6m-none-none-eabi --toolchain /Library/Developer/Toolchains/swift-latest.xctoolchain/ link --objcopy=/opt/homebrew/opt/llvm/bin/llvm-objcopy
Running make
will build the build/SwiftPico.elf
executable:
# Creates build/SwiftPico.elf
make
If you have a Raspberry Pi Debug Probe or a second Pico that you can use as a debug probe, use probe-rs
to flash it to the Pico:
probe-rs run --chip RP2040 build/SwiftPico.elf
Alternatively, use elf2uf2-rs
to create a UF2 file which you can copy to the Pico in BOOTSEL mode:
# Creates build/SwiftPico.uf2
elf2uf2-rs build/SwiftPico.elf