Canbus 160902031824
Canbus 160902031824
Canbus 160902031824
Controller Area
Network
mbedlabs Technosolutions
www.mbedlabs.com
[email protected]
+91 8050353585
Timeline of CAN Bus
CAN
Building Medical
Automation Equipments
Industrial
Automation
3
CAN and Other
BUS LIN CAN FlexRay MOST
Application
protocols
Low level
communication
Soft Real Time
Systems
Hard Real Time
Systems (X - by - Multimedia
Systems Wire)
Example Body Engine, Powertrain, Powertrain, Chassis Multimedia,
Chassis Telematics
Architecture Single Master Multi Master Multi Master up to Multi Master up to
typ 2…10 slaves typ 10…40 nodes 64 nodes 64 nodes
Bus Access polling CSMA/CA TDMA/FTDMA TDM CSMA/CS
Topology Bus Bus Bus/Star Ring/Star
Message Synchronous Asynchronous Synchronous & Synchronous &
Transmission Asynchronous Asynchronous
Msg Identification Identifier Identifier Time Slot
-Engine -Airbag
5
Bus characteristics
Serial data communications bus
Inexpensive and simple, but slower than parallel bus.
6
Data Rates
“Good” real-time capabilities
Small latency (“fast enough”)
indispensable for automotive
applications.
Data
Classrate dependent
C - data rate: 1 Mbit/secon bus
length
Bus length: 40 meters,
Class B - data rate: 125 kBit/sec Bus length: 500 meters,
Class A - data rate: 50 kBit/sec Bus length: 1000 meters.
Typical definitions:
Low-speed: 25 kBit/sec up to 125 kBit/sec.
High-speed: 500 kBit/sec up to 1 Mbit/sec.
7
Transmission principles
Multicast / broadcast philosophy
CAN messages do not include address of
sender or receivers, but to information contents.
8
Hardware characteristics
Hardware message acceptance filtering
Only messages that carry relevant information pass through
reduces CPU load.
Standardized connector
Recommended: 9-pin D-sub connectors (DIN 41652).
9
Signal coding
NRZ (non-return-to-zero) coding
Example: Voltage levels: 0V (dominant), 5V (recessive)
5V
recessive
1 0 1 0 0 0 1 1 0 1 0 0 1
0V
dominant
Note:
Different voltage levels are defined for different purposes.
10
Bus stations (Nodes)
Typically 3 to 40 nodes per bus
No limit for node number defined in CAN specification.
Number of nodes depends on capabilities of CAN transceivers.
Bus load usually gets higher with more nodes.
11
Advantages of CAN Bus
Advantages of
CAN Bus conventional
over cabling
12
International Standards
13
The ISO/OSI seven-layer model
7 •Applications, operating system
Application Layer
6 Presentation Layer
•Conversion of data formats
14
CAN within the ISO/OSI model
Application Layer
Presentation Layer
Network Layer
Implemented in Hardware
15
CAN ISO coverage on ISO/OSI layers1,2
CAN Driver
LLC: BusOff-Recovery
Software
Data Link Layer
2 LLC (Logical Link Control) Acceptance filtering,
Overload Notification
MAC (Medium Access
Control)
MAC: Data De-/Encryption, Frame Setup,
(De-)Stuffing, Collision Detection,
Arbitration, Error Management,
CAN Controller Acknowledgement
Physical Layer
PLS (Physical Signalling)
1 PMA (Physical Medium
PLS: Bit Encoding / Decoding Bit Timing,
Synchronization
Attachment)
MDT (Medium Dependent CAN
Hardware) Transceiver PMA: Transceiver Characteristics
MDT: Wiring, Connectors, Bus
CAN Bus
16
Frames: Overview
Frame: “Envelope” for transmission data
Exact frame format is defined in CAN specification
17
Frames
Remote
Data Frame Frame
Overload
Frame Error Frame
18
Data Frame: Formats
Standard Format (CAN 2.0A): 11-bit Identifier
211 = 2048 identifiers possible
Bus Idle Arbitration Control Data Field CRC Field Ack End of Inter- Bus Idle
Field Field Field Frame frame
Field
Space
recessive
S R I r DA D
Bus Idle O 11-bit Identifier TD 0 DLC 0..64 Bit Data 15-bit CRC ECE 7-bit EOF IFS Bus Idle
F RE LK L
dominant
Bus Idle Arbitration field Control Data Field CRC Field Ack End Inter- Bus Idle
Field of
Field Frame frame
Field
Space
recessive
S S I Rr r DA D
Bus Idle O
F
11-bit Base Id RD
RE
18-bit Ext. Id TR1 0 DLC 0..64 Bit Data 15-bit CRC E CL KE 7-bit
L
EOF IFS Bus Idle
dominant
19
Data Frame: Start Of Frame (SOF) bit
recessive
1 0
dominant
20
Data Frame: Arbitration Field
Arbitration Field
us Idle SOF Arbitration Field Contr
B ol Field
21
Data Frame: Control Field
Control Field
Ar bitration Field Control Field Data ield
F
RTR IDE r0 DLC3 DLC2 DLC1 DLC0
recessive
0 0
dominant
22
Data Frame: Data Field
Data Field
Contr ol Field Data Field CRC ield
F
DLC
recessive
0 .. 64 bits ( 0 .. 8 bytes )
dominant
23
Data Frame: CRC Field
CRC Field
Dat a Field CRC Field ACK ield
F
DEL
recessive
15-bit CRC code 1
0
dominant
24
Data Frame: Acknowledge (ACK) Field
26
Data Frame: End Of Frame (EOF) Field
DEL
recessive
1 1 1 1 1 1 1 1 1
dominant
27
Data Frame: Interframe Space (IFS)
recessive
1 1 1 1 1
dominant
28
Recessive and dominant bus levels
Hardware representation of
S1 0 1 0 1 0 1 0 1
S2 0 0 1 1 0 0 1 1
S3 0 0 0 0 1 1 1 1
Bus 0 0 0 0 0 0 0 1
S1 E1 S2 E2 S3 E3
Transceiver 1 Transceiver 2 Transceiver 3
29
Arbitration: Example
Start Withdraws and listen only
Bus
Idle SOF
recessive
Unit 1 1 0 0 0 1 0 0 1 1 1 1 0 1
loses arbitration
ID: 13Dh dominant
Start
Bus
Idle SOF
recessive
Unit 2 1 0 0 0 0 0 1 1 0 1 0 0 1
wins arbitration
ID: 069h dominant
Bus
SOF
recessive
Unit 3 Idle
1 0 0 0 0 0 1 1 1 1 0 0 1
loses arbitration
ID: 079h dominant
Bus recessive
1 0 0 0 0 0 1 1 0 1 0 0 1
level dominant
30
Arbitration: Identifier Priority
Consequence: Frames
carrying information of
priority should
have a identifier
31
Bit stuffing: Motivation
Problems when using NRZ coding
recessive
1 0 1 0 0 0 1 1 0 1 0 0 1
dominant
32
Bit stuffing: Approach
Solution: Bit stuffing
After five consecutive bits of same polarity, insert one bit of reverse polarity
CRC code is calculated before bit stuffing is done
bit stuffing is done by the sender directly before transmission
de-stuffing is done by the receiver directly after reception
CRC code check is done after de-stuffing the frame
bit stuffing is applied to part of the frame from SOF to CRC field
33
Bit stuffing: Examples
Example 1 recessive
1 0 0 0 0 0 0 1 0 1 0 0 1
original
sequence dominant
recessive
stuffed 1 0 0 0 0 0 1 0 1 0 1 0 0 1
sequence
dominant
stuff bit
Example 2 recessive
1 0 0 0 0 0 1 1 0 1 0 0 1
original
sequence dominant
recessive
stuffed 1 0 0 0 0 0 1 1 1 0 1 0 0 1
sequence
dominant
stuff bit
Example 3 recessive
1 0 0 0 0 0 1 1 1 1 0 0 1
original
sequence dominant
recessive
stuffed 1 0 0 0 0 0 1 1 1 1 1 0 0 0 1
sequence
dominant
stuff bit stuff bit
34
Data Frame: Maximum size
Maximum frame size (for 8 Data Bytes)
35
Error types
36
Error types: Bit errors
Error description
The bit actually appearing on the bus is different from the one transmitted
Method of detection
Sending unit constantly monitors the bus while transmitting
Possible cause
Sending unit hardware is defective
Exceptions
Arbitration phase (unit loses arbitration)
Acknowledgement bit (unit gets positive ACK from at least one receiver)
Sending of a Passive Error Frame
37
Error types: Bit stuffing errors
Error description
Data Frame contains six or more consecutive bits of the same polarity
Method of detection
Receiver detects error when de-stuffing a received frame
Possible causes
Error of sending unit during bit stuffing and/or transmission
Bit changed value during transmission, possibly due to EMI/RFI
An Active Error Frame was sent
Exceptions
Transmission of ACK delimiter, EOF field and Interframe Space (IFS):
11 consecutive recessive bits, bit stuffing does not apply to this section
38
Error types: CRC errors
Error description
CRC code calculated by receiver does not match received CRC code
Method of detection
Receiver calculates CRC code immediately after reception of the Data Field
Receiver compares calculated CRC code with the one contained in frame
Efficiency
Recognizes up to 5 single bit errors per frame Hamming distance: 6
Recognizes burst errors with lengths of up to 14 bits
Recognizes all odd numbers of bit errors
The more bits the CRC code has, the more efficient it is
39
Error types: Message format errors
Error description
Frame integrity is not preserved
Method of detection
Receiver checks received frames for bits or bit fields having a fixed
value (e.g. SOF bit, CRC delimiter, ACK delimiter, EOF field, Interframe
Space)
Violation of frame integrity when wrong value in one of these fields
Possible cause
Transmission error, error in sender and/or receiver
Transmission of an Active Error Frame during EOF field
Transmission of an Overload Frame during Interframe Space (IFS)
40
Error types: Acknowledgement errors
Error description
Acknowledge (ACK) bit is recessive
Method of detection
Sender monitors the bus while transmitting recessive ACK bit
Sender expects to observe dominant ACK bit on bus
Acknowledgement error when ACK bit on bus remains recessive
Possible cause
No other nodes are connected to the bus
Not one single receiver acknowledges that the received frame was error-
free
Cause of error is very likely to be found in sender
41
Error management
Procedure observed after detection of an error
Immediate transmission of an Error Frame
Data Frame Active Error Flag Error Delimiter Interframe Bus Idle
Space
recessive
Data Frame 6 dominant bits 8 recessive bits 3 bits Bus Idle
dominant
Error Frame violates the bit stuffing rules Other receivers are instantly
informed that an error has occurred (unless they already found out)
As a result, other units also send Error Frames
42
Error counters and node states
Two error counters for each unit
Transmit Error Counter (TEC)
Receive Error Counter (REC)
43
Transmit Error Counter (TEC)
The TEC is increased by 8 when
the unit detected an error in the frame it just +8
sent. It is very likely the unit itself is defective.
there are several other conditions of TEC
lesser importance and exceptions to
them. Refer to the CAN specification for
details.
44
Receive Error Counter (REC) +1
The REC is increased by 1 when
the unit detected an error in a received frame. REC
02.09.2016
46
Node states: Error management
Init (Normal Mode Request)
REC>127 ||
TEC>127
& TEC
<=255
Bus Off
Error Passive
TEC>255
47
Node states: Error passive
A unit is in Error Passive state when
its Transmit Error Counter (TEC) is greater than 127: TEC > 127 AND /
OR
its Receive Error Counter (REC) is greater than 127: REC > 127
48
Passive Error Frame
Passive Error Frame
Data Frame Passive Error Flag Error Delimiter Interframe Bus Idle
Space
recessive
Data Frame 6 recessive bits 8 recessive bits 3 bits Bus Idle
dominant
49
Node states: Bus off
A unit is in Bus Off state when
its Transmit Error Counter (TEC) is greater than 255: TEC > 255
Note: The value of the Receive Error Counter (REC) is of no importance
In case of an Acknowledge error a Bus Off never occurs in the sending
node
In Bus Off state a unit
is practically disconnected from the bus
cannot receive and transmit anything any more
can only leave Bus Off mode via a hardware reset OR
a software reset and subsequent initialization carried through by the host
(CAN specification: TEC and REC are set to zero and the unit must
receive 128 times a field of 11 consecutive recessive bits)
50
Node states: Warning level
The Warning Level for a unit is set when
its Transmit Error Counter (TEC) is greater than 96: TEC > 96 AND /
OR
its Receive Error Counter (REC) is greater than 96: REC > 96
255
Error Passive
127
Warning Level
96
Error Active
52
Receive Error Counter (REC): Example
TEC Bus Off
255
Error Passive
135
127
Warning Level
96
Error Active
53
Efficiency of the CAN error management
The probability for not discovering an error is
4.7 10-11
Example 1*
A CAN bus is used 365 days / year *Source: CiA
8 hours / day
with a transmission speed of 500 kBit / sec
and errors arise every 0.7 seconds
in 1.000 years, only one error remains
undiscovered
Example 2**
A CAN bus in a car is run at 500 kBit / sec **Source: Kaiser, Schröder:
“Maßnahmen zur Sicherung
with an average bus load of 15 % der Daten beim CAN-Bus”
an average data frame size 110 bits
of 4000 hours
for
an average
only operating
one error time automobiles remains
in 100.000
of
undetected
54
When is CAN Frame valid?
The CAN specification includes the following rules to this:
The CAN Frame is valid for the transmitter, if up to the end of END OF
FRAME no fault appears. Is a CAN Frame faulty, it will be repeated
automatically.
The CAN Frame is valid for the receiver, if up to the second last bit OF
END OF FRAME end no fault appears.
The receiver must reject the CAN Frame and has to force the transmitter to
repeat the frame, if a local error was detected during the last bit, which was
necessary for the validation of the CAN Frame.
So that the receiver can inform the transmitter, it is clear that the transmitter
must wait an additional bit time till the message for him is valid.
The problem is independent of this, which last bit is required for the validity.
Therefore nothing helps to introduce further additional bits at the end of
message.
55
Remote Frame
Remote Frame
Bus Idle Arbitration Field Control CRC Field Ack End Inter- Bus Idle
Field of
Field Frame frame
Field
Space
recessive
S R I r DA D
Bus Idle O 11-bit Identifier TD 0 DLC 15-bit CRC ECE 7-bit EOF IFS Bus Idle
F RE LK L
dominant
56
Overload Frame
Overload Frame
Bus Idle Overload Flag Overload Delimiter Interframe Bus Idle
Space
recessive
Bus Idle 6 dominant bits 8 recessive bits 3 bits Bus Idle
dominant
57
BasicCAN controllers
Characteristics
Electronic Control Unit
Limited number of Receive and Transmit
Buffers
Reception of all CAN identifiers, all the time
Additional transmit frames are stored in a
queue
Problems Transmit
Queue
High CPU load because all received
frames have to be evaluated
Low-priority frames in the Transmit Buffers Transmit
can block high-priority frames in the queue Buffers
Solution Bus
58
FullCAN controllers
Characteristics
Electronic Control Unit
Large number of buffers for reception and
transmission (usually 15 to 16)
Buffers can be pre-defined for specific
identifiers
All transmit frames in these buffers
participate
in arbitration
Problems
FullCAN controllers can only transmit Transmit
and receive pre-defined identifiers Buffers
Solution
Bus
Buffers are pre-defined for groups of identifiers
instead of identifiers only
59
Philips transceiver, hardware connection example
60
Double-Wire Voltage Levels: High Speed
High-Speed definitions according to ISO 11898-2
Volts
CAN-H
3.5
2.5
CAN-L
1.5
Time
61
Double-Wire Voltage Levels: Fault-Tolerant
Low Speed
Low-Speed definitions according to ISO 11519-2
Volts
CAN-H
4.0
3.25
1.75
CAN-L
1.0
Time
62
Double-Wire Bus High Speed Line
Termination
Node 1 . . . . . . Node n
CAN-H
120 120
CAN-L
63
Sub-D Connector
male female
1 - Reserved
4 - Reserved
8 - Reserved
64
CAN Bit Timing: Motivation
? Problem:
Frequencies of the quartz oscillators of the CAN nodes usually differ.
Re-synchronization of nodes during transmission is necessary.
! Solution:
CAN Bit Timing divides the bit time into several segments.
Segments can be lengthened or shortened during transmission.
Re-synchronization of nodes during transmission is possible.
65
CAN Bit Timing: Bit time
Bit time
Bit time
1 Bit
The bit time tBit is the time it takes to transmit one bit.
66
CAN Bit Timing: Time quantum
Time quantum
Bit time
1 Bit
tq
The bit time is divided up into time quanta tq.
Length of one time quantum: tq = BRP / fSys
Allowed number of time quanta: 8 nq 25
BRP: Baud Rate Prescaler, System clock
fSys:
67
CAN Bit Timing: Bit time segments
Bit time segments
Bit time
tq
Sync_Seg Synchronization Segment
Prop_Seg Propagation Time Segment
Phase_Seg1 Phase Buffer Segment 1
Phase Buffer Segment 2
Phase_Seg2
68
CAN Bit Timing: Synchronization Segment
Synchronization Segment
Bit time
tq
69
CAN Bit Timing: Propagation Time Segment
(1)
Propagation Time Segment
Bit time
tq
The Propagation Time Segment compensates for
physical delay times within the CAN network.
Length: 1 tq tProp_Seg 14 tq
70
CAN Bit Timing: Propagation Time Segment
(3)
How to determine the length of the
Propagation Time Segment: Method 2
Node 1 Node 2
Node_Output_Delay Node_Input_Delay
Bus_Line_Delay
Prop_Seg 2*
( Node_Output_Delay
+
Bus_Line_Delay
+
Node_Input_Delay ) 71
CAN Bit Timing: Sample point
Sample point
Bit time
tq Sample point
Lengths: 1 tq 8 tq
tPhase_Seg1
2 tq tPhase_Seg2 8 tq
72
CAN Bit Timing: Adjustable controller
parameters
Adjustable controller parameters
TSE G1 TSEG2
73
CAN Bit Timing: Parameters
Refer to
the microcontroller manual
to see which registers
have to be filled with
the calculated
Bit Timing Parameters
BRP, TSEG1, TSEG2 and
SJW.
74
Embedded Communication Software Layers
ISO/OSI Model
Presentation Layer
Controller independent DP IL
Diagnostic Interaction
NM Session Layer
Protocol Layer
Network
Management CCP Driver Transport Layer
TP Can
Transport Calibration
Protocol Protocol Network Layer
Software
75
CAN Driver
ISO/OSI Model
Presentation Layer
Transmits (Tx) and receives (Rx) frames
Session Layer
Transport Layer
Provides Tx and Rx frame buffers
Network Layer
Filters messages
Physical Layer
76
PD
U Term : PDU =
PROTOCOL
Meaning : unit of data that has
acertain meaning in a certain context
and can be differentiated from another
DATA unit depending on it’s purpose.
UNIT
Example : the bit is a PDU at
Physical Layer
77
Network Management (NM)
ISO/OSI Model
Presents current status and configuration Application Layer
of the CAN network to the application
Presentation Layer
78
Direct Network Management
ISO/OSI Model
ECUs send consecutively specific frames
Application Layer
containing Network Management
information
Presentation Layer
“Network Managament Token Ring”
Each ECU is always informed about other ECUs Session Layer
Physical Layer
3 1
ECU 3 2 3
79
Indirect Network Management
ISO/OSI Model
Each ECU constantly monitors one periodic
Application Layer
frame from each other ECU
Presentation Layer
When one supervised frame fails to
arrive, monitored ECU is considered not Session Layer
present
Transport Layer
On no internal activity and no bus
activity, ECU switches to Sleep Mode Network Layer
80
Network Management (NM)
Possible Network Management states:
Ready Sleep makes transition between Normal Operation and Prepare Bus Sleep
exists so that nodes can synchronize when entering Prepare Bus Sleep
81
Network Management (NM)
Network Management state machine:
82
Network Management (NM)
Notes:
83
Transport Protocol (TP)
ISO/OSI Model
Transfer of messages larger than 8 bytes
Application Layer
Transport Layer
Flow control handling
Network Layer
Time-out detection and handling
www.mbedlabs.com - +918050353585 84
Transport Protocol (TP)
Types of messages :
www.mbedlabs.com - +918050353585 85
Transport Protocol (TP)
Single Frame (SF) Messages:
F rmat Message
B1 B2 B3 B4 B5 B6 B7 B8
0L XX XX XX XX XX XX XX
www.mbedlabs.com - +918050353585 86
Transport Protocol (TP)
A Multi Frame message requires :
Flow Control frames (FC) contains data used for controlling frame
: flow
www.mbedlabs.com - +918050353585 87
Transport Protocol (TP)
First Frame (FF):
Format Message
B1 B3 B4 B5 B7 B8
B2
B6
1L LL XX XX XX XX XX XX
www.mbedlabs.com - +918050353585 88
Transport Protocol (TP)
Consecutive Frame (FF):
For atting Message
data
B1 B2 B3 B4 B5 B7 B8
B6
2S XX XX XX XX XX XX XX
www.mbedlabs.com - +918050353585 89
Transport Protocol (TP)
Flow Control frame (FC):
Control data Padding
B1 B2 B4 B5 B6 B8
B3 B7
3F BS ST XX XX XX XX XX
www.mbedlabs.com - +918050353585 90
Transport Protocol (TP)
Flow Control frame (FC):
www.mbedlabs.com - +918050353585 91
Transport Protocol (TP)
Flow Control frame (FC):
BS tells the receiver after how many CFs to send a new FC.CTS
ST tells the receiver what the time between every two CFs should be.
The FCs will always be sent by the node that receives the message, unlike
the FFs and the CFs.
www.mbedlabs.com - +918050353585 92
Transport Protocol (TP)
Message Transmission
www.mbedlabs.com - +918050353585 93
Transport Protocol (TP)
www.mbedlabs.com - +918050353585 94
Transport Protocol (TP)
Timing parameters :
N_As : Time for transmission of
the CAN frame (any N_PDU) on
the sender side
N_Ar : Time for transmission of
the CAN frame (any N_PDU) on
the receiver side
N_Bs : Time until reception of the
next FlowControl N_PDU
N_Br : Time until transmission of
the next FlowControl N_PDU
N_Cs : Time until transmission of
the next ConsecutiveFrame
N_PDU
N_Cr : Time until reception of the
next ConsecutiveFrame N_PDU
www.mbedlabs.com - +918050353585 95
Transport Protocol (TP)
Communication models:
Physical addressing :
corresponds to 1-to-1 communication
supports both multi-frame and single-frame messages
each node has unique frame IDs used for communication
known as Physical Addresses
Functional addressing :
corresponds to 1-to-n communication
supports only single-frame messages
functional addresses are recognized be each node (they
are not unique, and they may be multiple)
www.mbedlabs.com - +918050353585 96
Interaction Layer (IL) / Message Manager
ISO/OSI Model
Automatic transmission of frames
with the most recent data: Application Layer
Supports signal-based
API Physical Layer
Signal-based frames contain I_PDUs
www.mbedlabs.com - +918050353585 97
Interaction Layer (IL) / Message Manager
Signal:
www.mbedlabs.com - +918050353585 98
Interaction Layer (IL) / Message Manager
Signal-based frames can be:
www.mbedlabs.com - +918050353585 99
Interaction Layer (IL) / Message Manager
Possible signal usage
:
raw values : these signals correspond to a certain measurable property
that exists in the real world
update bits : these signals contain only one bit that shows whether data
on the frame is valid or not
checksums : these signal values rely on a checksum algorithm and on
the values of neighboring signals
counter : these signals are incremented for each frame
transmission
factors : their values influence decisions made by ECU
Application when using other data
block used for identifying different signal blocks on multiplexed
IDs : I_PDUs
No processing of requests !
Network Layer
Network Layer
Physical Layer