一、CAN总线
CAN是控制器局域网络(Controller Area Network)的简称, 它是由研发和生产汽车电子产品著称的德国BOSCH公司开发的, 并最终成为国际标准(ISO11519),是国际上应用最广泛的现场总线之一。
二、工作原理
CAN通讯的工作原理可以概括为以下几点:
- 报文传输:CAN总线以报文的方式发送数据。每组报文的前十一位是标识符,用于标识消息的类型和优先级,不包括详细发送数据。CAN总线通过标识符的逐位仲裁解决总线访问碰撞问题,确保了报文和时间均不损失。
- 帧结构:CAN总线中的数据传输是通过帧(Frame)的方式进行的。帧可以分为数据帧、远程帧、错误帧和过载帧四种类型。数据帧承担着数据从发送器到接收器的传输责任,远程帧由总线单元发出,用于请求发送相同的数据帧。错误帧可由任何单元在检测到总线错误时发出,过载帧用于在两数据帧或远程帧之间提供延时。
- 广播通信:CAN总线采用广播通信方式,从一个节点向另一个节点发送数据。当一个节点要发送数据时,它将待发送的数据和标识符发送给本节点的CAN芯片,并使其进入准备阶段。一旦CAN芯片收到总线分配,就变为发送报文阶段,将待发送的数据组成规定的报文格式发出。此时,网络中的其他节点都处于接收阶段,对接收到的报文进行检测,以判断该报文是否是发给自己的。
- 面向内容的编址方案:CAN总线采用面向内容的编址方案,使得控制体系的构建更加灵活和方便。在不修改软硬件的情况下,可以向CAN总线中添加新节点,这使得CAN总线系统具有很好的扩展性和适应性。
Standard CAN
• SOF–The single dominant start of frame (SOF) bit marks the start of a message, and is used to
synchronize the nodes on a bus after being idle.
• Identifier-The Standard CAN 11-bit identifier establishes the priority of the message. The lower the
binary value, the higher its priority.
• RTR–The single remote transmission request (RTR) bit is dominant when information is required from
another node. All nodes receive the request, but the identifier determines the specified node. The
responding data is also received by all nodes and used by any node interested. In this way, all data
being used in a system is uniform.
• IDE–A dominant single identifier extension (IDE) bit means that a standard CAN identifier with no
extension is being transmitted.
• r0–Reserved bit (for possible use by future standard amendment).
• DLC–The 4-bit data length code (DLC) contains the number of bytes of data being transmitted.
• Data–Up to 64 bits of application data may be transmitted.
• CRC–The 16-bit (15 bits plus delimiter) cyclic redundancy check (CRC) contains the checksum
(number of bits transmitted) of the preceding application data for error detection.
• ACK–Every node receiving an accurate message overwrites this recessive bit in the original message
with a dominate bit, indicating an error-free message has been sent. Should a receiving node detect an
error and leave this bit recessive, it discards the message and the sending node repeats the message
after rearbitration. In this way, each node acknowledges (ACK) the integrity of its data. ACK is 2 bits,
one is the acknowledgment bit and the second is a delimiter.
• EOF–This end-of-frame (EOF), 7-bit field marks the end of a CAN frame (message) and disables bitstuffing, indicating a stuffing error when dominant. When 5 bits of the same logic level occur in
succession during normal operation, a bit of the opposite logic level is stuffed into the data.
• IFS–This 7-bit interframe space (IFS) contains the time required by the controller to move a correctly
received frame to its proper position in a message buffer area.
Here is a breakdown of the different fields in the Standard CAN Frame:
- SOF (Start of Frame):
- Length: 1 bit
- Purpose: Indicates the beginning of a CAN frame. It is always dominant bit (0 in CAN).
- ID (Identifier):
- Length: 11 bits
- Purpose: Represents the priority and the address of the transmitting node. In CAN, the lower the identifier value, the higher the priority.
- RTR (Remote Transmission Request):
- Length: 1 bit
- Purpose: Used to differentiate a data frame from a remote request frame (RTR = 0 for data frames and RTR = 1 for remote request frames).
- Control
- Length: 6 bits
- Purpose: Contains control information like the data length code (DLC) which indicates the number of bytes in the data field.
- Data:
- Length: 0 to 64 bits (0 to 8 bytes)
- Purpose: Contains the actual data being transmitted. Its length is determined by the DLC in the control field.
- CRC (Cyclic Redundancy Check):
- Length: 16 bits
- Purpose: A polynomial code used to detect errors during data transmission. The transmitting node computers a CRC value based on the frame content and sends it along with the frame. The receiving node then calculates its own CRC from the received frame and compares it to the received CRC. If they match, it’s assumed that the frame was received correctly.
- ACK (Acknowledgement):
- Length: 2 bits (one for the slot and one for the delimiter)
- Purpose: The ACK slot is overwritten with a dominant bit by nodes that correctly receive the frame. If the transmitting node sees a dominant bit in the ACK slot, it knows that at least one other node on the network received its frame correctly. Following, there is an ACK delimiter bit, which is always recessive (1 in CAN).
- EOF (End of Frame):
- Length: 7 bits
- Purpose: Marks the end of a CAN frame. It consist of 7 consecutive recessive bits, ensuring that there’s enough separation between consecutive frames.
In addition to the fields described above, actual CAN communication also involves some other fields and error handling mechanisms not depicted in this standard CAN frame. These include:
- Interframe Space: A time interval between two consecutive frames.
- Error Frames: If a node detects an error in a frame, it will transmit an error frame to notify other nodes of the error.
- Overload Frames: Used to introduce a delay between consecutive data or remote transmit frames.
Extended CAN
the Extended CAN message is the same as the Standard message with the
addition of:
• SRR–The substitute remote request (SRR) bit replaces the RTR bit in the standard message location
as a placeholder in the extended format.
• IDE–A recessive bit in the identifier extension (IDE) indicates that more identifier bits follow. The 18-bit
extension follows IDE.
• r1–Following the RTR and r0 bits, an additional reserve bit has been included ahead of the DLC bit.
此外,CAN总线还采用了差分信号传输方式,即通过两个线路传输一个信号,其中一个线路传输高电平信号,另一个线路传输低电平信号。这种差分信号传输方式可以提供更好的抗干扰性能,确保数据传输的可靠性和稳定性。
参考https://blog.csdn.net/gtkknd/article/details/104813633
三、CAN总线特点
- 实时性: CAN总线具有优越的实时性能,适用于需要及时传输数据的应用,如汽车控制系统、工业自动化等。仲裁机制和帧优先级的设计保证了低延迟和可预测性。
- 多主站结构:CAN总线上的每个节点都可以在任何时刻主动地向网络上的其他节点发送信息,而不分主从。CAN支持多主机系统,多个节点可以同时发送和接收数据。这种分布式控制结构使得系统更加灵活,适用于复杂的嵌入式网络。CAN总线上的节点既可以发送数据又可以接收数据,没有主从之分。但是在同一个时刻,只能由一个节点发送数据,其他节点只能接收数据。
- 差分信号传输: CAN使用差分信号传输,通过两个线路(CAN_H和CAN_L)之间的电压差来传递信息。这种差分传输方式提供了良好的抗干扰性能,使得CAN总线适用于工业环境等有电磁干扰的场合。
- 仲裁机制: CAN总线采用非破坏性仲裁机制,通过比较消息标识符的优先级来决定哪个节点有权继续发送数据。这种机制确保了总线上数据传输的有序性,避免了冲突。
- 广播通信: CAN总线采用广播通信方式,即发送的数据帧可以被总线上的所有节点接收。这种特性有助于信息的共享和同步,同时减少了系统的复杂性。
- 低成本: CAN总线的硬件成本相对较低,适用于大规模的系统集成。由于CAN控制器在硬件上实现了仲裁机制,无需额外的主机处理器,减小了成本和复杂性。
- 灵活性: CAN协议灵活适应不同的应用场景,支持不同的波特率和通信速率。这使得CAN总线可以被广泛用于各种嵌入式系统,从低速的传感器网络到高速的汽车控制系统。
- 错误检测和处理:CAN总线具有强大的错误检测和处理能力。每帧信息都有CRC校验和其他检错措施,保证了通信的可靠性。当检测到错误时,节点会自动重新传输已损坏的信息。此外,如果节点出现严重错误,它可以自动关闭输出功能,以使总线上其他节点的操作不受影响。
- 多种帧类型:CAN总线上的节点没有地址的概念。CAN总线上的数据是以帧为单位传输的,帧又分为数据帧、遥控帧等多种帧类型,帧包含需要传输的数据或控制信息。
- 线与逻辑:CAN总线具有“线与”的特性,也就是当由两个节点同时向总线发送信号时,一个是发送显性电平(逻辑0),另一个发送隐性电平(逻辑1),则总线呈现为显性电平。这个特性被用于总线总裁,也就是哪个节点优先占用总线进行发送操作。
- 特定标识符:每一个帧有一个标识符(Identifier,一下简称ID)。ID不是地址,它表示传输数据的类型,也可以用于总线仲裁时确定优先级。例如,在汽车的CAN总线上,假设用于碰撞检测的节点输出数据帧ID为01,车内温度检测节点发送数据帧的ID为05等。
- 滤波特性:每个CAN节点都接收数据,但是可以对接收的帧根据ID进行过滤。只有节点需要的数据才会被接收并进一步处理,不需要的数据会被自动舍弃。例如,假设安全气囊控制器只接受碰撞检测节点发出的ID为01的帧,这种ID的过滤时有硬件完成的,以便安全气囊控制器在发送碰撞时能及时响应。
- 半双工:CAN总线通信时半双工的,即总线不能同时发送和接收。在多个节点竞争总线进行发送时,通过ID的优先级进行仲裁,竞争胜出的节点继续发送,竞争失败的节点立刻转入接收状态。
- 无时钟信号:CAN总线没有用于同步的时钟信号,所以需要规定CAN总线通信的波特率,所以节点都是用同样的波特率进行通信。
- 高传输速率和长距离通信:CAN总线的传输速率最高可以达到1Mbps,而直接通信距离最远可以达到10km(在传输速率低于5kbps的情况下)。
- 报文滤波和传输方式:CAN总线通过报文滤波实现点对点、一点对多点及全局广播等几种方式传送数据,无需专门的“调度”。此外,CAN总线支持短帧结构,每帧字节数最多为8个,传输时间短,抗干扰能力强,检错效果好。
- 节点数多和扩展性强:CAN总线上的节点数主要取决于总线驱动电路,目前可以达到110个。同时,CAN总线的报文标志符数量也很多,几乎不受限制,这使得CAN总线具有很强的扩展性。
- 通信介质灵活:CAN总线的通信介质可以是双绞线、同轴电缆或光纤,选择灵活,适应了不同的应用环境。
- 标准化和规范化:CAN总线是目前为止惟一有国际标准的现场总线(国际标准ISO11898),这保证了其广泛的应用和互操作性。
- 高性价比:CAN总线的设计使其具有很高的性价比,适用于各种规模和应用场景。
参考:
https://www.autopi.io/blog/can-bus-explained/
https://www.allaboutcircuits.com/technical-articles/introduction-to-can-controller-area-network/
https://www.ti.com/lit/an/sloa101b/sloa101b.pdf
https://mp.weixin.qq.com/s/QZ-VEf2qHqffgeJU0qGfJw
https://blog.csdn.net/wangguchao/article/details/124715519