[嵌入式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: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

嵌入式Linux CAN通讯C是指在嵌入式系统中使用Linux操作系统,并使用C语言编程实现CAN通讯功能。 CAN(Controller Area Network)是一种常用于嵌入式系统中的串行通信总线标准,用于连接不同设备和模块,实现数据的传输与通讯。在嵌入式系统中,使用CAN通讯可以方便地实现设备之间的数据交互和协同工作。 嵌入式LinuxLinux操作系统的一个衍生版本,经过特殊的优化和裁剪,适用于嵌入式系统的硬件资源限制和特定需求。使用嵌入式Linux可以充分利用Linux的丰富生态和强大的功能,快速开发出符合嵌入式系统要求的应用。 在嵌入式Linux中实现CAN通讯功能,需要根据硬件平台的要求选择相应的CAN驱动,并通过C语言编程与驱动进行交互。一般来说,通过访问Linux系统中的设备文件或使用相关系统调用来控制和处理CAN总线数据的接收和发送。可以使用Linux提供的套接字(socket)、文件(file)和字符设备(chrdev)等方式来实现CAN的读写操作,并利用C语言的数据结构和函数来处理CAN数据的解析和处理。 可以基于SocketCAN(Socket Controller Area Network)接口来进行CAN通讯,它是Linux每个CAN总线驱动实现的虚拟设备接口。通过SocketCAN,我们可以通过网卡设备文件进行数据收发,并在C程序中使用socket编程接口进行CAN数据包的发送和接收操作。 总之,嵌入式Linux CAN通讯C是一种使用Linux操作系统和C语言编程技术实现的CAN通讯功能。通过选择合适的CAN驱动和使用对应的系统调用和接口,可以实现CAN数据的收发、解析和处理,满足嵌入式系统中设备间的通讯需求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值