Skip to content

Using CAN bus

If your hardware features a CAN port and it is correctly setup in the devicetree, you can use it in Linux by installing the packages and can-utils.
If the Packages are installed you can set up the interface by using the command:

ip link set can0 down
ip link set can0 up type can bitrate 1000000 berr-reporting on

This configuration shows a can interface with 1MBit/s speed. The device can also use CAN-FD. For this the configuration of the interface changes a little bit (using 4MBit/s in fd mode):

ip link set can0 down
ip link set can0 up type can bitrate 1000000 dbitrate 4000000 fd on

Afterwars you'll see the interface can0listed when calling ip link without parameter.

Advanced Parameters

The communication parameters for a CAN bus interface are not limited to the appropriate baudrate! For reliable communication, especially in bigger CAN networks or different CAN participants, some more parameters besides the baudrate should be set for your configuration!
The CAN settings of all CAN bus participants should be the same or at least should be compatible!
Use the ip tool to set and get more advanced parameters to optimize your CAN bus communication.

If you wish to set special CAN parameters, this can be achieved with additional options for the ip command

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

To get the currently used parameters and statistics of the can interface use

ip -details -statistics link show can0

3: can0: <NOARP,UP,LOWER_UP,ECHO> mtu 72 qdisc pfifo_fast state UP mode DEFAULT group default qlen 10
    link/can  promiscuity 0 minmtu 0 maxmtu 0 
    can <FD> 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 brp 5
      mcp251xfd: tseg1 2..256 tseg2 1..128 sjw 1..128 brp 1..256 brp_inc 1
      dbitrate 4000000 dsample-point 0.700
      dtq 25 dprop-seg 3 dphase-seg1 3 dphase-seg2 3 dsjw 1 dbrp 1
      mcp251xfd: dtseg1 1..32 dtseg2 1..16 dsjw 1..16 dbrp 1..256 dbrp_inc 1
      clock 40000000 
      re-started bus-errors arbit-lost error-warn error-pass bus-off
      0          0          0          0          1          1         numtxqueues 1 numrxqueues 1 gso_max_size 65536 gso_max_segs 65535 parentbus spi parentdev spi1.0 
    RX:  bytes packets errors dropped  missed   mcast           
             0       0      0       0       0       0 
    TX:  bytes packets errors dropped carrier collsns           
             0       0      0       1       0       0 

Sending and receiving from the interface can be done with the commands cansend and candump.

To send a CAN 2.0 telegram on the previously configured bus use this:

cansend can0 010#00.10.20.30.40.50.60.70

To send a CAN-FD frame use:

cansend can0 010##0.00.11.22.33.44.55.66.77.88.99.AA.BB.CC.DD.EE.FF

When you want to use the CAN-FD baudrate switch, the flags field must be set to '1' (or a combination with other flags):

cansend can0 010##1.00.11.22.33.44.55.66.77.88.99.AA.BB.CC.DD.EE.FF

To receive from the can interface:

candump can0

Utils Packages

There are two packages of can tools available: canutils and can-utils. The commands above are for the package can-utils. The syntax for tools from canutils package may vary. Because can-utils seems to be the more modern package, it is the default in newer BSP releases.