can - dwilson2547/wiki_demo GitHub Wiki

CAN-Bus and CAN Network Communication

Controller Area Network (CAN) is a robust vehicle bus standard designed to allow microcontrollers and devices to communicate with each other without a host computer. Originally developed by Bosch for automotive applications, CAN-Bus has become a widely adopted communication protocol in automotive, industrial, and embedded systems due to its reliability, real-time performance, and fault tolerance.

Overview

CAN-Bus features:

  • Multi-master, multi-drop serial communication protocol
  • Message-based communication (not address-based)
  • Built-in error detection and fault confinement
  • Real-time capabilities with deterministic latency
  • High noise immunity and fault tolerance
  • Automatic retransmission of corrupted messages
  • Priority-based message arbitration
  • Operating speeds from 10 kbps to 1 Mbps
  • Network length up to 1000m at lower speeds
  • Support for up to 110 nodes (practical limit ~64 nodes)

CAN-Bus Physical Layer

Bus Topology

CAN networks use a linear bus topology with termination resistors at both ends:

    ECU 1         ECU 2         ECU 3         ECU 4
     β”‚             β”‚             β”‚             β”‚
     β”‚             β”‚             β”‚             β”‚
 ────┴─────────────┴─────────────┴─────────────┴────
CAN_H ═══════════════════════════════════════════════
                                                    
CAN_L ═══════════════════════════════════════════════
 ────┬─────────────┬─────────────┬─────────────┬────
     β”‚             β”‚             β”‚             β”‚
   [120Ξ©]                                    [120Ξ©]
Termination                              Termination
 Resistor                                 Resistor

Signal Levels and States

Differential Signaling

Bus State CAN_H Voltage CAN_L Voltage Differential Voltage Binary Value
Recessive ~2.5V ~2.5V ~0V Logic 1
Dominant ~3.5V ~1.5V ~2V Logic 0

Voltage Specifications (ISO 11898)

Parameter Minimum Typical Maximum Unit
Supply Voltage 4.5 5.0 5.5 V
Recessive CAN_H 2.0 2.5 3.0 V
Recessive CAN_L 2.0 2.5 3.0 V
Dominant CAN_H 2.75 3.5 4.5 V
Dominant CAN_L 0.5 1.5 2.25 V
Common Mode Voltage -2.0 2.5 7.0 V

Termination

  • 120Ξ© termination resistors required at both ends of the bus
  • Total bus resistance: 60Ξ© (two 120Ξ© resistors in parallel)
  • Stub lengths: Maximum 0.3m per node connection
  • Unterminated network: Results in signal reflections and errors

CAN Frame Format

Standard CAN Frame (CAN 2.0A) - 11-bit Identifier

SOF  Identifier  RTR IDE r0  DLC    Data Field     CRC    CRC  ACK ACK EOF  IFS
 β”‚   ←─11 bits─→  β”‚   β”‚   β”‚  ←4bitsβ†’ ←─0 to 64 bits─→ ←15bitsβ†’ DEL β”‚  DEL ←7bitsβ†’
 β”‚               β”‚   β”‚   β”‚          β”‚                      β”‚   β”‚   β”‚
 1      ID        0   0   0       Data                   CRC  0   1   1111111
bit   (0-2047)   bit bit bit    (0-8 bytes)            Field bit bit  bits

Field Breakdown:
SOF    = Start of Frame (1 bit dominant)
ID     = Message Identifier (11 bits)
RTR    = Remote Transmission Request (0=data, 1=remote)
IDE    = Identifier Extension (0=standard, 1=extended)
r0     = Reserved bit (must be 0)
DLC    = Data Length Code (4 bits, 0-8 bytes)
Data   = Data payload (0-64 bits)
CRC    = Cyclic Redundancy Check (15 bits)
CRC_D  = CRC Delimiter (1 bit recessive)
ACK    = Acknowledge slot (1 bit)
ACK_D  = ACK Delimiter (1 bit recessive)
EOF    = End of Frame (7 bits recessive)
IFS    = Inter Frame Space (3 bits minimum)

Extended CAN Frame (CAN 2.0B) - 29-bit Identifier

SOF  Base ID  SRR IDE  Extended ID   RTR r1 r0 DLC   Data Field    CRC   CRC ACK ACK EOF
 β”‚  ←11bitsβ†’  β”‚   β”‚   ←──18 bits───→  β”‚   β”‚  β”‚ ←4bβ†’ ←0 to 64 bitsβ†’ ←15bβ†’ DEL β”‚  DEL ←7bβ†’
 1     ID     1   1       ID         0   0  0        Data         CRC   0   1   1  1111111

Extended Frame Additional Fields:
SRR    = Substitute Remote Request (1 bit recessive)
IDE    = Identifier Extension (1=extended frame)
Ext_ID = Extended Identifier (18 bits)
r1     = Reserved bit (must be 0)

Message Types

Data Frame

  • Purpose: Transmit data from sender to receivers
  • RTR bit: 0 (dominant)
  • Data field: 0-8 bytes of payload data
  • Most common: Regular communication messages

Remote Frame

  • Purpose: Request data from specific node
  • RTR bit: 1 (recessive)
  • Data field: Empty (DLC indicates requested data length)
  • Response: Node responds with corresponding data frame

Error Frame

  • Purpose: Signal detection of bus errors
  • Structure: Error flag (6 bits) + Error delimiter (8 bits)
  • Transmission: Any node detecting error
  • Effect: Destroys current message, forces retransmission

Overload Frame

  • Purpose: Provide extra delay between frames
  • Structure: Similar to error frame
  • Usage: Node needs more time to process messages

Message Arbitration and Priority

Priority System

  • Lower identifier = Higher priority
  • Bitwise arbitration: Dominant bits (0) win over recessive bits (1)
  • Non-destructive: Losing nodes automatically become receivers

Arbitration Process

Time    Node A (ID=0x123)  Node B (ID=0x124)  Node C (ID=0x200)  Bus State
t0      Starts transmission Starts transmission Starts transmission  
t1      Sends 0 (dominant)  Sends 0 (dominant)  Sends 0 (dominant)   0
t2      Sends 0 (dominant)  Sends 0 (dominant)  Sends 0 (dominant)   0
t3      Sends 0 (dominant)  Sends 0 (dominant)  Sends 1 (recessive)  0
t3+     Continues          Continues          Stops (lost arbit.)    
t4      Sends 1 (recessive) Sends 0 (dominant) Silent               0
t4+     Stops (lost arbit.) Continues          Silent               
Result: Node B wins arbitration and transmits message

Explanation:
- All nodes start simultaneously
- Node C loses at bit 3 (sends 1, but bus shows 0)
- Node A loses at bit 4 (sends 1, but Node B sends 0)
- Node B has lowest ID, wins arbitration

CAN Protocol Layers

Physical Layer (ISO 11898-2)

  • Cable specifications: Twisted pair, shielded
  • Connector types: DB9, OBD-II, proprietary
  • Termination: 120Ξ© at both ends
  • Maximum nodes: 110 theoretical, 64 practical

Data Link Layer (ISO 11898-1)

  • Frame formatting: Standard and extended frames
  • Error detection: CRC, bit monitoring, frame check
  • Error signaling: Error and overload frames
  • Fault confinement: Error counters and states

Error Detection and Fault Tolerance

Error Types

Bit Error

  • Detection: Node monitors its own transmission
  • Condition: Transmitted bit differs from bus bit
  • Exception: Arbitration and acknowledge phases
  • Action: Transmit error frame

Stuff Error

  • Detection: More than 5 consecutive identical bits
  • Bit stuffing rule: Insert complementary bit after 5 identical bits
  • Purpose: Maintains synchronization
  • Action: Receiver sends error frame

CRC Error

  • Detection: Received CRC doesn't match calculated CRC
  • Coverage: SOF to end of data field
  • Polynomial: x^15 + x^14 + x^10 + x^8 + x^7 + x^4 + x^3 + 1
  • Action: Receiver sends error frame

Form Error

  • Detection: Invalid frame format
  • Examples: Wrong delimiter bits, invalid reserved bits
  • Action: Error frame transmission

Acknowledgment Error

  • Detection: No acknowledgment received
  • Condition: No node acknowledges valid frame
  • Cause: All receivers detected errors
  • Action: Transmitter sends error frame

Fault Confinement States

Error Active State

  • Normal operation: Can transmit and receive normally
  • Error counters: Both TEC and REC ≀ 127
  • Error frames: Transmits active error frames (6 dominant bits)

Error Passive State

  • Restricted operation: Can receive normally, limited transmission
  • Error counters: TEC or REC between 128-255
  • Error frames: Transmits passive error frames (6 recessive bits)
  • Transmission: Must wait 8 bit times before retransmission

Bus Off State

  • No communication: Cannot transmit or receive
  • Error counter: TEC β‰₯ 256
  • Recovery: Automatic after 128 occurrences of 11 recessive bits
  • Manual intervention: May require application reset

Error Counter Rules

Transmit Error Counter (TEC) Rules:
- Increment by 8 for each error during transmission
- Increment by 1 for each error during reception
- Decrement by 1 for each successful transmission
- Set to 0 when bus off recovery completes

Receive Error Counter (REC) Rules:  
- Increment by 1 for each error during reception
- Increment by 8 if node transmits error frame during reception
- Decrement by 1 for each successful reception (if >127)
- Stays at 0-127 range during error active state

CAN Baud Rates and Timing

Standard Baud Rates

Baud Rate Max Bus Length Typical Applications
1 Mbps 25m High-speed automotive networks
500 kbps 100m Automotive powertrain, chassis
250 kbps 250m Industrial automation
125 kbps 500m Body electronics, comfort systems
100 kbps 600m Industrial networks
50 kbps 1000m Long-distance industrial
20 kbps 2500m Building automation
10 kbps 5000m Very long distance applications

Bit Timing Parameters

CAN Bit Time Structure:
 
  Sync_Seg   Prop_Seg    Phase_Seg1    Phase_Seg2
     β”‚           β”‚            β”‚            β”‚
     │←─1TQ─→│←─1-8TQ─→│←─1-8TQ─→│←─2-8TQ─→│
     β”‚       β”‚         β”‚         β”‚         β”‚
   Sync    Propagation  Phase     Phase
 Segment    Delay      Buffer1   Buffer2
     β”‚                     β”‚         β”‚
     └─────Sample Pointβ”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Parameters:
- Sync_Seg: 1 Time Quantum (TQ) - synchronization
- Prop_Seg: 1-8 TQ - signal propagation delay
- Phase_Seg1: 1-8 TQ - phase buffer before sample point
- Phase_Seg2: 2-8 TQ - phase buffer after sample point
- Sample Point: Typically at 87.5% of bit time
- Total Bit Time: Sync_Seg + Prop_Seg + Phase_Seg1 + Phase_Seg2

Timing Configuration Example (500 kbps)

System Clock: 16 MHz
Desired Baud Rate: 500 kbps
Bit Time: 2 ΞΌs (1/500k)

Time Quantum (TQ) = System Clock / (BRP Γ— (1 + Prop_Seg + Phase_Seg1 + Phase_Seg2))

Example Configuration:
BRP (Baud Rate Prescaler): 2
Sync_Seg: 1 TQ
Prop_Seg: 2 TQ  
Phase_Seg1: 3 TQ
Phase_Seg2: 2 TQ
Total TQ: 8

TQ = 16MHz / (2 Γ— 8) = 1MHz β†’ 1ΞΌs per TQ
Bit Time = 8 Γ— 1ΞΌs = 8ΞΌs? No, this gives 125kbps

Correct Calculation:
BRP = 4, Total TQ = 8
TQ = 16MHz / 4 = 4MHz β†’ 0.25ΞΌs per TQ  
Bit Time = 8 Γ— 0.25ΞΌs = 2ΞΌs = 500kbps βœ“

CAN Network Topologies and Components

Network Architecture

Linear Bus (Most Common)

[Node1]─┬─────────[Node2]─┬─────────[Node3]─┬─────────[Node4]
        β”‚                 β”‚                 β”‚               
      [120Ξ©]             β”‚                 β”‚             [120Ξ©]
   Termination           β”‚                 β”‚          Termination
                         β”‚                 β”‚
                    [Node5]           [Node6]
                    
Maximum stub length: 0.3m per connection
Total bus length: Depends on baud rate

Star Topology (Using CAN Hubs)

                    [Node1]
                       β”‚
                       β”‚
[Node4]────────[CAN Hub]────────[Node2]  
                       β”‚
                       β”‚
                    [Node3]
                    
Advantages: Centralized control, easier diagnostics
Disadvantages: Single point of failure, more complex

CAN Transceiver ICs

Popular CAN Transceivers

Part Number Manufacturer Speed Features
MCP2551 Microchip 1 Mbps Industry standard
TJA1050 NXP 1 Mbps Automotive qualified
SN65HVD230 Texas Instruments 1 Mbps 3.3V operation
ISO1050 Texas Instruments 1 Mbps Galvanic isolation
MCP2562 Microchip 1 Mbps Fault protection

Transceiver Pin Configuration (MCP2551 Example)

MCP2551 Pinout (8-pin DIP/SOIC):
     β”Œβ”€β”€β”€β”€β”€β”
TxD  β”‚1   8β”‚  VDD (+5V)
VSS  β”‚2   7β”‚  CANH
VRef β”‚3   6β”‚  CANL  
RxD  β”‚4   5β”‚  Rs
     β””β”€β”€β”€β”€β”€β”˜

Pin Functions:
1. TxD: Transmit Data Input (from microcontroller)
2. VSS: Ground
3. VRef: Reference voltage output (VDD/2)
4. RxD: Receive Data Output (to microcontroller)
5. Rs: Slope control (normal/standby mode)
6. CANL: CAN Low bus line
7. CANH: CAN High bus line  
8. VDD: Power supply (+5V)

Microcontroller CAN Controllers

Built-in CAN Controllers

Microcontroller CAN Controllers Max Speed Additional Features
STM32F4xx 2 Γ— CAN 2.0B 1 Mbps Advanced filtering
PIC18F2580 1 Γ— CAN 2.0B 1 Mbps Multiple buffers
Arduino MKR CAN 1 Γ— CAN 2.0B 1 Mbps Shield format
ESP32 1 Γ— CAN 2.0B 1 Mbps Built-in transceiver
Raspberry Pi None - Requires CAN HAT

External CAN Controller ICs

Part Number Interface Speed Buffers Features
MCP2515 SPI 1 Mbps 3 TX, 2 RX Popular choice
MCP25625 SPI 1 Mbps 3 TX, 2 RX Integrated transceiver
SJA1000 Parallel 1 Mbps 1 TX, 1 RX Industrial standard

Higher Layer Protocols

CANopen

  • Application layer: Standardized device profiles
  • Object Dictionary: Structured data access
  • PDO: Process Data Objects for real-time data
  • SDO: Service Data Objects for configuration
  • Network Management: Node guarding, heartbeat
  • Applications: Industrial automation, robotics

DeviceNet

  • Based on: CAN with DeviceNet application layer
  • Power and data: Single cable solution
  • Device profiles: Standardized device behavior
  • Configuration: Network configuration tools
  • Applications: Factory automation, discrete manufacturing

J1939

  • Automotive standard: Heavy duty vehicles
  • 29-bit identifiers: Extended CAN frames
  • Parameter Groups: Standardized data definitions
  • Transport protocol: Multi-packet messages
  • Applications: Trucks, buses, agricultural equipment

OBD-II (ISO 15765)

  • Diagnostic protocol: Vehicle diagnostics
  • UDS support: Unified Diagnostic Services
  • Flow control: Multi-frame message handling
  • Standardized PIDs: Parameter identification
  • Applications: Automotive diagnostics, emissions testing

CAN-FD (CAN with Flexible Data-Rate)

CAN-FD Enhancements

  • Increased payload: Up to 64 bytes per frame (vs 8 bytes)
  • Variable data rate: Faster transmission in data phase
  • Improved CRC: Enhanced error detection
  • Backward compatibility: Coexists with classic CAN

CAN-FD Frame Format

Arbitration Phase (Classic CAN speed):
SOFβ”‚IDβ”‚RTRβ”‚IDEβ”‚FDFβ”‚r0β”‚BRSβ”‚ESIβ”‚DLCβ”‚

Data Phase (Higher speed):
       Data Field (up to 64 bytes)

CRC Phase (Classic CAN speed):
CRCβ”‚DELβ”‚ACKβ”‚DELβ”‚EOF

New Bits:
FDF = FD Format (1 = CAN-FD frame)
BRS = Bit Rate Switch (1 = switch to fast data rate)  
ESI = Error State Indicator

CAN-FD Data Length Codes

DLC Data Bytes DLC Data Bytes
0-8 0-8 12 24
9 12 13 32
10 16 14 48
11 20 15 64

Network Design Guidelines

Cable Specifications

Recommended Cable Types

Application Cable Type Impedance Capacity Max Length
Automotive ISO 11898-2 120Ξ© Β± 5% <100 pF/m Vehicle specific
Industrial DeviceNet Thick 120Ξ© Β± 5% <60 pF/m 500m @ 125kbps
Building Twisted pair 120Ξ© Β± 5% <100 pF/m Application specific

Cable Construction

  • Twisted pair: Reduces electromagnetic interference
  • Shielded: Additional EMI protection in noisy environments
  • Characteristic impedance: 120Ξ© Β± 5%
  • Capacitance: <100 pF/m between conductors
  • Wire gauge: 18-24 AWG typical

Network Topology Rules

Bus Length Limits

  • Propagation delay: Must be <2 bit times total
  • Signal quality: Maintain rise/fall times
  • Reflections: Proper termination essential
  • Stub lengths: Maximum 0.3m per connection

Node Count Limitations

  • Theoretical maximum: 110 nodes (address space)
  • Practical maximum: 64 nodes (electrical loading)
  • Bus loading: Each node adds capacitance
  • Current consumption: Termination and transceiver limits

Grounding and EMI

Grounding Strategy

Proper CAN Network Grounding:

Power Supply Ground ──┬── Node 1 Ground
                      β”œβ”€β”€ Node 2 Ground  
                      β”œβ”€β”€ Node 3 Ground
                      └── Shield Ground (one point only)

Guidelines:
- Single point grounding preferred
- Avoid ground loops
- Shield connection at one end only
- Separate digital and analog grounds in nodes

EMI Mitigation

  • Twisted pair cables: Reduces radiated emissions
  • Shielding: Prevents interference pickup
  • Ferrite cores: Suppress common-mode noise
  • Proper PCB layout: Ground planes, trace routing
  • Isolation: Galvanic isolation in harsh environments

Programming and Implementation

Arduino CAN Library Example

#include <SPI.h>
#include <mcp2515.h>

MCP2515 mcp2515(10); // CS pin 10

struct can_frame canMsg;

void setup() {
  Serial.begin(9600);
  mcp2515.reset();
  mcp2515.setBitrate(CAN_500KBPS, MCP_8MHZ);
  mcp2515.setNormalMode();
  Serial.println("CAN BUS Shield init ok!");
}

void loop() {
  // Send CAN message
  canMsg.can_id = 0x123;
  canMsg.can_dlc = 8;
  canMsg.data[0] = 0x01;
  canMsg.data[1] = 0x02;
  canMsg.data[2] = 0x03;
  canMsg.data[3] = 0x04;
  canMsg.data[4] = 0x05;
  canMsg.data[5] = 0x06;
  canMsg.data[6] = 0x07;
  canMsg.data[7] = 0x08;
  
  mcp2515.sendMessage(&canMsg);
  delay(1000);
  
  // Receive CAN message
  if (mcp2515.readMessage(&canMsg) == MCP2515::ERROR_OK) {
    Serial.print("ID: 0x");
    Serial.print(canMsg.can_id, HEX);
    Serial.print(" Data: ");
    for (int i = 0; i < canMsg.can_dlc; i++) {
      Serial.print("0x");
      Serial.print(canMsg.data[i], HEX);
      Serial.print(" ");
    }
    Serial.println();
  }
}

Message Filter Configuration

// Set filters to accept only specific message IDs
void setCANFilters() {
  // Filter 0: Accept ID 0x123
  mcp2515.setFilter(RXF0, false, 0x123);
  
  // Filter 1: Accept ID range 0x200-0x20F  
  mcp2515.setFilter(RXF1, false, 0x200);
  mcp2515.setFilterMask(MASK0, false, 0xFF0);
  
  // Enable interrupt on message reception
  mcp2515.setConfigMode();
  mcp2515.setCanInterrupt(MCP2515::CANINTF_RX0IF | MCP2515::CANINTF_RX1IF);
  mcp2515.setNormalMode();
}

Testing and Debugging

Common Diagnostic Tools

Hardware Tools

Tool Function Features
CAN Analyzer Bus monitoring, message analysis Real-time capture, protocol decode
Oscilloscope Signal analysis Voltage levels, timing, eye diagrams
Bus Load Generator Network stress testing Configurable message rates
Termination Tester Verify network termination Resistance measurement

Software Tools

Software Platform Capabilities
CANoe Windows Network simulation, testing
Wireshark Multi-platform Protocol analysis
BusMaster Windows Open source CAN tool
SocketCAN Linux Native CAN support

Troubleshooting Guide

Bus Communication Issues

Symptom Possible Causes Diagnostic Steps
No communication Termination, wiring, baud rate Check termination resistance (60Ξ©)
High error rate Noise, timing, signal quality Oscilloscope analysis of bus signals
Intermittent errors Loose connections, EMI Physical inspection, EMI testing
Bus-off condition Faulty node, excessive errors Isolate nodes one by one

Signal Quality Checks

CAN Signal Quality Measurements:

Differential Voltage:
- Recessive state: 0V Β± 0.5V
- Dominant state: 2.0V Β± 0.5V

Common Mode Voltage:
- Both states: 2.5V Β± 1.0V

Rise/Fall Times:
- 10%-90%: <100ns typical
- Slew rate: >1.5 V/ΞΌs

Bus Loading:
- Total capacitance: <400 pF
- DC resistance: 60Ξ© Β± 5%

Safety and Functional Safety

ISO 26262 Compliance (Automotive)

  • ASIL ratings: Safety Integrity Levels A-D
  • Fault detection: Error monitoring and reporting
  • Fail-safe behavior: Defined responses to faults
  • Redundancy: Backup communication paths
  • Validation: Extensive testing requirements

Industrial Safety (IEC 61508)

  • SIL ratings: Safety Integrity Levels 1-4
  • Black channel: CAN as transmission medium only
  • Safety protocols: Application-layer safety mechanisms
  • Diverse systems: Multiple independent channels

Applications and Use Cases

Automotive Applications

  • Powertrain: Engine, transmission control
  • Chassis: ABS, steering, suspension
  • Body: Lighting, HVAC, door controls
  • Infotainment: Audio, navigation, connectivity
  • Diagnostics: OBD-II, manufacturer diagnostics

Industrial Applications

  • Factory automation: PLC communication
  • Process control: Sensor networks
  • Building automation: HVAC, security systems
  • Medical devices: Equipment networking
  • Marine systems: Engine, navigation networks

Emerging Applications

  • IoT gateways: Industrial IoT connectivity
  • Robotics: Distributed control systems
  • Energy systems: Solar, battery management
  • Transportation: Rail, aviation systems
⚠️ **GitHub.com Fallback** ⚠️