Skip to content

ole/swift-rp-pico-bare

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

48 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

”Bare bare metal” Embedded Swift on the RP2040 (Raspberry Pi Pico)

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).

Installation

  1. 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.

  2. Download and install a current nightly Swift toolchain ("Trunk Development (main)"). Tested with 2024-01-04, newer versions will probably work too.

  3. Install Python 3 if you don’t have it.

  4. Install elf2uf2-rs and probe-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.

  5. Edit Makefile to specify the paths to clang, llvm-objcopy, and swiftc. These must point to the toolchains you just installed.

Building with SwiftPM

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

Building with make

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

About

Embedded Swift on the Raspberry Pi Pico without the Pico C/C++ SDK

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published