# Embedded Swift Example Projects for ARM and RISC-V Microcontrollers

**URL:** <https://forums.swift.org/t/embedded-swift-example-projects-for-arm-and-risc-v-microcontrollers/71066>\
**Category:** Community Showcase\
**Tags:** embedded\
**Created:** [April 3, 2024, 5:27pm UTC](https://forums.swift.org/t/embedded-swift-example-projects-for-arm-and-risc-v-microcontrollers/71066 "2024-04-03T17:27:19Z")\
**Posts on this page:** 17\
**Page:** 2

<div class="post-metadata">

**Author:** ![carlynorama](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/carlynorama/32/27072_2.png) [@carlynorama](https://forums.swift.org/u/carlynorama)\
**Post date:** [April 7, 2024, 12:58pm UTC](https://forums.swift.org/t/embedded-swift-example-projects-for-arm-and-risc-v-microcontrollers/71066/21 "2024-04-07T12:58:23Z")

</div>

That was a great link! I've added to my "good links to recommend" cache.

A learning embedded for Swift Developers and a learning Swift for embedded developers wiki/repo/shared annotated bibliography would be awesome. I am working on my own greatest hits page, but then, by definition, that only has resources that _I_ know about.

For example anyone have a "Biggest gotchas in switching to Clang/CMake for people who know GCC/Make" talk they like? I'm using them because of Swift actually, but I have a lot to learn.

For those who like books the second edtion of the below is hot of the press

> **[Making Embedded Systems, 2nd Edition](https://www.oreilly.com/library/view/making-embedded-systems/9781098151539/)**
>
> Interested in developing embedded systems? Since they don't tolerate inefficiency, these systems require a disciplined approach to programming. This easy-to-read guide helps you cultivate good development practices based on classic … - Selection...

The author is one of the hosts of [https://embedded.fm](https://embedded.fm) which interviews a lot of people in the biz. I haven't read it but they do have a 2016 series based on the ti MSP430 that on initial review looks pretty comprehensive and well beyond the teach a 6th grader to blink an LED level without requiring calculous - [Embedded Software Engineering 101 — Embedded](https://embedded.fm/blog/ese101)

---

<div class="post-metadata">

**Author:** ![orobio](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/orobio/32/5512_2.png) [@orobio](https://forums.swift.org/u/orobio)\
**Post date:** [April 7, 2024, 6:56pm UTC](https://forums.swift.org/t/embedded-swift-example-projects-for-arm-and-risc-v-microcontrollers/71066/22 "2024-04-07T18:56:23Z")

</div>

In case anyone is interested, I transformed the stm32-blink example to compile on Linux. It works with the nightly-jammy Docker image, and the only additional required package is binutils-arm-none-eabi.

> **[Comparing main...linux · orobio/swift-embedded-examples](https://github.com/orobio/swift-embedded-examples/compare/main...linux)**
>
> A collection of example projects using Embedded Swift - Comparing main...linux · orobio/swift-embedded-examples

I'm still getting familiar with this stuff, so it may not be perfect, but my LED is blinking!

---

<div class="post-metadata">

**Author:** ![kubamracek](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/kubamracek/32/1171_2.png) [@kubamracek](https://forums.swift.org/u/kubamracek)\
**Post date:** [April 7, 2024, 8:09pm UTC](https://forums.swift.org/t/embedded-swift-example-projects-for-arm-and-risc-v-microcontrollers/71066/23 "2024-04-07T20:09:45Z")

</div>

Excellent! I love how the changes are really minimal, and looking at them, most are pretty self-explanatory (e.g. you needed to add a linker script to produce a correctly laid out ELF file instead of a Mach-O file). I think we should be able to eliminate the need for these stubs:

```swift
void posix_memalign(void) {
    while (1) {}
}

void __stack_chk_fail(void) {
    while (1) {}
}

void __stack_chk_guard(void) {
    while (1) {}
}

void free(void *ptr) {
    while (1) {}
}

```

(1) If you disable stack protectors (`-disable-stack-protector` swiftc flag), I'd expect ` __stack_chk_fail` and `__ stack_chk_guard` to not be needed anymore. See [https://github.com/apple/swift/blob/main/docs/EmbeddedSwift/UserManual.md#external-dependencies](https://github.com/apple/swift/blob/main/docs/EmbeddedSwift/UserManual.md#external-dependencies). But of course, stack protectors are generally useful.  
(2) I think that asking the compiler to place functions into separate sections (`-Xfrontend -function-sections`, the equivalent of Clang's/GCC's `-ffunction-sections`) should enable dead-code stripping, and then the `posix_memalign` and `free` dependencies will go away because you don't use any allocations in the example. I am actually trying to pursue enabling this behavior by default on ELF, see [https://github.com/apple/swift/pull/70788](https://github.com/apple/swift/pull/70788).

---

<div class="post-metadata">

**Author:** ![orobio](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/orobio/32/5512_2.png) [@orobio](https://forums.swift.org/u/orobio)\
**Post date:** [April 8, 2024, 6:58pm UTC](https://forums.swift.org/t/embedded-swift-example-projects-for-arm-and-risc-v-microcontrollers/71066/24 "2024-04-08T18:58:19Z")

</div>

> [@kubamracek](#):
>
> I think that asking the compiler to place functions into separate sections (`-Xfrontend -function-sections`, the equivalent of Clang's/GCC's `-ffunction-sections`) should enable dead-code stripping, and then the `posix_memalign` and `free` dependencies will go away because you don't use any allocations in the example.

Thanks! I was indeed wondering how we could get rid of these stubs. As it turns out, using `-function-sections` is enough to get rid of all of them in this case. I pushed an update for that.

One strange thing I found is that I had to 'clean' the build (removing blink.o is enough). Otherwise, for some reason it doesn't have any effect when enabling (or disabling) `-function-sections`, even though everything is recompiled.

I pushed similar changes for the stm32-lcd-logo example, so that one can be compiled on Linux as well now.

---

<div class="post-metadata">

**Author:** ![Datagram](https://avatars.discourse-cdn.com/v4/letter/d/7cd45c/32.png) [@Datagram](https://forums.swift.org/u/Datagram)\
**Post date:** [April 25, 2024, 9:16pm UTC](https://forums.swift.org/t/embedded-swift-example-projects-for-arm-and-risc-v-microcontrollers/71066/25 "2024-04-25T21:16:28Z")

</div>

Experimenting with stm32-uart-echo with Swift 6 toolchain

No luck:

% export TOOLCHAINS='Swift Development Snapshot'  
admin@mbam1 stm32-uart-echo % make  
fatal: not a git repository (or any of the parent directories): .git  
compiling...  
/Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2024-04-22-a.xctoolchain/usr/bin/swift-build   
--configuration release   
--verbose   
--triple armv7em-apple-none-macho   
-Xcc -D\_\_APPLE\_\_ -Xcc -D\_\_MACH\_\_   
-Xswiftc -Xfrontend -Xswiftc -disable-stack-protector  
Planning build  
Building for production...

... here starting for long long time when compiling SwiftSyntax stuff...  
... then stop here:

....  
-o /Users/admin/Documents/atwork/swift-embedded-examples-main/stm32-uart-echo/.build/armv7em-apple-none-macho/release/Application.build/USART1.swift.o  
make: \*\*\* [build] Error 1

Any idea?  
Must we use a specific toolchain to experiment with Swift embedded?

---

<div class="post-metadata">

**Author:** ![kubamracek](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/kubamracek/32/1171_2.png) [@kubamracek](https://forums.swift.org/u/kubamracek)\
**Post date:** [May 8, 2024, 8:24pm UTC](https://forums.swift.org/t/embedded-swift-example-projects-for-arm-and-risc-v-microcontrollers/71066/26 "2024-05-08T20:24:12Z")

</div>

Can you post the entire log? I think the error message is cut off.

---

<div class="post-metadata">

**Author:** ![kubamracek](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/kubamracek/32/1171_2.png) [@kubamracek](https://forums.swift.org/u/kubamracek)\
**Post date:** [May 8, 2024, 10:55pm UTC](https://forums.swift.org/t/embedded-swift-example-projects-for-arm-and-risc-v-microcontrollers/71066/27 "2024-05-08T22:55:16Z")

</div>

Actually, I was able to reproduce the failure, and I think I've been able to fix it with: [Fix calling convention on putchar (silgen\_name -\> cdecl) by kubamracek · Pull Request #10 · apple/swift-embedded-examples · GitHub](https://github.com/apple/swift-embedded-examples/pull/10)

Can you try it now?

---

<div class="post-metadata">

**Author:** ![Datagram](https://avatars.discourse-cdn.com/v4/letter/d/7cd45c/32.png) [@Datagram](https://forums.swift.org/u/Datagram)\
**Post date:** [May 15, 2024, 7:01pm UTC](https://forums.swift.org/t/embedded-swift-example-projects-for-arm-and-risc-v-microcontrollers/71066/28 "2024-05-15T19:01:05Z")

</div>

Success with your patch and latest toolchain.  
Thanks !

---

<div class="post-metadata">

**Author:** ![dom](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/dom/32/35891_2.png) [@dom](https://forums.swift.org/u/dom)\
**Post date:** [June 13, 2024, 9:36pm UTC](https://forums.swift.org/t/embedded-swift-example-projects-for-arm-and-risc-v-microcontrollers/71066/29 "2024-06-13T21:36:44Z")

</div>

Do you happen to know if Clang support for Xtensa is in progress? Any active issues/PRs that I can follow along with?

---

<div class="post-metadata">

**Author:** ![kubamracek](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/kubamracek/32/1171_2.png) [@kubamracek](https://forums.swift.org/u/kubamracek)\
**Post date:** [June 13, 2024, 11:53pm UTC](https://forums.swift.org/t/embedded-swift-example-projects-for-arm-and-risc-v-microcontrollers/71066/30 "2024-06-13T23:53:15Z")

</div>

See discussion in this thread: [Is there support for Xtensa LX7 cores?](https://forums.swift.org/t/is-there-support-for-xtensa-lx7-cores/72458)

---

<div class="post-metadata">

**Author:** ![willswire](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/willswire/32/36018_2.png) [@willswire](https://forums.swift.org/u/willswire)\
**Post date:** [June 19, 2024, 2:32pm UTC](https://forums.swift.org/t/embedded-swift-example-projects-for-arm-and-risc-v-microcontrollers/71066/31 "2024-06-19T14:32:32Z")

</div>

I'm trying to get some basic network functionality working, and running into trouble. I've built the `wifi` modules in `pico-examples` for `pico_w` to verify my dev setup, but when pulling over some of that code into Swift, I'm having trouble.

I suspect the issue is with the `CMakeLists.txt`file, although I'm not sure.

I've [modified the necessary target link library](https://github.com/willswire/pico-swift/blob/4b90061062212a8633df1c9e7a8bb296af62682a/CMakeLists.txt#L58)entry to properly set `CYW43_LWIP`, but now I'm getting other errors:

```swift
In file included from /Users/willwalker/Developer/pico-swift/pico-sdk/lib/lwip/src/core/mem.c:59:
/Users/willwalker/Developer/pico-swift/pico-sdk/lib/lwip/src/include/lwip/sys.h:95:10: fatal error: arch/sys_arch.h: No such file or directory
   95 | #include "arch/sys_arch.h"
      | ^ ~~~~~~~~~~~~~~~~
compilation terminated.
[84/180] Building C object CMakeFiles/swift-pico.dir/pico-sdk/lib/tinyusb/src/common/tusb_fifo.c.obj
ninja: build stopped: subcommand failed.

```

Here is my full project: [GitHub - willswire/pico-swift at implement-networking](https://github.com/willswire/pico-swift/tree/implement-networking)

---

<div class="post-metadata">

**Author:** ![kubamracek](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/kubamracek/32/1171_2.png) [@kubamracek](https://forums.swift.org/u/kubamracek)\
**Post date:** [June 19, 2024, 3:51pm UTC](https://forums.swift.org/t/embedded-swift-example-projects-for-arm-and-risc-v-microcontrollers/71066/32 "2024-06-19T15:51:59Z")

</div>

Your lwipopts.h file is empty, if you want to actually use lwip, you have to use that file to "configure it". See e.g. this example config from the official Pico Examples repo: [pico-examples/pico\_w/wifi/lwipopts\_examples\_common.h at master · raspberrypi/pico-examples · GitHub](https://github.com/raspberrypi/pico-examples/blob/master/pico_w/wifi/lwipopts_examples_common.h)

---

<div class="post-metadata">

**Author:** ![willswire](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/willswire/32/36018_2.png) [@willswire](https://forums.swift.org/u/willswire)\
**Post date:** [June 19, 2024, 5:16pm UTC](https://forums.swift.org/t/embedded-swift-example-projects-for-arm-and-risc-v-microcontrollers/71066/33 "2024-06-19T17:16:44Z")

</div>

Oh duh!! That was it. Thanks so much!! 😃

For posterity's sake, I just brought over all of those definitions and interpolated them into my lwipopts.h file and it's working great now.

---

<div class="post-metadata">

**Author:** ![paulober](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/paulober/32/33965_2.png) [@paulober](https://forums.swift.org/u/paulober)\
**Post date:** [December 3, 2024, 11:03am UTC](https://forums.swift.org/t/embedded-swift-example-projects-for-arm-and-risc-v-microcontrollers/71066/34 "2024-12-03T11:03:45Z")

</div>

I just tried using a #define in Swift by creating a function in the bridging header that returns me that value. (It's a pointer to a c struct)  
But if I try to use it I get following error:

```console
lib/gcc/arm-none-eabi/13.3.1/../../../../arm-none-eabi/bin/ld: _swiftcode.o: in function `$ss17swift_allocObject8metadata12requiredSize0E13AlignmentMaskSpys04HeapC0VGSpys13ClassMetadataVG_S2itF':
_swiftcode.o:(.text.$ss17swift_allocObject8metadata12requiredSize0E13AlignmentMaskSpys04HeapC0VGSpys13ClassMetadataVG_S2itF+0x4e): undefined reference to `posix_memalign'
collect2: error: ld returned 1 exit status
ninja: build stopped: subcommand failed.

```

For me it seems like swift tries to use `posix_memalign` form the c-stdlib available on desktop but not in the ARM embedded toolchain. Does this make sense or is it an issue on my side?

---

<div class="post-metadata">

**Author:** ![kubamracek](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/kubamracek/32/1171_2.png) [@kubamracek](https://forums.swift.org/u/kubamracek)\
**Post date:** [December 3, 2024, 6:01pm UTC](https://forums.swift.org/t/embedded-swift-example-projects-for-arm-and-risc-v-microcontrollers/71066/35 "2024-12-03T18:01:05Z")

</div>

> [@paulober](#):
>
> For me it seems like swift tries to use `posix_memalign` form the c-stdlib available on desktop but not in the ARM embedded toolchain. Does this make sense or is it an issue on my side?

Depending on the libc you're using, posix\_memalign might not be available. You might need to provide it yourself -- if you have aligned\_alloc available instead then it's pretty simple: [swift-embedded-examples/nrfx-blink-sdk/Stubs.c at fc1942bc094947bb2c73fe194d3fc1d207cb418d · apple/swift-embedded-examples · GitHub](https://github.com/apple/swift-embedded-examples/blob/fc1942bc094947bb2c73fe194d3fc1d207cb418d/nrfx-blink-sdk/Stubs.c#L21)

---

<div class="post-metadata">

**Author:** ![paulober](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/paulober/32/33965_2.png) [@paulober](https://forums.swift.org/u/paulober)\
**Post date:** [December 4, 2024, 3:28pm UTC](https://forums.swift.org/t/embedded-swift-example-projects-for-arm-and-risc-v-microcontrollers/71066/36 "2024-12-04T15:28:24Z")

</div>

Thanks, by providing my own posix\_memalign implementation I was able to solve it.  
I also had an issue with the unicode stuff but found the library mentioned in the swift github. The only problem I have with this approach is that it seems to be a runtime requirement. So I had to manually include the whole lib because the linker would otherwise just exclude it.

```cmake
target_link_options(SwiftDMA PRIVATE
    -Wl,--whole-archive ${SWIFT_EMBEDDED_UNICODE_LIB} -Wl,--no-whole-archive
)

```

So i was wondering if there is recommended approach from the Swift community or is this the only option I have?

---

<div class="post-metadata">

**Author:** ![kubamracek](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/kubamracek/32/1171_2.png) [@kubamracek](https://forums.swift.org/u/kubamracek)\
**Post date:** [December 4, 2024, 11:35pm UTC](https://forums.swift.org/t/embedded-swift-example-projects-for-arm-and-risc-v-microcontrollers/71066/37 "2024-12-04T23:35:23Z")

</div>

> [@paulober](#):
>
> I also had an issue with the unicode stuff but found the library mentioned in the swift github. The only problem I have with this approach is that it seems to be a runtime requirement. So I had to manually include the whole lib because the linker would otherwise just exclude it.

That's surprising to me. Are you saying that just adding the library via -L/-l doesn't work? Can you post the resulting linker error and the linker command?

[Previous page](https://forums.swift.org/t/embedded-swift-example-projects-for-arm-and-risc-v-microcontrollers/71066.md?page=1)
