Controller Area Network (CAN)
CAN is a communication protocol widely used in the automotive industry because of its robust nature and ability to link many electronic control units (ECUs) together. In our car, each of our boards are connected to the CAN bus and is the primary way the car transmits messages.
Physical Bus:
CAN is a multi-master bus, meaning that each ECU connected to the bus has the ability to drive1 the bus. The bus is made up of a twisted pair of wires, CAN high (CANH) and CAN low (CANL), creating a differential signal. A differential signal simply means that instead of viewing the wire independently, the signal is interpreted using the difference in voltage between the two wires. These two wires can be driven as a pair into two states, dominant or recessive.
When nothing is driving the lines, a CAN bus idles in the recessive state, interpreted as a binary 1. A dominant bit is a binary 0.
The below images show some examples of the state of the bus, as well as the binary interpretation of them.
Hardware:
To use CAN, a CAN controller and CAN transceiver is needed, and turn the differential CAN into a tx and rx interface. The twisted pair introduced earlier talks to the transceiver, and the transceiver talks to the controller, which is then what our MCU interfaces with.
Note on the diagram the 120 ohm resistors terminating the CAN network. These are aptly named termination resistors, and are used to mitigate reflections.
CAN Controllers:
CAN controllers are the digital interface between the microprocessor and the physical CAN bus. They handle the core protocol functions of CAN communication. Key aspects of CAN controllers include:
Message Management: Controllers handle message framing, error detection, and acknowledgment. They assemble outgoing messages and parse incoming ones according to the CAN protocol.
Buffering: Most controllers have multiple message buffers for transmitting and receiving, allowing for efficient handling of message traffic without constant CPU intervention.
Filtering: Controllers often implement hardware-level message filtering, allowing the system to efficiently process only relevant messages.
Interrupt Generation: They can generate interrupts to notify the CPU of important events like message reception, transmission completion, or error conditions.
Error Handling: CAN controllers manage error detection, signaling, and recovery mechanisms as defined by the CAN protocol.
CAN Transceivers:
CAN transceivers act as the analog interface between the digital CAN controller and the physical CAN bus. They are responsible for the electrical characteristics of CAN communication. Key features of CAN transceivers include:
Signal Conversion: Transceivers convert the digital signals from the controller into differential signals for transmission on the CAN bus, and vice versa for reception.
Electrical Protection: They provide protection against electrical hazards like electrostatic discharge (ESD) and voltage spikes, enhancing the robustness of the CAN node.
Slew Rate Control: Transceivers control the slew rate of signals to reduce electromagnetic interference (EMI) and ensure signal integrity over long distances.
Bus Termination: Some transceivers include built-in termination resistors, simplifying network design and reducing component count.
Low-Power Modes: Many modern transceivers offer low-power or sleep modes to reduce energy consumption when the node is inactive.
Data Transmission:
CAN data is transmitted in frames. A frame is made up of the following pieces. There’s a few more below which are less important, but further reading is encouraged.
Start of Frame (SOF) marker
Arbitration ID
Control
Data
CRC
End of Frame (EOF) marker
SOF & EOF
The SOF and EOF markers are quite straightforward. The SOF is a single dominant bit (0) which marks the start of a frame. The EOF is 7 recessive bits (1s) that mark the end of the frame. This allows the bus to return to the idle state (observe that no other time will we allow 7 recessive bits within a frame).
Arbitration ID
The arbitration ID is the message identifier which determines the message priority and is used for filtering. More on priority and filtering below. In standard CAN, it’s an 11 bit wide field, however there’s also extended CAN which allows up to a 29 bit wide ID.
Control
This is a 6 bit field that includes the Data Length Code (DLC) and two reserved bits. The DLC indicates the number of bytes in the data field from 0 - 8.
Data
This is the main payload of the message. It contains up to 8 bytes, or 64 bits, of data. The Data block is all the useful information in a message, and is how our car transmits all our information such as faults, statuses, throttle readings, and much more.
CRC
A Cyclic Redundancy Check can be seen in lots of places outside of CAN as well, and is used to ensure data integrity by allowing the receiver to check for transmission errors. A CRC is calculated upon the message, and if the calculated CRC doesn’t match the one that was sent, and error must be present.
Priority:
Priority in CAN is determined by the message identifier in the arbitration field. Lower numerical values of the identifier represent higher priority. This system allows for efficient bus arbitration when multiple nodes attempt to transmit simultaneously. Here's how it works:
When multiple nodes start transmitting at the same time, they compare their message identifiers bit by bit.
The node with a dominant bit (logical 0) will continue transmitting, while nodes with a recessive bit (logical 1) will back off.
This process continues until only one node remains, ensuring the highest priority message is transmitted first.
Lower identifier values win arbitration, so they have higher priority on the bus.
Message Filtering:
Filtering allows CAN nodes to selectively receive only the messages they need, reducing processing overhead. There are two main types of filtering:
Hardware Filtering:
Implemented at the CAN controller level.
Uses acceptance filters to compare incoming message IDs against pre-configured filter values.
Messages that don't match the filter criteria are discarded without interrupting the CPU.
Allows for efficient processing of relevant messages only.
Software Filtering:
Implemented in the application software after messages pass hardware filters.
Provides more flexibility for complex filtering logic.
Can filter based on message content in addition to IDs.
May introduce more processing overhead compared to hardware filtering.
Filtering is crucial in CAN networks because:
It reduces the processing load on individual nodes.
It allows nodes to focus on relevant messages, improving system efficiency.
It enables the implementation of complex communication patterns in distributed systems.
Footnotes:
“Driving” a bus means taking control of the voltages being set on the physical hardware which makes up the bus. In the case of CAN, these are the CANH and CANL wires.