Atmel Microcontrollers CAN Tutorial

® Atmel Microcontrollers CAN Tutorial CAN Tutorial Agenda ® Introduction or: What is CAN? Why CAN? CAN Protocol CAN higher Layer Protocols C...
Author: Lee Williams
159 downloads 1 Views 982KB Size
®

Atmel Microcontrollers CAN Tutorial

CAN Tutorial Agenda

®

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

1 1 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 1 2 3 1 2 3 4

Number of consecutive bits with same polarity

Data Stream

$ = Staff Bits $

$

CAN Bus Bit Stream

$

1 1 1 2 3 4 5 1 1 2 3 4 5 1 2 3 4 5 1 1 1 2 3 1 2 3 4

CAN Protocol

CAN Tutorial

16/03/2004

12

CAN Tutorial CAN Bus Synchronization

®

Intermission / Idle

SOF

y Hard synchronization at Start Of Frame bit

ID10

ID9 ID8

ID7

ID6

ID5

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

EOF

DATA

RTR

1

RB0

IDE

Duration in Data Bit

ID

RTR

CAN - V2.0A

0…64

del

6

ACK

12- 32

CRC

del

1

Duration in Data Bit

DATA

1

1

RB0

CTRL

RB1

ARBITRATION

SOF

Bus Frame

SOF

Bit Stuffing

1 CTRL

RTR SRR RB0/1 DLC

DLC

DATA

4

Remote Transmission Request Substitute Remote Request Reserved bits Data Length Code

CAN Tutorial

16/03/2004

16

CAN Tutorial ®

SOF Start of Frame CRC Cyclic Redundancy Code del Delimiter ACK Acknowledge

CAN Protocol

EOF IFS ID IDE

End of Frame Inter Frame Spacing Identifier Identifier Extension

RTR SRR RB0/1 DLC

Remote Transmission Request Substitute Remote Request Reserved bits Data Length Code

CAN Tutorial

16/03/2004

17

CAN Tutorial Frame Formats (2)

®

1

12- 32

6

DATA 0…64

CRC

del

CTRL

ACK

Duration in Data Bit

ARBIT.*

del

Data Frame

SOF

Bit Stuffing EOF

15

1

1

1

7

IFS ≥3

(*) RTR = dominant

1

12- 32

6

CRC 15

del

CTRL

ACK

Duration in Data Bit

ARBIT.*

del

Remote Frame

SOF

Bit Stuffing EOF

1

1

1

7

IFS ≥3

(*) RTR = recessive

CAN Protocol

CAN Tutorial

16/03/2004

18

CAN Tutorial Frame Formats (3)

®

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

Communication active Reset

Warning at >96

TEC≥128

Error active

REC≥128 TEC

Suggest Documents