linux下的can驱动测试

转载地址:http://blog.csdn.net/luoqindong/article/details/43234807

测试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文件, 里边讲得很清楚

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值