You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: Sources/EmbeddedSwift/Documentation.docc/SDKSupport/IntegrateWithZephyr.md
+139-8Lines changed: 139 additions & 8 deletions
Original file line number
Diff line number
Diff line change
@@ -4,15 +4,15 @@
4
4
5
5
For an introduction and motivation into Embedded Swift, please see "[A Vision for Embedded Swift](https://github.com/swiftlang/swift-evolution/blob/main/visions/embedded-swift.md)", a Swift Evolution document highlighting the main goals and approaches.
6
6
7
-
Refer to the [nrfx-blink-sdk](../../../../nrfx-blink-sdk/)project for a complete working "blinky" example that can be used as a starting point. However, for a formal introduction to setting up a Swift Zephyr project and explanations into the various options, continue reading this document.
7
+
The following document outlines how to setup a Swift to Zephyr project for an emulated ARM Cortex M0, explaining a few key concepts along the way. For a complete working example on real hardware, however, refer to the [nrfx-blink-sdk](../../../../nrfx-blink-sdk/) project that is compatible with nRF or other boards.
8
8
9
9
## Zephyr Setup
10
10
11
11
Before setting up a Swift project that works with Zephyr, you need to setup dependencies and a Zephyr workspace as per the [Getting Started Guide](https://docs.zephyrproject.org/latest/develop/getting_started/index.html). Regardless of your platform (macOS or Linux), ensure that you can build the blinky example without errors before starting with Swift integration:
12
12
13
13
```bash
14
14
cd~/zephyrproject/zephyr
15
-
west build -p always -b <your-board-name> samples/basic/blinky
15
+
west build -p always -b reel_board samples/basic/blinky
16
16
```
17
17
18
18
By default, the `main` revision of the Zephyr sources are checked out when calling `west init`, which contains pre-release and development changes that may cause instability and changing APIs that are not desirable. To checkout a specific release version of Zephyr, use the following commands:
@@ -43,12 +43,13 @@ SwiftZephyrProject/prj.conf
43
43
44
44
These are the minimum required files in order to build a Zephyr project. By convention, source files should be placed in the src/ subdirectory, but this is not a hard requirement. Also, `prj.conf` is required even if it is empty, or the project will not build.
45
45
46
-
Inside of `src/BridgingHeader.h`, add the following content as a minimum:
46
+
Inside of `src/BridgingHeader.h`, add the following content:
47
47
48
48
```c
49
49
#pragma once
50
50
51
51
#include<autoconf.h>
52
+
#include<zephyr/kernel.h>
52
53
```
53
54
54
55
The `src/Main.swift` file must contain a `static func main()` as follows:
@@ -57,12 +58,17 @@ The `src/Main.swift` file must contain a `static func main()` as follows:
57
58
@main
58
59
structMain {
59
60
staticfuncmain() {
60
-
// code
61
+
print("Hello Zephyr from Swift!")
62
+
63
+
whiletrue {
64
+
k_msleep(1000)
65
+
print("Loop")
66
+
}
61
67
}
62
68
}
63
69
```
64
70
65
-
Since Embedded Swift requires posix_memalign to be defined, add the following to `src/Stubs.c` to define it:
71
+
Since Embedded Swift requires `posix_memalign` to be defined, add the following to `src/Stubs.c` to define it:
Finally, add the following line to `prj.conf` so that the output of `print()` statements is sent to stdout:
96
+
97
+
```conf
98
+
CONFIG_STDOUT_CONSOLE=y
99
+
```
100
+
89
101
### CMakeLists.txt Setup
90
102
91
103
The `CMakeLists.txt` setup is more involved and complex since target, compilation flags, and library linking must be specified for Swift. First, some initial setup and flags:
The following flags have been curated to work well with the `armv7em-none-none-eabi`target with `swiftc`. Some flags may need to change depending on the chosen target:
116
+
Next, set the compiler target to the arch you are building for. For this example we use `armv6m-none-none-eabi`which is compatible with the [Cortex-M0](https://docs.zephyrproject.org/latest/boards/qemu/cortex_m0/doc/index.html) which we will compile for in a later step. The `mfloat-abi=soft`, `-fshort-enums`, and `-fno-pic`flags are specifically for 32-bit arm architectures, so for other architectures they can be removed. However, the other flags and required for building Swift for Embedded and against Zephyr:
105
117
106
118
```cmake
107
-
# Use the armv7em-none-none-eabi target triple for Swift
[130/135] Linking C executable zephyr/zephyr_pre0.elf
220
+
~/zephyr-sdk-0.17.0/arm-zephyr-eabi/bin/../lib/gcc/arm-zephyr-eabi/12.2.0/../../../../arm-zephyr-eabi/bin/ld.bfd: warning: orphan section `.swift_modhash' from `app/libapp.a(Main.swift.obj)' being placed in section `.swift_modhash'
221
+
[135/135] Linking C executable zephyr/zephyr.elf
222
+
~/zephyr-sdk-0.17.0/arm-zephyr-eabi/bin/../lib/gcc/arm-zephyr-eabi/12.2.0/../../../../arm-zephyr-eabi/bin/ld.bfd: warning: orphan section `.swift_modhash' from `app/libapp.a(Main.swift.obj)' being placed in section `.swift_modhash'
223
+
Memory region Used Size Region Size %age Used
224
+
FLASH: 14674 B 256 KB 5.60%
225
+
RAM: 4032 B 16 KB 24.61%
226
+
IDT_LIST: 0 GB 32 KB 0.00%
227
+
Generating files from ~/SwiftZephyrProject/build/zephyr/zephyr.elf for board: qemu_cortex_m0
228
+
```
229
+
230
+
Finally, to run the example in the qemu emulator, use `ninja run`:
231
+
232
+
```console
233
+
(.venv)> ninja -C build run
234
+
ninja: Entering directory `build'
235
+
[0/1] To exit from QEMU enter: 'CTRL+a, x'[QEMU] CPU: cortex-m0
236
+
*** Booting Zephyr OS build v4.1.0 ***
237
+
Hello Zephyr from Swift!
238
+
Loop
239
+
Loop
240
+
Loop
241
+
```
242
+
243
+
Congrats, you now have a Swift project running using Zephyr!
244
+
245
+
## West Integration
246
+
247
+
Up to now we have setup a project that works perfectly fine when used with just CMake and Ninja. However, projects can also be integrated with West, which is the official CLI tool used for Zephyr projects. To use `west`, start by adding a `west.yml` file to the root of the project:
This could even be set as a global env variable for the user in `~/.bashrc` or `~/.zshrc` if desired.
273
+
274
+
With this, `west` commands now can be run instead of having to use `cmake` and `ninja` to build and run:
275
+
276
+
```bash
277
+
(.venv)> west build -b qemu_cortex_m0 . -p always
278
+
...
279
+
(.venv)> west build -t run
280
+
-- west build: running target run
281
+
[0/1] To exit from QEMU enter: 'CTRL+a, x'[QEMU] CPU: cortex-m0
282
+
*** Booting Zephyr OS build v4.1.0 ***
283
+
Hello Zephyr from Swift!
284
+
Loop
285
+
Loop
286
+
Loop
287
+
Loop
288
+
```
289
+
290
+
This setup may also desirable since `west flash` is also available and can be used instead of invoking the flashing tools manually.
291
+
292
+
If compiling a firmware for a real/physical board such as the `nrf52840dk/nrf52840`, `west flash` will work if the SEGGER J-Link host tools are installed. As an example, with the [nrfx-blink-sdk](../../../../nrfx-blink-sdk/) project:
The `-r jlink` param is needed for this example to use the J-Link tools instead of using `nrfjprog`, which is the default for this board and also [deprecated](https://www.nordicsemi.com/Products/Development-tools/nRF-Command-Line-Tools).
0 commit comments