はじめに

Raspberry Pi 4 Model BでROSを触りたくなり、

こちらの記事でUbuntu Server 20.04.2 LTS 64bit版をインストールしました。

今度は、Ubuntu Server 20.04でLチカなどもやってみたくなったので、

GPIO(general purpose input/output)を制御するWiring Piを使えるようにする方法をまとめました。

前提として、最初からWiring Piは入っているのですが、

最初から入っているWiring Piのバージョンだと、

Raspberry Pi 4 Model Bに対応していないそうなので、Wiring Piのバージョンアップを行います。

 

環境

・Raspberry Pi 4 Model B 4GB

・Ubuntu Server 20.04.2 LTS 64bit版

手順

1. Wiring Piのバージョンを確認

前述の通り、Wiring Piは入っているはずですので、それを確認します。

Wiring Piが入っていると、gpioコマンドが打てるため、下記のコマンドを実行して確認します。

$ gpio -v

下記のようなレスポンスがあれば、入っていることが確認できます。

1行目に記載の通り、version 2.50のようです。

また、入ってはいますが、”gpio readall”といったコマンドを打っても、正しく動かない状態だと思います。

gpio version: 2.50
Copyright (c) 2012-2018 Gordon Henderson
This is free software with ABSOLUTELY NO WARRANTY.
For details type: gpio -warranty

Raspberry Pi Details:
  Type: Unknown17, Revision: 02, Memory: 0MB, Maker: Sony
  * Device tree is enabled.
  *--> Raspberry Pi 4 Model B Rev 1.2
  * This Raspberry Pi supports user-level GPIO access.

2. Wiring Piのバージョンアップ

バージョンは2.50が入っていましたが、

Raspberry Pi 4 Model Bに対応するためには、バージョンを2.52に上げる必要があります。

手順は、Wiring Piのサイトで紹介されているので、それを元に進めて行きます。

$ cd /tmp
$ wget https://project-downloads.drogon.net/wiringpi-latest.deb
$ sudo dpkg -i wiringpi-latest.deb

上記の3行目のコマンド実行後、

下記のように、アーキテクチャがマッチしていないという内容のエラーが返ってきます。

dpkg: error processing archive wiringpi-latest.deb (--install):
 package architecture (armhf) does not match system (arm64)
Errors were encountered while processing:
 wiringpi-latest.deb

対応としては、インストールに利用しているdpkgにはマルチアーキテクチャサポートという機能があるため、

下記のコマンドを実行し、armhfを外来アーキテクチャとして定義します。

確認用コマンドでarmhfが返ってくるようになっていれば、定義されています。

$ sudo dpkg --add-architecture armhf

# 確認用コマンド
sudo dpkg --print-foreign-architectures

再度、先ほどのコマンドを実行すると、また、異なるエラーが返ってきます。

今度は、wiringpiが依存するlibc6が無いことが原因のようでした。

$  sudo dpkg -i wiringpi-latest.deb
(Reading database ... 250343 files and directories currently installed.)
Preparing to unpack wiringpi-latest.deb ...
Unpacking wiringpi:armhf (2.52) over (2.50-0ubuntu1) ...
dpkg: dependency problems prevent configuration of wiringpi:armhf:
 wiringpi:armhf depends on libc6.

dpkg: error processing package wiringpi:armhf (--install):
 dependency problems - leaving unconfigured
Processing triggers for man-db (2.9.1-1) ...
Errors were encountered while processing:
 wiringpi:armhf

armhfアーキテクチャのlibc6パッケージをインストールするために、下記のコマンドを実行します。

今度は、パッケージが見つからないというエラーができました。

$ sudo apt-get install libc6:armhf
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
Package libc6:armhf is not available, but is referred to by another package.
This may mean that the package is missing, has been obsoleted, or
is only available from another source
However the following packages replace it:
  libdb1-compat libnss-nis libnss-nisplus libcrypt1

E: Package 'libc6:armhf' has no installation candidate

パッケージを更新するために、updateコマンドを実行しました。

armhfアーキテクチャについても、パッケージが更新されているように見えました。

$ sudo apt update
Hit:1 http://ports.ubuntu.com/ubuntu-ports impish InRelease
Get:2 http://ports.ubuntu.com/ubuntu-ports impish-updates InRelease [110 kB]
Get:3 http://ports.ubuntu.com/ubuntu-ports impish-backports InRelease [101 kB]
Get:4 http://ports.ubuntu.com/ubuntu-ports impish-security InRelease [110 kB]
Get:5 http://ports.ubuntu.com/ubuntu-ports impish/main armhf Packages [1,347 kB]
Get:6 http://ports.ubuntu.com/ubuntu-ports impish/restricted armhf Packages [9,728 B]
Get:7 http://ports.ubuntu.com/ubuntu-ports impish/universe armhf Packages [12.6 MB]
Get:8 http://ports.ubuntu.com/ubuntu-ports impish/multiverse armhf Packages [161 kB]
Get:9 http://ports.ubuntu.com/ubuntu-ports impish-updates/main armhf Packages [153 kB]
Get:10 http://ports.ubuntu.com/ubuntu-ports impish-updates/main arm64 Packages [162 kB]
Get:11 http://ports.ubuntu.com/ubuntu-ports impish-updates/main Translation-en [45.9 kB]
Get:12 http://ports.ubuntu.com/ubuntu-ports impish-updates/main arm64 DEP-11 Metadata [18.8 kB]
Get:13 http://ports.ubuntu.com/ubuntu-ports impish-updates/main arm64 c-n-f Metadata [2,820 B]
Get:14 http://ports.ubuntu.com/ubuntu-ports impish-updates/restricted armhf Packages [2,248 B]
Get:15 http://ports.ubuntu.com/ubuntu-ports impish-updates/universe armhf Packages [49.6 kB]
Get:16 http://ports.ubuntu.com/ubuntu-ports impish-updates/universe arm64 Packages [49.6 kB]
Get:17 http://ports.ubuntu.com/ubuntu-ports impish-updates/universe Translation-en [22.2 kB]
Get:18 http://ports.ubuntu.com/ubuntu-ports impish-updates/universe arm64 DEP-11 Metadata [4,480 B]
Get:19 http://ports.ubuntu.com/ubuntu-ports impish-updates/universe arm64 c-n-f Metadata [2,032 B]
Get:20 http://ports.ubuntu.com/ubuntu-ports impish-backports/universe armhf Packages [3,428 B]
Get:21 http://ports.ubuntu.com/ubuntu-ports impish-backports/universe arm64 DEP-11 Metadata [9,284 B]
Get:22 http://ports.ubuntu.com/ubuntu-ports impish-security/main arm64 Packages [96.3 kB]
Get:23 http://ports.ubuntu.com/ubuntu-ports impish-security/main armhf Packages [87.5 kB]
Get:24 http://ports.ubuntu.com/ubuntu-ports impish-security/main Translation-en [29.0 kB]
Get:25 http://ports.ubuntu.com/ubuntu-ports impish-security/main arm64 DEP-11 Metadata [6,428 B]
Get:26 http://ports.ubuntu.com/ubuntu-ports impish-security/main arm64 c-n-f Metadata [1,560 B]
Get:27 http://ports.ubuntu.com/ubuntu-ports impish-security/restricted armhf Packages [2,248 B]
Get:28 http://ports.ubuntu.com/ubuntu-ports impish-security/universe armhf Packages [22.1 kB]
Get:29 http://ports.ubuntu.com/ubuntu-ports impish-security/universe arm64 Packages [22.1 kB]
Get:30 http://ports.ubuntu.com/ubuntu-ports impish-security/universe Translation-en [11.6 kB]
Get:31 http://ports.ubuntu.com/ubuntu-ports impish-security/universe arm64 DEP-11 Metadata [2,308 B]
Get:32 http://ports.ubuntu.com/ubuntu-ports impish-security/universe arm64 c-n-f Metadata [1,312 B]
Fetched 15.2 MB in 10s (1,535 kB/s)
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
3 packages can be upgraded. Run 'apt list --upgradable' to see them.

再度、libc6をインストールするコマンドを実行したところ、

また、異なるエラーがでました。

$ sudo apt-get install libc6:armhf
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
You might want to run 'apt --fix-broken install' to correct these.
The following packages have unmet dependencies:
 libc6:armhf : Depends: libgcc-s1:armhf but it is not going to be installed
               Depends: libcrypt1:armhf (>= 1:4.4.10-10ubuntu4) but it is not going to be installed
               Recommends: libidn2-0:armhf (>= 2.0.5~) but it is not going to be installed
               Recommends: libnss-nis:armhf but it is not going to be installed
               Recommends: libnss-nisplus:armhf but it is not going to be installed
E: Unmet dependencies. Try 'apt --fix-broken install' with no packages (or specify a solution).

このタイミングで、sudo apt updateをしてみましたが、解決しなかったため、

エラー文にあるとおりに、下記のコマンドを実行しました。

ログを見ていると、上記のエラーで記載のあったlibgcc-s1なども確認できるようになりました。

$ sudo apt --fix-broken install
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
Correcting dependencies... Done
The following additional packages will be installed:
  gcc-11-base:armhf krb5-locales libc6:armhf libcom-err2:armhf libcrypt1:armhf libgcc-s1:armhf libgssapi-krb5-2:armhf libidn2-0:armhf libk5crypto3:armhf
  libkeyutils1:armhf libkrb5-3:armhf libkrb5support0:armhf libnsl2:armhf libnss-nis:armhf libnss-nisplus:armhf libssl1.1:armhf libtirpc3:armhf libunistring2:armhf
Suggested packages:
  glibc-doc:armhf locales:armhf krb5-doc:armhf krb5-user:armhf
The following NEW packages will be installed:
  gcc-11-base:armhf krb5-locales libc6:armhf libcom-err2:armhf libcrypt1:armhf libgcc-s1:armhf libgssapi-krb5-2:armhf libidn2-0:armhf libk5crypto3:armhf
  libkeyutils1:armhf libkrb5-3:armhf libkrb5support0:armhf libnsl2:armhf libnss-nis:armhf libnss-nisplus:armhf libssl1.1:armhf libtirpc3:armhf libunistring2:armhf
0 upgraded, 18 newly installed, 0 to remove and 3 not upgraded.
1 not fully installed or removed.
Need to get 5,125 kB of archives.
After this operation, 15.4 MB of additional disk space will be used.
Do you want to continue? [Y/n] Y
Get:1 http://ports.ubuntu.com/ubuntu-ports impish/main armhf gcc-11-base armhf 11.2.0-7ubuntu2 [20.6 kB]
Get:2 http://ports.ubuntu.com/ubuntu-ports impish/main armhf libgcc-s1 armhf 11.2.0-7ubuntu2 [42.6 kB]
Get:3 http://ports.ubuntu.com/ubuntu-ports impish/main armhf libcrypt1 armhf 1:4.4.18-4ubuntu1 [86.6 kB]
Get:4 http://ports.ubuntu.com/ubuntu-ports impish/main armhf libc6 armhf 2.34-0ubuntu3 [2,500 kB]
Get:5 http://ports.ubuntu.com/ubuntu-ports impish/main armhf libcom-err2 armhf 1.46.3-1ubuntu3 [9,852 B]
Get:6 http://ports.ubuntu.com/ubuntu-ports impish/main armhf libkrb5support0 armhf 1.18.3-6 [30.2 kB]
Get:7 http://ports.ubuntu.com/ubuntu-ports impish/main armhf libk5crypto3 armhf 1.18.3-6 [83.9 kB]
Get:8 http://ports.ubuntu.com/ubuntu-ports impish/main armhf libkeyutils1 armhf 1.6.1-2ubuntu1 [9,028 B]
Get:9 http://ports.ubuntu.com/ubuntu-ports impish/main armhf libssl1.1 armhf 1.1.1l-1ubuntu1 [1,200 kB]
Get:10 http://ports.ubuntu.com/ubuntu-ports impish/main armhf libkrb5-3 armhf 1.18.3-6 [330 kB]
Get:11 http://ports.ubuntu.com/ubuntu-ports impish/main armhf libgssapi-krb5-2 armhf 1.18.3-6 [120 kB]
Get:12 http://ports.ubuntu.com/ubuntu-ports impish/main armhf libtirpc3 armhf 1.3.2-2 [72.3 kB]
Get:13 http://ports.ubuntu.com/ubuntu-ports impish/main armhf libnsl2 armhf 1.3.0-2build1 [36.5 kB]
Get:14 http://ports.ubuntu.com/ubuntu-ports impish/main armhf libunistring2 armhf 0.9.10-6 [478 kB]
Get:15 http://ports.ubuntu.com/ubuntu-ports impish/main armhf libidn2-0 armhf 2.3.1-1 [50.3 kB]
Get:16 http://ports.ubuntu.com/ubuntu-ports impish/main arm64 krb5-locales all 1.18.3-6 [11.6 kB]
Get:17 http://ports.ubuntu.com/ubuntu-ports impish/main armhf libnss-nis armhf 3.1-0ubuntu5 [23.4 kB]
Get:18 http://ports.ubuntu.com/ubuntu-ports impish/main armhf libnss-nisplus armhf 1.3-0ubuntu5 [20.1 kB]
Fetched 5,125 kB in 3s (1,616 kB/s)
Preconfiguring packages ...
Selecting previously unselected package gcc-11-base:armhf.
(Reading database ... 171940 files and directories currently installed.)
Preparing to unpack .../00-gcc-11-base_11.2.0-7ubuntu2_armhf.deb ...
Unpacking gcc-11-base:armhf (11.2.0-7ubuntu2) ...
Selecting previously unselected package libgcc-s1:armhf.
Preparing to unpack .../01-libgcc-s1_11.2.0-7ubuntu2_armhf.deb ...
Unpacking libgcc-s1:armhf (11.2.0-7ubuntu2) ...
Selecting previously unselected package libcrypt1:armhf.
Preparing to unpack .../02-libcrypt1_1%3a4.4.18-4ubuntu1_armhf.deb ...
Unpacking libcrypt1:armhf (1:4.4.18-4ubuntu1) ...
Selecting previously unselected package libc6:armhf.
Preparing to unpack .../03-libc6_2.34-0ubuntu3_armhf.deb ...
Unpacking libc6:armhf (2.34-0ubuntu3) ...
Selecting previously unselected package libcom-err2:armhf.
Preparing to unpack .../04-libcom-err2_1.46.3-1ubuntu3_armhf.deb ...
Unpacking libcom-err2:armhf (1.46.3-1ubuntu3) ...
Selecting previously unselected package libkrb5support0:armhf.
Preparing to unpack .../05-libkrb5support0_1.18.3-6_armhf.deb ...
Unpacking libkrb5support0:armhf (1.18.3-6) ...
Selecting previously unselected package libk5crypto3:armhf.
Preparing to unpack .../06-libk5crypto3_1.18.3-6_armhf.deb ...
Unpacking libk5crypto3:armhf (1.18.3-6) ...
Selecting previously unselected package libkeyutils1:armhf.
Preparing to unpack .../07-libkeyutils1_1.6.1-2ubuntu1_armhf.deb ...
Unpacking libkeyutils1:armhf (1.6.1-2ubuntu1) ...
Selecting previously unselected package libssl1.1:armhf.
Preparing to unpack .../08-libssl1.1_1.1.1l-1ubuntu1_armhf.deb ...
Unpacking libssl1.1:armhf (1.1.1l-1ubuntu1) ...
Selecting previously unselected package libkrb5-3:armhf.
Preparing to unpack .../09-libkrb5-3_1.18.3-6_armhf.deb ...
Unpacking libkrb5-3:armhf (1.18.3-6) ...
Selecting previously unselected package libgssapi-krb5-2:armhf.
Preparing to unpack .../10-libgssapi-krb5-2_1.18.3-6_armhf.deb ...
Unpacking libgssapi-krb5-2:armhf (1.18.3-6) ...
Selecting previously unselected package libtirpc3:armhf.
Preparing to unpack .../11-libtirpc3_1.3.2-2_armhf.deb ...
Unpacking libtirpc3:armhf (1.3.2-2) ...
Selecting previously unselected package libnsl2:armhf.
Preparing to unpack .../12-libnsl2_1.3.0-2build1_armhf.deb ...
Unpacking libnsl2:armhf (1.3.0-2build1) ...
Selecting previously unselected package libunistring2:armhf.
Preparing to unpack .../13-libunistring2_0.9.10-6_armhf.deb ...
Unpacking libunistring2:armhf (0.9.10-6) ...
Selecting previously unselected package libidn2-0:armhf.
Preparing to unpack .../14-libidn2-0_2.3.1-1_armhf.deb ...
Unpacking libidn2-0:armhf (2.3.1-1) ...
Selecting previously unselected package krb5-locales.
Preparing to unpack .../15-krb5-locales_1.18.3-6_all.deb ...
Unpacking krb5-locales (1.18.3-6) ...
Selecting previously unselected package libnss-nis:armhf.
Preparing to unpack .../16-libnss-nis_3.1-0ubuntu5_armhf.deb ...
Unpacking libnss-nis:armhf (3.1-0ubuntu5) ...
Selecting previously unselected package libnss-nisplus:armhf.
Preparing to unpack .../17-libnss-nisplus_1.3-0ubuntu5_armhf.deb ...
Unpacking libnss-nisplus:armhf (1.3-0ubuntu5) ...
Setting up gcc-11-base:armhf (11.2.0-7ubuntu2) ...
Setting up krb5-locales (1.18.3-6) ...
Setting up libgcc-s1:armhf (11.2.0-7ubuntu2) ...
Setting up libcrypt1:armhf (1:4.4.18-4ubuntu1) ...
Setting up libc6:armhf (2.34-0ubuntu3) ...
Setting up wiringpi:armhf (2.52) ...
Setting up libkeyutils1:armhf (1.6.1-2ubuntu1) ...
Setting up libssl1.1:armhf (1.1.1l-1ubuntu1) ...
Setting up libunistring2:armhf (0.9.10-6) ...
Setting up libidn2-0:armhf (2.3.1-1) ...
Setting up libcom-err2:armhf (1.46.3-1ubuntu3) ...
Setting up libkrb5support0:armhf (1.18.3-6) ...
Setting up libk5crypto3:armhf (1.18.3-6) ...
Setting up libkrb5-3:armhf (1.18.3-6) ...
Setting up libgssapi-krb5-2:armhf (1.18.3-6) ...
Setting up libtirpc3:armhf (1.3.2-2) ...
Setting up libnsl2:armhf (1.3.0-2build1) ...
Setting up libnss-nisplus:armhf (1.3-0ubuntu5) ...
Setting up libnss-nis:armhf (3.1-0ubuntu5) ...
Processing triggers for libc-bin (2.34-0ubuntu3) ...

改めて、libc6:armhfをインストールするコマンドを実行しました。

$ sudo apt-get install libc6:armhf
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
libc6:armhf is already the newest version (2.34-0ubuntu3).
libc6:armhf set to manually installed.
0 upgraded, 0 newly installed, 0 to remove and 3 not upgraded.

libc6:armhfをインストールできたようなので、

wiringpiをインストールするコマンドを再度実行したところ、

正常にインストールできました。

$ sudo dpkg -i wiringpi-latest.deb
(Reading database ... 172274 files and directories currently installed.)
Preparing to unpack wiringpi-latest.deb ...
Unpacking wiringpi:armhf (2.52) over (2.52) ...
Setting up wiringpi:armhf (2.52) ...
Processing triggers for man-db (2.9.4-2) ...
Processing triggers for libc-bin (2.34-0ubuntu3) ...

インストールできていることを確認

まず、バージョンを確認するコマンドを実行しました。

バージョンが2.52になっていることを確認できました。

$ gpio -v
gpio version: 2.52
Copyright (c) 2012-2018 Gordon Henderson
This is free software with ABSOLUTELY NO WARRANTY.
For details type: gpio -warranty

Raspberry Pi Details:
  Type: Pi 4B, Revision: 02, Memory: 4096MB, Maker: Sony
  * Device tree is enabled.
  *--> Raspberry Pi 4 Model B Rev 1.2
  * This Raspberry Pi supports user-level GPIO access.

ピン配置を確認するコマンドを実行してみました。

下記のようなレスポンスがあれば、ちゃんと実行できています。

$ gpio readall
 +-----+-----+---------+------+---+---Pi 4B--+---+------+---------+-----+-----+
 | BCM | wPi |   Name  | Mode | V | Physical | V | Mode | Name    | wPi | BCM |
 +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+
 |     |     |    3.3v |      |   |  1 || 2  |   |      | 5v      |     |     |
 |   2 |   8 |   SDA.1 | ALT0 | 1 |  3 || 4  |   |      | 5v      |     |     |
 |   3 |   9 |   SCL.1 | ALT0 | 1 |  5 || 6  |   |      | 0v      |     |     |
 |   4 |   7 | GPIO. 7 |   IN | 1 |  7 || 8  | 1 | ALT5 | TxD     | 15  | 14  |
 |     |     |      0v |      |   |  9 || 10 | 1 | ALT5 | RxD     | 16  | 15  |
 |  17 |   0 | GPIO. 0 |   IN | 0 | 11 || 12 | 0 | IN   | GPIO. 1 | 1   | 18  |
 |  27 |   2 | GPIO. 2 |   IN | 0 | 13 || 14 |   |      | 0v      |     |     |
 |  22 |   3 | GPIO. 3 |   IN | 0 | 15 || 16 | 0 | IN   | GPIO. 4 | 4   | 23  |
 |     |     |    3.3v |      |   | 17 || 18 | 0 | IN   | GPIO. 5 | 5   | 24  |
 |  10 |  12 |    MOSI | ALT0 | 0 | 19 || 20 |   |      | 0v      |     |     |
 |   9 |  13 |    MISO | ALT0 | 0 | 21 || 22 | 0 | IN   | GPIO. 6 | 6   | 25  |
 |  11 |  14 |    SCLK | ALT0 | 0 | 23 || 24 | 1 | OUT  | CE0     | 10  | 8   |
 |     |     |      0v |      |   | 25 || 26 | 1 | OUT  | CE1     | 11  | 7   |
 |   0 |  30 |   SDA.0 |   IN | 1 | 27 || 28 | 1 | IN   | SCL.0   | 31  | 1   |
 |   5 |  21 | GPIO.21 |   IN | 1 | 29 || 30 |   |      | 0v      |     |     |
 |   6 |  22 | GPIO.22 |   IN | 1 | 31 || 32 | 0 | IN   | GPIO.26 | 26  | 12  |
 |  13 |  23 | GPIO.23 |   IN | 0 | 33 || 34 |   |      | 0v      |     |     |
 |  19 |  24 | GPIO.24 |   IN | 0 | 35 || 36 | 0 | IN   | GPIO.27 | 27  | 16  |
 |  26 |  25 | GPIO.25 |   IN | 0 | 37 || 38 | 0 | IN   | GPIO.28 | 28  | 20  |
 |     |     |      0v |      |   | 39 || 40 | 0 | IN   | GPIO.29 | 29  | 21  |
 +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+
 | BCM | wPi |   Name  | Mode | V | Physical | V | Mode | Name    | wPi | BCM |
 +-----+-----+---------+------+---+---Pi 4B--+---+------+---------+-----+-----+