转载地址:https://www.cnblogs.com/zym0805/p/4345175.html
前提:交叉编译了can-utils 4.0.6的几个重要工具。busybox的文件系统还要移植ip命令。
1、
首先配置can0
ip link set can0 type can tq 125 prop-seg 6 phase-seg1 7 phase-seg2 2 sjw 1
这时dmesg可以看到sja1000_fpga_pci 0000:07:04.0: setting BTR0=0x01 BTR1=0x1c
周立功的usbcan-2a测试模块里,波特率250kbs时就是BTR0=0x01 BTR1=0x1c
2、
ip -details link show can0 查看一下
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 500000 sample-point 0.875
tq 125 prop-seg 6 phase-seg1 7 phase-seg2 2 sjw 1
sja1000: tseg1 1..16 tseg2 1..8 sjw 1..4 brp 1..64 brp-inc 1
clock 16000000
3、接收测试,接收测试软件发送的帧:
# ./candump can0
interface = can0, family = 29, type = 3, proto = 1
<0x00000002> [8] 70 01 02 03 04 05 06 07
<0x00000002> [8] 70 01 02 03 04 05 06 07
<0x00000002> [8] 70 01 02 03 04 05 06 07
<0x00000002> [8] 70 01 02 03 04 05 06 07
<0x00000002> [8] 70 01 02 03 04 05 06 07
<0x00000002> [8] 70 01 02 03 04 05 06 07
<0x00000002> [8] 70 01 02 03 04 05 06 07
<0x00000002> [8] 70 01 02 03 04 05 06 07
<0x00000002> [8] 70 01 02 03 04 05 06 07
4、发送测试
./cansend can0 -e 0x11 0x22 0x33 0x44 0x55 0x66 0x77 0x88
interface = can0, family = 29, type = 3, proto = 1
测试软件上能看到接收的帧
5、重启
使用内核文档说的ip link set can0 type can restart-ms 100 会报
RTNETLINK answers: Device or resource busy
使用ifconfig can0 down ;ip link set can0 up type can
即可
4种常见波特率:
250kbps:
ip link set can0 type can tq 125 prop-seg 6 phase-seg1 7 phase-seg2 2 sjw 1
125kbps:
ip link set can0 type can tq 250 prop-seg 6 phase-seg1 7 phase-seg2 2 sjw 1
500kbps:
ip link set can0 type can tq 75 prop-seg 6 phase-seg1 7 phase-seg2 2 sjw 1
1000kbps:
ip link set can0 up type can bitrate 2000000
常见用法:
ip -details link show can0
ifconfig can0 down ;ip link set can0 up type can
./candump can0
./cansend can0 -e 0x11 0x22 0x33 0x44 0x55 0x66 0x77 0x88