Socket CAN driver understanding under embedded Linux

Because Socket CAN involves CAN bus protocol, sockets, Linux network device drivers and so on. Therefore, in order to fully understand the principle of Socket CAN. We need to understand the following points:

(1) CAN bus protocol;

(2) Socket principle;

(3) Linux network device driver;

After familiarizing ourselves with the following three aspects of knowledge, we will analyze the Linux-based Socket CAN driver. This is easier and easier to understand.

(4) Socket CAN driver;

First, the CAN bus protocol

Because the content of the CAN bus protocol is too much, as a blog post, it is not suitable for a detailed explanation. Friends who need to know more can use Google. The following is just a brief description.

CAN is an abbreviation for ControllerArea Network. The CAN communication protocol was developed by the German electrician Bosch in 1986 and is mainly for automotive communication systems. It is now an ISO international standard serial communication protocol. Different speeds can be configured according to different distances and different networks, and the maximum speed is 1MBit/s.

CAN is subdivided into three levels:

(1) the CAN object layer (the object layer);

(2) CAN transfer layer (the transfer layer);

(3) CAN physical layer (the phyical layer);

The object layer and transport layer include all the services and functions of the data link layer defined by the ISO/OSI model.

The scope of the object layer includes:

(1) Find the message that was sent.

(2) Determine which message is received by the transport layer to be actually used.

(3) Provide an interface for application layer related hardware.

The role of the transport layer is mainly:

(1) Transmission rules, that is, control frame structure, execution arbitration, error detection, error calibration, and fault definition.

(2) When the bus starts to send new messages and when it starts to receive messages, it is determined in the transport layer.

(3) Some common functions of bit timing can also be regarded as part of the transport layer.

(4) Modification of the transport layer is limited.

The role of the physical layer:

The actual transmission of bit information is performed between different nodes based on all electrical properties. Of course, within the same network, the physical layer must be the same for all nodes. Still, there is still freedom in choosing the physical layer.

Figure 1 ISO model corresponding to the CAN protocol

CAN has the following properties:

(1) Messages: In a nutshell, packets with a fixed format.

(2) Information Routing: That is, the way the message finds a node.

(3) Bit rate: The transmission speed of data bits.

(4) Priority (Priorities): the priority of the message.

(5) Remote Data Request: By sending a remote frame, the node that needs the data can request another node to send the corresponding data frame.

(6) Multimaster: When the bus is idle, any node can start transmitting messages.

(7) Arbitration: When two or more units start transmitting messages at the same time, there will be a bus access violation. Arbitration is to determine which unit has the transmission priority.

(8) Safety: Every node of CAN takes strong measures for error detection, error calibration and error self-test.

(9) Error Detection: including monitoring, cyclic redundancy check, bit filling, and message format check.

(10) Performance of Error Detection (Performance of Error Detection)

(11) Error Sinalling and Recovery Time: Any node that detects an error will mark a corrupted message. This message will expire and will automatically begin retransmission. If a new error no longer occurs, the recovery time is up to 29 digits from the detection of the error to the start of the transmission of the next message.

(12) Fault Confinement: CAN nodes can distinguish between permanent faults and short disturbances. Nodes that are permanently faulty will be shut down.

(13) Connections: The CAN serial communication link is a bus that can connect many nodes. In theory, you can connect countless nodes. However, the number of connection nodes is limited due to the actual delay time or the electrical load on the bus line.

(14) Single Channel: The bus is composed of a single channel for bidirectional bit signal transmission.

(15) Bus value: The bus can have one of two complementary logical values: "dominant" (which can be represented as logical 0) or "recessive" (which can be represented as logical 1).

(16) Acknowledgment: All receivers check the continuity of the message. For a coherent message, the receiver responds; for a non-coherent message, the receiver makes a flag.

(17) Sleep Mode / Wake-up: To reduce the power consumption of the system power supply, the CAN device can be set to sleep mode to stop internal activity and disconnect the bus driver. The CAN device can be activated by the bus or by the internal state of the system.

1, CAN bus message format

CAN transmission messages can be divided into five types:

(1) Data frame: A frame for transmitting a node to transmit data to a receiving node.

(2) Remote frame: The bus node issues a remote frame requesting to send a data frame with the same identifier.

(3) Error frame: An error frame is issued when any node detects a bus error.

(4) Overload frame: The overload frame is used to provide an additional delay between the preceding and subsequent data frames (or remote frames).

(5) Frame interval: A frame used to separate a data frame and a remote frame from the previous frame.

The data frame consists of 7 different bit fields:

Data frames are available in standard and remote formats. The following is the format representation:

Figure 2 data frame format

The remote frame consists of 6 different bit fields:

The remote frame has no data field for the data frame. The following is the format representation:

Figure 3 remote frame format

Error frames are used to detect errors when receiving and sending messages, notifying the wrong frames. The error frame consists of an error flag and an error delimiter.

The error flag includes both an active error flag and a passive error flag.

Active error flag: 6-bit dominant bit, the error flag output when the unit in the active error state detects an error.

Passive error flag: 6-bit recessive bit, the error flag output when the unit in the passive error state detects an error.

The error delimiter consists of 8 bits of recessive bits.

The error frame format is as follows:

Figure 4 error frame format

The overload frame is a frame for the receiving unit to notify that it has not completed the reception preparation. The overload frame consists of an overload flag and an overload delimiter. The overload frame format is as follows:

Figure 5 overload frame format

The frame interval is a frame for separating data frames and remote frames. Data frames and remote frames can be separated from any previous frame (data frame, remote frame, error frame, overload frame) by inserting a frame interval. Frame intervals cannot be inserted before overload frames and error frames. The frame interval is as shown below:

Figure 6 frame interval format

2. CAN bus arbitration method

In the bus idle state, the unit that first started transmitting the message gets the transmission right. When multiple units start transmitting at the same time, each transmitting unit arbitrates from the first bit of the arbitration segment. Units that continuously output the most dominant level can continue to transmit. That is, the message IDs sent by the respective nodes are compared bit by bit. Due to the relationship between lines and lines, the display bit "0" can cover the recessive bit "1", so the node with the smallest ID wins the arbitration, the message on the bus is represented as the node, and the other nodes lose the arbitration, exit the transmission, and change to Receive status.

When the standard format ID and the remote frame with the same ID or the extended format data frame compete on the bus, the standard format RTR bit has priority for the dominant bit and can continue to be transmitted.

Figure 7 Arbitration method

3, bit stuffing (BitStuffing)

Bit stuffing is a function that is set to prevent sudden errors. The rules for bit filling are as follows:

(1) After 5 consecutive consecutive levels, one bit of reverse bit must be filled, ie 6 consecutive identical bits are not allowed;

(2) Before the SOF is the bus idle state, no synchronization is needed, so no bit stuffing is needed;

(3) The CRC is followed by a fixed format, which is not allowed to be filled;

(4) automatically implemented by the CAN controller;

4, CAN error handling

There are five types of CAN controller detection errors:

(1) Bit filling error;

In a frame field filled with bits, if a node detects six consecutive identical bit values, a bit stuffing error is generated, and at the beginning of the next bit, the node will send an error frame.

(2) a bit error;

During transmission, when the node detects that the bit value of the bus does not match the bit value sent by itself, a bit error occurs. At the beginning of the next bit, the node will send an error frame.

(3) CRC error;

The CRC code calculated by the receiving node is inconsistent with the CRC code of the data frame itself, and the receiving node will discard the frame and send an error frame after the ACK delimiter.

(4) response error;

The sending node will send a recessive bit in the ACK Slot bit, and listen to whether the bus is a dominant bit. If it is a dominant bit, it indicates that at least one node correctly receives the frame; if it is a recessive bit, an ACK error will be generated. The sending node sends an error frame.

(5) The format is incorrect;

When a transmit node detects a dominant bit at a fixed format position (CRC delimiter, ACK delimiter, end of frame EOF), a format error occurs and an error frame is sent.

5, CAN bus synchronization

The CAN bus communication method is NRZ mode. There is no additional sync signal at the switch or at the end of each bit. The sending node begins to send data in synchronization with the bit timing. In addition, the receiving node synchronizes and performs reception according to changes in the level on the bus.

However, the clock frequency error in the transmit and receive nodes and the phase delay (cable, driver, etc.) on the transmission path introduce a synchronization offset. Therefore, the receiving node needs to adjust the timing to receive in a synchronized manner.

The role of synchronization is to make the position of the position consistent with the bit timing of the summary signal (the local synchronization segment is synchronized with the summary signal edge). Only the receiving nodes need to be synchronized; synchronization only occurs at the edge of the recessive to dominant level.

The way to synchronize is hardware synchronization and resynchronization.

LiFePo4 Battery

Our Lithium Battery includes 5G Base Station Backup Power System,like 48V 100Ah/150Ah/200Ah Lithium Battery. 3.2V Prismatic cells,like 3.2V 50Ah/105Ah/202Ah Lithium Battery. And Lithium Ion Pouch Cells, including 3.2V 12-30Ah.

Lifepo4 Battery,Lifepo4 Lithium Ion Battery,Lifepo4 48V 100Ah Lithium Ion Battery,Lithium Ion Battery For Solar 100Ah

Jiangsu Zhitai New Energy Technology Co.,Ltd , https://www.zhitainewenergy.com

Posted on