The CAN (Controller Area Network) protocol works as a robust, message-based communication protocol designed to allow microcontrollers and devices to communicate with each other in applications without a host computer. It operates as a carrier-sense, multiple-access protocol with collision detection and arbitration on message priority (CSMA/CD+AMP).
Here's a breakdown of how CAN protocol works:
Key Concepts
- Message-Based: Instead of addressing nodes (devices) directly, CAN transmits messages. Each message has an identifier that indicates its content and priority.
- Carrier-Sense Multiple Access (CSMA): Each node "listens" to the bus before transmitting. If the bus is idle, the node can begin transmitting.
- Collision Detection: If two or more nodes start transmitting at the same time, a "collision" occurs.
- Arbitration on Message Priority: CAN uses a unique bit-wise arbitration process to resolve collisions. The message with the higher priority (lower numerical identifier value) wins access to the bus, and the lower priority message(s) stop transmitting. This ensures that critical data is transmitted first.
The CAN Communication Process: A Step-by-Step Guide
-
Bus Idle State: The CAN bus is initially in an idle state, meaning no nodes are transmitting.
-
Node Transmits: A node that wants to send a message waits for the bus to be idle. Once the bus is idle, the node starts transmitting its message. This message includes:
- Start of Frame (SOF): Marks the beginning of the CAN frame.
- Identifier: A unique identifier that indicates the message's content and priority. Lower numerical values indicate higher priority.
- Control Field: Specifies the data length code (DLC) which indicates the number of data bytes.
- Data Field: Contains the actual data being transmitted (0-8 bytes).
- CRC Field: A checksum used for error detection.
- ACK Field: Acknowledgment field where other nodes signal that they have received the message correctly.
- End of Frame (EOF): Marks the end of the CAN frame.
-
Arbitration: If multiple nodes begin transmitting simultaneously, arbitration occurs. During the identifier transmission, each node monitors the bus. If a node transmits a "recessive" bit (logical 1) but detects a "dominant" bit (logical 0) on the bus, it means another node is transmitting a higher-priority message. The node transmitting the recessive bit immediately stops transmitting. This continues until only the highest priority message is being transmitted.
-
Message Reception: All other nodes on the bus listen to the transmitted message. They filter messages based on their identifier. If a node is interested in the message (based on its internal filters), it receives the message and processes the data.
-
Acknowledgment: The receiving node that successfully receives the message sends an "acknowledgment" (ACK) bit back to the transmitting node, confirming that the message was received without errors.
-
Error Handling: CAN incorporates robust error detection mechanisms. If an error is detected (e.g., CRC error), the transmitting node re-transmits the message until it's successfully acknowledged. Excessive errors can lead to a node going into an error passive state to avoid disrupting the bus.
Advantages of CAN
- Priority Handling: Ensures critical messages are transmitted first.
- Robustness: Designed for harsh environments and is resistant to noise.
- Error Detection and Handling: Provides reliable communication.
- Flexibility: Nodes can be added or removed without significantly impacting the network.
- Cost-Effective: Reduces wiring complexity compared to point-to-point communication.
Example
Imagine a car with sensors monitoring engine temperature and oil pressure. The engine temperature sensor has a higher priority identifier (e.g., 0x100) than the oil pressure sensor (e.g., 0x200). If both sensors attempt to transmit simultaneously, the engine temperature message will win arbitration and be transmitted first, ensuring the critical engine temperature data is delivered promptly.
Summary
CAN protocol provides a reliable and efficient communication system for real-time applications, particularly in automotive, industrial automation, and embedded systems. Its message-based architecture, priority arbitration, and error-handling capabilities make it a robust and flexible communication solution.