STM32 example project for NUCLEO-F411RE with Swift concurrency, using newlib on Linux

I was curious to see if I could add some Swift code to my colleague’s STM32 project. This was easy enough, but as a next step I wanted to make Swift concurrency work as well. I’m on Linux though, for which Swift doesn’t include any concurrency support for armv7em-none-none-eabi targets. After digging around for a while, I managed to compile an armv7em-none-none-eabi runtime that includes support for concurrency, and was able to use that in the project. Just like the project, the runtime is compiled against newlib.

Since the original project was proprietary, I’ve created an open-source version. It consists of an STM32CubeMX-generated project for the NUCLEO-F411RE development board, plus some Swift code that will blink an LED. The project includes a pre-built version of the armv7em-none-none-eabi runtime. In case anyone is interested, the project can be found here:

Note: I’m not familiar with STM32 and newlib, and only a little bit with CMake, so I probably made a few questionable choices.

5 Likes

Small update: I added a custom executor, GlobalExecutor.swift, which takes care of running the async jobs and tasklets. It replaces the default executor, CooperativeGlobalExecutor.cpp, from the Swift runtime.

It is a very basic executor that doesn't conform to any Executor protocols and doesn't use any supporting Swift types, like for example UnownedJob. It provides the same interface as the default executor and just runs the jobs that are enqueued. This was the easiest way to quickly get a custom executor working. As an improvement, UnownedJob could perhaps be used to get access to some job specific data, like the job's priority.

3 Likes