linux下的can驱动测试

测试can需要ip,can-utils和libsocketcan库.
通过ip工具配置can,如速率,启用和禁用can等.不能用buildroot编译出来的ip,需要重新编译.


1. 编译ip:
ip源码 http://pkgs.fedoraproject.org/repo/pkgs/iproute/iproute2-2.6.39.tar.gz/8a3b6bc77c2ecf752284aa4a6fc630a6/iproute2-2.6.39.tar.gz

1>. 修改Makefile
    DESTDIR=/home/tom/myd-xc7z010/usr/  修改安装目录
    #DBM_INCLUDE:=$(ROOTDIR)/usr/include 屏蔽屏这行
CC = arm-xilinx-linux-gnueabi-gcc    使用交叉编译器编译


2>. 运行ip源码目录下的build.sh编译, build.sh内容:
#!/bin/sh
# create by myir tom 2015-01-28
# put this file under the ip source root dir
cp misc/Makefile{,.orig}
sed ‘/^TARGETS/s@arpd@@g‘ misc/Makefile.orig > misc/Makefile
export GNUEABI=arm-xilinx-linux-gnueabi
export CC=$GNUEABI-gcc
export LD=$GNUEABI-ld
export NM=$GNUEABI-nm
export AR=$GNUEABI-ar
export RANLIB=$GNUEABI-ranlib
export CXX=$GNUEABI-c++
export PREFIX=$FILESYS_PATH/usr
export CROSS_COMPILE_PREFIX=$PREFIX
export PATH=$TOOL_CHAIN_PATH/bin:$PATH
export DBM_INCLUDE=/usr/include
export INCLUDES=/usr/include
export DESTDIR=$PREFIX/


./configure --host=arm-xilinx-linux-gnueabi --prefix=$PREFIX --enable-debug
make
sudo make install


3>. 将ip工具copy到文件系统下
将/home/tom/myd-xc7z010/usr/sbin/ip copy到嵌入式文件系统目录/usr/sbin下,还需要把buildroot自带的ip(在/sbin目录下)重命名为ip_buildroot.


查看ip版本和用法:
$ ./ip -V
ip utility, iproute2-ss110629


$ ./ip link help
Usage: ip link add link DEV [ name ] NAME
                  [ txqueuelen PACKETS ]
                  [ address LLADDR ]
                  [ broadcast LLADDR ]
                  [ mtu MTU ]
                  type TYPE [ ARGS ]
      ip link delete DEV type TYPE [ ARGS ]


      ip link set { dev DEVICE | group DEVGROUP } [ { up | down } ]
                         [ arp { on | off } ]
                         [ dynamic { on | off } ]
                         [ multicast { on | off } ]
                         [ allmulticast { on | off } ]
                         [ promisc { on | off } ]
                         [ trailers { on | off } ]
                         [ txqueuelen PACKETS ]
                         [ name NEWNAME ]
                         [ address LLADDR ]
                         [ broadcast LLADDR ]
                         [ mtu MTU ]
                         [ netns PID ]
                         [ alias NAME ]
                         [ vf NUM [ mac LLADDR ]
                                  [ vlan VLANID [ qos VLAN-QOS ] ]
                                  [ rate TXRATE ] ]
                         [ master DEVICE ]
                         [ nomaster ]
      ip link show [ DEVICE | group GROUP ]


TYPE := { vlan | veth | vcan | dummy | ifb | macvlan | can }


2. can-utils和libsocketcan可以使用buildroot编译
Target packages > Libraries > Networking选上
[*] libsocketcan
  Target packages > Networking applications选上
[*] can-utils


3. 测试
   can接口需要在down状态下才能设置


   1>. 查看can接口状态
down下的状态
# ip link set can0 down
# ip -details link show can0
2: can0: <NOARP,ECHO> mtu 16 qdisc pfifo_fast state DOWN qlen 10
    link/can 
    can state STOPPED (berr-counter tx 0 rx 0) restart-ms 0 
    bitrate 49999 sample-point 0.875 
    tq 2500 prop-seg 3 phase-seg1 3 phase-seg2 1 sjw 1
    xilinx_can: tseg1 1..16 tseg2 1..8 sjw 1..4 brp 1..256 brp-inc 1
    clock 99999999


loopback状态
# ip link set can0 type can bitrate 50000 loopback on
xilinx_can e0008000.can can0: bitrate error 0.0%
# ip -details link show can0
2: can0: <NOARP,ECHO> mtu 16 qdisc pfifo_fast state DOWN qlen 10
    link/can 
    can <LOOPBACK> state STOPPED (berr-counter tx 0 rx 0) restart-ms 0 
    bitrate 49999 sample-point 0.875 
    tq 2500 prop-seg 3 phase-seg1 3 phase-seg2 1 sjw 1
    xilinx_can: tseg1 1..16 tseg2 1..8 sjw 1..4 brp 1..256 brp-inc 1
    clock 99999999


up下的状态
# ip link set can0 down
# ip link set can0 type can bitrate 50000 loopback off
xilinx_can e0008000.can can0: bitrate error 0.0%
# ip link set can0 up
# ip -details link show can0
2: can0: <NOARP,UP,LOWER_UP,ECHO> mtu 16 qdisc pfifo_fast state UNKNOWN qlen 10
    link/can 
    can state ERROR-ACTIVE (berr-counter tx 0 rx 0) restart-ms 0 
    bitrate 49999 sample-point 0.875 
    tq 2500 prop-seg 3 phase-seg1 3 phase-seg2 1 sjw 1
    xilinx_can: tseg1 1..16 tseg2 1..8 sjw 1..4 brp 1..256 brp-inc 1
    clock 99999999


   2>. 使用loopback模式测试:
使用loopback模式可测试本机的can驱动是否正常工作.
# ip link set can0 down
# ip link set can0 type can bitrate 50000 loopback on
xilinx_can e0008000.can can0: bitrate error 0.0%
# ip link set can0 up
# candump can0 &
# cansend can0 123#11223344556677
#   can0  123   [7]  11 22 33 44 55 66 77
  can0  123   [7]  11 22 33 44 55 66 77


   3>. 将两块板的can接口接起来,CANH对CANH, CANL对CANL,一边发送,另一边接收,测试之前速率要设置好,且can接口已经up.
#candump can0 开发板1执行接收命令

      #cansend can0 123#11223344556677  开发板2发送数据  

   4> 关于can工具的使用(cansend等),参考can源码目录下的lib.h文件, 里边讲得很清楚

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。