How to test CAN/CANFD in linux

The Linux kernel supports CAN with the SocketCAN framework.

Driver installation

$ make linux-menuconfig
Networking support  --->
    <M>   CAN bus subsystem support ---> 
        --- CAN bus subsystem support
        <M>   Raw CAN Protocol (raw access with CAN-ID filtering)
        <M>   Broadcast Manager CAN Protocol (with content filtering)
              CAN Device Drivers  --->
                  <M> Virtual Local CAN Interface (vcan)
                  <M> Platform CAN drivers with Netlink support
                  [*]   CAN bit-timing calculation   
                  <M> Microchip 251x series SPI CAN Controller

RootFs

You need to prepare 3 components for the root file system.

要在linux下面配置和测试CAN,需要安装以下三个组件。

  • iproute2 (配置CAN接口时需要)
  • libsocketcan(使用CAN必须)
  • can-utils (CAN的测试小工具,linux下测试CAN比较好用应用程序)

下面提供buildroot和源码安装两种方式,根据自己的情况取其一就可以了。

iproute2

  • buildroot
Package Selection for the target  --->
    Networking applications  --->
        [*] iproute2
  • Source install
    • Download:Index of /pub/linux/utils/net/iproute2/
    • Edit config.mk : Modify "AR" and "CC" according to the compiler you are using.
      ifeq ("$(origin V)", "command line")
        VERBOSE = $(V)
      endif
      ifndef VERBOSE
        VERBOSE = 0
      endif
      ifeq ($(VERBOSE),1)
        Q =
      else
        Q = @
      endif
      
      ifeq ($(VERBOSE), 0)
          QUIET_CC       = @echo '    CC       '$@;
          QUIET_AR       = @echo '    AR       '$@;
          QUIET_LINK     = @echo '    LINK     '$@;
          QUIET_YACC     = @echo '    YACC     '$@;
          QUIET_LEX      = @echo '    LEX      '$@;
      endif
      AR:=aarch64-linux-gnu-ar
      CC:=aarch64-linux-gnu-gcc
      TC_CONFIG_NO_XT:=y
      IP_CONFIG_SETNS:=y
      CFLAGS += -DHAVE_SETNS
      CFLAGS += -DNEED_STRLCPY
      
      %.o: %.c
      	$(QUIET_CC)$(CC) $(CFLAGS) $(EXTRA_CFLAGS) $(CPPFLAGS) -c -o $@ $<
      TC_CONFIG_XT:=n

    • make&install
      cd ${BR_BINARIES_DIR}/iproute2-4.20.0
      make clean
      make
      make install DESTDIR=$TMP_ROOT_DIR

libsocktcan

  • buildroot
Target packages  --->
        Libraries  --->
            Networking  --->
                [*] libsocketcan

can-utils

  • buildroot
Target packages  --->
        Networking applications  --->
            [*] can-utils

Usage of can-utils

  • Link up
    1. CAN 2.0 link up
      ip link set can0 up type can bitrate 100000

    2. CAN FD link up
      ip link set can0 up type can bitrate 500000 dbitrate 2000000  fd on
      • If the following error occurs when you do the last instruction :
        ip: either "dev" is duplicate, or "type" is garbage
        
        check that this command: # which ip return this message: /sbin/ip and not this one : /bin/ip If the binary is installed in /bin instead of /sbin, the executable file is a link to busybox and the command to set the bitrate doesn't work on busybox, so try the following instructions: $ make busybox-clean $ make busybox-dirclean $ make menuconfig Package Selection for the target ---> Networking applications ---> [*] iproute2 $ make Then, reflash your rootfs.
  1. Send can frames
    #The following command sends 3 bytes on the bus (0x1E, 0x10, 0x10) with the identifier 500.
    cansend can0 500#1E.10.10
    
    #You can send a remote request message
    cansend can0 500#R 
    
    #gen and send random frames
    cangen can0 -I i -m -v -n 100 -g 10

 cansen 后的帧数据格式详解如下

* Examples:
 *
 * 123# -> standard CAN-Id = 0x123, len = 0
 * 12345678# -> extended CAN-Id = 0x12345678, len = 0
 * 123#R -> standard CAN-Id = 0x123, len = 0, RTR-frame
 * 123#R0 -> standard CAN-Id = 0x123, len = 0, RTR-frame
 * 123#R7 -> standard CAN-Id = 0x123, len = 7, RTR-frame
 * 123#R8_9 -> standard CAN-Id = 0x123, len = 8, dlc = 9, RTR-frame
 * 7A1#r -> standard CAN-Id = 0x7A1, len = 0, RTR-frame
 *
 * 123#00 -> standard CAN-Id = 0x123, len = 1, data[0] = 0x00
 * 123#1122334455667788 -> standard CAN-Id = 0x123, len = 8
 * 123#1122334455667788_E -> standard CAN-Id = 0x123, len = 8, dlc = 14
 * 123#11.22.33.44.55.66.77.88 -> standard CAN-Id = 0x123, len = 8
 * 123#11.2233.44556677.88 -> standard CAN-Id = 0x123, len = 8
 * 32345678#112233 -> error frame with CAN_ERR_FLAG (0x2000000) set
 *
 * 123##0112233 -> CAN FD frame standard CAN-Id = 0x123, flags = 0, len = 3
 * 123##1112233 -> CAN FD frame, flags = CANFD_BRS, len = 3
 * 123##2112233 -> CAN FD frame, flags = CANFD_ESI, len = 3
 * 123##3 -> CAN FD frame, flags = (CANFD_ESI | CANFD_BRS), len = 0
 *     ^^
 *     CAN FD extension to handle the canfd_frame.flags content

例子:

cansend can0 1F334455##0112233445566778899aabbccdd

cansend can0 1F334455##1112233445566778899aabbccdd

 

cansend can0 1F334455##3112233445566778899aabbccdd

 

 

补充

can在linux下的配置,详细的内容还是参见:https://www.kernel.org/doc/Documentation/networking/can.txt

 $ 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 }

  Example configuring 500 kbit/s arbitration bitrate and 4 Mbit/s data bitrate:

    $ ip link set can0 up type can bitrate 500000 sample-point 0.75 \
                                   dbitrate 4000000 dsample-point 0.8 fd on
    $ ip -details link show can0
    5: 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 500000 sample-point 0.750
          tq 50 prop-seg 14 phase-seg1 15 phase-seg2 10 sjw 1
          pcan_usb_pro_fd: tseg1 1..64 tseg2 1..16 sjw 1..16 brp 1..1024 \
          brp-inc 1
          dbitrate 4000000 dsample-point 0.800
          dtq 12 dprop-seg 7 dphase-seg1 8 dphase-seg2 4 dsjw 1
          pcan_usb_pro_fd: dtseg1 1..16 dtseg2 1..8 dsjw 1..4 dbrp 1..1024 \
          dbrp-inc 1
          clock 80000000

CAN in Automation (CiA): CAN knowledge

start - CAN Wiki

CAN Bus - eLinux.org

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐