Swift embedded and pico SDK

In this post we’ll learn how to:

  • Install Swift embedded
  • Install the pico-sdk
  • Run a simple blink

To install Swift embedded we need to download a nightly toolchain from here:

https://www.swift.org/download/#snapshots

We can’t use offciale release because the embedded feature is not ready for production.

After that we can clone the https://github.com/apple/swift-embedded-examples.git that contains the pico-blink-sdk

This example use the c library from the swift code to blink the led.

@main
struct Main {
  static func main() {
    let led = UInt32(PICO_DEFAULT_LED_PIN)
    gpio_init(led)
    gpio_set_dir(led, /*out*/true)
    while true {
      gpio_put(led, true)
      sleep_ms(250)
      gpio_put(led, false)
      sleep_ms(250)
    }
  }
}

Introduced in Swift 5.3, the @main attribute designates a particular type as the entry point for program execution.

To compile (how is written the the github page):

$ cd pico-blink-sdk
$ export TOOLCHAINS='<toolchain-name>'
$ export PICO_BOARD=pico
$ export PICO_SDK_PATH='<path-to-your-pico-sdk>'
$ export PICO_TOOLCHAIN_PATH='<path-to-the-arm-toolchain>'
$ cmake -B build -G Ninja .
$ cmake --build build

for the PICO_TOOLCHAIN_PATH i use:

export TOOLCHAINS=$(plutil -extract CFBundleIdentifier raw /Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2024-06-13-a.xctoolchain/Info.plist)

For the PICO_SDK_PATH you can see the previous post. About the PICO_TOOLCHAIN_PATH you could skip.

After the build, you should have your first swift embedded program: swift.blinky.uf2.

Leave a Comment