This is sort of a follow-up on Embedded Swift running on the Raspberry Pi Pico, but with a different take.
I managed to create a very small Embedded Swift "toolchain" for building pure Swift executables for the Raspberry Pi Pico (it might also work with other RP2040 boards, but I'm only testing on the Pico). The "toolchain" is just a Makefile, so no SwiftPM integration yet, but I feel we're not too far off. The repository is here if you want to try it out:
It should work on macOS and Linux as hosts (tested with Ubuntu 22.04 via Docker). Please report back if you try this out and manage to get something running on a Pico.
Unlike our other approach (in Embedded Swift running on the Raspberry Pi Pico), this doesn't use the Raspberry Pi Pico C/C++ SDK as a basis, which has pros and cons:
-
Much less build system complexity. This makes it much more feasible IMO to get it working with SwiftPM eventually. I found the complexity of the Pico SDK's CMake config very overwhelming. The Makefile we have here isn't pretty, but it's soooo much smaller.
-
We have to build everything from scratch, so almost nothing works yet. I wrote a few simple functions to talk to a GPIO pin (very ugly code currently), but that's it for now — we have no timers, no I²C, no SPI, etc.
-
This clean slate is also exciting, of course. Lots of room for experimentation and learning.
The main application code is all Swift. There's very little C and Assembly involved for the bootstrapping. I copied the necessary files (linker script etc.) from the Pico SDK and patched them slightly. The bootstrapping code still does a few things we don't really need. I hope to make whittle it down to the bare minimum we need eventually, if only to understand it better.
I'm using the C compiler and linker from the ARM Embedded LLVM toolchain. If I'm reading @kubamracek current work in the Swift repo correctly (e.g. [embedded] Start building and including lld even in Darwin toolchains by kubamracek · Pull Request #70715 · apple/swift · GitHub), we may soon be able to use the Swift linker for this?
By the way, the current executable, which blinks an LED and listens to a GPIO input, is less than 1 KB:
/Applications/LLVMEmbeddedToolchainForArm-17.0.1-Darwin/bin/llvm-size SwiftPico.elf
text data bss dec hex filename
992 0 0 992 3e0 SwiftPico.elf
The next step I'd love to take is SwiftPM integration as this would make it so much easier to integrate other packages. I started experimenting with building an SE-0387-style SDK for the RP-2040, but I haven't succeeded yet. I'll probably post questions in a separate thread.