CAN overview

来自百问网嵌入式Linux wiki
Zhouyuebiao讨论 | 贡献2020年5月8日 (五) 00:14的版本

This article gives information about the Linux® Controller Area Network (CAN) framework. It explains how to activate the CAN interface and, based on examples, how to use it.

Framework purpose

The Controller Area Network (CAN) is a multi-master serial bus standard connecting at least two nodes. It is a message-based protocol originally designed for in-vehicle communication and which main benefits are a significant reduction of wiring and the prevention of message collision.

For better real-time performance, CAN with Flexible Data-Rate (CAN FD)[1] is used as an extension to the classic CAN protocol[2]. It allows data rates higher than 1 MBit/s and payloads longer than 8 bytes per frame (up to 64 data bytes).

SocketCAN [3] is a uniform CAN Framework for the Linux kernel. It implements a new protocol family called PF_CAN[4] and allows applications to receive and transmit CAN messages via Socket APIs with CAN specific socket options.

You can find many applications of CAN in the automotive industry. In vehicles, it allows electronic control units and devices to communicate with each other in applications without a host computer. For example, a high speed CAN bus is dedicated to security devices such as emergency brake system or airbags. Another low speed CAN bus is dedicated to comfort devices such as interiror lighting or seat control.

This article describes the main components and APIs of the CAN Framework and gives examples of CAN usage.

System overview

Alternate text
CAN Overview

Component description

From user space to hardware

  • Application (User space)

Application to read/write data on the SocketCAN interface for communication with external devices connected on the CAN network (such as can-utils).

  • CAN tools (User space)

Set of utilities for configuring and enabling SocketCAN interface (such as iproute2).

  • SocketCAN (Kernel space)

Socket interface with specific CAN options which builds upon the Linux network layer.

  • Linux Socket Layer and CAN Protocols (PF_CAN) (Kernel space)

The protocol family, PF_CAN[4], provides an API for transport protocol modules to register and the structures to enable different CAN protocols on the bus.

  • Linux Networking Core (Kernel space)

Kernel network layer that adapts the message to the transport protocol in use. The network subsystem of the Linux kernel is designed to be completely protocol-independent.

  • M_CAN Driver (Kernel space)

Driver implemented as a network interface for Bosch M_CAN controller[5].

  • CAN (Hardware)

This is the CAN Core IP.

  • CAN Transceiver (Hardware)

Interface between the CAN protocol controller and the physical wires of the CAN bus lines.

API description

The SocketCAN interface API description can be found in kernel documentation [3].

Configuration

Kernel configuration

Activate the CAN driver in kernel configuration with Linux Menuconfig tool.

For compiling M_CAN driver, select "Bosch M_CAN devices":

 [*] Networking support  --->                                                                                   
        <*>   CAN bus subsystem support  --->
                CAN Device Drivers  --->
                        <*>   Bosch M_CAN devices                 

M_CAN driver is activated by default in ST deliveries.

Device tree configuration

CAN generic DT bindings:

  • The M_CAN device tree bindings[6] describe all the required and optional properties.

Detailed DT configuration for STM32 internal peripherals:

How to use the framework

The CAN device must be configured via netlink interface. The following articles give user space examples of how to set up a SockeCAN interface (and configure settings like bit-timing parameters) and how to send/receive data on the CAN bus.

How to set up a SocketCAN interface

如何设置SocketCAN接口

How to send/receive CAN data

如何发送或接收CAN数据

How to trace and debug the framework

How to trace

CAN Framework, specifically M_CAN driver, print out info and error messages. You can display them with dmesg command:

Board $> dmesg | grep m_can
[    1.327824] m_can 4400e000.can: m_can device registered (irq=30, version=32)
[   25.560759] m_can 4400e000.can can0: bitrate error 0.3%
[   25.564630] m_can 4400e000.can can0: bitrate error 1.6%

How to monitor CAN bus

You can use the CAN FD adapter PCAN-USB Pro FD[7] to connect a computer to the CAN network via USB. The PCAN-View software provided with the tool is a monitoring program that allows to supervise the data flow on the CAN network and to detect frame errors.

Source code location

The source files are located inside the Linux kernel.

  • PF_CAN: af_can.c[4]
  • M_CAN driver: m_can.c[5]

To go further

CAN bit timing calculation plays an important role in ensuring performance of CAN network. To avoid transmission errors, the bit timing must be configured properly.

For more information about CAN bit timing:

  • Computation of CAN Bit Timing Parameters Simplified[8], from the CAN in Automation group (CiA)
  • The Configuration of the CAN Bit Timing[9], from Bosch documentation

References

<securetransclude src="ProtectedTemplate:ArticleBasedOnModel" params="Framework overview article model"></securetransclude> <securetransclude src="ProtectedTemplate:PublicationRequestId" params="9718 | 2018-11-20 | BrunoB"></securetransclude>