e99a036.pdf

(1012 KB) Pobierz
138905992 UNPDF
MICROPROCESSORS
network (CAN)
intelligent, decentralized data
communications Part 2
The first part
of this article
described the
history, stan-
dardization,
and the basic
setup of the
Controller
Area Network
(CAN) devel-
oped by the
Robert Bosch
Company in
Germany. In
this second
part, the
attention is
focused on the data
transmission proto-
col that determines
the capabilities and
reliability of this auto-
motive digital data
system.
INTRODUCTION
As already stated in Part 1, CAN is a
serial asynchronous communication
protocol that connects sensors and
actuators of electronic control stations
in cars. Among its many functions is a
digital data link. It is an asynchronous
system because each station (also called
‘node’) synchronizes to messages of
other stations on the leading edge of
the first message bit and on subsequent
leading edges throughout the rest of
the message. The ability of any station
to synchronize to another station is
determined by the maximum differ-
ences in oscillator frequencies. Other
factors are, for instance, bit duration,
message duration and composition,
and handshaking.
The most important parts of the net-
work are the physical layer , comprising
the topology of the network and the
link to the bus, and the data link layer ,
which lays down how the data trans-
mission medium is accessed, how a
message is constructed (address, data,
control and protection against errors)
and how the data transmission proto-
col is structured.
By B vom Berg & P Groppe
IN PRACTICE
The exchange of messages between
two network stations may take place in
two basically different ways: station-
36
Elektor Electronics
10/99
controller area
138905992.018.png 138905992.019.png
6
Arbitration
Control
Data
CRC
Acknowledge
EOF
Intermission
Field
Field
Field
Field
Field
Field
Field
11 Bit
Identifier
DLC
user data, 0 - 8 bytes
15-bit CRC
checksum
ACK
Slot
EOF
7 Bits
3 Bits
3210
S
O
F
R
T
R
I
D
E
r
0
C
R
C
Delimits
ACK
Delimiter
990066 - 11
oriented and message-
oriented.
Figure 6. Composition
of a data frame (stan-
dard frame format –
Specification CAN 20A).
to the bus sends a
recessive bit and
another station at the
same time sends a
dominant bit, the dominant bit takes
priority over the recessive bit, that is,
the dominant state is accepted by the
entire bus. The assignment of logic
states to the bus is generally so that a
logic 0 repretransmitteds a dominant
state, and a logic 1, a recessive state.
These arrangements constitute one
of the foundations of the CAN specifi-
cation and will be elaborated on later.
lated. In this, 2 29 =536 870 912 frames
can be handled.
Station-oriented
exchange
In this mode, the sender addresses the
receiver simply by means of the
receiver address, for instance: ‘Station
25 is sending a message to station 37’.
In this way, a virtual link between the
sender and receiver is established via
the bus.
The transmitted packet of data
therefore contains the address of the
receiver station and that of the sending
station. All other stations connected to
the bus ignore the packet since it is not
addressed to them.
The receiving station evaluates the
message and normally acknowledges
its receipt. In case of an error during
the data transmission (no acknowledg-
ment from the receiver), the sender
repeats the message.
RTR (Remote Transmission Request)
bit. This bit, which is always dominant
(0), enables a station to address and
send messages to another specified sta-
tion. This is of great value when certain
data are urgently needed to be
processed (more about this later).
Control field . This 6-bit long section
contains the information as to how a
data frame is composed.
Data packets
The network uses four kinds of data
packets, normally called frames, to
exchange data on the bus: data frame;
remote frame; error frame; and over-
load frame.
IDE (Identifier Extension) bit. This bit
indicates whether a standard-format
frame with an 11-bit identifier (IDE
= dominant = 0), or an extended-for-
mat frame with a 29-bit identifier
(IDE = recessive = 1) is being trans-
mitted.
Data frame
The data frame is used by the stations
to send their data in line with their pro-
gramming. The composition of a typical
data frame, which consists of a single
field, is shown in Figure 6 . This is a
standard frame format according to
Specification CAN 20A. The meaning
of the various terms in the figure is as
follows.
r0 (Reserve bit 0). This dominant bit is
transmitted as a spare bit for future
expansion specifications.
Message-oriented exchange
In this mode, the sender adds to the
message an unambiguous identifier
and sends the message and identifier
via the bus, for instance: ‘Station A is
sending a voltage measurement with
identifier 978’. In this mode, the
addresses of the sender and receiver
are not included.
Such a message is clearly intended
for several receivers connected to the
bus under the motto: ‘Take from the
bus what you need’ (broadcast princi-
ple). The various receive stations must
determine, on the basis of their pro-
gramming, whether the message is rel-
evant to them or not.
DLC (Data Length Code). This 4-bit
long section indicates how many data
bytes are being transmitted succes-
sively in a data field. The CAN Specifi-
cation allows data field lengths of 0–8
bytes, that is, a single data frame may
transmit not more than eight data
bytes.
SOF . This is the start-of-frame bit,
which is always dominant (0). All sta-
tions connected to the bus synchronize
their internal receive stages to the trail-
ing edge of this bit.
Data field . This 8-bit long section con-
tains the data bytes (0–8) to be trans-
mitted.
Arbitration field . This field, which is 12
bits long, contains the data for access-
ing the bus.
CRC field . The 16-bit long CRC field
contains additional information for
protecting the data being transmitted
against interference. For this purpose,
the sender station constructs, accord-
ing to specific rules, a 15-bit CRC
check sum from the preceding data
and sends this, together with the
frames. The receiver station calculates
a similar check sum according to the
same rules and compares this with the
transmitted check sum. If the two
sums are identical (the normal case),
data transmission can commence. If
the sums are not identical, an error
handling procedure is initiated. The
CRC field is limited by a CRC delim-
iter bit which is always transmitted
recessively.
Flow of communications
The flow of communications between
the individual stations connected to
the CAN bus takes place in the form of
a broadcast of event-controlled, priori-
tized (through-numbered) messages or
frames (communication messages).
11-bit identifier . This section contains
the identifier (ID) of the transmitted
frames. The 11 bits allow up to
2 11 =2048 different identifiers to be
constructed, of which only 2032 are
freely available: the remaining 16 are
reserved for certain special functions.
This means that a single controller
area network can process 2032 differ-
ent messages (measurement values,
switch positions, light functions, and
so on). Although this seems a fairly
large number, in many applications it
is not enough. Therefore, an
Extended Frame Format with 29 iden-
tifier bits (CAN 20B) has been formu-
Dominant and recessive bus/bit states
The actual data transmission via the
data transmission medium does not
take place, as usual, in the form of ‘1s’
and ‘0s’, but by dominant and reces-
sive bits. Recessive typifies a bus state
that may be overwritten by a dominant
bus state. So, when a station connected
Elektor Electronics
10/99
37
138905992.020.png
Acknowledge field . The 2-bit long
acknowledge field serves to transmis-
sion acknowledgments of correctly
received data frames.
problem. As explained earlier, a reces-
sive bit (1) can be overwritten by a
dominant bit (0).
Figure 7 gives a highly simplified
repretransmittedation of some linking
stages of the bus. Basically, these are
open-collector output stages that are
arranged as wired-AND gates. With
reference to station 1, a recessively
transmitted bit (1) ensures that transis-
tor T 1 remains cut off. This means that
the recessive level is pretransmitted at
the bus. After this bit has been trans-
mitted, station 1 reads the bus status
and recognizes the bit it has transmit-
ted. If then a dominant bit (0) is trans-
mitted, T 1
ing back, station 1 notices that its reces-
sive bit has been overwritten, which
means that it has lost access to the bus
to at least one other station. Station 1
then assumes the receive mode (but
tries to send its message at a later time
again). Stations 2 and 3 continue as
before.
At time j, station 3 sends a recessive
bit that is promptly overwritten by the
dominant level transmitted by station
2. This is noticed by station 3, which
thereupon also assumes the receive
mode (and, like station 1, tries to send
its message at a later time again). Sta-
tion 2 is the ‘victor ’ and can send its
message without fur-
ACK slot . This 1-bit long section is
transmitted as a recessive bit and may,
therefore, be overwritten by a domi-
nant bit transmitted by another station
connected to the bus. It allows receive
stations to send an acknowledgement
of a correctly received data frame. The
acknowledgment bit is dominant and
is transmitted by each and every rele-
vant station upon error-free reception
of messages. Since it is dominant, it
overwrites the recessive bit sent by the
transmitting station. Thus, if the trans-
mitting station receives a dominant bit
during the ACK slot window, instead
of its own transmitted recessive bit, it
‘knows’ that at least one station has
received the message.
The ACK slot window is restricted
by a recessively transmitted ACK
Delimiter bit.
comes on
Figure 7. Highly simpli-
fied repretransmitteda-
tion of how stations are
linked to the CAN bus.
7
5V
R
bus line
EOF (End Of Frame) field . This field
consists of seven recessive bits and
serves to terminate the data frame.
Before the next data frame can be
transmitted, the receive stations need
a short intermission to enable them to
process, or at least store, the received
data. The intermission is arranged by a
recessive 3-bit intermission field end-
ing the data frame.
T1
T2
T3
1
1
1
Station 1
Station 2
Station 3
transmitted
received
transmitted
received
transmitted
received
data
data
data
data
data
data
990066 - 12
Owing to lack of space, the Extended
Frame Format cannot be discussed; its
principles are, however, the same as
those discussed for the Standard Frame
Format.
and switches the bus line to earth. The
bus line is then dominant (0). Again,
station 1 reads back the bit it transmit-
ted.
Considering the three stages, if one
of them sends a dominant bit, the
busline becomes dominant (0) and the
other stations read this level.
An example will show how the bus
access procedure takes place. Assume
that the stations in Figure 2 are all
ready to transmit their data frames
with three different identifiers:
ther hindrance to the bus.
AVOIDANCE OF
CONFLICT
Since all stations connected to the Con-
troller Area Network bus, two ques-
tions arise:
A closer look at the identifiers
shows that it is the station with the
smallest identifier that gains access to
the bus first: it has the highest send pri-
ority. In other words: the identifier also
automatically contains the message pri-
ority. A message with identifier 0 will
always be the first to be received by the
stations connected to the bus, since it
has the highest priority. A message
with identifier 2032 has a long wait
since it has the lowest priority.
* What happens when several sta-
tions want to send a message at the
same time?
* How is it decided which station can
start and which stations must wait
their turn?
Station 1: identifier 367
Station 2: identifier 232
Station 3: identifier 239.
Clearly, these matters may give rise to
conflicts and to avoid those there is a
special bus access procedure which
must be obeyed by all stations when
they want to send a message. In this,
an important role is played by the
dominant and recessive bits in the
Arbitration Field.
Basically, each sender ‘hears’ its
own transmission to the bus: it sends a
bit, receives it back and compares the
two. If they are identical, transmission
of the message is allowed. If, however,
the two bits are dissimilar, there is a
Remote Request Frame
The remote request frame is an impor-
tant one in the network. Assume that
station D connected to the CAN bus
transmits three temperature measure-
ment data every five minutes with
Identifier 598. This means that the data
field contains three bytes. These mes-
sages are received and processed by
other stations.
However, station G urgently needs
the actual temperature measurement
and cannot in any circumstances wait
for five minutes. It has the facility,
All three start with the arbitration (bus
access) phase by transmitting a SOF bit
(see Figure 8). This is a dominant bit,
and each station reads back its own
(correct) bit from the bus. Then, the
identifiers are transmitted. Up to time
b, all stations send a dominant bit and
all is well. At time c, there is still no
problem. At time d, station 1 sends a
recessive bit, but stations 2 and 3 con-
tinue with a dominant bit. When read-
38
Elektor Electronics
10/99
138905992.021.png 138905992.001.png
therefore, to request the measurements
directly from station D, that is, it can
bypass the data transmission cycle. To
do so, it sends a so-called Remote
Request Frame, which is composed
similar to a Data Frame (Figure 6), but
with some small differences:
DETECTION OF
TRANSMISSION
ERRORS
In a CAN, several means are used
simultaneously to detect errors.
Acknowledgment error detection
In the description of the frame format
(see Figure 6) mention was made of the
ACK slot bit, which is transmitted by a
station as a recessive bit. All stations
that have received the previous frame
correctly overwrite this bit with a dom-
inant bit. The sender detects this and
‘knows’ that at least one station has
received its data correctly.
If the sender detects that its ACK
slot bit is not overwritten, it ‘knows’
that not one station has received its
message correctly. It then shifts its
operation to an error treatment routine
(see later).
Bit error detection
Each and every station receives its own
transmission back. If, therefore, after
the arbitration phase, a station is the
only one that sends a message to the
bus and it receives back a different bus
status than it transmitted, it is clear that
an error has occurred on the bus. The
station then shifts its operation to an
error treatment routine (see later).
• The identifier of the station to whom
the request is transmitted (here,
598) is entered in the identifier
field.
• In the DLC field, the number of
useful bytes contained in the
requested message (here, 3) is
entered.
• The Remote Transmission Request
(RTR) which is dominant (0) in the
Data Frame is made and transmit-
ted recessively (1). This is a typical
identification of a station that
requests data direct from another
specific station.
• There is no data field in the Remote
Request Frame: the DLC field is fol-
lowed immediately by the CRC
field. In other words, the Remote
Request Frame is composed like a
Data Frame but with 0 bytes of data.
Stuffbit error detection
The CAN specification states clearly
that when in a data frame more than
five bits of the same value are trans-
mitted in sequence (for
instance, seven times a
0 in a field), each and
Figure 8. Diagram-
matic repretransmitte-
dation of how a station
does access the bus
(Arbitration).
Format error detection
In this, use is made of the fact that the
network format has several fields that
must always have a fixed content: the
CRC delimiter, the
Acknowledgment
Delimiter, and the EOF
8
The transmitted Remote Request
Frame functions as follows. All stations
connected to the bus receive the frame
and recognize by the set RTR bits that
a station has requested specific data
from another station. Station D recog-
nizes that the identifier in the Remote
Request Frame is the same as its own
identifier and immediately sends its
response in the form of a Data Frame
with the requested data.
bit instant
a
bcde f gh i j k l
S
O
F
Identifier
Station 1
000101101111
Station 2
000011101000001...
Station 3
000011101111
bus state
000011101000001...
Station 2 wins and keeps transmitting ...
ERROR DETECTION
AND REMEDIES
One of the most striking properties of
the Controller Area Network concept
is its uncanny capability of detecting a
multitude of errors during the data
transmission and react to them accord-
ingly. It has a Hamming Distance (also
called signal distance) of 6. The signal
distance between two binary words of
the same length is the number of the
corresponding bit positions in which
the two words have different bit val-
ues. For instance, the signal distance
between 11011010 and 10000110 is four,
since the 3rd, 4th, 5th, and 7th bits
(counting from the left) are different.
In a CAN data are transmitted per-
manently at a transmission rate of
500 kbit/s. Every 0.7 s a one-bit error is
caused by external interference. The
network operates eight hours a day,
365 days a year. The built-in protection
against errors in a CAN guarantees
that in 1000 years of operation only
one error will not be detected. Errors
can and do, of course, occur, but once
they are known, they can be remedied.
Only unknown errors can cause false
measurements to be processed.
log. '0'
dominant bit (bus state)
log. '1'
recessive bit (bus state)
990066 - 13
every group of five bits is followed by
a complementary bit (here, a 1, of
course). This introduced bit, which, of
course, contains no information what-
ever, is called a stuffbit. At the receive
end, these bits are removed from the
data stream, so that only the original
message is processed.
The stuffbits may readily be used
for error checking. If the receiver
detects more than five sequential bits
of the same value in a frame (but not in
the EOF field), it is clear that this can-
not be right and that an error in the
data transmission has occurred which
has inverted one or more bits. The
receiver then shifts its operation to an
error treatment routine (see later).
field are always composed of recessive
bits. If a dominant bit is detected, this
can have been caused only by a data
transmission error. Here also, operation
is shifted to an error treatment routine
(see later).
ERROR TREATMENT
The error treatment routine in
response to a data transmission error
takes two forms.
In the first place, frames in which
an error has been detected are imme-
diately rejected by the relevant station
and not processed. Secondly, if any sta-
tion within the system detects an error,
it immediately transmits an error frame
that consists of six dominant bits (=
error flag) and an error delimiter of
eight recessive bits. The result of this is
that all recessive bits on the bus are
overwritten, so that six dominant bits
remain. This is, however, a contraven-
tion of the stuffbit rule that not more
than five sequential bits may have the
same value.
CRC error detection
This consists, as already described, of
an evaluation of the CRC check sum at
the receiver. When the received and
calculated check sums are dissimilar,
the receiver shifts its operation to an
error treatment routine (see later).
Elektor Electronics
10/99
39
138905992.002.png 138905992.003.png 138905992.004.png
All other stations connected to the
bus detect this error condition and
treat the frame just transmitted as
faulty, reject it and also send out an
error frame. In other words, a station
that detects an error purposely muti-
lates the entire transmitted frame so
that all other stations connected to
the bus receive a faulty frame. This
means that an error local to a station
is immediately communicated to all
other stations. The motto of the net-
work is that all stations receive cor-
rect data that can be processed as
required, or all stations receive faulty
data that are rejected. The original
sender detects, of course, that the
frame it transmitted is mutilated,
adjust its message and resends it
after a short while.
Table 3.
CAN 20A
CAN 20B
Maximum number of identifiers
2 11
2 29
Number of stations (nodes)
32
32
Data transfer rate
5–125 kbit/s 5–1000 kbit/s
Number of permissible bytes per frame
0–8
0–8
Maximum length of a frame
117 bits
13 bits
Maximum bus expansion
see text
see text
Table 3. Comparison of CAN 20A (standard frame
format) and CAN 20B (extended frame format).
SUMMARY
The specifications of the two CAN ver-
sions, CAN 20A (standard frame for-
mat) and CAN 20B (extended frame
format) are compared in Table 3.
Although the Controller Area Net-
work is a powerful and highly reliable
system for data communications, the
reader and prospective user may well
ask how it can be turned into a practical
application. There are dominant and
recessive bits, an 11-bit identifier, a
15-bit CRC check sum, a 1-bit delimiter,
a 7-bit EOF field, a 6-bit error frame,
and many more. None of this resem-
bles the 8-bit or 16-bit data structure of
the microcontroller.
So how is it possible to program
according to the network protocol?
Here, the future constructor need not
worry. There is a plethora of ready-
made, inexpensive building blocks
available for the network. It is this sup-
port by IC manufacturers for the CAN
that has made the network so popular
in such a short time.
ERROR INSIDE A
STATION
What happens when a station itself
becomes defect, is damaged, oper-
ates with an inaccurate transmission
rate, or is the only station that gets
interference? Such a station would
permanently send out error frame
and so disable the entire network.
The CAN concept has adequate pro-
tection against such an occurrence,
but space prohibits describing this in
this article.
The next instalment will deal with
these building blocks, with the pro-
gramming according to the CAN pro-
tocol and with practical application of
the network.
[990066]
C ONSTRUCTION GUIDELINES
Elektor Electronics (Publishing) does not provide parts and compo-
nents other than PCB s, fornt panel foils and software on diskette or IC
(not necessarily for all projects). Components are usually available
form a number of retailers – see the adverts in the magazine.
all the components in the correct position? Has correct polarity been
observed? Have the powerlines been reversed? Are all solder joints
sound? Have any wire bridges been forgotten?
If voltage levels have been given on the circuit diagram, do those
measured on the board match them – note that deviations up to ±10%
from the specified values are acceptable.
Large and small values of components are indicated by means of one
of the following prefixes :
Possible corrections to published projects are published from time to
time in this magazine. Also, the readers letters column often contains
useful comments/additions to the published projects.
E (exa) = 10 18
a (atto) = 10 –18
P (peta) = 10 15
f (femto) = 10 –15
T (tera) = 10 12
p (pico) = 10 –12
The value of a resistor is indicated by a colour code as follows.
G (giga) = 10 9
n (nano) = 10 –9
M (mega) = 10 6
µ (micro) = 10 –6
k (kilo) = 10 3
m (milli) = 10 –3
h (hecto) = 10 2
c (centi) = 10 –2
da (deca) = 10 1
d (deci) = 10 –1
In some circuit diagrams, to avoid confusion, but contrary to IEC and
BS recommandations, the value of components is given by substitut-
ing the relevant prefix for the decimal point. For example,
3k9 = 3.9 kΩ
4µ7 = 4.7 µF
color
1st digit
2nd digit
mult. factor
tolerance
Unless otherwise indicated, the tolerance of resistors is ±5% and their
rating is 1 3 1 2 watt. The working voltage of capacitors is ≥
black
0
50 V.
brown
1
1
×
10 1
±
1%
red
2
2
×10 2
±2%
orange
3
3
×
10 3
In populating a PCB , always start with the smallest passive compo-
nents, that is, wire bridges, resistors and small capacitors; and then IC
sockets, relays, electrolytic and other large capacitors, and connectors.
Vulnerable semiconductors and ICS should be done last.
yellow
4
4
×10 4
green
5
5
×
10 5
±
0,5%
blue
6
6
×
10 6
violet
7
7
grey
8
8
Soldering. Use a 15–30 W soldering iron with a fine tip and tin with
a resin core (60/40) Insert the terminals of components in the board,
bend them slightly, cut them short, and solder: wait 1–2 seconds for
the tin to flow smoothly and remove the iron. Do not overheat, par-
ticularly when soldering ICS and semiconductors. Unsoldering is best
done with a suction iron or special unsoldering braid.
white
9
9
gold
×
10 –1
±
5%
silver
×
10 –2
±
10%
none
±20%
Examples:
brown-red-brown-gold = 120
Ω,
5%
Faultfinding. If the circuit does not work, carefully compare the pop-
ulated board with the published component layout and parts list. Are
yellow-violet-orange-gold = 47 k
Ω,
5%
Elektor Electronics
10/99
41
138905992.005.png 138905992.006.png 138905992.007.png 138905992.008.png 138905992.009.png 138905992.010.png 138905992.011.png 138905992.012.png 138905992.013.png 138905992.014.png 138905992.015.png 138905992.016.png 138905992.017.png
Zgłoś jeśli naruszono regulamin