π CAN Busο
CAN (Controller Area Network) is a robust serial communication protocol widely used in automotive, industrial, and embedded systems. It allows multiple nodes to communicate over a shared two-wire bus without a central host controller.
Note
Good course: Understanding the CAN FD Protocol
Overviewο
Physical Layerο
The bus uses two wires β CANH and CANL β operating differentially. The bus is terminated at both ends with 120 Ξ© resistors to prevent signal reflections.
Two logic states exist on the bus:
Dominant β logical 0 (CANH driven high, CANL driven low)
Recessive β logical 1 (both wires at the same voltage)
When nodes drive conflicting states simultaneously, dominant wins. This is the basis for non-destructive bus arbitration.
There are two layers in the CAN protocol stack:
CAN Transceiver β handles the physical layer (drives and reads the differential bus voltages)
CAN Controller β handles the data link layer (framing, arbitration, error detection)
Each node on the bus contains a transceiver and a controller:
Node Communicationο
CAN uses CSMA/CD+CR (Carrier Sense Multiple Access with Collision Detection and Collision Resolution):
Carrier Sense (CS) β nodes monitor the bus and wait for it to be idle before transmitting
Multiple Access (MA) β when the bus is idle, every node has an equal opportunity to transmit
Collision Detection (CD) β nodes can begin transmitting simultaneously; this is detected by all nodes
Collision Resolution (CR) β when a collision occurs it is resolved non-destructively β no messages are lost
The resolution mechanism relies on the dominant/recessive logic: a node that writes a recessive bit but reads back dominant has lost arbitration and immediately backs off. The winning nodeβs frame continues uninterrupted.
Frame Typesο
CAN defines four frame types:
Data Frame β carries payload data from a transmitter to one or more receivers
Remote Frame β requests a data frame with a specific identifier from another node
Error Frame β signals that an error has been detected; all nodes discard the current frame
Overload Frame β signals that a receiving node needs more processing time before the next data or remote frame
Arbitrationο
CAN uses identifier-based arbitration, not addressing. Every frame carries an identifier. Lower identifier values have higher priority β because a lower ID has more leading dominant bits and will win arbitration.
When two nodes begin transmitting simultaneously, each node monitors the bus while it transmits. As long as what it writes matches what it reads, it continues. The moment a node writes a recessive bit but reads a dominant bit back, it loses arbitration, stops transmitting immediately, and waits for the bus to become free.
The winning node never even knows a collision occurred β transmission continues uninterrupted.
Note
CAN controllers have filter and mask registers so a node only receives frames whose identifiers match a configured pattern, ignoring everything else on the bus.
Bit Stuffingο
To ensure regular transitions on the bus (needed for clock synchronisation), CAN inserts a stuff bit after every 5 consecutive bits of the same polarity. Receivers strip these extra bits automatically.
Stuff bits apply to the SOF, arbitration, control, data, and CRC fields. They do not apply to the CRC delimiter, ACK field, or end of frame.
Frame Structure (Classical CAN)ο
A standard CAN data frame (11-bit identifier) has the following structure:
Field |
Size |
Description |
|---|---|---|
Start of Frame (SOF) |
1 bit |
Single dominant bit β signals the start of a frame and synchronises all nodes. |
Identifier |
11 bits |
Frame identifier. Lower value = higher priority. |
RTR |
1 bit |
Remote Transmission Request. Dominant = data frame. Recessive = remote frame (requests data from another node). |
IDE |
1 bit |
Identifier Extension. Dominant = standard 11-bit frame. Recessive = extended 29-bit frame. |
r0 |
1 bit |
Reserved bit, transmitted dominant. |
DLC |
4 bits |
Data Length Code β number of bytes in the data field (0β8). |
Data Field |
0β64 bits |
Payload (0β8 bytes as specified by DLC). |
CRC |
15 bits |
Cyclic redundancy check covering SOF, arbitration, control, and data fields. |
CRC Delimiter |
1 bit |
Recessive bit β gives receivers time to process the CRC before the ACK slot. |
ACK Slot |
1 bit |
Transmitter sends recessive. Any receiver that passed the CRC check writes dominant here β confirming at least one node received the frame correctly. |
ACK Delimiter |
1 bit |
Recessive bit. |
End of Frame (EOF) |
7 bits |
Seven recessive bits β marks the end of the frame. |
Intermission |
3 bits |
Three recessive bits β minimum bus-idle time before the next frame. |
CAN FDο
CAN FD (Flexible Data-rate) extends classical CAN with two improvements:
Higher payload β up to 64 bytes (vs. 8 in classical CAN)
Faster data phase β the data and CRC fields are transmitted at a higher bit rate than the arbitration phase
The control field adds an FDF (FD Frame) bit to distinguish CAN FD frames from classical CAN frames:
FDF dominant β classical CAN frame
FDF recessive β CAN FD frame
CAN FD also replaces the 15-bit CRC with a longer CRC (17 or 21 bits depending on payload size) for improved error detection.
Synchronizationο
CAN uses asynchronous communication β there is no shared clock between nodes. Each node must synchronise its internal clock to the received bit stream.
Synchronisation happens on recessive-to-dominant transitions, most importantly the SOF bit at the start of each frame, and at other transitions throughout the frame.
To prevent long stretches without a transition (which would cause clocks to drift), CAN inserts stuff bits after every 5 consecutive bits of the same polarity. Receivers strip these automatically. A violation of this rule β 6 or more consecutive bits of the same polarity β is treated as a stuff error.
Error Handlingο
Error Detectionο
CAN defines five error types:
Bit error β the transmitter monitors the bus and finds a bit different from what it sent (excluding arbitration and ACK phases)
Stuff error β six or more consecutive bits of the same polarity detected (violates the stuffing rule)
CRC error β receiverβs calculated CRC does not match the transmitted CRC
Form error β a fixed-format field (CRC delimiter, ACK delimiter, EOF) contains an invalid bit
Acknowledgement error β transmitter sees no dominant bit in the ACK slot (no receiver confirmed reception)
Error Flagsο
When a node detects an error, it immediately aborts the current frame by transmitting an error flag. This corrupts the frame for all other nodes, which then also send error flags.
There are two types of error flag:
Active error flag β 6 consecutive dominant bits (violates bit stuffing, making the error visible to all nodes)
Passive error flag β 6 consecutive recessive bits (does not disrupt other nodes)
The error flag is followed by an error delimiter β 8 recessive bits.
Error Confinementο
Each node maintains two counters:
Transmit Error Counter (TEC)
Receive Error Counter (REC)
Counters are incremented on errors and decremented on successful operations. The exact increments depend on the error type, but as a rule: errors increment by +8 for the transmitter and +1 for the receiver; a successful frame decrements by 1.
Based on these counters, each node operates in one of three states:
State |
Counter threshold |
Behaviour |
|---|---|---|
Error Active |
TEC < 128 and REC < 128 |
Normal operation. Sends active error flags (6 dominant bits) on error detection. |
Error Passive |
TEC β₯ 128 or REC β₯ 128 |
Still participates on the bus, but sends passive error flags (6 recessive bits) β cannot disturb other nodes. |
Bus-Off |
TEC β₯ 256 |
Node disconnects from the bus entirely β cannot transmit or receive. |
Recovery from Bus-Offο
A Bus-Off node can request recovery. The node monitors the bus for 128 occurrences of 11 consecutive recessive bits before re-entering Error Active state.
CAN Databaseο
A CAN database (commonly a .dbc file) lets you define your network in one place β node names, message identifiers, signal names, scaling, units, and more. Once designed, the database can be exported and used with tooling such as:
Loading into a CAN bus analyser to decode raw traffic with meaningful labels
Code generation for embedded targets
Tools for creating and editing CAN databases:
Kvaser Database Editor β desktop tool
CSS Electronics DBC Editor β online editor