Controller Area Network (CAN) Basics
Introduction
The Controller Area Network (CAN) is a robust serial communication protocol widely used in automotive, industrial, and embedded applications. Developed by Bosch in the 1980s, CAN allows microcontrollers and devices to communicate without a central host, making it ideal for real-time, high-reliability systems.
This article explores the fundamentals of CAN, including its architecture, message format, error handling, and practical applications.
1. CAN Architecture
Overview
CAN follows a multi-master, message-based communication model where multiple nodes can transmit and receive data over a shared bus.
Key Components:
- CAN Nodes: Devices or microcontrollers connected to the CAN bus.
- CAN Bus: A two-wire differential signaling bus (CAN_H, CAN_L) that reduces noise interference.
- CAN Transceiver: Converts logic-level signals to differential CAN signals and vice versa.
- CAN Controller: Manages message arbitration, error detection, and data transmission.
CAN Bus Communication Model:
- Uses a differential signal (CAN_H and CAN_L) to improve noise immunity.
- Follows the Carrier Sense Multiple Access/Collision Resolution (CSMA/CR) protocol for message arbitration.
2. CAN Message Format
Standard and Extended CAN Frames
CAN supports two frame formats:
- Standard CAN (11-bit identifier): 2¹¹ = 2048 unique message identifiers.
- Extended CAN (29-bit identifier): 2²⁹ = 536 million unique message identifiers.
Frame Structure:
- Start of Frame (SOF): Indicates the beginning of a frame.
- Identifier (ID): Unique message ID used for arbitration.
- Control Field: Contains data length (DLC – Data Length Code).
- Data Field: Contains 0 to 8 bytes of payload data.
- CRC Field: Ensures data integrity.
- ACK Field: Acknowledgment from receiving nodes.
- End of Frame (EOF): Marks the frame’s end.
Message Prioritization:
- Lower identifier values indicate higher priority messages.
- The arbitration process ensures that only one node transmits at a time.
3. CAN Communication Mechanism
Arbitration Process
- CAN uses bitwise arbitration to resolve conflicts.
- A node with a dominant bit (0) wins arbitration over a recessive bit (1).
- The lowest identifier number (highest priority) gets bus access.
Error Handling in CAN
CAN has built-in error detection and correction mechanisms:
- Bit Errors: Detects incorrect bit values during transmission.
- Stuff Errors: Ensures proper frame synchronization by inserting stuffing bits.
- CRC Errors: Validates data integrity using cyclic redundancy check (CRC).
- Acknowledgment Errors: Ensures proper frame reception.
- Form Errors: Detects incorrect format in fixed fields.
If an error is detected, the erroneous node sends an Error Frame and retransmits the message.
4. CAN Bus Implementation
Hardware Requirements
- Microcontroller with CAN Controller: STM32, ESP32, ATmega32M1, etc.
- CAN Transceiver: MCP2551, SN65HVD230, TJA1050.
- Termination Resistors (120Ω): Placed at both ends of the CAN bus to prevent signal reflection.
CAN Communication Example (C – STM32 HAL)
CAN_HandleTypeDef hcan;
CAN_TxHeaderTypeDef TxHeader;
CAN_RxHeaderTypeDef RxHeader;
uint8_t TxData[8];
uint8_t RxData[8];
uint32_t TxMailbox;
void CAN_Init() {
hcan.Instance = CAN1;
hcan.Init.Prescaler = 16;
hcan.Init.Mode = CAN_MODE_NORMAL;
hcan.Init.SyncJumpWidth = CAN_SJW_1TQ;
hcan.Init.TimeSeg1 = CAN_BS1_8TQ;
hcan.Init.TimeSeg2 = CAN_BS2_1TQ;
HAL_CAN_Init(&hcan);
}
void CAN_SendMessage() {
TxHeader.StdId = 0x123;
TxHeader.DLC = 2;
TxData[0] = 0xAB;
TxData[1] = 0xCD;
HAL_CAN_AddTxMessage(&hcan, &TxHeader, TxData, &TxMailbox);
}
void CAN_ReceiveMessage() {
if (HAL_CAN_GetRxFifoFillLevel(&hcan, CAN_RX_FIFO0) > 0) {
HAL_CAN_GetRxMessage(&hcan, CAN_RX_FIFO0, &RxHeader, RxData);
}
}
5. CAN Variants and Extensions
CAN FD (Flexible Data Rate)
- Increases data payload up to 64 bytes.
- Supports variable bit rates for faster communication.
CANopen & J1939 Protocols
- CANopen: Industrial automation applications.
- J1939: Heavy-duty vehicle and automotive applications.
6. CAN Applications
- Automotive Systems: Engine control units (ECUs), airbag systems, ABS.
- Industrial Automation: Factory automation, motor control, robotic systems.
- Medical Devices: Patient monitoring systems, diagnostic equipment.
- Aerospace & Defense: Avionics communication, unmanned systems.
Conclusion
CAN is a powerful, reliable, and efficient communication protocol widely used in automotive and industrial applications. Its ability to handle multiple nodes, error detection mechanisms, and robust performance make it a preferred choice for real-time embedded systems.