You are on page 1of 23

Controller Area Network in LPC2129

© SPJETPL All Rights Reserved 29 January 2009 1


Features:
Ø CAN is a serial bus system
Ø 2 CAN controllers and buses
Ø Data rates to 1 Mbits/second on each bus
Ø Compatible with CAN specification 2.0B, ISO 11898-1
Ø Multi-master architecture.
Ø Bus access priority determined by the message identifier (11-bit or 29-bit).
Ø Guaranteed latency time for high priority messages.
Ø Programmable transfer rate (up to 1 Mbit/s).
Ø Multicast and broadcast message facility.
Ø Data length from 0 up to 8 bytes.
Ø Powerful error handling capability.
capability
Ø Non-return-to-zero (NRZ) coding/decoding with bit stuffing.

© SPJETPL All Rights Reserved 29 January 2009 2


Attributes of CAN:
• CAN is a serial bus system with multi-master
multi capabilities, that is, all CAN nodes are
able to transmit data and several CAN nodes can request the bus simultaneously.

• In CAN networks there is no addressing of stations, but instead, prioritized messages


are transmitted. A transmitter sends a message to all CAN nodes (broadcasting).
Each node decides on the basis of the identifier received whether it should process
the message or not. The identifier also determines the priority that the message
enjoys in competition for bus access.
access

• Each CAN message can transmit from 0 to 8 bytes of user information. Of course, you
can transmit longer data information by using segmentation. The maximum
transmission rate is specified as 1 Mbit/s.
Mbit/s This value applies to networks up to 40 m.
For longer distances the data rate must be reduced. For distances up to 500 m a
speed of 125 kbit/s is possible, and for transmissions up to 1 km a data rate of 50
kbit/s is permitted.

© SPJETPL All Rights Reserved 29 January 2009 3


CAN Node Design:
• A typical CAN node is shown below.
below

• Each node consists of a microcontroller and a separate CAN controller.


• The CAN controller may, as in the case of the LPC2000, be fabricated on
the same silicon as the microcontroller or it may be a stand-alone
controller in a separate chip to the microcontroller.

© SPJETPL All Rights Reserved 29 January 2009 4


CAN Node Design….....

• The CAN controller is interfaced to the twisted pair by a line driver


and the twisted pair is terminated at either end by a 120 Ohm
resistor. The most common mistake with a first CAN network is to
forget the terminating resistors and then nothing works.

• One important feature about the CAN node design is that the CAN
controller has separate transmit and receive paths. So, as the node
is writing on to the bus it is also listening back at the same time.

© SPJETPL All Rights Reserved 29 January 2009 5


CAN Node Design….....
The two logic levels are written onto the twisted pair as follows:

On the CAN bus, logic zero is


represented by a maximum
voltage difference called
“Dominant” and logic one by a
bus idle state called “ recessive”
. A dominant bit will overwrite a
recessive bit.

In “CAN speak” a logic one is called a recessive bit and a logic zero is
called a dominant bit. In all cases a dominant bit will overwrite a recessive
bit. So, if ten nodes write recessive and one writes dominant, then each
node will read back a dominant bit.
bit

© SPJETPL All Rights Reserved 29 January 2009 6


CAN Message Objects:

The CAN bus has two message objects which may be generated by the
application software. The message object is used to transfer data around
the network. The message packet is shown below:

CAN message packet : The message packet is formed by the CAN


controller, the application software provides the data bytes, the message
identifier and the RTR bit

© SPJETPL All Rights Reserved 29 January 2009 7


CAN Message Objects……..
• The message packet starts with a dominant bit to mark the start of frame.
• Next comes the message identifier which may be up to 29 bits long. The
message identifier is used to label the data being sent in the message
packet.
• CAN is a producer / consumer protocol. A given message is produced from
one unique node and then may be consumed by any number of nodes on
the network simultaneously.
• It is also possible to do point--to-point communication by making only one
node interested in a given identifier.
identifier Then a message can be sent from the
producer node to one given consumer node on the network.
• In the message packet the RTR bit is always set to zero.
• The DLC field is the data length code and contains an integer between 0
and 8 which indicates the number of data bytes being sent in this message
packet.

© SPJETPL All Rights Reserved 29 January 2009 8


CAN Message Objects……..
• So, although you can send a maximum of 8 bytes in the message payload it is
possible to truncate the message packet in order to save bandwidth on the CAN
bus.
• After the 8 bytes of data there is a 15-bit cyclic redundancy check. This provides
error detection and correction from the start of frame up to the beginning of the
CRC field.
• After the CRC there is an acknowledge slot. The transmitting node expects the
receiving nodes to assert an acknowledge in this slot within the transmitting CAN
packet. In practice the transmitter sends a recessive bit and any node which has
received the CAN message up to this point will assert a dominant bit on the bus,
thus generating the acknowledge.
acknowledge This means that the transmitter will be happy if
just one node acknowledges its message, or if 100 nodes generate the
acknowledge. So when developing your application layer care must be taken to
treat the acknowledge as a weak acknowledge, rather than confirmation that the
message has reached all its destination nodes. After the acknowledge slot there
is an end of frame message delimiter.
delimiter

© SPJETPL All Rights Reserved 29 January 2009 9


CAN Message Objects……..

• It is also possible to operate the CAN bus in a master / slave mode. A


CAN node may make a remote request onto the network by sending
a message packet which contains no data, but has the RTR bit set.
The remote frame is requesting a message packet to be transmitted
with a matching identifier. On receiving a remote frame, the node
which generates the matching message will transmit the
corresponding message frame.
frame

Remote Transmit request: The RTR frame is used to request


message packets from the network as a master / slave transaction

© SPJETPL All Rights Reserved 29 January 2009 10


CAN Bus Arbitration:

• If a message is scheduled to be transmitted on to the bus and the bus is idle,


it will be transmitted and may be picked up by any interested node.
• If a message is scheduled and the bus is active, it will have to wait until the
bus is idle before it can be transmitted.
transmitted
• If several messages are scheduled while the bus is active, they will start
transmission simultaneously once the bus becomes idle, being synchronized
by the start of frame bit. When this happens, the CAN bus arbitration will
take place to determine which message wins the bus and is transmitted.
• CAN arbitrates its messages by a method called “non-destructive bit-wise
arbitration”.
• In the diagram, three messages are pending transmission. Once the bus is
idle and they are synchronized by the start bit, they will start to write their
identifiers onto the bus.

© SPJETPL All Rights Reserved 29 January 2009 11


CAN Bus Arbitration…………

CAN arbitration:
Message arbitration guarantees that the most important message will win the
bus and be sent without any delay. Stalled messages will then be sent in order of
priority, lowest value identifier first.

© SPJETPL All Rights Reserved 29 January 2009 12


CAN Bus Arbitration…………

• For the first two bits, all three messages write the same logic and hence read
back the same logic so each node continues transmission. However on the third
bit, node A and C write dominant bits and node B writes recessive. At this point,
node B wrote recessive but reads back dominant. In this case it will back off the
bus and start listening. Node A and C will continue transmission until node C
write recessive and node A writes dominant. Now node C stops transmission and
starts listening. Now node A has won the bus and will send its message. Once A
has finished, nodes B and C will transmit and node C will win and send its
message. Finally node B will send its message. If node A is scheduled again, it
will win the bus even though the node B and C messages have been waiting. In
practice the CAN bus will transmit the message with the lowest value identifier.

© SPJETPL All Rights Reserved 29 January 2009 13


CAN Message Transmission:
• In the LPC2000, each CAN controller has a number of status and control
registers plus three transmit buffers and a receive buffer. Once the CAN
controller has been initialized, it is possible to transmit a message by writing
to a transmit buffer. Each transmit buffer is made up of four words. Two
words are used to hold the 8 bytes of data and one word holds the message
identifier. The final register is the frame information register.

© SPJETPL All Rights Reserved 29 January 2009 14


CAN Message Transmission……..

Ø SFRs Required:

Ø PINSEL1:

PINSEL1 Pin Name Function when 01


15:14 P0.23 RD2 (CAN Controller 2)
17:16 P0.24 TD2 (CAN Controller 2)
19:18 P0.25 RD1 (CAN Controller 1)

© SPJETPL All Rights Reserved 29 January 2009 15


CAN Message Transmission………
Ø Bus Timing Register (CANBTR):
(CANBTR)
• To configure CAN controller we must program the bit timing register. It is a
protected register and may only be written to when the CAN controller is in
reset. Bit zero of the mode register is used to place the CAN controller into
reset.

CANBTR Name Function


0:9 BRP Baud Rate Prescaler.

• BRP = Pclk/(Bit rate x QUANTA)


• Value of QUANTA is from 8 to 25.
• For QUANTA = 16 and Bit Rate of 25K, BRP = 30 (0x1E).
CANBTR = 0x001C001E

© SPJETPL All Rights Reserved 29 January 2009 16


CAN Message Transmission………

Ø Mode Register (CANMOD):

CANMOD Name Function


0 RM 0: the CAN Controller operates, and certain
registers can not be written.
1: Reset Mode -- CAN operation is disabled, and
writable registers can be written.

3 TPM 0: Transmit buffer with the lowest value identifier


will be sent first.
1: Arbitration will use the values stored in the
PRIO field in the Tx Frame Information register
and the buffer with the lowest PRIO value is sent
first.

© SPJETPL All Rights Reserved 29 January 2009 17


CAN Message Transmission………
Ø Tx Frame Information Register (CANTFI1, 2,3):

CANTFI Name Function

7:0 PRIO If the TPM bit in the CANMOD register is 1, enabled Tx Buffers
send their messages based on this field. The lowest binary value
highest is the priority.
priority
19:16 DLC This value is sent in the DLC field of the next transmit message. In
addition, if RTR=0,, this value controls the number of Data bytes
sent in the next transmit message, from the CANTDA and CANTDB
registers: 0000-0111
0111 = 0-7 bytes 1xxx = 8 bytes
30 RTR If this bit is 0, the number of data bytes called out by the DLC field
are sent from the CANTDA and CANTDB registers.
If it’s 1, a Remote Frame is sent, containing a request for that
number of bytes.
31 FF If this bit is 0, the next transmit message will be sent with an 11-bit
Identifier, while if it’s 1, the message will be sent with a 29-bit
Identifier.

© SPJETPL All Rights Reserved 29 January 2009 18


CAN Message Transmission………
• Tx Identifier Register (CANTID1, 2, 3):
CAN Tx Identifier Register when FF = 0

CANTID Name Function


10:0 ID The 11-bit Identifier to be sent in the next transmit
message.

CAN Tx Identifier Register when FF=1

CANTID Name Function


28:0 ID The 29-bit Identifier to be sent in the next transmit
message.

© SPJETPL All Rights Reserved 29 January 2009 19


CAN Message Transmission………

Ø Status Register (CANSR):

CANSR Name Function


2 TBS1, 1: software may write a message into the CANTFI,
TBS2, CANTID, CANTDA, and CANTDB registers for this Tx
TBS3 Buffer.
0: software should not write to any of the CANTFI,
CANTID, CANTDA, and CANTDB registers for this Tx
Buffer.

© SPJETPL All Rights Reserved 29 January 2009 20


CAN Message Transmission………

Ø Tx Data Register A (CANTDA1, 2, 3):

CANTDA Name Function


7:0 Data 1 If RTR=0 and DLC >= 0001 in the corresponding
CANTFI, this byte is sent as the first Data byte of the
next transmit message.
15:8 Data 2 If RTR=0 and DLC >= 0010 in the corresponding
CANTFI, this byte is sent as the 2nd Data byte of the
next transmit message.
23:16 Data 3 If RTR=0 and DLC >= 0011 in the corresponding
CANTFI, this byte is sent as the 3rd Data byte of the
next transmit message.
31:24 Data 4 If RTR=0 and DLC >= 0100 in the corresponding
CANTFI, this byte is sent as the 4th Data byte of the
next transmit message.

© SPJETPL All Rights Reserved 29 January 2009 21


CAN Message Transmission………

Ø Command Register (CANCMR):

CANCMR Name Function


0 TR 1: Transmission Request -- the message, previously
written to the CANTFI, CANTID, and optionally the
CANTDA and CANTDB registers, is queued for
transmission
transmission.
2 RRB 1: Release Receive Buffer -- the information in the
CANRFS, CANRID, and if applicable the CANRDA
and CANRDB registers is released, and becomes
eligible for replacement by the next received frame. If
the next received frame is not available, writing this
command clears the RBS bit in CANSR.
5 STB1 1: Select Tx Buffer 1 for transmission

© SPJETPL All Rights Reserved 29 January 2009 22


CAN Message Reception:

Ø Rx Data Register A (CANRDA):


• This register contains the first 1-4
4 Data bytes of the current received message. It
is read-only
only in normal operation, but can be written for testing purposes if the RM
bit in CANMOD is 1.

CANRDA Name Function


7:0 Data 1 If the DLC field in CANRFS >= 0001, this contains the
first Data byte of the current received message.
15:8 Data 2 If the DLC field in CANRFS >= 0010, this contains the
second Data byte of the current received message.
23:16 Data 3 If the DLC field in CANRFS >= 0011, this contains the
third Data byte of the current received message.
31:24 Data 4 If the DLC field in CANRFS >= 0100, this contains the
fourth Data byte of the current received message.

© SPJETPL All Rights Reserved 29 January 2009 23

You might also like