“CAN overview”的版本间的差异

来自百问网嵌入式Linux wiki
第1行: 第1行:
{{DISPLAYTITLE:CAN总线概述}}
+
This article gives information about the Linux<sup>&reg;</sup> Controller Area Network (CAN) framework.
 +
It explains how to activate the CAN interface and, based on examples, how to use it.
  
[[Category:Linux_Operating_System]]
+
== Framework purpose ==
[[Category:Networking]]
+
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.
[[Category:Netdev]]
+
 
[[Category:CAN]]
+
For better real-time performance, CAN with Flexible Data-Rate (CAN FD)<ref>[https://www.can-cia.org/can-knowledge/can/can-fd/ CAN FD - The basic idea], from the CAN in Automation group (CiA)</ref> is used as an extension to the classic CAN protocol<ref>[https://www.can-cia.org/can-knowledge/can/can-implementations/ CAN protocol implementations], from the CAN in Automation group (CiA)</ref>. It allows data rates higher than 1 MBit/s and payloads longer than 8 bytes per frame (up to 64 data bytes).
 +
 
 +
'''SocketCAN''' <ref name=SocketCAN>{{CodeSource | Linux kernel | Documentation/networking/can.rst | Kernel SocketCAN documentation}}, Linux Foundation</ref>  is a uniform CAN Framework for the Linux kernel. It implements a new protocol family called '''PF_CAN'''<ref name=PF_CAN>{{CodeSource | Linux kernel | net/can/af_can.c | net/can/af_can.c}}, Protocol family CAN core module</ref> 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==
 +
[[File:CAN overview V1.0.png|thumb|link=|center|766px|alt=Alternate text|CAN Overview]]
 +
 
 +
===Component description===
 +
''From user space to hardware''
 +
*'''Application''' (User space)
 +
Application to read/write data on the [[CAN overview#API description | 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<ref name=PF_CAN/>, 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<ref name=M_CAN>{{CodeSource | Linux kernel | drivers/net/can/m_can}}, Driver for Bosch M_CAN controller</ref>.
 +
 
 +
*'''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 <ref name=SocketCAN/>.
 +
 
 +
==Configuration ==
 +
===Kernel configuration===
 +
Activate the CAN driver in kernel configuration with Linux [[Menuconfig or how to configure kernel | Menuconfig ]] tool.
 +
 
 +
For compiling M_CAN driver, select "Bosch M_CAN devices":
 +
<pre>
 +
[*] Networking support  --->                                                                                 
 +
        <*>  CAN bus subsystem support  --->
 +
                CAN Device Drivers  --->
 +
                        <*>  Bosch M_CAN devices               
 +
</pre>
 +
M_CAN driver is activated by default in ST deliveries.
 +
 
 +
===Device tree configuration===
 +
CAN generic DT bindings:
 +
* The M_CAN device tree bindings<ref>{{CodeSource | Linux kernel | Documentation/devicetree/bindings/net/can/m_can.txt | Documentation/devicetree/bindings/net/can/m_can.txt}} M_CAN device tree bindings</ref> describe all the required and optional properties.
 +
Detailed DT configuration for STM32 internal peripherals:
 +
* [[FDCAN device tree configuration]]
 +
 
 +
== 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 ===
 +
[[How to set up a SocketCAN interface]]
 +
 
 +
=== How to send/receive CAN data ===
 +
[[How to send or receive CAN data]]
 +
 
 +
==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'''<ref>[http://www.peak-system.com/PCAN-USB-Pro-FD.366.0.html?&L=1 PCAN-USB Pro FD description], by PEAK System</ref> 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<ref name=PF_CAN/>
 +
 
 +
*'''M_CAN driver''': m_can.c<ref name=M_CAN/>
 +
 
 +
==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''<ref>[https://www.can-cia.org/fileadmin/resources/documents/proceedings/2012_taralkar.pdf Computation of CAN Bit Timing Parameters Simplified], from the CAN in Automation group (CiA)</ref>, from the CAN in Automation group (CiA)
 +
* ''The Configuration of the CAN Bit Timing''<ref>[https://www.mikrocontroller.net/attachment/114193/BOSCH_The_config_of_CAN_Bit_Timing_L-1.pdf The Configuration of the CAN Bit Timing], from Bosch documentation</ref>, from Bosch documentation
 +
 
 +
==References==
 +
<references />
 +
 
 +
<noinclude>
 +
[[Category:CAN|1]]
 +
{{ArticleBasedOnModel | Framework overview article model}}
 +
{{PublicationRequestId | 9718 | 2018-11-20 | BrunoB}}
 +
</noinclude>

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>