Back

CAN_Bus

Loading views...

CAN Bus (Overview)

Overview

CAN (Controller Area Network) is a robust serial bus for multi-master systems. See also CAN FD for the modern extension. This file covers CAN 2.0A/B fundamentals as the base knowledge required for all CAN-based protocols (CANopen, J1939, OBD-II).


1. Theory & Fundamentals

  • ISO 11898 standard; Bosch original spec 1983
  • Differential bus: CANH, CANL; idle=recessive (both ~2.5 V)
  • Dominant=0: CANH≥3.5V, CANL≤1.5V (differential ≥2V)
  • Multi-master: Any node transmits when bus idle
  • Priority: Lower CAN ID wins arbitration (dominant bit wins)
  • Speed: up to 1 Mbps; distance inversely proportional to speed
  • Error detection: 5 mechanisms built in

2. Frame / Packet Structure

Standard Frame (11-bit ID, CAN 2.0A):
  SOF|ID[10:0]|RTR|IDE(0)|r0|DLC[3:0]|DATA[0–8B]|CRC[14:0]|CRCDEL|ACK|ACKDEL|EOF(7)|IFS(3)
Extended Frame (29-bit ID, CAN 2.0B):
  SOF|ID[28:18]|SRR|IDE(1)|ID[17:0]|RTR|r1|r0|DLC|DATA|CRC|ACK|EOF|IFS
Error Frame: 6 dominant + 8 recessive bits
Overload Frame: delays next frame

3. Protocol Mechanics

  • Non-destructive bitwise ARBITRATION: All transmitters compare sent vs sampled bit
  • Bit stuffing: insert opposite bit after 5 identical
  • ACK slot: all receivers pull dominant; missing ACK = error
  • Error counters: TEC (TX), REC (RX); bus-off at TEC≥256
  • Error passive: TEC/REC 128–255; error active: <128

4. Hardware Implementation

  • Transceivers: TJA1050, SN65HVD230, MCP2551
  • Termination: 120 Ω at each end; split (2×60 Ω + cap) for EMC
  • Cable: twisted pair, 120 Ω characteristic impedance
  • Stub length: <30 cm at 1 Mbps
  • MCU: STM32 bxCAN/FDCAN, NXP S32K, PIC18 ECAN

5. Register-Level / Configuration

// STM32 HAL CAN send
CAN_TxHeaderTypeDef hdr = {.StdId=0x123,.DLC=4,.IDE=CAN_ID_STD,.RTR=CAN_RTR_DATA};
uint8_t d[4]={0xDE,0xAD,0xBE,0xEF}; uint32_t mb;
HAL_CAN_AddTxMessage(&hcan1,&hdr,d,&mb);
// Receive with filter
CAN_FilterTypeDef f={.FilterBank=0,.FilterMode=CAN_FILTERMODE_IDMASK,
  .FilterScale=CAN_FILTERSCALE_32BIT,.FilterIdHigh=0,.FilterMaskIdHigh=0,
  .FilterFIFOAssignment=0,.FilterActivation=ENABLE};
HAL_CAN_ConfigFilter(&hcan1,&f);
HAL_CAN_Start(&hcan1);
HAL_CAN_ActivateNotification(&hcan1,CAN_IT_RX_FIFO0_MSG_PENDING);

6. Driver / Software Development

  • Implement init, TX, RX functions; use interrupts or DMA
  • Handle errors: timeout, CRC mismatch, arbitration loss
  • Use circular/ring buffers for high-throughput RX
  • Add retry logic and watchdog for reliability
  • Separate hardware layer from protocol logic

7. Debugging & Testing

  • Logic analyzer: decode CAN at target baud; check ID, DLC, data
  • PEAK PCAN-USB or Kvaser leaf for live bus monitoring
  • Common issues: missing termination; TEC rising = bit-timing wrong
  • CANalyzer / SavvyCAN for higher-layer decode

8. Real-World Applications

  1. Automotive ECU network (engine, ABS, airbag)
  2. CANopen industrial automation
  3. J1939 heavy vehicle
  4. Medical device networks
  5. Robotic joint control

9. Advanced Topics & Edge Cases

  • CAN FD: up to 64B payload, 8 Mbps data phase
  • ISO CAN FD: adds stuff-bit counter and CRC improvements
  • TTCAN (ISO 11898-4): time-triggered CAN scheduling
  • CAN XL: up to 2048B, 20 Mbps (draft)
  • SocketCAN: Linux kernel CAN interface

10. Standards & Variants

Feature CAN 2.0A CAN 2.0B CAN FD
ID bits 11 29 11/29
Payload 8 B 8 B 64 B
Max speed 1 Mbps 1 Mbps 8 Mbps

💡 Practical Examples

Example 1

Baud rate calc: BTR = SJW + BS1 + BS2; freq=CAN_CLK/(prescaler×(1+BS1+BS2))

Example 2

CAN filter: mask=0x7FF, id=0x123 accepts only 0x123; mask=0 accepts all

Example 3

Error recovery: monitor TEC; if >96, switch to error-passive; reset if bus-off


🧪 Practice Questions

Beginner

  1. What are dominant and recessive states?
  2. How does CAN arbitration work?
  3. What is RTR frame?
  4. What causes bus-off?
  5. What is the max payload of CAN 2.0?
  6. Intermediate

  7. Calculate bit timing for 500 kbps on 36 MHz APB.
  8. Implement CAN acceptance filter for ID range 0x200–0x2FF.
  9. Explain the 5 error detection mechanisms.
  10. How does CAN error confinement protect the network?
  11. Write a CAN TX interrupt handler.
  12. Advanced

  13. Implement full CANopen NMT state machine.
  14. Build a CAN bus load monitor with alert.
  15. Design CAN network for 10-ECU vehicle.
  16. Implement CAN to USB bridge with SocketCAN.
  17. Debug intermittent CAN errors in production.
  18. Hands-on Projects

  19. Logger: capture all CAN frames to SD at 1 Mbps.
  20. OBD reader: request and display PIDs via CAN OBD-II.
  21. CANopen node: implement heartbeat + PDO.

Checklist

  • [ ] Explain dominant/recessive and arbitration
  • [ ] Configure bit timing registers
  • [ ] Send and receive frames with HAL
  • [ ] Configure acceptance filters
  • [ ] Monitor TEC/REC error counters
  • [ ] Use logic analyzer to decode CAN
  • [ ] Handle bus-off and error recovery
  • [ ] Implement CAN message dispatcher
  • [ ] Upgrade to CAN FD
  • [ ] Build higher-layer protocol (CANopen/J1939)