[嵌入式linux]CAN/CAN FD配置及測試

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:https://mirrors.edge.kernel.org/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
      Note: An error occurs when you try to set the bitrate with an old Linux kernel. In that case, echo 125000 > /sys/devices/platform/FlexCAN.0/bitrate was reported to be a good alternative.
      • 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
    
     
  2. Print received can frames
    candump can0

     

https://www.can-cia.org/can-knowledge/

http://www.can-wiki.info/doku.php

https://elinux.org/CAN_Bus

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章