PCAN-USB(PEAK-System)를 사용한 SocketCAN

10441 단어 RaspberryPican

개요

  • Kernel source에 드라이버가 포함되어 있지만 기본적으로 따르지 않기 때문에 구축peak_usb.ko합니다.
  • SocketCAN을 사용할 경우 SPI와 동일하게 처리할 수 있습니다.
  • 컨디션

  • Raspberry Pi 2/3
  • 랩빈: 2016-11-25-raspbian-jessie(GUI의 Wireshark를 사용하면 이쪽)2016-11-25-raspbian-jessie-lite
  • CAN I/F: PCAN-USB & PCAN-USB FD (PEAK-System)
  • 절차.

  • 그대로...Raspbian을 마이크로SDsudo apt update ; sudo apt upgrade -y로 변환
  • 필요한 패키지 설치: sudo apt install -y bc build-essential libpopt-dev rpi-update

  • 펌웨어 업데이트
  • sudo rpi-update 또는
  • sudo rpi-update af9cb14d5053f89857225bd18d1df59a089c171e#(4.4.38-v7+#938의 경우)

  • 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
    #
    

  • 구축: peak_usb.ko개구쟁이
    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
    

  • 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)
    
  • 사용 예


    사용한 부적(비트레이트가 환경에 적합하도록 지정)

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

    can-utils - SocketCAN userspace utilities and tools


  • 도구 설치
    sudo apt install -y can-utils
    

  • 메시지 덤프 candump -cae -t z can0,0:0,#FFFFFFFFmsgid는 xxxxxxx...
    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


  • 도구 설치
    sudo apt-get install -y wireshark tshark
    #
    #>Configuring wireshark-common
    # --> YES! 
    #  <Yes> wiresharkグループに所属する、nonsuperuserがパケットキャップできる.
    #  <No> rootだけ.
    # 
    sudo usermod -aG wireshark pi
    # logout and login
    

  • 실행 예
  • wireshark(GUI) X 필요
  • 인터페이스를 선택하고 Start에서 Packet Capture를 시작합니다.
  • tshark
    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
    
  • 좋은 웹페이지 즐겨찾기