Embedded Swift - supporting Cortex M architecture


(Nicholas Outram) #1

Hi

This is a general suggestion, but one I’ve pondered about for quite a long time.

In short, it would be great if the Swift compiler could target embedded systems, and specifically the ARM Cortex M4 architecture (or later).

Rationale:

To date, Swift is ported to x86 and ARM Cortex A architectures (and there may be others in the pipeline)

There are billions of low-power devices out there running "single process - multithreaded applications" on ARM Cortex M class devices (https://en.wikipedia.org/wiki/ARM_Cortex-M). This is typically the world of the hidden embedded computer, be that antilock braking systems, lightbulbs, instrumentation, toys, consumer products, peripherals devices (accelerometers, gps, wifi, bluetooth, gyros, power management, screen controllers, huid) and white goods etc. The list goes on.

These are by far the most pervasive computers in the world, yet we are still writing embedded software in (older versions of) C and C++.

Now, with IoT, they are being increasingly connected to the internet - security and vulnerabilities are a big concern for IoT (yes, the first fridge has been hacked - http://www.cnet.com/uk/news/fridge-caught-sending-spam-emails-in-botnet-attack/!)

Cortex M devices are typically used for sense, control and real-time tasks, but now also IoT applications as M-class devices become more capable.

Of course, as with anything connected to the Internet, IoT applications are potentially vulnerable.

If you look at mbed 3 from ARM https://www.mbed.com/en/ (which ARM seem to be pushing quite hard), as I understand it, you see only the Cortex M featured (others might be featured, but MBED 2 was certainly M-class only).

(Also, in the education space, where embedded computing is taught, Cortex M commonly features - the mbed 2 C++ abstraction libraries were originally designed for education and are excellent for prototyping).

Java ME has recently moved into this space, although I’ve no figures on uptake or how many platforms are supported.

Most embedded software is still written in C and C++. C is widely viewed to be simply unsafe; safer versions of C++ exist but adoption is slow, and performance is getting slower as I understand it. Also, as a language for humans to write and maintain, it has legacy issues and does not benefit from the complete recent design you get with Swift.

Swift has a promise to be all things, and coupled with ARC, would seem to be an ideal candidate embedded language?

Now I’m no compiler engineer (I just happen to teach iOS and real-time embedded computing).

My take on this is as follows:

For this to happen, Swift + Swift Std. Libs. would need to target the Cortex M instruction set (I suggest M4 or later) and live with more limited protection.
Maybe it would require an “embedded Swift” variant?
Some quirks of embedded software: Embedded software often allows, but avoids dynamic memory allocation; there is only one process, but possibly multiple threads; it would need to target the reduced M instruction set; memory is much more limited; direct hardware access is available and required at some level, although most manufacturers (including mbed) provide some abstraction layers (a very good thing).

I think it would be great if platforms such as mbed 3 could offer Swift as an alternative to C++. I know which language I would rather write.

I'm not best placed to say what technical issues might/might not prevent the Swift compiler from targeting Cortex M devices. What’s the view of the community on this?

Nick Outram

Plymouth University