Linux Socket can drive-can bus principle __linux

Source: Internet
Author: User
Tags ack
Because the socket can relates to can bus protocol, socket, Linux network device driver and so on. Therefore, in order to be able to fully understand the principle of socket can. We need to know some of the following aspects of knowledge:

(1) Can bus protocol;

(2) Socket principle;

(3) Linux network device driver;

When familiar with the following three aspects of the knowledge point, we will analyze the Linux based socket can driver. It is easier and more understandable to understand.

(4) the Socket can drive;

one, can bus protocol

Can is an abbreviation for CONTROLLERAREA network (Controller LAN). The Can communication protocol was developed by the German Electric company Bosch in 1986, mainly for automotive communication systems. ISO is now an international standard serial communication protocol. According to different distance, different network, can configure different speed, the highest speed is 1mbit/s.

Can is subdivided into three levels:

(1) Can object layer (the object layer);

(2) Can transport layer (the transfer layer);

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

The object layer and transport layer includes all the services and functionality of the data link layer defined by the Iso/osi model.

The scope of the object layer includes:

(1) Find the sent message.

(2) Determine which message is received by the transport layer that is actually being used.

(3) Provide interface for the related hardware of the application layer.

The function of the transport layer is mainly:

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

(2) When the bus began to send new messages and when to start receiving messages, are determined in the transport layer.

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

(4) The modification of the transport layer is limited.

The function of the physical layer:

The actual transmission of bit information is carried out between different nodes according to all electrical properties. Of course, within the same network, the physical layer must be the same for all nodes. Still, it is free to choose the physical layer.

Fig. 1 the ISO model corresponding to the CAN protocol

Can has the following properties:

(1) message (Messages): In short, it is a data packet with a fixed format.

(2) Information routing (Information Routing): That is, the way messages look for nodes.

(3) Bit rate: The rate at which data bits are transmitted.

(4) Priority (priorities): The priority of message sending.

(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) Multi-Host (multimaster): When the bus is idle, any node can begin to transmit the message.

(7) Arbitration (Arbitration): When 2 and more units start sending messages at the same time, there is a bus access violation. Arbitration is the determination of which unit has a send priority.

(8) Security (Safety): Every node of can has taken powerful measures for error detection, error calibration and error self-test.

(9) Error detection (detection): including monitoring, cyclic redundancy check, bit fill, message format check.

(10) Error detection Execution (performance of error detection)

(11) Error Calibration and recovery time (sinalling and Recovery times): Any node detected incorrectly will mark the corrupted message. This message is invalidated and will automatically begin the transfer. If no new errors occur, the recovery time is up to 29 digits from the time the error is detected to the beginning of the next message's transmission.

(12) fault definition (Fault confinement): Can nodes can distinguish between permanent faults and transient disturbances. The node for permanent failure will be closed.

(13) connection (connections): Can serial communication link is a number of nodes to connect the bus. Theoretically, many nodes can be connected. However, the number of connection nodes is limited due to the effect of delay time or the electrical load on the bus line.

(14) One channel (single Channel): The bus is composed of a single two-way bit signal transmission channel.

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

(16) Response (acknowledgment): All receivers Check the consistency of the message. For a coherent message, the receiver responds; for incoherent packets, the receiver makes a mark.

(17) Sleep mode/wake mode/wake-up: In order to reduce the power consumption of the system power, you can set the can device to sleep mode to stop the internal activity and disconnect from the bus driver. Can devices can be activated by the bus, or system internal state and be awakened.

1, CAN bus message format

Can transmission of messages, can be divided into five kinds of types:

(1) Data frame: A frame used to send a node to transmit data to a receiving node.

(2) Remote frame: The bus node emits a remote frame and requests that a data frame with the same identifier be sent.

(3) Error frame: Any node detects a bus error and emits an error frame.

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

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

A data frame consists of 7 different bit fields:

Frame start Arbitration Court Control field Data field CRC Field Ack Field Frame End

Data frames have standard and remote formats, and the following are the format representations:


Figure 2 Data Frame format

A remote frame consists of 6 different bit fields:

Frame start Arbitration Court Control field CRC Field Ack Field Frame End

A data field where the remote frame has no data frame, and the following is the format representation:


Figure 3 Remote Frame format

The error frame is used to detect errors when receiving and sending messages, and to notify the wrong frames. Error frames consist of error flags and error identifiers.

The error flag includes both the active error flag and the passive error flag.

Active Error Flag: 6-bit dominant bit, the active error state of the unit to detect errors when the output error flag.

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

The error qualifier is composed of 8 bits of hidden bits.

The error frame format is represented as follows:



Figure 4 Error Frame format

An overload frame is a frame used by the receiving unit to inform that it has not completed receiving preparation. Overload frame is composed of overload sign and overload definition character. Overload frame format is represented as follows:



Figure 5 Overload frame format

Frame spacing is the frame used to separate data frames from remote frames. Data frames and remote frames can separate this frame from any previous frame (data frame, remote frame, error frame, overload frame) by inserting a frame interval. Frame spacing cannot be inserted before overload frames and error frames. The frame spacing is shown in the following illustration:



Figure 6 Frame Spacing format

2, CAN bus arbitration mode

in the bus idle state, the unit that first starts sending the message gets the right to send. When multiple units start sending at the same time, each sending unit starts with arbitration from the first of the arbitration section. The unit with the most continuous output dominant level can continue to be sent. That is, a bitwise comparison of the message IDs issued by each node. As a result of the line and the relationship between the display bit "0" can cover the recessive bit "1", so the least ID node WINS arbitration, the bus on the performance of the nodes of the message, other nodes lost arbitration, exit send, to receive state.

When a standard format ID competes with a remote frame with the same ID or an extended-format data frame on the bus, the RTR bit of the standard format has precedence over the dominant bit and can continue to be sent.



Fig. 7 Arbitration method

3, Bit Fill (bitstuffing)

bit padding is a function that is set to prevent burst errors. The rules for bit padding are as follows:

(1) After 5-bit continuous same level, a reverse bit must be filled, that is, 6 consecutive identical bits are not allowed;

(2) SOF before the bus idle state, do not need to sync, so do not need bit padding;

(3) The CRC is a fixed format, the filling is not allowed, and (4) is automatically realized by the can controller;

4, can error handling

Can controller detection error total of the following 5:

(1) bit fill error;

In a frame field with a bit fill, if the node detects 6 consecutive identical bit values, a bit fill error is generated, and the node sends an error frame at the next start.

(2) Bit error;

During delivery, the node detects that the bit value of the bus is inconsistent with the bit value sent by itself, resulting in a bit error, and the node sends an error frame at the next start.

(3) CRC error;

The CRC code of the receiving node calculation is inconsistent with the CRC code of the data frame itself, and the receiving node discards the frame and sends an error frame after the ACK qualifier.

(4) Answer error;

Sending a node in the ACK slot bit will send a hidden bit, while listening to whether the bus is the dominant bit, if the dominant bit, it indicates that at least one node received the frame correctly; if it is an implicit bit, an ACK error will be generated, sending a node to send an error frame.

(5) wrong format;

Sending a node when the dominant bit is detected in a fixed-format position (CRC-delimited, ack-delimited, frame-end EOF), a format error occurs and an error frame is sent.

5, can bus synchronization

The communication mode of Can bus is NRZ mode. No sync signal is attached to the switch or end of each bit. Send a node to start sending data in a bit-time sequence synchronization. In addition, the receiving nodes are synchronized and receive work according to the change of the level on the bus.

However, the clock frequency error of sending node and receiving node and the phase delay of transmission path (cable, driver, etc.) will introduce synchronization deviation. Therefore, the receiving node needs to adjust the time sequence to receive in a synchronized way.

The role of synchronization is to try to make the status sequence consistent with the sum of the time of the summary signal (the local synchronization segment is synchronized with the summary signal edge). Only the receiving node needs to be synchronized, and synchronization only occurs at the recessive to the dominant level of the jump edge.

Synchronization is the way for hardware synchronization and resynchronization.


The above is the CAN bus protocol some summary, deficiencies, please point out, thank you. The next article will begin to explain the principle of the socket.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.