Raspberry Piで、PCAN-USB (PEAK-System) を使う

## 概要/Overview
- PEAK-SystemのPCAN-USBのドライバをビルドして使う
- (Updated) 2017-01-10: for `2016-11-25-raspbian` + `peak-linux-driver-8.3`
- Ref: [PEAK-System LINUX Website](http://www.peak-system.com/fileadmin/media/linux/index.htm)
- **<font color='red'>SocketCAN ?</font>** => [Raspberry Pi: PCAN-USB(PEAK-System) で、SocketCAN](http://qiita.com/mt08/items/cd853d318b336e2b0fde)

## 環境/Environment
- Raspberry Pi 2/3
    - For **RPi1/ZERO**, see `# cp $HOME/Module.symvers Module.symvers  ## RPi1 ????`
- Raspbian: `2016-11-25-raspbian-jessie-lite`
- [raspberrypi/firmware](https://github.com/raspberrypi/firmware) - [Hexxeh/rpi-firmware](https://github.com/Hexxeh/rpi-firmware/commits/master)
    - rpi-update(2017-01-10):<br> <font color='red'>`Linux raspberrypi `4.4.41-v7+ #942` SMP Mon Jan 9 15:00:25 GMT 2017 armv7l GNU/Linux`</font> (`8f43681f762e4f632b31572f31f42d405ba9a663`)
    - apt upgrade(2017-01-10):<br> `Linux raspberrypi `4.4.38-v7+ #938` SMP Thu Dec 15 15:22:21 GMT 2016 armv7l GNU/Linux` (`af9cb14d5053f89857225bd18d1df59a089c171e`)
    - Default:<br> `Linux raspberrypi `4.4.34-v7+ #930` SMP Wed Nov 23 15:20:41 GMT 2016 armv7l GNU/Linux` (? `85f52e4b452ac83f4475e5c1e505a32061631c7d` ?)

- CAN I/F: `PCAN-USB` & `PCAN-USB FD` (PEAK-System)


## 初期設定/Initial settings

1. いつもどおり、RaspbianをmicroSDに焼く.
2. SSH-Enabled: /bootパーティションに、`ssh`というファイルを置く
3. パッケージ更新:<br>`sudo apt-get update; time sudo apt-get upgrade -y`
4. 再起動:<br>`sudo reboot`


## ビルド/Build

1. 準備: Kernel updateと

    ```bash:Preparations
sudo apt install -y bc build-essential libpopt-dev rpi-update
sudo rpi-update
sudo reboot
```

2. Source code取得して、Moduleビルドの準備

    ```bash:Preparation_for_module_build
# Firmeare hash => Kernel hash, then get kernel source code and `Module{,7}.symvers`
#
FIRMWARE_REV=`cat /boot/.firmware_revision` # rpi-update generates.
KERNEL_REV=`curl -L https://github.com/Hexxeh/rpi-firmware/raw/${FIRMWARE_REV}/git_hash`
echo Firmware Rev: ${FIRMWARE_REV}
echo kernel Rev  : ${KERNEL_REV}
curl -L https://github.com/Hexxeh/rpi-firmware/raw/${FIRMWARE_REV}/Module.symvers >Module.symvers
curl -L https://github.com/Hexxeh/rpi-firmware/raw/${FIRMWARE_REV}/Module7.symvers >Module7.symvers
curl -L https://github.com/raspberrypi/linux/archive/${KERNEL_REV}.tar.gz >rpi-linux.tar.gz
#
# root
sudo -s
#
# Extract kernel source code
HOME=/home/pi
cd /usr/src
mkdir rpi-linux
cd rpi-linux
tar --strip-components 1 -xf ${HOME}/rpi-linux.tar.gz
#
# Place Module.symvers and .config to the proper folder.
modprobe configs
gunzip -c /proc/config.gz >.config
# cp $HOME/Module.symvers Module.symvers  ## RPi1 ????
cp $HOME/Module7.symvers Module.symvers
#
# Prepare for module build.
time make modules_prepare
ln -sv /usr/src/rpi-linux /lib/modules/$(uname -r)/build
#
chown pi.pi -R /usr/src/rpi-linux
#
exit ## back to user 'pi'
cd ${HOME}
#
```

3. Download/Build/Intall 
    - PCAN Driver: `/dev/pcan*`

    ```bash
PCAN_DRIVER_VER=8.3
cd $HOME
[ -f peak-linux-driver-${PCAN_DRIVER_VER}.tar.gz ] || wget http://www.peak-system.com/fileadmin/media/linux/files/peak-linux-driver-${PCAN_DRIVER_VER}.tar.gz
tar xf ./peak-linux-driver-${PCAN_DRIVER_VER}.tar.gz
cd ./peak-linux-driver-${PCAN_DRIVER_VER}
#
#
# Cleanup
sudo make uninstall
make clean
#
make NET=NO PCI=NO PAR=NO ISA=NO PCC=NO DNG=NO
#
sudo make install
#
```

4. 例　

    ```shell-session:接続すると/dev/pcan*が現れるぞ
pi@raspberrypi:~ $ lsmod | grep pcan
pcan                   89809  0 
pi@raspberrypi:~ $ ls -l /dev/pcanusbfd32 
crw-rw-rw- 1 root root 243, 32 Jan 11 01:49 /dev/pcanusbfd32
pi@raspberrypi:~ $ 
```

5. Test
    - See the bottom for `bitratetest` and `receivetest`.

----
----
----
## 概要
- PEAK-SystemのPCAN-USBのドライバビルドのメモ(peak-linux-driver-7.15.2.tar.gz)
- Kernelモジュールをビルドする(pcan.ko)
- SocketCANではなく、pcan.hを使った、test sampleがビルド
- `rpi-update` はやらない。(Kernelヘッダあわせるのが面倒かも?)
- Kernel headerは、https://www.niksula.hut.fi/~mhiienka/Rpi/linux-headers-rpi/ のを使わせてもらう。(=> gcc-4.7が必要)



## 環境
- Raspberry Pi 2 Model B
- Raspbian: `2015-11-21-raspbian-jessie-lite`
- `uname -a`: `Linux rpi3 4.1.13-v7+ #826 SMP PREEMPT Fri Nov 13 20:19:03 GMT 2015 armv7l GNU/Linux`
- CAN I/F: `PCAN-USB (PEAK-System)`
- microSD: 2GB

## 初期設定
1. microSDにJessie Liteのイメージをいれる
2. `sudo raspi-config` で設定
    - `1 Expand Filesystem`
    - `5 Internationalisation Options`
        - `I2 Change Timezone` 
    - `9 Advanced Options`
        - `A2 Hostname`
        - `A3 Memory Split` => 16
3. WiFi設定(必要あれば)

    ```shell-session
% sudo iwlist wlan0 scan
% sudo su
% wpa_passphrase SSID 'KEY' >> /etc/wpa_supplicant/wpa_supplicant.conf
###
# Wifiの素のパスワードを消す
sudo vi /etc/wpa_supplicant/wpa_supplicant.conf
``` 
4. swap無効(必要あれば)

    ```
sudo dphys-swapfile uninstall
sudo dpkg --purge dphys-swapfile
sudo rm -f /var/swap
```
5. リブート (`% sudo reboot`)

## ビルド
1. linux-headerの準備

    ```
sudo apt-get install gcc-4.7 g++-4.7 libpopt-dev
wget http://www.niksula.hut.fi/~mhiienka/Rpi/linux-headers-rpi/linux-headers-4.1.13-v7+_4.1.13-v7+-2_armhf.deb
sudo dpkg -i linux-headers-4.1.13-v7+_4.1.13-v7+-2_armhf.deb
```
    - alternativesを設定しておく (gcc-4.7, gcc-4.9)

    ```
% sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.7 20
% sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.9 60
% update-alternatives --config gcc
There are 2 choices for the alternative gcc (providing /usr/bin/gcc).
  Selection    Path              Priority   Status
------------------------------------------------------------
* 0            /usr/bin/gcc-4.9   60        auto mode
  1            /usr/bin/gcc-4.7   20        manual mode
  2            /usr/bin/gcc-4.9   60        manual mode
```
2. peak-linux-driverのビルド・インストール

    ```
wget http://www.peak-system.com/fileadmin/media/linux/files/peak-linux-driver-7.15.2.tar.gz
tar xf ./peak-linux-driver-7.15.2.tar.gz
cd ./peak-linux-driver-7.15.2/
sudo make NET=NO PCI=NO PAR=NO ISA=NO PCC=NO DNG=NO
sudo make install
```
    - pcan.ko モジュールが、`/lib/modules/4.1.13-v7+/misc`ではなく、`/lib/modules/4.1.13-v7/misc`にインストールされてしまうので、直す。(`+`があるとまずい？)
    
    ```
cd /lib/modules
sudo mkdir /lib/modules/4.1.13-v7+/misc
sudo mv 4.1.13-v7/misc/* 4.1.13-v7+/misc/
sudo rmdir 4.1.13-v7/misc
sudo rmdir 4.1.13-v7
sudo depmod -a
```

3. テスト
    1. PCAN-USBを差す
    
        ```
$ lsusb
Bus 001 Device 004: ID 0c72:000c PEAK System PCAN-USB
...
$ lsmod
Module                  Size  Used by
pcan                   56986  0
...
$ ls -l /dev/pcanusb0
crw-rw-rw- 1 root root 180, 32 Jan  1 00:01 /dev/pcanusb0
```
    2. サンプルプログラムの実行
        - 250kbpsでExtendedFrameのデータ受信
        - `bitratetest` で250kbpsの設定を見つけて、`receivetest`で受信してみる(`-e`:extended frameに対応)
    
           ```
$ bitratetest -f=/dev/pcanusb0
...
bitratetest: 250001 bits/sec    ->BTR0BTR1=0x011c
             250000 bits/sec    ->BTR0BTR1=0x011c
             249999 bits/sec    ->BTR0BTR1=0x011c
...
$ receivetest -f=/dev/pcanusb0 -b=0x11c -e 
...
receivetest: device node="/dev/pcanusb0"
             Extended frames are accepted, init with BTR0BTR1=0x011c
receivetest: driver version = Release_20150729_n
100851283.640 receivetest: m e 0x0XXXXXXX 8 01 23 45 67 89 AB CD EF
100851283.640 receivetest: m e 0x0XXXXXXX 8 01 23 45 67 89 AB CD EF
100851284.238 receivetest: m e 0x0XXXXXXX 8 01 23 45 67 89 AB CD EF
100851284.878 receivetest: m e 0x0XXXXXXX 8 01 23 45 67 89 AB CD EF
100851285.475 receivetest: m e 0x0XXXXXXX 8 01 23 45 67 89 AB CD EF
100851286.72 receivetest: m e 0x0XXXXXXX 8 01 23 45 67 89 AB CD EF
...
```
    3. 送信テスト (未確認)
        テキストファイルを用意する(sample)

        ```
$ transmitest -f=/dev/pcanusb0 -b=0x11c -e ~/peak-linux-driver-7.15.2/test/transmit.txt
```

## プログラムの作成 (予定)
1. `receivetest.c`をベースに改造していく。