The SJA 1000 CAN Controller and Linux Driver

  • Slides: 23
Download presentation
The SJA 1000 CAN Controller and Linux Driver Cristiano Brudna Universität Ulm Fakultät für

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

Contents " The SJA 1000 �Physical connection, registers, message formats, filtering. " SJA 1000

Contents " The SJA 1000 �Physical connection, registers, message formats, filtering. " SJA 1000 Linux Driver �How to send and receive messages, special functions. 2

SJA 1000 Basic Features " Pin and electrical compatibility to the PCA 82 C

SJA 1000 Basic Features " Pin and electrical compatibility to the PCA 82 C 200 " 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 3

Physical Connection Microcontroller SJA 1000 TX RX Transceiver (PCA 82 C 250) CAN_H 4

Physical Connection Microcontroller SJA 1000 TX RX Transceiver (PCA 82 C 250) CAN_H 4 CAN_L CAN bus

Modes of Operation " Basic. CAN (compatible with PCA 82 C 200) �Only Standard

Modes of Operation " Basic. CAN (compatible with PCA 82 C 200) �Only Standard Frame Format (11 bit identifiers) �Extended Frame Format is tolerated " Peli. CAN �Standard and Extended Frame Format (11 and 29 bit identifiers) 5

Registers (Peli. CAN mode) " 8 bit registers " Registers for configuration and control

Registers (Peli. CAN 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 6

Mode Register " Sleep mode �Goal: save energy �First message will be lost! "

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 7

Interrupts " Interrupts provided: �Bus Error �Arbitration Lost �Error Passive �Wake-up �Data Overrun �Error

Interrupts " Interrupts provided: �Bus Error �Arbitration Lost �Error Passive �Wake-up �Data Overrun �Error Warning �Transmit �Receive " Can be idividually enabled/disabled 8

Bus Timing 0/1 Registers " Can be written only in RESET MODE " Bus

Bus Timing 0/1 Registers " Can be written only in RESET MODE " Bus Timing 0 �BTR 0. 7 to 6: Synchronization Jump Width (2 bits) �BTR 0. 5 to 0: Baud Rate Prescaler (6 bits), define TQ " Bus Timing 1 �BTR 1. 7: Sampling (1 bit), 1 or 3 times �BTR 1. 6 to 4: Time Segment 1 (4 bits) �BTR 1. 3 to 0: Time Segment 2 (3 bits) 9

Bus Timing 0/1 Registers (cont) Time quantum (TQ) is used to define segments Nominal

Bus Timing 0/1 Registers (cont) Time quantum (TQ) is used to define segments Nominal bit time SYNC SEG TSEG 1 = 1 TQ Sample point(s) 10 TSEG 2

Error registers " RX error counter: number of receive errors " TX error counter:

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 11

Data Frame, Remote Frame " Frame Information (1 byte): �FF: frame format �RTR: remote

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 12

Message Format 13 Standard Frame Extended Frame Tx frame information Tx identifier 1 Tx

Message Format 13 Standard Frame Extended Frame Tx frame information Tx identifier 1 Tx identifier 2 Tx data byte 1 Tx identifier 3 Tx data byte 2 Tx identifier 4 Tx data byte 3 Tx data byte 1 Tx data byte 4 Tx data byte 2 Tx data byte 5 Tx data byte 3 Tx data byte 6 Tx data byte 4 Tx data byte 7 Tx data byte 5 Tx data byte 8 Tx data byte 6 Tx data byte 7 Tx data byte 8

Filtering " Two modes of filtering: �Single filter: 32 -bit filter " Standard Frame:

Filtering " Two modes of filtering: �Single filter: 32 -bit filter " Standard Frame: identifier + RTR + data 1 + data 2 " Extended Frame: identifier + RTR �Dual filter: at least one filter must match " SF: filter 1(ID + RTR + data 1), filter 2 (ID + RTR) " EF: filter 1 and 2 (2 upper bytes of the ID) 14 " Acceptance Mask Register: define relevant bits ('1' = don't care) " Acceptance Code Register: define bits to be matched

Single Filter for Extended Frame Format 15 Byte 0 Byte 1 Byte 2 Message

Single Filter for Extended Frame Format 15 Byte 0 Byte 1 Byte 2 Message ID ID. 28 to 21 ID. 20 to 13 ID. 12 to 5 Acceptance Mask Register AMR 0 AMR 1 AMR 2 Acceptance Code Register ACR 0 ACR 1 ACR 2 Byte 3 ID. 4 ID. 3 ID. 2 ID. 1 ID. 0 RTR

Single Filter Example Byte 3 Byte 2 Byte 1 Message ID ID. 28 to

Single Filter Example Byte 3 Byte 2 Byte 1 Message ID ID. 28 to 21 ID. 20 to 13 ID. 12 to 5 Acceptance Mask Register "1" "1" Acceptance Code Register "0" "0" Byte 0 Accepted 16

Using the SJA 1000 Linux driver " The driver uses receive and transmit interrupts

Using the SJA 1000 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(). 17

Using the SJA 1000 Linux driver File operations (1) " Open: provide blocking and

Using the SJA 1000 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); 18

Using the SJA 1000 Linux driver CAN message data structure typedef struct { Can.

Using the SJA 1000 Linux driver CAN message data structure typedef struct { Can. Id 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; 19

Using the SJA 1000 Linux driver File operations (2) " Read: return a message

Using the SJA 1000 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. 20 ret = write(can, &msg, sizeof(canmsg));

Using the SJA 1000 Linux driver File operations (3) " ioctl: specific control operations.

Using the SJA 1000 Linux driver File operations (3) " ioctl: specific control operations. unsigned long baud_rate = B 1000; ioctl(can, CAN_IOCSBAUD, &baud_rate); " Supported operations: �CAN_IOCSBAUD: set baud rate. The following constants are used to set it: B 1000 (1 Mbit/s), B 500 (500 kbit/s), B 250 (250 kbit/s), B 125 (125 kbit/s), B 20 (20 kbit/s). 21

Using the SJA 1000 Linux driver File operations (4) " Supported operations: �CAN_IOCSAMASK: set

Using the SJA 1000 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. 22

Using the SJA 1000 Linux driver File operations (5) �CAN_IOCSACTIVE: set active mode. �CAN_IOCSPASSIVE:

Using the SJA 1000 Linux driver File operations (5) �CAN_IOCSACTIVE: set active mode. �CAN_IOCSPASSIVE: set passive mode. �CAN_IOCRREG: read a SJA 1000 register. The most useful ones for applications are 'ERROR_CODE_CAPTURE' 'RX_ERROR_COUNTER' 'TX_ERROR_COUNTER' 23