1. Purpose[edit | edit source]
This article describes how to set up a SocketCAN interface using the iproute2[1] suite of tools.
iproute2 is a set of utilities for Linux® networking, integrated in the SDK for the STM32 microprocessors.
2. Configuring the SocketCAN interface[edit | edit source]
The available CAN devices are listed in /sys/class/net/:
Board $> ls /sys/class/net can0 eth0 /* can0 interface is available but not necessarily active */
You can also display all the available network interfaces to find out the available CAN devices:
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
Configure the available SocketCAN interface using the ip link command line as follow:
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%
To list CAN user-configurable options, use the following command line:
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 }
If the parameters sjw and dsjw are not specified with the ip link command, the Linux kernel will assign them with the default value 1. This default value is often too small and can cause receive errors if the bitrate of the transmitter is slightly different from the bitrate of the receiver. |
3. Printing SocketCAN information[edit | edit source]
To get a detailed status of the SocketCAN link, use the following command line:
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
4. Enabling/disabling the SocketCAN interface[edit | edit source]
Then enable the connection by bringing the SocketCAN interface up:
Board $> ip link set can0 up
You can check that the interface is up by printing the netlink status:
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 ...
You can disable the connection by bringing the SocketCAN interface down. This command is useful when you need to reconfigure the SocketCAN interface:
Board $> ip link set can0 down
5. Loopback test mode[edit | edit source]
It is possible to configure the SocketCAN in internal Loopback test mode. In that case, the FDCAN treats its own transmitted messages as received messages. This mode can be used for hardware self-test:
Board $> ip link set can0 type can bitrate 1000000 dbitrate 2000000 fd on loopback on
You can check that loopback option is on by printing the netlink status:
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
...
6. Example of SocketCAN interface setup[edit | edit source]
You can configure and enable the SocketCAN interface in the same command line:
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
7. References[edit | edit source]
- ↑ IPROUTE2 information, by The Linux Foundation