0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Swift-ARM on Debian 12 bookworm & Lチカ

Last updated at Posted at 2024-04-17

Swift-ARM を bookworm にインストール

Swift Community Apt Repository
(https://swiftlang.xyz/user-guide)

インストール手順は 上記サイトの写経である。

Swift-ARMサポート対象

  • Debian / Raspberry Pi OS - arm64/amd64

    • bullseye / 11
    • bookworm / 12
       
  • Raspbian / Raspberry Pi OS (legacy)- armv6/7

    • buster

インストール方法

Step 1 - System Update and Install curl

システムのアップデートとアップグレードを実行

sudo apt update && sudo apt upgrade

次に、curlをインストール

sudo apt install curl

Step 2 - Run the quick install script

curl -s https://archive.swiftlang.xyz/install.sh | sudo bash

(aptリポジトリの検証と追加)

Step 3 - Install Swift

sudo apt install swiftlang

Step 4 - Check Swift version

swift --version

動作確認

runtimeエラー(?)がいくつか出ますが、REPLも動きます。

pi5@raspberrypi5:~ $ swift repl
swift runtime: unable to protect path to swift-backtrace at 0x7fff42fd7000: 22; disabling backtracing.
swift runtime: unable to protect environment for swift-backtrace at 0x7fff42fcf000: 22; disabling backtracing.
swift runtime: unable to protect path to swift-backtrace at 0x7ffeee037000: 22; disabling backtracing.
swift runtime: unable to protect environment for swift-backtrace at 0x7ffeee02f000: 22; disabling backtracing.
swift runtime: unable to protect path to swift-backtrace at 0x7fff72cc7000: 22; disabling backtracing.
swift runtime: unable to protect environment for swift-backtrace at 0x7fff72cbf000: 22; disabling backtracing.
error: This version of LLDB does not support DWARF version 5 or later.
swift runtime: unable to protect path to swift-backtrace at 0x7ffff7db7000: 22; disabling backtracing.
swift runtime: unable to protect environment for swift-backtrace at 0x7ffff7daf000: 22; disabling backtracing.
Welcome to Swift version 5.10 (swift-5.10-RELEASE).
Type :help for assistance.
  1> import Foundation
  2> print("Hello Swift!")
Hello Swift!
  3> ^D

pi5@raspberrypi5:~ $

Swift de Lチカ

Raspberry Pi 5 のGPIOを使い SwiftでLチカする。
(Raspberry Pi 4も)

1. プロジェクトフォルダを作成

$ mkdir -p swift-projects/Lchika 

2. プロジェクフォルダを初期化

$ cd swift-projects/Lchika
$ swift package init --name Lchika --type executable
$ tree .
.
├── Package.swift
└── Sources
    └── main.swift

3. Package.swiftを作成

SwiftGPIOを使うためPackage.swiftを更新する。

Package.swift
// swift-tools-version: 5.10
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
    name: "Lchika",
    products: [
        .executable(name: "Lchika", targets: ["Lchika"]),
    ],
    dependencies: [
        // Dependencies declare other packages that this package depends on.
        .package(url: "https://github.com/uraimo/SwiftyGPIO.git", from: "1.0.0")
    ],
    targets: [
        // Targets are the basic building blocks of a package, defining a module or a test suite.
        // Targets can depend on other targets in this package and products from dependencies.
        .executableTarget(
            name: "Lchika",
            dependencies: [
                .product(name: "SwiftyGPIO", package: "SwiftyGPIO"),
            ]),
    ]
)

4. Lチカコード

Sources/main.swift
import Foundation
import SwiftyGPIO

let gpios = SwiftyGPIO.GPIOs(for: .RaspberryPi4)
let ledGpio = gpios[.P17]!
ledGpio.direction = .OUT

while true  {
    ledGpio.value = 1 - ledGpio.value
    Thread.sleep(forTimeInterval: 1.0)
}

5行目で.RaspberryPi5を指定すると、error: type 'SupportedBoard' has no member 'RaspberryPi5'となり、コンパイルできません。
仕方なく、RaspberryPi4を指定した。

5. ビルド

$ swift build

Fetching https://github.com/uraimo/SwiftyGPIO.git
Fetched https://github.com/uraimo/SwiftyGPIO.git from cache (1.71s)
Computing version for https://github.com/uraimo/SwiftyGPIO.git
Computed https://github.com/uraimo/SwiftyGPIO.git at 1.4.3 (3.24s)
Creating working copy for https://github.com/uraimo/SwiftyGPIO.git
Working copy of https://github.com/uraimo/SwiftyGPIO.git resolved at 1.4.3
Building for debugging...
[22/22] Linking Lchika
Build complete! (26.99s)

6. executable(実行形式ファイル)のパスを確認する

$ swift build --show-bin-path

7. 実行する

$ .build/aarch64-unknown-linux-gnu/debug/Lchika

SwiftyGPIO/SwiftyGPIO.swift:344: Fatal error: Can't open /dev/mem , use sudo!

落ちました。メッセージに従い、管理者権限で実行します。

$ sudo .build/aarch64-unknown-linux-gnu/debug/Lchika

しかし、光りません!! (エラーにもならず)
SwiftGPIO が Raspberry Pi 5を正式にサポートしていない からですかね・・・

仕方ないので、まったく同じ手順で Raspberry Pi 4 に環境を作ったところ、(sudo不要で)無事にLチカしました。

IMG_3200-.gif

Raspberry Pi 5 でLチカ

SwiftによるLチカができなかったので、Pythonでやってみた。

import gpiod
import time

PIN_NO = 17
chip = gpiod.Chip('gpiochip4', gpiod.Chip.OPEN_BY_NAME)
led = chip.get_line(PIN_NO)
led.request(consumer="LED", type=gpiod.LINE_REQ_DIR_OUT)

try:
    value = 1
    while True:
        led.set_value(value)
        value = 1 - value
        time.sleep(1)
finally:
    led.release()

↑このコードで問題なくLチカしました。

IMG_3201.gif
(手ブレすみません)

おわりに

Raspberry Pi 5 は、GPIOの制御方法が変更になっているらしい。
SwiftGPIO の Raspberry Pi 5 サポートを待とう。

以上

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?