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: docs/EmbeddedSwift/IntegratingWithSDKs.md
+100-1Lines changed: 100 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -10,4 +10,103 @@ The following document sketches how to integrate Swift code into some popular em
10
10
11
11
## Integrating with Raspberry Pi Pico (W) build system:
12
12
13
-
*TODO, documentation under construction.*
13
+
Development for [Raspberry Pi Pico and Pico W](https://www.raspberrypi.com/products/raspberry-pi-pico/) normally uses the [Pico SDK](https://github.com/raspberrypi/pico-sdk) and the vendor provides several [sample projects in the pico-examples repository](https://github.com/raspberrypi/pico-examples). The SDK and sample project setup is described in:
Before trying to use Swift with the Pico SDK, make sure your environment works and can build the provided C/C++ sample projects.
19
+
20
+
### CMake setup with a bridging header
21
+
22
+
The Pico SDK is using CMake as its build system, and so the simplest way to integrate with it is to also use CMake to build a Swift firmware application on top of the SDK and the libraries from it. The following describes an example set up of that on a "blinky" example (code that just blinks the built-in LED).
23
+
24
+
Let's create a directory with a Swift source file, a bridging header, and a CMake definition file:
25
+
26
+
```
27
+
./SwiftPicoBlinky/Main.swift
28
+
./SwiftPicoBlinky/BridgingHeader.h
29
+
./SwiftPicoBlinky/CMakeLists.txt
30
+
```
31
+
32
+
In `Main.swift`, let's add basic logic to initialize the GPIO port for the Pico's built-in LED, and then turn it on and off in a loop:
33
+
34
+
```swift
35
+
@main
36
+
structMain {
37
+
staticfuncmain() {
38
+
let led =UInt32(PICO_DEFAULT_LED_PIN)
39
+
gpio_init(led)
40
+
gpio_set_dir(led, /*out*/true)
41
+
whiletrue {
42
+
gpio_put(led, true)
43
+
sleep_ms(250)
44
+
gpio_put(led, false)
45
+
sleep_ms(250)
46
+
}
47
+
}
48
+
}
49
+
```
50
+
51
+
Notice that we're using functions and variables defined in C in the Pico SDK. For that to be possible, the Swift compiler needs to have access to the C header files that define these functions and variables. The cleanest option would be to define a modulemap, but for simplicity let's just use a bridging header to make declarations visible in Swift without a module. `BridgingHeader.h` should contain:
52
+
53
+
```c
54
+
#pragma once
55
+
56
+
#include"pico/stdlib.h"
57
+
```
58
+
59
+
Finally, we need to define the application's build rules in CMake that will be using CMake logic from the Pico SDK. The following content of `CMakeLists.txt` shows how to *manually call swiftc, the Swift compiler* instead of using the recently added CMake native support for Swift, so that we can see the full Swift compilation command.
-rw-r--r-- 1 kuba staff 39B Feb 2 22:08 BridgingHeader.h
104
+
-rw-r--r-- 1 kuba staff 1.3K Feb 2 22:08 CMakeLists.txt
105
+
-rw-r--r-- 1 kuba staff 262B Feb 2 22:08 Main.swift
106
+
$ mkdir build
107
+
$ cd build
108
+
$ cmake -S ../ -B . -G Ninja
109
+
$ ninja -v
110
+
```
111
+
112
+
This should produce several build artifacts in the `build/` subdirectory, include `swift-blinky.uf2`, which can be directly uploaded to the Pico by copying it into the fake Mass Storage Volume that the device presents when plugged over USB in BOOTSEL mode.
0 commit comments