CAN总线配置

在Linux系统中,CAN总线接口设备作为网络设备被系统进行统一管理。在控制台下, CAN总线的配置和以太网的配置使用相同的命令。

在控制台上输入命令:

ifconfig –a

可以得到以下结果:

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)

Interrupt:18

eth0   Link encap:Ethernet  HWaddr 00:50:c2:22:3b:0e

UP BROADCAST MULTICAST  MTU:1500  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:1000

RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

eth1   Link encap:Ethernet  HWaddr 00:50:c2:22:3b:60

UP BROADCAST MULTICAST  MTU:1500  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:1000

RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

Interrupt:41 Base address:0xe000

lo     Link encap:Local Loopback

inet addr:127.0.0.1  Mask:255.0.0.0

inet6 addr: ::1/128 Scope:Host

UP LOOPBACK RUNNING  MTU:16436  Metric:1

RX packets:256 errors:0 dropped:0 overruns:0 frame:0

TX packets:256 errors:0 dropped:0 overruns:0 carrier:0

collisions:0 txqueuelen:0

RX bytes:19952 (19.9 KB)  TX bytes:19952 (19.9 KB)

在上面的结果中,eth0和eth1设备为以太网接口,can0设备为CAN总线接口。接下来使用ip命令来配置CAN总线的位速率:

ip link set can0 type can tq 125 prop-seg 6phase-seg1 7 phase-seg2 2 sjw 1

也可以使用ip命令直接设定位速率:

ip link set can0 type can bitrate 125000

当设置完成后,可以通过下面的命令查询can0设备的参数设置:

ip -details link show can0

当设置完成后,可以使用下面的命令使能can0设备:

ifconfig can0 up

使用下面的命令取消can0设备使能:

ifconfig can0 down

###########################################

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

2、

ip -details link show can0 查看一下

can0: 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

即可

并没有掌握sja1000波特率的配置。摸索出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

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^6

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

GitHub 加速计划 / li / linux-dash
13
2
下载
A beautiful web dashboard for Linux
最近提交(Master分支:4 个月前 )
186a802e added ecosystem file for PM2 5 年前
5def40a3 Add host customization support for the NodeJS version 5 年前
Logo

AtomGit 是由开放原子开源基金会联合 CSDN 等生态伙伴共同推出的新一代开源与人工智能协作平台。平台坚持“开放、中立、公益”的理念,把代码托管、模型共享、数据集托管、智能体开发体验和算力服务整合在一起,为开发者提供从开发、训练到部署的一站式体验。

更多推荐