Introduce embedded development using Swift

Hi @andyliu,
I just went to findchips for the pricing info. And yes, it is mostly a supply constraint at the moment.
What I was trying to say was (at least in my experience) that the most embedded chips are not 600MHz Cortex M7 chips but more lower end MCU like the Cortex M0/M3 types.
If the idea is to support lower end chips eventually but to get it working on a higher end first (more room / faster etc) I fully understand, since there is little to no room on an M0. But I would like to make the point that lower end MCUs are in much larger volume and not taking these into account for swift embedded would be missing a large target audience.

I'm happy to read that CMSIS support is already in Zephyr, although in a limited fashion. I will take a further look into Zephyr. A quick read of the original link didn't mention CMSIS (my assumption that it was missing was incorrect) but I am more familiar with RTX / FreeRTOS / SafeRTOS, which are CMSIS compliant.

Please do not misunderstand me that I am criticizing what you are tying to do, I fully support your idea :100: and the time you are spending on this. I am just trying to give some input from someone who spend the last 20 years working on embedded devices.

4 Likes

Never thought to meet any embedded engineer here :handshake:

I really appreciate your advice. And you are right, our plan is to use this high-end chip to find out the limitation of Swift in this area. If everything goes well, we can do some cost-down work then. Due to the chip shortage, seems there is still a long way to go :joy:

3 Likes

Good to hear. I can’t wait to program my keyboard using swift. https://zmk.dev/

2 Likes

Very interesting! I know that some RTOS provide posix compatibility layers, like freeRTOS. Would make targeting generic unix pretty easy.

1 Like

Zephyr is very promising. Of course, they won't miss this part :joy:. But it is still tough to combine all these stuff together.

Something I have found really interesting is the way SwiftWASM has approach their project. If you look closely at their additions to the codebases you can see how similar it is to bare-metal-esque targets. I have been reading into it even more and am slowly working towards creating something similar for thumbv7em targets, with the hope that it can be kept in sync with upstream until it can be upstreamed.

edit: assuming we use the thumbv7em target that is already in LLVM.

3 Likes

I'm working on the Swift 5.6 source recently (Not finished yet). This commit is all the modification you need to add thumbv7em triple.

2 Likes

Yeah, I followed this patch: Driver: add support for baremetal targets by compnerd · Pull Request #35970 · apple/swift · GitHub which does something similar except doesn't add the os(MadMachine) but instead just os(none) switch.

edit: at least it should do that. I have trouble with this sometimes lol.

I'm a huge proponent of being able to use Swift in embedded projects, so this is great work! But I do think we need to be able to target Cortex M0 and M4 MCUs, as well.

5 Likes

This is awesome! Would love to be able to use swift develop firmware:

possibly/maybe related to the C++ Interop work group?

2 Likes

Thank you for this great post, @andyliu! I've sent out a call for interest for a video call to kickoff more community discussion in these areas:

9 Likes

I have already sent the survey. Thank you for arranging such a discussion :grin:

1 Like

Sort of off topic - but I believe that M4 and M7 use the same instruction set so it would probably work?

Yeah, M4 and M7 mostly share the same instruction set. The only critical problems are still the runtime binding and std size.

I can add extra Flash and RAM to the M7 MCU easily, but this is hard to do with most M4 based MCU :face_exhaling:

2 Likes

Did anyone work on getting Swift usable on RISC-V yet? In particular the ESP-C3 looks like an interesting target.

5 Likes

I've build Swift for RISCV64 and Buildroot. The caveat is that I only was able to compile targeting Soft Float.

3 Likes

In the previous release, I added flags to the Swift frontend, but I found that there were standard flags later.

Now I use -Xcc -mhard-float, -Xcc -mfloat-abi=hard flags to tell the compiler to generate hard float instruction and ABI. I don't know if this would work in RISCV64.

2 Likes

I did try that, it was an issue of Clang assuming soft float for Swift when specifying the triple. Wasn't a dealbreaker for me.

3 Likes

This got me going on a chill Saturday morning. So good

I'm still going to learn C on a fundamental level before trying anything embedded with Swift. Would you agree that it is vital to know where and why Swift came from?

1 Like

I'd like to encourage anyone interested in embedded development to check out Embedded Swift, where a vision for a possible first class support for embedded programming is presented.

7 Likes