Apple Embeds Swift Into Hardware

Apple has released Embedded Swift, a subset of the Swift language, bringing Swift to both Arm and RISC-V microcontrollers.

Alasdair Allan
1 month ago β€’ Internet of Things
Embedded Swift running on the ESP32-C6-DevKitC-1 development board. (πŸ“·: Apple)

Last week was Apple WWDC, the company's annual developer conference. If you're deep into the Apple ecosystem β€” and having written half a dozen books on iOS development back in the day, I am β€”Β you probably paid more than just passing attention. Because while it's rare to get new hardware announcements at WWDC it is Apple's flagship software conference, so if you want to get a feel for where they're going, you always need to watch WWDC fairly closely. But for those of you out there whose day-to-day computer isn't a MacBook, you might want to look up and pay attention this time.

This year the preview of Apple Intelligence got all the press coverage, and the majority of time during the keynote β€”Β at least if you ignore the arrival of the calculator app on iPad. However, it wasn't the only thing announced at the conference, and amongst those other things was Embedded Swift.

WWDC24: Go small with Embedded Swift (πŸ“·: πŸ“Ή)

A cut down version of the Swift language, Embedded Swift is a port of the language to microcontroller-sized hardware with a much smaller footprint, with binaries weighing in kilobytes rather than megabytes.

Embedded Swift is a subset of Swift, covering most of the language you know and love β€”Β it's a full featured subset that includes support for value and reference types, closures, optionals, error handling, generics, and more.

Embedded Swift also has additions needed for microcontrollers like Swift MMIO, which makes it easy to work with registers directly in Swift and manipulate them in a safe way.

Using the Swift language on embedded hardware isn't really a new idea. MadMachine has been shipping microcontroller boards with their SwiftIO framework layered on top of Zephyr RTOS for over four years, allowing you to write Swift on their own microcontroller hardware. There's also been a more recent bare metal port of Swift to the Raspberry Pi RP2040, along with another proof-of-concept route to run embedded Swift code using the Pico SDK, along with an entirely separate port to the STM32F4 series of microcontrollers.

The big difference with the release this week of Embedded Swift by Apple β€” potentially sherlock'ing yet another bunch of projects and products in the process β€” is that not only does it work across a range of microcontrollers boards, it is officially supported by Apple.

The Embedded Swift release also isn't hardware specific, it's not tied to a single architecture. Both 32-bit and 64-bit Arm and RISC-V chips are supported, and it should be fairly easy to port it to new instructions sets if someone wants to do that. Right now any board based on an STMicro STM32F746, Raspberry Pi RP2040, Nordic nRF52840, or the Espressif ESP32-C6 should work with Embedded Swift right out of the box.

While the video introducing Embedded Swift showed code running on a ESP32-C6-DevKitC-1 development board there is example code available for each of the supported release platforms. Apple has even provided code showing a proof-of-concept example of Embedded Swift running on the Flipper Zero.

Interestingly, we also learned that Apple devices already use Embedded Swift in their Secure Enclave processor. Not only does this tell us that the "cut down" port of Swift to microcontrollers has been around a while, it also tells us that it's probably not going away any time soon. Apple are eating their own dog food here, and like Forth as firmware, Embedded Swift will probably live a long life as a result.

Traditional embedded developers might be confused at this point, about just why this might be important, and why they should be paying attention. What they should remember is that Swift β€” like Objective-C before it β€” can import C and C++ libraries and frameworks easily. It's compatible. That means that interoperability with existing microcontroller environments is just built-in, and the large existing cadre of iOS and macOS developers who are used to and comfortable with the Swift language can integrate microcontroller hardware with the Apple platform β€” something I've been talking about for more than 10 years β€” with very little learning curve.

I think the real niche that Embedded Swift is going to fill is to be building Matter-enabled devices that can be used directly from Apple's HomeKit. After all building a smart light is only around sixty lines of code. But you can potentially see this getting much wider adoption. Apple has just brought a large existing developer community to door of embedded hardware, then proceeded to kick the door in for them.

I’d written several books on Objective-C and iPhone development. When Apple announced Swift 10 years ago my default tooling to solve any programming problem was to reach for XCode, and Objective-C. After starting to transition to Swift three times, and Apple making major breaking changes every time I started, I gave up. Rumors of disasters porting and building large code bases, like those coming out of Uber, really didn't help. Apple lost me, and a lot of other developers, in the transition from Objective-C to Swift.

Interestingly perhaps, a lot of us ended up building hardware, and this release is going to make me and probably a number of others look hard at Swift once again. Because it definitely all looks easier than it was, more than a decade ago now, when I had to use an RS232 cable to connect an Arduino to my iPad.

If you want to take a look at Embedded Swift as well you should bear in mind that it is "under active development" and is not source stable β€” although I think many would claim that's still the case for Swift proper, despite it having theoretically replaced Objective-C as the main development language for Apple products some years back β€” so is only available as a preview and part of the nightly toolchains from swift.org.

Support is available through the Swift forums, where there is already a Q&A thread on how to get started with the provided example code. Documentation for Embedded Swift can be found, at least for now, on GitHub.

Alasdair Allan
Scientist, author, hacker, maker, and journalist. Building, breaking, and writing. For hire. You can reach me at πŸ“« alasdair@babilim.co.uk.
Latest articles
Sponsored articles
Related articles
Latest articles
Read more
Related articles