6
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Raspberry Pi: PCAN-USB(PEAK-System) で、SocketCAN

Posted at

Qiita-PCAN-SocketCAN_01.png

概要

  • Kernel sourceにドライバが含まれているが、デフォルトでついてこないので、そいつ(peak_usb.ko)をビルドする。
  • SocketCANにすると、SPIのやつと同様に扱える。

環境

  • Raspberry Pi 2/3
  • Raspbian:
    2016-11-25-raspbian-jessie (GUIなWiresharkをつかうなら、こっち)
    2016-11-25-raspbian-jessie-lite
  • CAN I/F: PCAN-USB & PCAN-USB FD (PEAK-System)

手順

  1. いつもの...
    RaspbianをmicroSDにやく
    sudo apt update ; sudo apt upgrade -y

  2. 必要そうなパッケージをインストール:
    sudo apt install -y bc build-essential libpopt-dev rpi-update

  3. ファームウェアアップデート

    • sudo rpi-update
      または
    • sudo rpi-update af9cb14d5053f89857225bd18d1df59a089c171e #(4.4.38-v7+ #938 の場合)
  4. Kernel source取得、解凍、モジュールビルドの準備

    こぴぺ

Retrieve kernel source

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

Extract

sudo mkdir -p /usr/src/rpi-linux
sudo chown pi.pi -R /usr/src/rpi-linux
cd /usr/src/rpi-linux
tar --strip-components 1 -xf ${HOME}/rpi-linux.tar.gz

Place Module.symvers and .config to the proper folder.

sudo modprobe configs
gunzip -c /proc/config.gz >.config
sudo rmmod configs
[ -e /lib/modules/$(uname -r)/build ] || sudo ln -sv /usr/src/rpi-linux /lib/modules/$(uname -r)/build

cp $HOME/Module.symvers Module.symvers ## RPi1 ????

cp $HOME/Module7.symvers Module.symvers


5. ビルド: `peak_usb.ko`

    ```bash:こぴぺ
cd /usr/src/rpi-linux
sed -i -e 's/# CONFIG_CAN_PEAK_USB is not set/CONFIG_CAN_PEAK_USB=m/g' .config
make oldconfig
make modules_prepare
cd drivers/net/can/usb/peak_usb
make -C /usr/src/rpi-linux M=`pwd` clean
make -C /usr/src/rpi-linux M=`pwd` modules
#
# !! This copies to Not "4.4.xx-v7+" but "/lib/modules/4.4.xx-v7/extra/" 
# sudo make -C /usr/src/rpi-linux M=`pwd` modules_install
#
# Manually copied
sudo mkdir -p /lib/modules/`uname -r`/kernel/drivers/net/can/usb/peak_usb
sudo cp -v ./*.ko /lib/modules/`uname -r`/kernel/drivers/net/can/usb/peak_usb
#
# おまじない、して、再起動.
sudo depmod -a
sudo reboot
  1. PCAN-USBを接続
    peak_usbcan_devが読み込まれる。

    実行例

pi@raspberrypi:~ $ lsmod
Module Size Used by
peak_usb 27417 0
can_dev 9907 1 peak_usb
bnep 10340 2
bluetooth 326105 5 bnep
cfg80211 428431 0
rfkill 16037 3 cfg80211,bluetooth
snd_bcm2835 20447 1
snd_pcm 75762 1 snd_bcm2835
snd_timer 19288 1 snd_pcm
snd 51908 5 snd_bcm2835,snd_timer,snd_pcm
bcm2835_gpiomem 3040 0
bcm2835_wdt 3225 0
uio_pdrv_genirq 3164 0
uio 8000 1 uio_pdrv_genirq
i2c_dev 5859 0
fuse 84037 3
ipv6 347594 65
pi@raspberrypi:~ $ lsusb
Bus 001 Device 004: ID 0c72:0012 PEAK System
Bus 001 Device 003: ID 0424:ec00 Standard Microsystems Corp. SMSC9512/9514 Fast Ethernet Adapter
Bus 001 Device 002: ID 0424:9514 Standard Microsystems Corp.
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
pi@raspberrypi:~ $
pi@raspberrypi:~ $ sudo ip link set can0 type can bitrate 500000
pi@raspberrypi:~ $ ifconfig can0
can0 Link encap:UNSPEC HWaddr 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00
NOARP MTU:16 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:10
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)


## 使用例

### <font color='red'>**使うためのおまじない**</font>(ビットレートの指定は、環境に合わせる)

```bash
sudo ip link set can0 type can bitrate 500000
sudo ifconfig can0 up

can-utils - SocketCAN userspace utilities and tools

  1. ツールインストール

sudo apt install -y can-utils


2. メッセージダンプ<br>`candump -cae -t z can0,0:0,#FFFFFFFF`

    ```txt:(実行例)msg_idは、xxxxxxxxにしてます...
	pi@raspberrypi:~/can-utils$ ./candump -cae -t z can0,0:0,#FFFFFFFF
	 (000.000000)  can0  xxxxxxxx   [8]  01 20 34 F3 34 F3 34 F3   '. 4.4.4.'
	 (000.000464)  can0  xxxxxxxx   [6]  01 43 34 F3 34 F3         '.C4.4.'
	 (000.001028)  can0  xxxxxxxx   [8]  08 20 34 F3 34 F3 00 00   '. 4.4...'
	 (000.001603)  can0  xxxxxxxx   [8]  08 00 00 00 00 00 00 00   '........'
...

wireshark / tshark

  1. ツールインストール

sudo apt-get install -y wireshark tshark

#>Configuring wireshark-common

--> YES!

wiresharkグループに所属する、nonsuperuserがパケットキャップできる.

rootだけ.

sudo usermod -aG wireshark pi

logout and login


2. 実行例
    - wireshark (GUI) Xが必要
        - インターフェースを選んで、**Start**でPacket Capture開始。<br>![Qiita-PCAN-SocketCAN_01.png](https://qiita-image-store.s3.amazonaws.com/0/75594/a16a51af-7b91-c095-186e-32cdc0272871.png)
    -  `tshark`

        ```shell-session
		pi@raspberrypi:~ $ tshark -D  # List interfaces
		1. eth0
		2. can0
		3. any
		4. lo (Loopback)
		5. nflog
		6. nfqueue
		7. usbmon1
		pi@raspberrypi:~ $ tshark -i 2 # Interface Index
		Capturing on 'can0'
		  1   0.000000              ->              CAN 16 STD: 0x00000123   02 7e d0 37 4e bd d0 17
		  2   0.009879              ->              CAN 16 STD: 0x00000223   02 7e d0 37 4e bd d0 17
		  3   0.019870              ->              CAN 16 STD: 0x00000443   02 7e d0 37 4e bd d0 17
		  4   0.029968              ->              CAN 16 STD: 0x00000342   02 7e d0 37 4e bd d0 17
		  5   0.039877              ->              CAN 16 STD: 0x00000123   02 7e d0 37 4e bd d0 17
6
10
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
6
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?