FreescaleのCortex-Mシリーズマイコンの開発環境をLinux上にセットアップする


FreescaleのCortex-M0のMCUの評価ボードであるFRDM-KL02Z を動かしてみました。
Linux(Ubuntu 14.04 x86_64)の上に開発環境を構築しました。

準備

SDKのインストール

以下のサイトからFreescale kinetisのSDKをダウンロードして展開します。
http://www.freescale.com/tools/software-and-tools/run-time-software/kinetis-software-and-tools/development-platforms-with-mbed/software-development-kit-for-kinetis-mcus:KINETIS-SDK

$ WORK=$PWD
$ tar xf Kinetis\ SDK\ 1.3.0\ Mainline\ -\ Linux.tar.gz 

以降SDKの中のドキュメント"Getting Started with Kinetis SDK (KSDK) v.1.3.pdf"に沿って進めて行くのですが、このドキュメントはWindows向けに書かれているので、けっこう苦労しました。
作業用のトップディレクトリは $WORK と表記します。

GNUツールチェインのインストール

以下のサイトからダウンロードして/usr/localの下に展開します。
https://launchpad.net/gcc-arm-embedded

$ sudo tar xf gcc-arm-none-eabi-4_9-2015q3-20150921-linux.tar.bz2 -C /usr/local
$ sudo apt-get install cmake
$ sudo apt-get install lib32ncurses5

ダウンロードしたツールチェインは32bit用にビルドされているので、32bitの標準ライブラリが必要です。lib32ncurses5をインストールするとlib32用のlibcなど必要なものが揃います。

環境変数のセット
$ cd $WORK/KSDK_1.3.0/
$ cat env.sh 
export ARMGCC_DIR=/usr/local/gcc-arm-none-eabi-4_9-2015q3/
export PATH=$ARMGCC_DIR/bin:$PATH

SDKの中のビルドスクリプト環境変数ARMGCC_DIRでツールチェインのありかを参照しているので、これをセットします。

$ source env.sh

ビルド

ライブラリのビルド
$ cd $WORK/KSDK_1.3.0/lib/ksdk_platform_lib/armgcc/KL02Z4/
$ ./build_all.sh
hello_worldのデモアプリのビルド
$ cd $WORK/KSDK_1.3.0/examples/frdmkl02z/demo_apps/hello_world/armgcc/
$ ./build_all.sh

以下のようにファイルができれいればビルド成功。

$ ls -l debug release
debug:
total 452
-rwxrwxr-x 1 koba koba  18976 Oct 30 12:13 hello_world.bin
-rwxrwxr-x 1 koba koba 190613 Oct 30 12:13 hello_world.elf
-rw-rw-r-- 1 koba koba  51103 Oct 30 12:13 hello_world.hex
-rw-rw-r-- 1 koba koba 229412 Oct 30 12:13 hello_world.map

release:
total 172
-rwxrwxr-x 1 koba koba   6676 Oct 30 12:13 hello_world.bin
-rwxrwxr-x 1 koba koba  76881 Oct 30 12:13 hello_world.elf
-rw-rw-r-- 1 koba koba  16506 Oct 30 12:13 hello_world.hex
-rw-rw-r-- 1 koba koba 115515 Oct 30 12:13 hello_world.map

実機で実行

デバッガの準備

JLinkGDBServer は以下のサイトから入手できます。
https://www.segger.com/jlink-software.html

$ cd $WORK
$ tar xf JLink_Linux_V502f_x86_64.tgz 

FRDM-KL02ZをUSBケーブルで挿したときにできるデバイスファイルを一般ユーザでもアクセスできるようにudevのルールを追加しておきます。

$ cat /etc/udev/rules.d/99-jlink.rules 
ENV{ID_BUS}=="usb", ENV{ID_MODEL}=="J-Link", MODE="666"
ファームウェアのアップデート

FRDM-KL02ZはデフォルトではP&E Micro OpenSDAのファームウェアが書かれています。KL02のデバッグインタフェース仕様は OpenSDA v1.0 なので、これに対応したJLink用のファームウェアにアップデートします。
https://www.segger.com/opensda.html からJLink_OpenSDA_2015-10-13.zipをダウンロードします。zipの中身はJLink_OpenSDA_2015-10-13.sdaというひとつのファイル。

FRDM-KL02Z をリセットボタンを押しながらUSBに接続するとファームウェアアップデートのモードになり、PC側からUSBストレージとして見えるボリュームの名称がBOOTLOADERになります。
そこに、JLink_OpenSDA_2015-10-13.sda をドラッグ&ドロップします。書き込みに成功するとLASTSTAT.TXTの内容がCompletedになります。
ドキュメントにはWindowsLinuxのどちらでもこのファームウェアアップデートはできると書いてあったのですが、実際にはLinuxではうまくいかなくて、Windwos7でやったら成功しました。
ファームウェアアップデートが完了したら、USBケーブルを一度抜いて挿し直します。これで通常モードになります。

(2015.11.13 追記)
LinuxからでもGUIでなくてコマンドラインで以下のようにコピーしたらアップデートできました。

$ cp JLink_OpenSDA_2015-10-13.sda /media/koba/BOOTLOADER/

(追記 ここまで)

シリアルコンソールを開く

別のターミナルで評価ボードのシリアルコンソールを開いておく。

$ screen /dev/ttyACM0 115200

このコマンドの終了は ^Ak。

JLinkGDBserverの起動

別のターミナルで

$ cd $WORK/JLink_Linux_V502f_x86_64
$ ./JLinkGDBServer -select USB -device MKL02Z32xxx4 -if SWD -speed 1000
SEGGER J-Link GDB Server V5.02f Command Line Version

JLinkARM.dll V5.02f (DLL compiled Oct  2 2015 20:53:57)

-----GDB Server start settings-----
GDBInit file:                  none
GDB Server Listening port:     2331
SWO raw output listening port: 2332
Terminal I/O port:             2333
Accept remote connection:      yes
Generate logfile:              off
Verify download:               off
Init regs on start:            off
Silent mode:                   off
Single run mode:               off
Target connection timeout:     0 ms
------J-Link related settings------
J-Link Host interface:         USB
J-Link script:                 none
J-Link settings file:          none
------Target related settings------
Target device:                 MKL02Z32xxx4
Target interface:              SWD
Target interface speed:        1000kHz
Target endian:                 little

Connecting to J-Link...
J-Link is connected.
Firmware: J-Link OpenSDA compiled Oct 13 2015 12:09:57
Hardware: V1.00
S/N: 621000000
Checking target voltage...
Target voltage: 3.30 V
Listening on TCP/IP port 2331
Connecting to target...Connected to target
Waiting for GDB connection...

ファームウェアをアップデートしていないと、J-Link is connected.のあたりで失敗します。

gdbの起動

debug用にビルドしたオブジェクトのあるディレクトリに移動して

$ cd $WORK/KSDK_1.3.0/examples/frdmkl02z/demo_apps/hello_world/armgcc/debug
$ arm-none-eabi-gdb hello_world.elf

gdbのプロンプトで

target remote localhost:2331
monitor reset
monitor halt
load
monitor reset
monitor halt
b main
c

これでフラッシュメモリにロードしてmain関数のところまで実行して止まります。
この内容を.gdbinit に書いておけば、次回から自動で実行されます。

c

これでmain関数の中が実行されます。シリアルコンソールに"Hello, world" が表示され、LEDが点滅すれば成功です。

Raspberry Pi 2でiPhoneのUSBテザリングを使う

新しい記事をQiitaに書きました。
Raspberry Pi 2/3/Zero でiPhoneのUSBテザリングを使う

Raspbian jessie を使っています。
iPhoneのUSBテザリングについては以下のページに情報がありますが、この方法ではwheezyではうまくいきましたが、jessieではダメでした。
http://www.daveconroy.com/how-to-tether-your-raspberry-pi-with-your-iphone-5
Raspberry Pi How To: Tether to iPhone 5
でもうまくいく方法を見つけました。

準備。

$ sudo apt-get install ipheth-utils libimobiledevice-utils ifuse usbmuxd
$ sudo mkdir /media/iphone

iPhoneをUSBケーブルでつなぎます。私が試したのはiPhone6

$ sudo ifuse /media/iphone

これでテザリングできるようになります。
iPhoneをつなげたときに、自動でifuseを実行するようにするためには上記のページを参考にしてudevのスクリプトを修正すればよいはずです。

以下はメモ

このページの手順をなぞってみる
Raspberry Pi How To: Tether to iPhone 5

$ sudo apt-get install ipheth-utils libimobiledevice-utils ifuse

ここでiphoneをUSBケーブルでつなぐ。

$ ifconfig -s
Iface   MTU Met   RX-OK RX-ERR RX-DRP RX-OVR    TX-OK TX-ERR TX-DRP TX-OVR Flg
eth0       1500 0       344      0      0 0           290      0      0      0 BMRU
lo        65536 0       136      0      0 0           136      0      0      0 LRU

インタフェースが増えてない。

$ dmesg |tail 
[   33.030589] cfg80211: Calling CRDA to update world regulatory domain
[   36.190643] cfg80211: Calling CRDA to update world regulatory domain
[   39.350699] cfg80211: Calling CRDA to update world regulatory domain
[   42.510714] cfg80211: Exceeded CRDA call max attempts. Not calling CRDA
[  306.713474] usb 1-1.5: new high-speed USB device number 4 using dwc_otg
[  306.816790] usb 1-1.5: New USB device found, idVendor=05ac, idProduct=12a8
[  306.816816] usb 1-1.5: New USB device strings: Mfr=1, Product=2, SerialNumber=3
[  306.816833] usb 1-1.5: Product: iPhone
[  306.816849] usb 1-1.5: Manufacturer: Apple Inc.
[  306.816866] usb 1-1.5: SerialNumber: 4c6f6a8607e5231b47ae51381dc55f2306b1120e

なんか反応が薄い。ipheth が登録されていない。
手動でmodprobeしてみる。

$ sudo modprobe ipheth
$ dmesg |tail
[   36.190643] cfg80211: Calling CRDA to update world regulatory domain
[   39.350699] cfg80211: Calling CRDA to update world regulatory domain
[   42.510714] cfg80211: Exceeded CRDA call max attempts. Not calling CRDA
[  306.713474] usb 1-1.5: new high-speed USB device number 4 using dwc_otg
[  306.816790] usb 1-1.5: New USB device found, idVendor=05ac, idProduct=12a8
[  306.816816] usb 1-1.5: New USB device strings: Mfr=1, Product=2, SerialNumber=3
[  306.816833] usb 1-1.5: Product: iPhone
[  306.816849] usb 1-1.5: Manufacturer: Apple Inc.
[  306.816866] usb 1-1.5: SerialNumber: 4c6f6a8607e5231b47ae51381dc55f2306b1120e
[  432.201372] usbcore: registered new interface driver ipheth
$ ifconfig -s
Iface   MTU Met   RX-OK RX-ERR RX-DRP RX-OVR    TX-OK TX-ERR TX-DRP TX-OVR Flg
eth0       1500 0       344      0      0 0           290      0      0      0 BMRU
lo        65536 0       136      0      0 0           136      0      0      0 LRU

まだダメ。udevから起動されるはずの/lib/udev/ipheth-pairを手動で動かしてみる。

$ sudo /lib/udev/ipheth-pair 
/lib/udev/ipheth-pair: -3: cannot get default device

なんかエラーになっているので、straceで見てみる。

$ sudo strace -o /tmp/strace.log /lib/udev/ipheth-pair 
/lib/udev/ipheth-pair: -3: cannot get default device
  ... 
 getrusage(0x1 /* RUSAGE_??? */, {ru_utime={0, 0}, ru_stime={0, 20000}, ...}) = 0
 read(3, "\3351\243}]\325\326\202\231\202\337\211\tX%\2626\6\254\230fMp\261\2507\223\310\373\347E1", 32) = 32
 read(3, "#\246\301a\375\336\247N\177\320\277BzhXQ\\2\264\360\206\262(\254\364<\234\307=A*\317", 32) = 32
 read(3, "\214\316\305\352z\":\345", 8)  = 8
 fstat64(3, {st_mode=S_IFCHR|0666, st_rdev=makedev(1, 9), ...}) = 0
 futex(0x76f4c3a4, FUTEX_WAKE_PRIVATE, 2147483647) = 0
 stat64("/var/run/usbmuxd", 0x7eeb59c8)  = -1 ENOENT (No such file or directory)
 write(2, "/lib/udev/ipheth-pair: -3: canno"..., 53) = 53
 futex(0x76f4c3a8, FUTEX_WAKE_PRIVATE, 2147483647) = 0
 close(3)                                = 0
 exit_group(-1)                          = ?
 +++ exited with 255 +++

"/var/run/usbmuxd"が無いのが直接の原因。ネットでusbmuxdを検索するとそういうパッケージがあることがわかる。

$ sudo apt-get install usbmuxd

このパッケージがインストールされていなかった。
これでiPhoneを抜いて、挿し直すと

$ ifconfig -s
Iface   MTU Met   RX-OK RX-ERR RX-DRP RX-OVR    TX-OK TX-ERR TX-DRP TX-OVR Flg
eth0       1500 0       434      0      0 0           358      0      0      0 BMRU
eth1       1500 0         0      0      0 0             3      2      0      0 BMRU
lo        65536 0       136      0      0 0           136      0      0      0 LRU

eth1ができた。

$ sudo mkdir /media/iphone
$ sudo ifuse /media/iphone

iPhoneの画面に「このコンピュータを信頼しますか?」というダイアログが出ているので、とりあえず「信頼する」のボタンを押す。

$ ifconfig eth1
eth1      Link encap:Ethernet  HWaddr d2:4f:7e:c4:26:f7  
          inet addr:172.20.10.3  Bcast:172.20.10.15  Mask:255.255.255.240
          inet6 addr: fe80::d04f:7eff:fec4:26f7/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:272 errors:0 dropped:1 overruns:0 frame:0
          TX packets:271 errors:17 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:52399 (51.1 KiB)  TX bytes:39804 (38.8 KiB)

使えるようになった。
要するに、usbmuxd も必要だった。

Raspberry Pi 2でAndroidのUSBテザリングを使う

Raspbian jessieを使っています。
Nexus One (Android 2.3.6)をUSBケーブルでつなぎ、Androidの設定でUSBテザリングを有効にするとそれだけで使えました。

$ dmesg |tail
[12881.591125] usb 1-1.5: USB disconnect, device number 7
[12881.871995] usb 1-1.5: new high-speed USB device number 8 using dwc_otg
[12881.985095] usb 1-1.5: New USB device found, idVendor=18d1, idProduct=4e13
[12881.985123] usb 1-1.5: New USB device strings: Mfr=1, Product=2, SerialNumber=3
[12881.985140] usb 1-1.5: Product: Nexus One
[12881.985155] usb 1-1.5: Manufacturer: Google, Inc.
[12881.985172] usb 1-1.5: SerialNumber: HT015P803242
[12882.009486] usbcore: registered new interface driver cdc_ether
[12882.028650] rndis_host 1-1.5:1.0 usb0: register 'rndis_host' at usb-3f980000.usb-1.5, RNDIS device, a2:cd:1a:a1:66:8a
[12882.028851] usbcore: registered new interface driver rndis_host

新しくusb0というインタフェースができています。

$ ifconfig -s
Iface   MTU Met   RX-OK RX-ERR RX-DRP RX-OVR    TX-OK TX-ERR TX-DRP TX-OVR Flg
eth0       1500 0       493      0      0 0           388      0      0      0 BMU
lo        65536 0       136      0      0 0           136      0      0      0 LRU
usb0       1500 0      6443      0      0 0          4149      0      0      0 BMRU
$ ifconfig usb0
usb0      Link encap:Ethernet  HWaddr a2:cd:1a:a1:66:8a  
          inet addr:192.168.42.204  Bcast:192.168.42.255  Mask:255.255.255.0
          inet6 addr: fe80::a0cd:1aff:fea1:668a/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:6443 errors:0 dropped:0 overruns:0 frame:0
          TX packets:4149 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:9455321 (9.0 MiB)  TX bytes:494963 (483.3 KiB)

$ route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
default         192.168.42.129  0.0.0.0         UG    205    0        0 usb0
192.168.42.0    *               255.255.255.0   U     205    0        0 usb0

Raspbian jessie で起動時にGUIのログイン画面を出さないようにする方法

Raspberry Pi 2 Raspbian jessie をシリアルコンソールで使っています。
ふとps axでプロセスを見るとXサーバーとか不要なプロセスがたくさん動いているのに気がつきました。確かRaspbian wheezy では明示的にstartxコマンドを実行しないとこれらのプロセスは起動されなかったはずです。jessieになってからこのあたりが変わってしまいました。

最初に結論

余計なプロセスが動いているのは嫌なので、これらを止めます。
一時的に止めるには

$ sudo systemctl stop lightdm

起動時にlightdmが動かないようにするには

$ sudo systemctl set-default multi-user.target

元に戻すときには

$ sudo systemctl set-default graphical.target

以下はメモ

Raspbian jessie ではデフォルトで以下のようなプロセスが動いていました。

$ ps ax
  PID TTY      STAT   TIME COMMAND
    1 ?        Ss     0:04 /sbin/init
    2 ?        S      0:00 [kthreadd]
    3 ?        S      0:00 [ksoftirqd/0]
    5 ?        S<     0:00 [kworker/0:0H]
    6 ?        S      0:00 [kworker/u8:0]
    7 ?        S      0:00 [rcu_preempt]
    8 ?        S      0:00 [rcu_sched]
    9 ?        S      0:00 [rcu_bh]
   10 ?        S      0:00 [migration/0]
   11 ?        S      0:00 [migration/1]
   12 ?        S      0:00 [ksoftirqd/1]
   14 ?        S<     0:00 [kworker/1:0H]
   15 ?        S      0:00 [migration/2]
   16 ?        S      0:00 [ksoftirqd/2]
   19 ?        S      0:00 [migration/3]
   20 ?        S      0:00 [ksoftirqd/3]
   22 ?        S<     0:00 [kworker/3:0H]
   23 ?        S<     0:00 [khelper]
   24 ?        S      0:00 [kdevtmpfs]
   25 ?        S<     0:00 [netns]
   26 ?        S<     0:00 [perf]
   27 ?        S      0:00 [khungtaskd]
   28 ?        S<     0:00 [writeback]
   29 ?        S<     0:00 [crypto]
   30 ?        S<     0:00 [bioset]
   31 ?        S<     0:00 [kblockd]
   32 ?        S      0:00 [kworker/1:1]
   33 ?        S<     0:00 [rpciod]
   34 ?        S      0:00 [kswapd0]
   35 ?        S      0:00 [fsnotify_mark]
   36 ?        S<     0:00 [nfsiod]
   42 ?        S<     0:00 [kthrotld]
   43 ?        S      0:00 [kworker/0:1]
   44 ?        S<     0:00 [VCHIQ-0]
   45 ?        S<     0:00 [VCHIQr-0]
   46 ?        S<     0:00 [VCHIQs-0]
   47 ?        S<     0:00 [iscsi_eh]
   48 ?        S<     0:00 [dwc_otg]
   49 ?        S<     0:00 [DWC Notificatio]
   51 ?        S      0:00 [VCHIQka-0]
   52 ?        S<     0:00 [SMIO]
   53 ?        S<     0:00 [deferwq]
   54 ?        S      0:00 [kworker/u8:2]
   55 ?        S      0:00 [mmcqd/0]
   57 ?        S      0:00 [kworker/2:1]
   58 ?        S<     0:01 [kworker/0:1H]
   59 ?        S<     0:00 [kworker/1:1H]
   60 ?        S<     0:00 [ipv6_addrconf]
   71 ?        S<     0:00 [kworker/3:1H]
   72 ?        S<     0:00 [kworker/2:1H]
   95 ?        D      0:00 [kworker/0:2]
  104 ?        Ss     0:00 /lib/systemd/systemd-journald
  107 ?        Ss     0:00 /lib/systemd/systemd-udevd
  211 ?        S      0:00 [kworker/3:2]
  233 ?        S      0:00 [kworker/2:2]
  290 ?        S      0:00 [kworker/1:3]
  407 ?        S<     0:00 [cfg80211]
  497 ?        S      0:00 [kworker/3:3]
  499 ?        Ss     0:00 avahi-daemon: running [raspberrypi.local]
  501 ?        Ss     0:00 /usr/sbin/cron -f
  502 ?        Ssl    0:00 /usr/sbin/rsyslogd -n
  503 ?        Ss     0:00 /lib/systemd/systemd-logind
  504 ?        Ss     0:00 /usr/bin/dbus-daemon --system --address=systemd: --no
  505 ?        Ss     0:00 /sbin/dhcpcd -q -b
  507 ?        S      0:00 avahi-daemon: chroot helper
  536 ?        Ss     0:00 /usr/sbin/thd --daemon --triggers /etc/triggerhappy/t
  540 ?        Ss     0:00 /usr/sbin/sshd -D
  549 ?        Ssl    0:00 /usr/sbin/lightdm
  571 tty1     Ss     0:00 /bin/login -f   
  575 ?        Ss     0:00 /bin/login --   
  590 ?        S      0:00 /usr/sbin/ifplugd -i eth0 -q -f -u0 -d10 -w -I
  613 ?        Ss     0:00 /usr/sbin/ntpd -p /var/run/ntpd.pid -g -u 107:112
  637 tty7     Ss+    0:01 /usr/bin/X :0 -seat seat0 -auth /var/run/lightdm/root
  710 ?        Ss     0:00 /lib/systemd/systemd --user
  718 ?        S      0:00 (sd-pam)  
  726 ?        Sl     0:00 lightdm --session-child 13 16
  729 tty1     S+     0:00 -bash
  746 ?        Ssl    0:00 /usr/bin/lxsession -s LXDE-pi -e LXDE
  777 ?        Ss     0:00 /usr/bin/ssh-agent /usr/bin/dbus-launch --exit-with-s
  780 ?        S      0:00 /usr/bin/dbus-launch --exit-with-session x-session-ma
  781 ?        Ss     0:00 /usr/bin/dbus-daemon --fork --print-pid 5 --print-add
  788 ?        Sl     0:00 /usr/lib/gvfs/gvfsd
  792 ?        Sl     0:00 /usr/lib/gvfs/gvfsd-fuse /run/user/1000/gvfs -f -o bi
  802 ?        S      0:00 openbox --config-file /home/pi/.config/openbox/lxde-p
  803 ?        Sl     0:00 lxpolkit
  806 ?        Sl     0:04 lxpanel --profile LXDE-pi
  808 ?        Sl     0:01 pcmanfm --desktop --profile LXDE-pi
  814 ?        Ss     0:00 /usr/bin/ssh-agent -s
  818 ?        Ssl    0:00 /usr/lib/policykit-1/polkitd --no-debug
  824 ?        Sl     0:00 /usr/lib/gvfs/gvfs-udisks2-volume-monitor
  832 ?        Ssl    0:00 /usr/lib/udisks2/udisksd --no-debug
  842 ?        S

/usr/bin/X がXサーバ。シリアルコンソールだけで使っているなら不要なので、止めたい。
プロセスの親子関係を調べる。

$ pstree
systemd─┬─avahi-daemon───avahi-daemon
        ├─cron
        ├─2*[dbus-daemon]
        ├─dbus-launch
        ├─dhcpcd
        ├─gvfs-afc-volume─┬─{gdbus}
        │                 └─{gvfs-afc-volume}
        ├─gvfs-goa-volume───{gdbus}
        ├─gvfs-gphoto2-vo───{gdbus}
        ├─gvfs-mtp-volume───{gdbus}
        ├─gvfs-udisks2-vo─┬─{gdbus}
        │                 └─{gmain}
        ├─gvfsd───{gdbus}
        ├─gvfsd-fuse─┬─{gdbus}
        │            ├─{gvfs-fuse-sub}
        │            └─2*[{gvfsd-fuse}]
        ├─gvfsd-trash─┬─{gdbus}
        │             └─{gmain}
        ├─ifplugd
        ├─lightdm─┬─Xorg
        │         ├─lightdm─┬─lxsession─┬─lxpanel─┬─{gdbus}
        │         │         │           │         ├─{gmain}
        │         │         │           │         ├─{menu-cache-io}
        │         │         │           │         ├─{task0}
        │         │         │           │         └─{task1}
        │         │         │           ├─lxpolkit───{gdbus}
        │         │         │           ├─openbox
        │         │         │           ├─pcmanfm─┬─{gdbus}
        │         │         │           │         └─{gmain}
        │         │         │           ├─ssh-agent
        │         │         │           ├─{gdbus}
        │         │         │           └─{gmain}
        │         │         ├─{gdbus}
        │         │         └─{gmain}
        │         ├─{gdbus}
        │         └─{gmain}
        ├─login───bash
        ├─login───bash───pstree
        ├─menu-cached─┬─{gdbus}
        │             └─{gmain}
        ├─ntpd
        ├─polkitd─┬─{gdbus}
        │         └─{gmain}
        ├─pulseaudio───{alsa-sink-bcm28}
        ├─rsyslogd─┬─{in:imklog}
        │          ├─{in:imuxsock}
        │          └─{rs:main Q:Reg}
        ├─rtkit-daemon───2*[{rtkit-daemon}]
        ├─ssh-agent
        ├─sshd
        ├─start-pulseaudi───xprop
        ├─systemd───(sd-pam)
        ├─systemd-journal
        ├─systemd-logind
        ├─systemd-udevd
        ├─thd
        └─udisksd─┬─{cleanup}
                  ├─{gdbus}
                  ├─{gmain}
                  └─{probing-thread}

systemdがlightdmを起動するのを止めればよさそう。

$ sudo systemctl stop lightdm
$ ps ax
  PID TTY      STAT   TIME COMMAND
    1 ?        Ss     0:04 /sbin/init
    2 ?        S      0:00 [kthreadd]
    3 ?        S      0:00 [ksoftirqd/0]
    5 ?        S<     0:00 [kworker/0:0H]
    6 ?        S      0:00 [kworker/u8:0]
    7 ?        S      0:00 [rcu_preempt]
    8 ?        S      0:00 [rcu_sched]
    9 ?        S      0:00 [rcu_bh]
   10 ?        S      0:00 [migration/0]
   11 ?        S      0:00 [migration/1]
   12 ?        S      0:00 [ksoftirqd/1]
   14 ?        S<     0:00 [kworker/1:0H]
   15 ?        S      0:00 [migration/2]
   16 ?        S      0:00 [ksoftirqd/2]
   19 ?        S      0:00 [migration/3]
   20 ?        S      0:00 [ksoftirqd/3]
   22 ?        S<     0:00 [kworker/3:0H]
   23 ?        S<     0:00 [khelper]
   24 ?        S      0:00 [kdevtmpfs]
   25 ?        S<     0:00 [netns]
   26 ?        S<     0:00 [perf]
   27 ?        S      0:00 [khungtaskd]
   28 ?        S<     0:00 [writeback]
   29 ?        S<     0:00 [crypto]
   30 ?        S<     0:00 [bioset]
   31 ?        S<     0:00 [kblockd]
   32 ?        S      0:00 [kworker/1:1]
   33 ?        S<     0:00 [rpciod]
   34 ?        S      0:00 [kswapd0]
   35 ?        S      0:00 [fsnotify_mark]
   36 ?        S<     0:00 [nfsiod]
   42 ?        S<     0:00 [kthrotld]
   44 ?        S<     0:00 [VCHIQ-0]
   45 ?        S<     0:00 [VCHIQr-0]
   46 ?        S<     0:00 [VCHIQs-0]
   47 ?        S<     0:00 [iscsi_eh]
   48 ?        S<     0:00 [dwc_otg]
   49 ?        S<     0:00 [DWC Notificatio]
   51 ?        S      0:00 [VCHIQka-0]
   52 ?        S<     0:00 [SMIO]
   53 ?        S<     0:00 [deferwq]
   54 ?        S      0:00 [kworker/u8:2]
   55 ?        S      0:00 [mmcqd/0]
   57 ?        S      0:00 [kworker/2:1]
   58 ?        S<     0:01 [kworker/0:1H]
   59 ?        S<     0:00 [kworker/1:1H]
   60 ?        S<     0:00 [ipv6_addrconf]
   71 ?        S<     0:00 [kworker/3:1H]
   72 ?        S<     0:00 [kworker/2:1H]
   95 ?        S      0:00 [kworker/0:2]
  104 ?        Ss     0:00 /lib/systemd/systemd-journald
  107 ?        Ss     0:00 /lib/systemd/systemd-udevd
  211 ?        S      0:00 [kworker/3:2]
  233 ?        S      0:00 [kworker/2:2]
  407 ?        S<     0:00 [cfg80211]
  497 ?        S      0:00 [kworker/3:3]
  499 ?        Ss     0:00 avahi-daemon: running [raspberrypi.local]
  501 ?        Ss     0:00 /usr/sbin/cron -f
  502 ?        Ssl    0:00 /usr/sbin/rsyslogd -n
  503 ?        Ss     0:00 /lib/systemd/systemd-logind
  504 ?        Ss     0:00 /usr/bin/dbus-daemon --system --address=systemd: --no
  505 ?        Ss     0:00 /sbin/dhcpcd -q -b
  507 ?        S      0:00 avahi-daemon: chroot helper
  536 ?        Ss     0:00 /usr/sbin/thd --daemon --triggers /etc/triggerhappy/t
  540 ?        Ss     0:00 /usr/sbin/sshd -D
  571 tty1     Ss     0:00 /bin/login -f   
  575 ?        Ss     0:00 /bin/login --   
  590 ?        S      0:00 /usr/sbin/ifplugd -i eth0 -q -f -u0 -d10 -w -I
  613 ?        Ss     0:00 /usr/sbin/ntpd -p /var/run/ntpd.pid -g -u 107:112
  710 ?        Ss     0:00 /lib/systemd/systemd --user
  718 ?        S      0:00 (sd-pam)  
  729 tty1     S+     0:00 -bash
  814 ?        Ss     0:00 /usr/bin/ssh-agent -s
  818 ?        Ssl    0:00 /usr/lib/policykit-1/polkitd --no-debug
  832 ?        Ssl    0:00 /usr/lib/udisks2/udisksd --no-debug
  843 ?        SNsl   0:00 /usr/lib/rtkit/rtkit-daemon
  868 ttyAMA0  S      0:00 -bash
  882 ?        S<     0:00 [kworker/2:2H]
 1792 ?        S      0:00 [kworker/0:0]
 1793 ?        S      0:00 [kworker/1:0]
 2130 ?        S      0:00 [kworker/3:0]
 2133 ?        S      0:00 [kworker/0:1]
 2173 ttyAMA0  R+     0:00 ps ax

これでスッキリしました。
最初から動かないようにしたいので、

$ sudo systemctl disable lightdm

としてみたけど、リブートするとまだlightdm以下いろいろなプロセスが起動されてしまって効果無し。

ネットで調べて以下のページを発見。
Debian User Forums • View topic - [SOLVED] systemd, masked services, and disabling lightdm

$ systemctl get-default
graphical.target

となっていたので、以下のようにして解決。

$ sudo systemctl set-default multi-user.target
Created symlink from /etc/systemd/system/default.target to /lib/systemd/system/multi-user.target.

Raspberry Pi 2で現在のカーネルのコンフィグを確認する

Raspbian jessi のカーネルでは/proc/config.gz がありません。

$ ls /proc/config.gz
ls: cannot access /proc/config.gz: No such file or directory

でも以下のようにconfigs.koをロードすれば大丈夫。

$ sudo modprobe configs
$ ls /proc/config.gz
/proc/config.gz

これで現在のカーネルのコンフィグを確認できます。

$ zcat /proc/config.gz |grep IKCONFIG
CONFIG_IKCONFIG=m
CONFIG_IKCONFIG_PROC=y

ちなみに、CONFIG_IKCONFIG=y でカーネルをビルドしておくと常に/proc/config.gz があるようになります。

Raspberry Pi 2でWiFiを使う

Raspberry Pi 2 のRaspbian jessie を使っています。
まずは、Linuxカーネルで標準サポートされているであろう少し古いWiFiドングルで試します。今回使用したのはBUFFALOのWLI-UC-GN。

WiFiドングルを挿してみてカーネルの反応を見ます。

$ dmesg |tail -20
  ... 
[ 1143.178911] usb 1-1.5: new high-speed USB device number 4 using dwc_otg
[ 1143.296458] usb 1-1.5: New USB device found, idVendor=0411, idProduct=015d
[ 1143.296485] usb 1-1.5: New USB device strings: Mfr=1, Product=2, SerialNumber=3
[ 1143.296502] usb 1-1.5: Product: 802.11 n WLAN
[ 1143.296519] usb 1-1.5: Manufacturer: Ralink
[ 1143.296534] usb 1-1.5: SerialNumber: 1.0
[ 1143.718897] usb 1-1.5: reset high-speed USB device number 4 using dwc_otg
[ 1143.829525] ieee80211 phy0: rt2x00_set_rt: Info - RT chipset 3070, rev 0201 detected
[ 1143.883948] ieee80211 phy0: rt2x00_set_rf: Info - RF chipset 0005 detected
[ 1144.082664] ieee80211 phy0: Selected rate control algorithm 'minstrel_ht'
[ 1144.084402] usbcore: registered new interface driver rt2800usb
[ 1144.314294] ieee80211 phy0: rt2x00lib_request_firmware: Info - Loading firmware file 'rt2870.bin'
[ 1144.328254] ieee80211 phy0: rt2x00lib_request_firmware: Info - Firmware detected - version: 0.29
[ 1144.777172] IPv6: ADDRCONF(NETDEV_UP): wlan0: link is not ready

認識したようです。

$ iwconfig
wlan0     IEEE 802.11bgn  ESSID:off/any  
          Mode:Managed  Access Point: Not-Associated   Tx-Power=20 dBm   
          Retry short limit:7   RTS thr:off   Fragment thr:off
          Power Management:off
          
lo        no wireless extensions.

eth0      no wireless extensions.

wlan0 というのができています。

$ sudo iwlist wlan0 scan |grep ESSID
                    ...
                    ESSID:"0024A52FB295-1"
                    ESSID:"0024A52FB295"
                    ...

こんなふうにこれから接続しようとするWiFiアクセスポイントが見えていればOK。WiFiのドライバは正しく動いていそうです。

$ ps ax |grep wpa
 2273 ?        Ss     0:00 /sbin/wpa_supplicant -s -B -P /run/wpa_supplicant.wlan0.pid -i wlan0 -D nl80211,wext -c /etc/wpa_supplicant/wpa_supplicant.conf
 3018 ttyAMA0  S+     0:00 grep --color=auto wpa

wpa_supplicant も起動済みなので、後はWiFiの認証だけです。

GUIは使わずにwpa_cliを使います。

$ wpa_cli
wpa_cli v2.3
Copyright (c) 2004-2014, Jouni Malinen  and contributors

This software may be distributed under the terms of the BSD license.
See README for more details.


Selected interface 'wlan0'

Interactive mode

> 

'>' のプロンプトにコマンドを入力していきます。
アクセスポイントの確認。(入力したのは赤字の部分。)

    
OK <3>CTRL-EVENT-SCAN-STARTED <3>CTRL-EVENT-SCAN-RESULTS <3>WPS-AP-AVAILABLE
bssid / frequency / signal level / flags / ssid 06:24:a5:2f:b2:95 2447 -49 [WPA-PSK-CCMP][ESS] 0024A52FB295-1 00:24:a5:2f:b2:95 2447 -49 [WPA-PSK-CCMP+TKIP][WPA2-PSK-CCMP+TKIP][WPS][ESS] 0024A52FB295 >
SSID "0024A52FB295", パスワード "xxxxxxxx" を登録します。
    
OK
OK
OK <3>CTRL-EVENT-SCAN-STARTED <3>CTRL-EVENT-SCAN-RESULTS <3>WPS-AP-AVAILABLE <3>SME: Trying to authenticate with 00:24:a5:2f:b2:95 (SSID='0024A52FB295' freq=2447 MHz) <3>Trying to associate with 00:24:a5:2f:b2:95 (SSID='0024A52FB295' freq=2447 MHz) <3>Associated with 00:24:a5:2f:b2:95 <3>WPA: Key negotiation completed with 00:24:a5:2f:b2:95 [PTK=CCMP GTK=TKIP] <3>CTRL-EVENT-CONNECTED - Connection to 00:24:a5:2f:b2:95 completed [id=0 id_str=]
OK
$
wlan0の接続は成功して、以下のようにIPアドレスが割り当てられています。
$ ifconfig wlan0
wlan0     Link encap:Ethernet  HWaddr 00:24:a5:7e:e4:7f  
          inet addr:192.168.0.8  Bcast:192.168.0.255  Mask:255.255.255.0
          inet6 addr: fe80::a1d:3d6f:e6df:ae25/64 Scope:Link
          inet6 addr: 240f:16:6d51:1:a15a:8fe7:49dd:ae77/64 Scope:Global
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:313 errors:0 dropped:0 overruns:0 frame:0
          TX packets:54 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:89509 (87.4 KiB)  TX bytes:11524 (11.2 KiB)
設定は以下のところに保存されています。次回からはWiFiドングルを挿したら自動的にこの設定で接続されます。
$ sudo cat /etc/wpa_supplicant/wpa_supplicant.conf
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1

network={
	ssid="0024A52FB295"
	psk="xxxxxxxx"
}
参考にしたのは以下のページ。 https://wiki.archlinuxjp.org/index.php/WPA_supplicant