Introduction or: What is CAN? Why CAN? CAN Protocol CAN higher Layer Protocols CAN Applications Atmel CAN Microcontrollers Family CAN Registers Details Conclusion
CAN Tutorial
16/03/2004
2
CAN Tutorial Introduction
®
y The CAN is an ISO standard (ISO 11898) for serial communication y The protocol was developed 1980 by BOSCH for automotive applications y Today CAN has gained widespread use: ¾ Industrial Automation ¾ Automotive, …etc.
y The CAN standard includes: ¾ Physical layer ¾ Data-link layer 9 Some message types 9 Arbitration rules for bus access 9 Methods for fault detection and fault confinement
*
Introduction
CAN Tutorial
16/03/2004
3
CAN Tutorial Why CAN?
®
y Mature Standard ¾ CAN protocol more than 14 years ¾ Numerous CAN products and tools on the market
y Hardware implementation of the protocol ¾ Combination of error handling and fault confinement with high transmission speed
y Simple Transmission Medium ¾ Twisted pair of wires is the standard, but also just one wire will work ¾ Other links works, too: Opto - or radio links
y Excellent Error Handling ¾ CRC error detection mechanism
y Fault Confinement ¾ Built-in feature to prevent faulty node to block system
y Most used protocol in industrial and automotive world y Best Performance / Price ratio
Why CAN?
CAN Tutorial
16/03/2004
4
CAN Tutorial CAN Protocol
®
What is CAN?
Frame Formats (1)
ISO-OSI Reference Model
Frame Formats (2)
CAN Bus Logic
Frame Formats (3)
Typical CAN Node
Frame Formats (4)
CAN Bus Access and Arbitration
Fault Confinement (1)
CAN Bit Coding & Bit Stuffing
Undetected Errors
Fault Confinement (2)
CAN Bus Synchronization CAN Bit Construction Relation between Baud Rate and Bus Length
CAN Protocol
CAN Tutorial
16/03/2004
5
CAN Tutorial What is CAN?
®
y Controller Area Network ¾ Invented by Robert Bosch GmbH ¾ Asynchronous Serial Bus ¾ Absence of node addressing 9 Message identifier specifies contents and priority 9 Lowest message identifier has highest priority
¾ Non-destructive arbitration system by CSMA with collision detection ¾ Multi-master / Broadcasting concept ¾ Sophisticated error detection & handling system ¾ Industrial and Automotive Applications
CAN Protocol
CAN Tutorial
16/03/2004
6
CAN Tutorial ISO-OSI* Reference Model
®
7. Application Layer
HLPs: CANopen, DeviceNet, OSEK/V**
6. Presentation Layer Partially implemented by Higher Layer Protocols (HLP)
5. Session Layer 4. Transport Layer 3. Network Layer 2. Data Link Layer
CAN Protocol
1. Physical Layer *) OSI - Open System Interconnection
CAN Protocol
CAN Tutorial
16/03/2004
7
CAN Tutorial CAN Bus Logic
®
“1” = recessive
Two logic states on the CAN bus
“0” = dominant
Node A
Node B
Node C
D D D D R R R R
D D R R D D R R
D R D R D R D R
CAN Protocol
BUS D D D D D D D R
“Wired-AND” function: as soon as one node transmit a dominant bit (zero) the bus is in the dominant state
Only if all nodes transmit recessive bits (ones) the Bus is in the recessive state
CAN Tutorial
16/03/2004
8
CAN Tutorial Typical CAN Node
®
T89C51CC01/02
I/O
µController
CAN Controller
TxD
RxD
Diff. CAN Line Driver
CAN_H
CAN_L
CAN Bus (terminated by 120 Ohm on each side)
CAN Protocol
CAN Tutorial
16/03/2004
9
CAN Tutorial ®
TXd TXd CAN Controller
CANh
RXd
CAN Transceiver
CANl
Dominant CANh
Recessive
CANl Dominant RXd
CAN Bus (up to 40m @1Mb/s, up to 1km @50Kb/s) Stub Device #1
CAN Protocol
Device #2
Device #3
Device #n
CAN Tutorial
16/03/2004
10
CAN Tutorial ®
CAN Bus Access and Arbitration: CSMA/CD and AMP *) Start of Frame Arbitration Field Node 1: TxD Node 2: TxD Node 3: TxD CAN Bus
Node 2 loses Arbitration
Node 3 loses Arbitration
*)Carrier Sense Multiple Access/Collision Detection and Arbitration by Message Priority
CAN Protocol
CAN Tutorial
16/03/2004
11
CAN Tutorial CAN Bit Coding & Bit Staffing
®
y Bit Coding : NRZ (Non-Return-To-Zero code) does not ensure enough edges for synchronization y Stuff Bits are inserted after 5 consecutive bits of the same level y Stuff Bits have the inverse level of the previous bit. y No deterministic encoding, frame length depends on transmitted data
All nodes synchronize on leading edge of SOF bit (Hard Synchronization)
y Re-Synchronization on each Recessive to Dominant bit
Resynch
CAN Protocol
Resynch
Resynch CAN Tutorial
16/03/2004
13
CAN Tutorial CAN Bit Construction
®
y Length of one time quanta can be set to multiple of µController clock y 1 Time quantum = 1 period of CAN Controller base clock y Number of time quanta in Propag and Phase segments is programmable
1 Bit Time 8 to 25 Time Quanta R
NRZ Signal D
Phases
Sync Seg 1 tq
Propag Seg 1 to 8 tq
Phase Seg 1
Phase Seg 2
1 to 8 tq
1 to 8 tq
1 to16 tq
Sample Point
CAN Protocol
CAN Tutorial
16/03/2004
14
CAN Tutorial Relation between Baud Rate and Bus Length
®
Bit Rate [kbps]
1000 500 200 100 50 20 10 5 0
1O
40
100
200
1000
10000
CAN Bus Length [m] Example based on CAN Bus Lines by twisted pair
CAN Protocol
CAN Tutorial
16/03/2004
15
CAN Tutorial Frame Formats (1)
®
11
1
1
1
11
1
SOF Start of Frame CRC Cyclic Redundancy Code del Delimiter ACK Acknowledge
CAN Protocol
EOF IFS ID IDE
IDE
Duration in Data Bit
ID[28..18]
SRR
CAN - V2.0B
SOF
ARBITRATION
DLC
1 CTRL
15
1
1
1
7
IFS ≥3
4
ID[17..0]
1 18 ARBITRATION
End of Frame Inter Frame Spacing Identifier Identifier Extension
y If any of the CAN nodes detects a violation of the frame format y or a stuff error, it immediately sends an Error Frame
Data Frame
Error Frame Duration in Data Bit
Superposition of Error Flag
Error Delimiter
IFS or Overload Frame
Error Flag
6
6
8
Error Frame
CAN Protocol
CAN Tutorial
16/03/2004
19
CAN Tutorial Frame Formats (4)
®
y If any of the CAN nodes suffers from a “data over flow”, it might send y up two consecutive Overload Frames to delay the network
EOF or Error delimiter or Overload delimiter
Overload Frame Duration in Data Bit
Superposition of Overload Flag
Overload Delimiter
IFS or Overload Frame
Overload Flag
6
6
8
Overload Frame
CAN Protocol
CAN Tutorial
16/03/2004
20
CAN Tutorial Fault Confinement (1)
®
y Three fundamental states define each node’s error signaling ¾ Error active: Normal state, node can send all frames incl.error frames ¾ Error passive: Node can send all frames excluding error frames ¾ Bus off: Node is isolated from bus
y Internal error counts determine the state ¾ Transmit error counter (TEC) by 8 ¾ Receive error counter (REC) decreases by 1
An error increases the counter A successful operation
y Aims to prevent from bus dead-locks by faulty nodes Communication suspended