Hey Swift Community!
Developing software for embedded devices is steadily gaining in popularity, with hobbyists and professionals alike writing custom logic for microcontroller boards such as Arduino. Much of this programming is done in C or C-like languages, which lacks both the ergonomics that make modern programming more pleasant and the language safety features that make programs more correct.
Swift represents the state of the art in both these areas, without asking developers to choose between the two. This represents an exciting opportunity to expand Swift's use cases to a whole new domain: embedded programming.
There is existing work in this area, and great past discussions, both here on the forums (link) and in last year’s video call (link), which shows there is a lot of interest and potential for Swift in this space. There is also ongoing work to make Swift more applicable to system libraries and kernel development, which has some of the same challenges and requirements.
The current approaches face challenges (e.g. codesize and memory usage) and could benefit from a vision document to capture the goals and ideas necessary for bringing Swift into these environments. Here’s a prospective vision for “Embedded Swift”, a new compilation mode with first class support for embedded/low-level environments:
Embedded Swift Vision Document:
GitHub Pull Request:
We have built a custom toolchain that implements some of the ideas from the vision document, and we have successfully built sample firmwares that demonstrate that the goals of the vision can be achieved:
- (1) A Swift firmware running on an STM32 MCU board animating colors on an LED strip. This shows that we are able to build a fully standalone firmware binary with Swift with minimal dependencies, and run it in a “bare metal” (no underlying operating system) environment. We have originally built this firmware using the existing LTO-based “hermetic seal” approach, which resulted in final firmware size of ~500 kB, most of which is non-application code (Swift runtime, very trimmed down Swift standard library, libc, libc++, libm). We then built the firmware using Embedded Swift which resulted in firmware size of under 10 kB.
- (2) A Swift firmware animating a Swift logo on an LCD screen of a STM32 MCU board. The total size of this firmware is ~15 kB, most of which is spent on the uncompressed RGBA pixels of the logo (50x50 pixels, 4 bytes each = 10 kB).
Here is an excerpt of the code shown running in the video. It demonstrates the extent to which Embedded Swift programs are natural and idiomatic Swift, with uses of standard library facilities, generics, string literals, and so on:
// Generics, optionals, stdlib types, custom types, yet the following still compiles // down to a straightforward sequence of MMIO register accesses without any other // runtime calls. Results in a few hundred bytes of dependency-free code. let usart1 = USART(baseAddress: UnsafeMutablePointer<USART_t>(bitPattern: 0x40011000 as UInt)!) var uart = UART<STM32F746>(uart: usart1) uart.configure(configuration: .init(enableRead: true, enableWrite: true, baudRate: 19200)) uart.print(message: "Hello UART!")
We are seeking community input on the model for Embedded Swift and its usability, and we welcome any discussion on use cases as we start building support into the Swift project.
Please send any feedback as a reply directly under this posting.
I’m looking forward to working with everyone on bringing Swift to this exciting new frontier!