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.
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
4 Physical Connection Microcontroller SJA1000 Transceiver (PCA82C250) TX RX CAN_H CAN_L CAN bus
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)
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
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
8 Interrupts "Interrupts provided: Bus Error Arbitration Lost Error Passive Wake-up Data Overrun Error Warning Transmit Receive "Can be idividually enabled/disabled
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)
10 Bus Timing 0/1 Registers (cont) SYNC SEG TSEG1 TSEG2 Sample point(s) Nominal bit time = 1 TQ Time quantum (TQ) is used to define segments
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
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 FFRTRXXDLC.3DLC.2DLC.1DLC.0
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
14 Filtering "Two modes of filtering: Single filter: 32-bit filter "Standard Frame: identifier + RTR + data1 + data2 "Extended Frame: identifier + RTR Dual filter: at least one filter must match "SF: 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
15 Single Filter for Extended Frame Format ID.4ID.3ID.2ID.1ID.0 Message ID AMR3.7AMR3.6AMR3.5 AMR3.4 AMR3.3AMR3.2 X X Acceptance Mask Register ACR3.7ACR3.6ACR3.5ACR3.4ACR3.3ACR3.2 XX Acceptance Code Register AMR 2 AMR 1 AMR 0 ID.12 to 5ID.20 to 13ID.28 to 21 RTR ACR 2ACR 1ACR 0 Byte 0 Byte 1 Byte 2Byte 3
16 Single Filter Example Message ID Acceptance Mask Register Acceptance Code Register Accepted "1" ID.12 to 5ID.20 to 13ID.28 to 21 "0" Byte 3 Byte 2Byte 1 Byte 0
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().
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);
19 Using the SJA1000 Linux driver CAN message data structure typedef 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 ). } canmsg;
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));
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).
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.
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'