DragonBoard 410c(Debian)でGPIOを使用する

DragonBoard 410c(Debian)でGPIOを使用する

そろそろDragonBoard(debian)でもGPIOを使用したくなってきましたので、GPIOを使用するための設定を行いたいと思います。

お急ぎの方は下の方にある解答編まで飛ばしてください。

手順

DragonBoard(debian)でGPIOを使用するには2つののライブラリが必要になります。

  • libsoc
  • 96BoardsGPIO

これらを用いることでGPIOがC言語およびpythonでも使用できるようになります。 ※pythonに関しては言語用Bindingがあるので、それを経由してLibraryにアクセスすることになります。

今回はこちらを参考にさせていただいています。

qiita.com

参考にしていたエントリのまんまなので楽勝と思っている自分がいました。

f:id:ueponx:20170528160909j:plain

基本的にはどちらもソースファイルをGithubから取得しbuildしていくというものになります。

buildに必要なパッケージのインストー

96BoardsGPIOをbuildする際にautogen.shを使用するので以下のパッケージを事前にインストールしておきます。

$ sudo apt-get install autoconf automake libtool

事前の準備は終了しました。

libsocをインストールする

libsocのgithubページは以下になります。

github.com

$ git clone https://github.com/jackmitch/libsoc.git
$ cd libsoc
$ autoreconf -i
$ ./configure --enable-board="dragonboard410c"
$ make
$ sudo make install

以上の手順で無事にインストールできました。

96BoardsGPIOをインストールする

github.com

手順を確認すると以下の様になるのですが…

$ git clone https://github.com/96boards/96BoardsGPIO.git
$ cd 96BoardsGPIO
$ ./autogen.sh
$ ./configure
$ make
$ sudo make install
$ sudo ldconfig

makeを実行すると以下のようにErrorが発生してしまいます。

$ make
Making all in lib
make[1]: Entering directory '/home/linaro/96BoardsGPIO/lib'
/bin/bash ../libtool  --tag=CC   --mode=compile gcc -DPACKAGE_NAME=\"96BoardsGPIO\" -DPACKAGE_TARNAME=\"96boardsgpio\" -DPACKAGE_VERSION=\"0.1\" -DPACKAGE_STRING=\"96BoardsGPIO\ 0.1\" -DPACKAGE_BUGREPORT=\"\" -DPACKAGE_URL=\"\" -DPACKAGE=\"96boardsgpio\" -DVERSION=\"0.1\" -DSTDC_HEADERS=1 -DHAVE_SYS_TYPES_H=1 -DHAVE_SYS_STAT_H=1 -DHAVE_STDLIB_H=1 -DHAVE_STRING_H=1 -DHAVE_MEMORY_H=1 -DHAVE_STRINGS_H=1 -DHAVE_INTTYPES_H=1 -DHAVE_STDINT_H=1 -DHAVE_UNISTD_H=1 -DHAVE_DLFCN_H=1 -DLT_OBJDIR=\".libs/\" -I.  -I../lib   -g -O2 -MT lib96BoardsGPIO_la-gpio.lo -MD -MP -MF .deps/lib96BoardsGPIO_la-gpio.Tpo -c -o lib96BoardsGPIO_la-gpio.lo `test -f 'gpio.c' || echo './'`gpio.c
libtool: compile:  gcc -DPACKAGE_NAME=\"96BoardsGPIO\" -DPACKAGE_TARNAME=\"96boardsgpio\" -DPACKAGE_VERSION=\"0.1\" "-DPACKAGE_STRING=\"96BoardsGPIO 0.1\"" -DPACKAGE_BUGREPORT=\"\" -DPACKAGE_URL=\"\" -DPACKAGE=\"96boardsgpio\" -DVERSION=\"0.1\" -DSTDC_HEADERS=1 -DHAVE_SYS_TYPES_H=1 -DHAVE_SYS_STAT_H=1 -DHAVE_STDLIB_H=1 -DHAVE_STRING_H=1 -DHAVE_MEMORY_H=1 -DHAVE_STRINGS_H=1 -DHAVE_INTTYPES_H=1 -DHAVE_STDINT_H=1 -DHAVE_UNISTD_H=1 -DHAVE_DLFCN_H=1 -DLT_OBJDIR=\".libs/\" -I. -I../lib -g -O2 -MT lib96BoardsGPIO_la-gpio.lo -MD -MP -MF .deps/lib96BoardsGPIO_la-gpio.Tpo -c gpio.c  -fPIC -DPIC -o .libs/lib96BoardsGPIO_la-gpio.o
gpio.c: In function ‘gpio_id’:
gpio.c:63:17: warning: implicit declaration of function ‘strdup’ [-Wimplicit-function-declaration]
   char *fixed = strdup(pin_name);
                 ^~~~~~
gpio.c:63:17: warning: incompatible implicit declaration of built-in function   strdup’
gpio.c: In function ‘gpio_open’:
gpio.c:98:41: error: ‘LS_SHARED’ undeclared (first use in this function)
  gpio *g = libsoc_gpio_request(gpio_id, LS_SHARED);
                                         ^~~~~~~~~
gpio.c:98:41: note: each undeclared identifier is reported only once for each function it appears in
gpio.c:101:7: warning: implicit declaration of function ‘strcmp’ [-Wimplicit-function-declaration]
  if (!strcmp(direction, "in"))
       ^~~~~~
Makefile:459: recipe for target 'lib96BoardsGPIO_la-gpio.lo' failed
make[1]: *** [lib96BoardsGPIO_la-gpio.lo] Error 1
make[1]: Leaving directory '/home/linaro/96BoardsGPIO/lib'
Makefile:459: recipe for target 'all-recursive' failed
make: *** [all-recursive] Error 1

warningは目を瞑るとして、

gpio.c:98:41: error: ‘LS_SHARED’ undeclared (first use in this function)
  gpio *g = libsoc_gpio_request(gpio_id, LS_SHARED);
                                         ^~~~~~~~~
gpio.c:98:41: note: each undeclared identifier is reported only once for each function it appears in

エラーメッセージから推測するとLS_SHAREDというシンボルが読み取れないということのようです。LS_SHAREDはlibsocのヘッダーファイルに定義されていたので、ヘッダーファイルのパスが想定とは違っているようです。っていうか、参考にしたサイトはこれでいいということのようなんですが、OSのバージョンということなんでしょうか。

色々試してみたのですが、自分のキャパを超えていたのでどうすればいいかわからず、OSを入れたりconfigureをいじってみたり、オプションを変えてみたりしました。

解答編

OSを何度もいれたりして試したのですが諦めて色々と調べているとlibsocのLibraryのReadmeに以下のような部分を発見。

First, check if your distro packages libsoc. If it does it is easiest to install your distros version of libsoc through it’s provided package manager. Depending on your distro this may be apt-get, yum, pacman, etc. Consult your distro documentation for more info

もしかしてパッケージ化されたLibraryがあるんじゃない?と思って試してみました。

あるし… (ちなみになぜかデフォルトでlibsocのpythonバインドのパッケージが既にインストールされています。)

気を取り直して、まっさらな状態からのインストール手順をまとめます。

$ sudo apt-get install autoconf automake libtool
$ sudo apt-get install libsoc2 libsoc-dev python-libsoc
$ git clone https://github.com/96boards/96BoardsGPIO.git
$ cd 96BoardsGPIO/
$ ./autogen.sh
$ ./configure
$ make

...()...

gpio.Tpo -c gpio.c  -fPIC -DPIC -o .libs/lib96BoardsGPIO_la-gpio.o
gpio.c: In function ‘gpio_id’:
gpio.c:63:17: warning: implicit declaration of function ‘strdup’ [-Wimplicit-function-declaration]
   char *fixed = strdup(pin_name);
                 ^~~~~~
gpio.c:63:17: warning: incompatible implicit declaration of built-in function   strdup’
gpio.c: In function ‘gpio_open’:
gpio.c:101:7: warning: implicit declaration of function ‘strcmp’ [-Wimplicit-function-declaration]
  if (!strcmp(direction, "in"))
       ^~~~~~
...()...

$ sudo make install
$ sudo ldconfig

今回はwaringは発生していますが、Errorは発生せずにインストールできるようになりました。(96BoardsGPIOのインストール手順は変更ありません。)

これでOKです。

サンプルの実行

インストールが無事終了したので実際のサンプルでLチカを行ってみたいと思います。

https://az835927.vo.msecnd.net/sites/iot/Resources/images/PinMappings/DB_Pinout.png

上記の図を参考に

  • PIN1がGND
  • PIN23がGPIO-A

としてLEDを接続します。 96BoardsGPIOのソースにあるexampleディレクトリに移動して、サンプルソースmakeを行い成功したらできた実行ファイルを実行します。

$ cd examples/
$ make
$ sudo ./blink

するとこのようになります。

【動画】

youtu.be

無事にLチカすることができました。 同じフォルダにあるpythonのサンプルでもLチカはできています。

設定は終わったはずなんだけど…

インストール後に再度ログインするとログイン時にErrorが発生しています。

Linux linaro-alip 4.9.27-linaro-lt-qcom #1 SMP PREEMPT Wed May 10 19:59:33 UTC 2017 aarch64

The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
Last login: Wed May 24 19:26:42 2017 from 192.168.0.7
ERROR: Unable to find libsoc_gpio.conf
cat: /usr/local/etc/libsoc_gpio.conf: No such file or directory
declare -x HOME="/home/linaro"
declare -x JAVA_TOOL_OPTIONS="-Dgnu.io.rxtx.SerialPorts=/dev/tty96B0"
declare -x LANG="C.UTF-8"
declare -x LOGNAME="linaro"
declare -x MAIL="/var/mail/linaro"
declare -x MONITOR_PORT="/dev/tty96B0"
declare -x OLDPWD
declare -x PATH="/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games"
declare -x PWD="/home/linaro"
declare -x SHELL="/bin/bash"
declare -x SHLVL="1"
declare -x SSH_CLIENT="192.168.0.7 7154 22"
declare -x SSH_CONNECTION="192.168.0.7 7154 192.168.0.8 22"
declare -x SSH_TTY="/dev/pts/0"
declare -x TERM="xterm"
declare -x USER="linaro"
declare -x XDG_RUNTIME_DIR="/run/user/1000"
declare -x XDG_SESSION_ID="5"
linaro@linaro-alip:~$

エラーメッセージを確認するとERROR: Unable to find libsoc_gpio.confというエラーが発生しています。libsocの設定ファイルがみあたらないようです。ソースからインストールした時には発生しなようですが、パッケージでのインストールでは発生してしまっているようです。

仕方なくエラーメッセージからググってみると…

discuss.96boards.org

というようなページを見つけました。なければ作れということのようです。 そこで以下のファイルを開いて

$ sudo vi /etc/libsoc_gpio.conf

以下のように編集します。

【/etc/libsoc_gpio.conf】

# board
model = Qualcomm Technologies, Inc. APQ 8016 SBC

# GPIO
# dragonboard 410c pin layout
#<Pin Name> = <SoC Num>
GPIO_A = 36
GPIO_B = 12
GPIO_C = 13
GPIO_D = 69
GPIO_E = 115
GPIO_F = 4
GPIO_G = 24
GPIO_H = 25
GPIO_I = 35
GPIO_J = 34
GPIO_K = 28
GPIO_L = 33

# include mappings by pin number on board
GPIO_23 = 36
GPIO_24 = 12
GPIO_25 = 13
GPIO_26 = 69
GPIO_27 = 115
GPIO_28 = 4
GPIO_29 = 24
GPIO_30 = 25
GPIO_31 = 35
GPIO_32 = 34
GPIO_33 = 28
GPIO_34 = 33

内容としてはGPIOのpinとアサインの設定を記載したファイルになるのだと思います。 このあと再度ログインをするとエラメッセージは消えました。

終わりに

今回もいろいろと迷い路をしていましたが、ようやくDragonBoardでもGPIOが使用できるようになりました。電圧が1.8Vなのが気になりますが、これからまた考えていこうかと思います。

/* -----codeの行番号----- */