CAN总线

CAN是控制器局域网络(Controller Area Network, CAN)的简称,是由以研发和生产汽车电子产品著称的德国BOSCH公司开发的,并最终成为国际标准(ISO 11898),是国际上应用最广泛的现场总线之一。 在北美和西欧,CAN总线协议已经成为汽车计算机控制系统和嵌入式工业控制局域网的标准总线,并且拥有以CAN为底层协议专为大型货车和重工机械车辆设计的J1939协议。

配置can

user@server:~$ cat /etc/can_setup.sh 
#!/bin/sh

modprobe can
modprobe can_raw
modprobe mttcan

ip link set can0 up type can bitrate 500000 restart-ms 5000 
ip link set can1 up type can bitrate 500000 restart-ms 5000

验证can

user@server:~$ ip -details -statistics link show can0 
8: can0: <NOARP,UP,LOWER_UP,ECHO> mtu 16 qdisc pfifo_fast state UNKNOWN mode DEFAULT group default qlen 10
    link/can  promiscuity 0 
    can state ERROR-ACTIVE (berr-counter tx 0 rx 0) restart-ms 5000 
      bitrate 498701 sample-point 0.870 
      tq 26 prop-seg 33 phase-seg1 33 phase-seg2 10 sjw 1
      mttcan: tseg1 2..255 tseg2 0..127 sjw 1..127 brp 1..511 brp-inc 1
      mttcan: dtseg1 1..31 dtseg2 0..15 dsjw 1..15 dbrp 1..15 dbrp-inc 1
      clock 38400000
      re-started bus-errors arbit-lost error-warn error-pass bus-off
      0          0          0          0          0          0         numtxqueues 1 numrxqueues 1 gso_max_size 65536 gso_max_segs 65535 
    RX: bytes  packets  errors  dropped overrun mcast   
    2984408    373051   0       0       0       0       
    TX: bytes  packets  errors  dropped carrier collsns 
    210824     26870    0       0       0       0  

检查dmesg消息

user@server:~$ dmesg |grep can
[    0.000000] OF: fdt:memory scan node memory, reg size 48,
[    1.521230] tegra-pwm 3280000.pwm: PWM clk can sleep in ops
[    1.521649] tegra-pwm c340000.pwm: PWM clk can sleep in ops
[    1.522360] tegra-pwm 32c0000.pwm: PWM clk can sleep in ops
[    1.522751] tegra-pwm 32f0000.pwm: PWM clk can sleep in ops
[    6.330840] FAN: can't find tach_gpio
[   21.855925] can: controller area network core (rev 20120528 abi 9)
[   21.868303] can: raw protocol (rev 20120528)
[   21.912229] net can0: mttcan device registered (regs=ffffff800d141000, irq=69)
[   21.926728] net can1: mttcan device registered (regs=ffffff800d147000, irq=70)
[   21.934051] mttcan c310000.mttcan can0: bitrate error 0.2%
[   21.934179] mttcan c310000.mttcan can0: Bitrate set
[   21.934238] mttcan_controller_config: ctrlmode 0
[   21.934252] mttcan c310000.mttcan can0: Bitrate set
[   21.934310] IPv6: ADDRCONF(NETDEV_CHANGE): can0: link becomes ready
[   21.949344] mttcan c320000.mttcan can1: bitrate error 0.2%
[   21.949478] mttcan c320000.mttcan can1: Bitrate set
[   21.949546] mttcan_controller_config: ctrlmode 0
[   21.949563] mttcan c320000.mttcan can1: Bitrate set
[   21.949621] IPv6: ADDRCONF(NETDEV_CHANGE): can1: link becomes ready

检查candump输出

user@server:~$ candump can0 |head
  can0  607   [8]  61 00 00 00 0F A0 06 C8
  can0  601   [8]  00 3C 00 02 01 01 44 0B
  can0  602   [8]  00 00 00 00 00 00 00 00
  can0  605   [8]  1F C8 4D 40 00 2C F6 00
  can0  606   [8]  80 01 37 E0 00 39 00 00
  can0  602   [8]  00 00 00 00 00 00 00 00
  can0  601   [8]  00 3C 00 02 01 01 44 0C
  can0  605   [8]  1F BA 4C F0 00 30 29 00
  can0  606   [8]  80 01 37 E0 00 39 00 00
  can0  607   [8]  61 00 00 00 0F A0 07 C9