Top Banner
1 The SJA1000 CAN Controller and Linux Driver Cristiano Brudna Universität Ulm Fakultät für Informatik Abteilung Rechnerstrukturen
23

1 The SJA1000 CAN Controller and Linux Driver Cristiano Brudna Universität Ulm Fakultät für Informatik Abteilung Rechnerstrukturen.

Mar 29, 2015

Download

Documents

Mikaela Wixon
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 1 The SJA1000 CAN Controller and Linux Driver Cristiano Brudna Universität Ulm Fakultät für Informatik Abteilung Rechnerstrukturen.

1

The SJA1000 CAN Controllerand Linux Driver

Cristiano Brudna

Universität Ulm

Fakultät für Informatik

Abteilung Rechnerstrukturen

Page 2: 1 The SJA1000 CAN Controller and Linux Driver Cristiano Brudna Universität Ulm Fakultät für Informatik Abteilung Rechnerstrukturen.

2

Contents

The SJA1000 Physical connection, registers, message formats,

filtering.

SJA1000 Linux Driver How to send and receive messages, special functions.

Page 3: 1 The SJA1000 CAN Controller and Linux Driver Cristiano Brudna Universität Ulm Fakultät für Informatik Abteilung Rechnerstrukturen.

3

SJA1000 Basic Features

Pin and electrical compatibility to the PCA82C200 Extended (29 bit) and Standard (11 bit) Frame

Formats Bit rate up to 1 Mbit/s Buffer:

Receive buffer: 64-byte ringbuffer Transmit buffer: 1 message

Page 4: 1 The SJA1000 CAN Controller and Linux Driver Cristiano Brudna Universität Ulm Fakultät für Informatik Abteilung Rechnerstrukturen.

4

Physical Connection

Microcontroller

SJA1000

Transceiver(PCA82C250)

TX RX

CAN_H CAN_L

CAN bus

Page 5: 1 The SJA1000 CAN Controller and Linux Driver Cristiano Brudna Universität Ulm Fakultät für Informatik Abteilung Rechnerstrukturen.

5

Modes of Operation

BasicCAN (compatible with PCA82C200) Only Standard Frame Format (11 bit identifiers) Extended Frame Format is tolerated

PeliCAN Standard and Extended Frame Format (11 and

29 bit identifiers)

Page 6: 1 The SJA1000 CAN Controller and Linux Driver Cristiano Brudna Universität Ulm Fakultät für Informatik Abteilung Rechnerstrukturen.

6

Registers (PeliCAN mode)

8 bit registers Registers for configuration and control

Mode, Command, Interrupt Enable, Bus Timing 0/1, Output Control, Acceptance Code, Acceptance Mask

Registers that provide status Status, Interrupt, RX Error Counter, TX Error Counter,

Error Warning Limit, Error Code Capture, Arbitration Lost capture, RX Message Counter, RX Buffer Start Address, Clock Divider

Page 7: 1 The SJA1000 CAN Controller and Linux Driver Cristiano Brudna Universität Ulm Fakultät für Informatik Abteilung Rechnerstrukturen.

7

Mode Register

Sleep mode Goal: save energy First message will be lost!

Acceptance Filter Mode Self Test Mode Listen Only Mode

No ancknowledge is sent, error counters stop

Reset Mode

Page 8: 1 The SJA1000 CAN Controller and Linux Driver Cristiano Brudna Universität Ulm Fakultät für Informatik Abteilung Rechnerstrukturen.

8

Interrupts

Interrupts provided: Bus Error Arbitration Lost Error Passive Wake-up Data Overrun Error Warning Transmit Receive

Can be idividually enabled/disabled

Page 9: 1 The SJA1000 CAN Controller and Linux Driver Cristiano Brudna Universität Ulm Fakultät für Informatik Abteilung Rechnerstrukturen.

9

Bus Timing 0/1 Registers

Can be written only in RESET MODE Bus Timing 0

BTR0.7 to 6: Synchronization Jump Width (2 bits) BTR0.5 to 0: Baud Rate Prescaler (6 bits), define TQ

Bus Timing 1 BTR1.7: Sampling (1 bit), 1 or 3 times BTR1.6 to 4: Time Segment 1 (4 bits) BTR1.3 to 0: Time Segment 2 (3 bits)

Page 10: 1 The SJA1000 CAN Controller and Linux Driver Cristiano Brudna Universität Ulm Fakultät für Informatik Abteilung Rechnerstrukturen.

10

Bus Timing 0/1 Registers (cont)

SYNCSEG

TSEG1 TSEG2

Sample point(s)

Nominal bit time

= 1 TQ

Time quantum (TQ) is used to define segments

Page 11: 1 The SJA1000 CAN Controller and Linux Driver Cristiano Brudna Universität Ulm Fakultät für Informatik Abteilung Rechnerstrukturen.

11

Error registers

RX error counter: number of receive errors TX error counter: number of transmit errors Error code capture: identify the type of error

occurred during transmition/reception

Page 12: 1 The SJA1000 CAN Controller and Linux Driver Cristiano Brudna Universität Ulm Fakultät für Informatik Abteilung Rechnerstrukturen.

12

Data Frame, Remote Frame

Frame Information (1 byte): FF: frame format RTR: remote transmission request DLC: data length code

Identifier: Standard frame format: 2 bytes Extended frame format: 4 bytes

Data Bytes: 0-8 bytes

FF RTR X X DLC.3 DLC.2 DLC.1 DLC.0

Page 13: 1 The SJA1000 CAN Controller and Linux Driver Cristiano Brudna Universität Ulm Fakultät für Informatik Abteilung Rechnerstrukturen.

13

Message Format

Tx frame information

Tx identifier 1

Tx identifier 2

Tx data byte 1

Tx data byte 2

Tx data byte 3

Tx data byte 4

Tx data byte 5

Tx data byte 6

Tx data byte 7

Tx data byte 8

Tx frame information

Tx identifier 1

Tx identifier 2

Tx data byte 1

Tx data byte 2

Tx data byte 3

Tx data byte 4

Tx data byte 5

Tx data byte 6

Tx data byte 7

Tx data byte 8

Tx identifier 3

Tx identifier 4

Standard Frame Extended Frame

Page 14: 1 The SJA1000 CAN Controller and Linux Driver Cristiano Brudna Universität Ulm Fakultät für Informatik Abteilung Rechnerstrukturen.

14

Filtering

Two modes of filtering: Single filter: 32-bit filter

Standard Frame: identifier + RTR + data1 + data2Extended Frame: identifier + RTR

Dual filter: at least one filter must matchSF: filter 1(ID + RTR + data1), filter 2 (ID + RTR)EF: filter 1and 2 (2 upper bytes of the ID)

Acceptance Mask Register: define relevant bits ('1' = don't care)

Acceptance Code Register: define bits to be matched

Page 15: 1 The SJA1000 CAN Controller and Linux Driver Cristiano Brudna Universität Ulm Fakultät für Informatik Abteilung Rechnerstrukturen.

15

Single Filter for Extended Frame Format

ID.4 ID.3 ID.2 ID.1 ID.0Message ID

AMR3.7 AMR3.6 AMR3.5 AMR3.4 AMR3.3 AMR3.2 X XAcceptance Mask Register

ACR3.7 ACR3.6 ACR3.5 ACR3.4 ACR3.3 ACR3.2 X XAcceptance Code Register

AMR 2AMR 1AMR 0

ID.12 to 5ID.20 to 13ID.28 to 21 RTR

ACR 2ACR 1ACR 0

Byte 0 Byte 1 Byte 2 Byte 3

Page 16: 1 The SJA1000 CAN Controller and Linux Driver Cristiano Brudna Universität Ulm Fakultät für Informatik Abteilung Rechnerstrukturen.

16

Single Filter Example

0 1 1 0 0 0Message ID

0 0 0 1 1 1Acceptance Mask Register

0 1 1 0 1 0Acceptance Code Register

Accepted

"1""1""1"

ID.12 to 5ID.20 to 13ID.28 to 21

"0""0""0"

Byte 3 Byte 2 Byte 1 Byte 0

Page 17: 1 The SJA1000 CAN Controller and Linux Driver Cristiano Brudna Universität Ulm Fakultät für Informatik Abteilung Rechnerstrukturen.

17

Using the SJA1000 Linux driver

The driver uses receive and transmit interrupts and read and write buffers

File operations for applications: open(), close(), read(), write(), ioctl(), and select().

Page 18: 1 The SJA1000 CAN Controller and Linux Driver Cristiano Brudna Universität Ulm Fakultät für Informatik Abteilung Rechnerstrukturen.

18

Using the SJA1000 Linux driver

File operations (1) Open: provide blocking and non-blocking modes

can = open("/dev/can", O_RDWR);

can = open("/dev/can", O_RDWR | O_NONBLOCK);

Close:

close(can);

Page 19: 1 The SJA1000 CAN Controller and Linux Driver Cristiano Brudna Universität Ulm Fakultät für Informatik Abteilung Rechnerstrukturen.

19

Using the SJA1000 Linux driver

CAN message data structuretypedef struct {

CanId id; // identifier (11 or 29 bits)

int type; // standard (0) or extended frame (1)

int rtr; // remote transmission request (1 when true)

int len; // data length 0..8

unsigned char d[8]; // data bytes

struct timeval timestamp; // timestamp for received messages in the format of [seconds, microseconds] since Epoch (january 1. 1970).

} canmsg;

Page 20: 1 The SJA1000 CAN Controller and Linux Driver Cristiano Brudna Universität Ulm Fakultät für Informatik Abteilung Rechnerstrukturen.

20

Using the SJA1000 Linux driver

File operations (2) Read: return a message from the read buffer

Return '32' when a message is available and -EAGAIN when there is no message.

ret = read(can, &msg, sizeof(canmsg));

Write: write a message to the driver. Return '32' when the message is succesfully stored in

the transmit buffer and -EAGAIN when the buffer is full.

ret = write(can, &msg, sizeof(canmsg));

Page 21: 1 The SJA1000 CAN Controller and Linux Driver Cristiano Brudna Universität Ulm Fakultät für Informatik Abteilung Rechnerstrukturen.

21

Using the SJA1000 Linux driver

File operations (3) ioctl: specific control operations.

unsigned long baud_rate = B1000;

ioctl(can, CAN_IOCSBAUD, &baud_rate);

Supported operations: CAN_IOCSBAUD: set baud rate. The following

constants are used to set it: B1000 (1Mbit/s), B500 (500kbit/s), B250 (250kbit/s), B125 (125kbit/s), B20 (20kbit/s).

Page 22: 1 The SJA1000 CAN Controller and Linux Driver Cristiano Brudna Universität Ulm Fakultät für Informatik Abteilung Rechnerstrukturen.

22

Using the SJA1000 Linux driver

File operations (4) Supported operations:

CAN_IOCSAMASK: set a 32-bit acceptance mask. CAN_IOCSACODE: set a 32-bit acceptance code. CAN_IOCCRBUF: clear read buffer. CAN_IOCCWBUF: clear write buffer. CAN_IOCRTTS: read the timestamp of the last

transmitted message. The timestamp is returned in a timeval data structure.

Page 23: 1 The SJA1000 CAN Controller and Linux Driver Cristiano Brudna Universität Ulm Fakultät für Informatik Abteilung Rechnerstrukturen.

23

Using the SJA1000 Linux driver

File operations (5) CAN_IOCSACTIVE: set active mode. CAN_IOCSPASSIVE: set passive mode. CAN_IOCRREG: read a SJA1000 register. The most

useful ones for applications are'ERROR_CODE_CAPTURE'

'RX_ERROR_COUNTER'

'TX_ERROR_COUNTER'