How to set up a SocketCAN interface
来自百问网嵌入式Linux wiki
Zhouyuebiao(讨论 | 贡献)2019年12月26日 (四) 15:51的版本
目录
如何配置 SocketCAN 接口
目的
- 本文介绍如何使用 iproute2 [1 ]工具套件配置 SocketCAN 接口。
- iproute2 是一组用于 Linux® 网络的命令行实用程序,已集成在 STM32 微处理器的 SDK 中。
配置 SocketCAN 接口
- 可用的 CAN 设备在 /sys/class/net/ 中列出:
Board $> ls /sys/class/net
can0 eth0 /* can0 interface is available but not necessarily active */
- 还可以显示所有可用的网络接口以找出可用的 CAN 设备:
Board $> 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:30
eth0 Link encap:Ethernet HWaddr 00:80:E1:42:45:EC
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:54 Base address:0x6000
- 使用 ip link 命令行配置可用的 SocketCAN 接口,如下所示:
Board $> ip link set can0 type can bitrate 1000000 dbitrate 2000000 fd on
[ 78.700698] m_can 4400e000.can can0: bitrate error 0.3%
[ 78.704568] m_can 4400e000.can can0: bitrate error 1.6%
- 要列出 CAN 用户可配置的选项,请使用以下命令:
Board $> ip link set can0 type can help
Usage: ip link set DEVICE type can
[ bitrate BITRATE [ sample-point SAMPLE-POINT] ] |
[ tq TQ prop-seg PROP_SEG phase-seg1 PHASE-SEG1
phase-seg2 PHASE-SEG2 [ sjw SJW ] ]
[ dbitrate BITRATE [ dsample-point SAMPLE-POINT] ] |
[ dtq TQ dprop-seg PROP_SEG dphase-seg1 PHASE-SEG1
dphase-seg2 PHASE-SEG2 [ dsjw SJW ] ]
[ loopback { on | off } ]
[ listen-only { on | off } ]
[ triple-sampling { on | off } ]
[ one-shot { on | off } ]
[ berr-reporting { on | off } ]
[ fd { on | off } ]
[ fd-non-iso { on | off } ]
[ presume-ack { on | off } ]
[ restart-ms TIME-MS ]
[ restart ]
Where: BITRATE := { 1..1000000 }
SAMPLE-POINT := { 0.000..0.999 }
TQ := { NUMBER }
PROP-SEG := { 1..8 }
PHASE-SEG1 := { 1..8 }
PHASE-SEG2 := { 1..8 }
SJW := { 1..4 }
RESTART-MS := { 0 | NUMBER }
打印 SocketCAN 信息
- 要获取 SocketCAN 链接的详细状态,请使用以下命令:
Board $> ip -details link show can0
2: can0: <NOARP,ECHO> mtu 72 qdisc pfifo_fast state DOWN mode DEFAULT group default qlen 10
link/can promiscuity 0
can <FD> state STOPPED (berr-counter tx 0 rx 0) restart-ms 0
bitrate 996078 sample-point 0.745
tq 19 prop-seg 18 phase-seg1 19 phase-seg2 13 sjw 1
m_can: tseg1 2..256 tseg2 1..128 sjw 1..128 brp 1..512 brp-inc 1
dbitrate 2032000 dsample-point 0.720
dtq 19 dprop-seg 8 dphase-seg1 9 dphase-seg2 7 dsjw 1
m_can: dtseg1 1..32 dtseg2 1..16 dsjw 1..16 dbrp 1..32 dbrp-inc 1
clock 50800000numtxqueues 1 numrxqueues 1 gso_max_size 65536 gso_max_segs 65535
启用/禁用 SocketCAN 接口
- 然后通过打开 SocketCAN 接口来启用连接:
Board $> ip link set can0 up
- 可以通过下面的命令打印网络链接状态信息来检查接口是否已启动:
Board $> ip -details link show can0
2: can0: <NOARP,UP,LOWER_UP,ECHO> mtu 72 qdisc pfifo_fast state UNKNOWN mode DEFAULT group default qlen 10
link/can promiscuity 0
can <FD> state ERROR-ACTIVE (berr-counter tx 0 rx 0) restart-ms 0
bitrate 996078 sample-point 0.745
...
- 可以通过关闭 SocketCAN 接口来禁用连接。当需要重新配置 SocketCAN 接口时,这个命令很有用:
Board $> ip link set can0 down
回环测试模式
- 可以在内部环回测试模式下配置 SocketCAN。在这种情况下,FDCAN 将其自身的已发送消息视为已接收消息。此模式可用于硬件自检:
Board $> ip link set can0 type can bitrate 1000000 dbitrate 2000000 fd on loopback on
- 可以通过下面的命令打印网络链接状态信息来检查环回选项是否打开:
Board $> ip -details link show can0
2: can0: <NOARP,ECHO> mtu 72 qdisc pfifo_fast state DOWN mode DEFAULT group default qlen 10
link/can promiscuity 0
can <LOOPBACK,FD> state STOPPED (berr-counter tx 0 rx 0) restart-ms 0
bitrate 996078 sample-point 0.745
...
SocketCAN 接口设置示例
- 可以在同一命令行中配置和启用 SocketCAN 接口:
Board $> ip link set can0 up type can bitrate 1000000 dbitrate 2000000 fd on
[ 78.700698] m_can 4400e000.can can0: bitrate error 0.3%
[ 78.704568] m_can 4400e000.can can0: bitrate error 1.6%
[ 78.710140] IPv6: ADDRCONF(NETDEV_CHANGE): can0: link becomes ready
参考
- 1. IPROUTE2 信息,由 Linux Foundation 提供