----------------------------------------------------------------------------------------------- Typical CAN sequence (open -> close) ----------------------------------------------------------------------------------------------- - open device with ''FILE can_fd = open("/dev/can0", O_RDWR)'' (MANDATORY) starting with 2008-Mar-01 the CAN chip starts with CAN_CONFIG_MODE before CAN_NORMAL_MODE was set immediately after ioctl CAN_IOC_BAUDRATE!!) => ioctl CAN_IOC_MODE with CAN_NORMAL_MODE is MANDATORY!!) - set CAN chip to configuration mode with ''ioctl(can_fd, CAN_IOC_MODE, CAN_CONFIG_MODE)'' (optional) only needed for compatibility with versions before 2008-Mar-01 can be omitted for new implementations after 2008-Mar-01 - set baud rate with ''ioctl(can_fd, CAN_IOC_BAUDRATE, iBaudrate)'' (optional, default: 1000000 bit/s) - set sample mode with ''ioctl(can_fd, CAN_IOC_SAMPLE, b3SamplesPerBit)'' (optional, default: 0) - set a receive filter with ''ioctl(can_fd, CAN_IOC_FILTER, sFilter)'' (optional, default: receive all messages) ATTENTION: even if you are ONLY transmitting, the driver receives CAN-messages!! => - change chip to normal operational mode ''ioctl(can_fd, CAN_IOC_MODE, CAN_NORMAL_MODE)'' (MANDATORY for normal CAN-operation) CAN_LISTEN_MODE is also possible (LOOPBACK and SLEEP are not tested now 2008-Mar) - normal/listen CAN operation - set maximum retry count with ''ioctl(can_fd, CAN_IOC_SET_RETRY, iMaxTxRetries)'' (optional, default: 10) the retry count can be set at ANY time, can also be set with other configurations - stop CAN-communication with ''ioctl(can_fd, CAN_IOC_MODE, CAN_CONFIG_MODE)'' (optional) - reset driver to free all remaining rx/tx mesages ''ioctl(can_fd, CAN_IOC_RESET, 0)'' (optional) - OR close device directly with ''close(can_fd);'' (MANDATORY) internal ''ioctl(can_fd, CAN_IOC_MODE, CAN_CONFIG_MODE)'' and ''ioctl(can_fd, CAN_IOC_RESET, 0)'' are executed!!
#include <linux/ioctl.h>
Go to the source code of this file.
Classes | |
struct | mcp2515_filter_t |
Filter structure of the MCP2515 CAN chip. More... | |
struct | msg_t |
default CAN message structure More... | |
struct | reg_t |
struct to set CAN register via ioctl() More... | |
Defines | |
Mode definitions for CAN_IOC_MODE calls | |
#define | CAN_CONFIG_MODE 4 |
CONFIG mode used with ioctl CAN_IOC_MODE. | |
#define | CAN_LISTEN_MODE 3 |
LISTEN mode used with ioctl CAN_IOC_MODE. | |
#define | CAN_LOOPBACK_MODE 2 |
LOOPBACK mode used with ioctl CAN_IOC_MODE. | |
#define | CAN_NORMAL_MODE 0 |
NORMAL mode used with ioctl CAN_IOC_MODE. | |
#define | CAN_SLEEP_MODE 1 |
SLEEP mode used with ioctl CAN_IOC_MODE. | |
CAN driver ioctl-commands | |
If the ioctl is write or read/write (meaning output is returned to the calling process), the ioctl call returns the output of this function.
Otherwise the returnvalue is ALWAYs zero except for an undefined ioctl command call that returns -EINVAL. | |
#define | CAN_IOC_BAUDRATE _IOW(CAN_MAGIC, 0x80, unsigned long) |
Set the baudrate for CAN communication. | |
#define | CAN_IOC_FILTER _IOW(CAN_MAGIC, 0x87, struct mcp2515_filter_t *) |
Set CAN chip filter values for CAN message receive. | |
#define | CAN_IOC_GET_BUS_OFF_ERR _IO(CAN_MAGIC, 0x93) |
Get the number of bus off errors. | |
#define | CAN_IOC_GET_FILTER _IOR(CAN_MAGIC, 0x88, struct mcp2515_filter_t *) |
Retrieve current CAN chip filter settings. | |
#define | CAN_IOC_GET_RX_ERR _IO(CAN_MAGIC, 0x91) |
Get the number of errors receiving a message. | |
#define | CAN_IOC_GET_RX_OK _IO(CAN_MAGIC, 0x92) |
Get the number of successfull received messages. | |
#define | CAN_IOC_GET_TX_ERR _IO(CAN_MAGIC, 0x8F) |
Get the number of the actual lost transmit messages. | |
#define | CAN_IOC_GET_TX_OK _IO(CAN_MAGIC, 0x90) |
Get the number successfull transmitted messages. | |
#define | CAN_IOC_INT _IO(CAN_MAGIC, 0x85) |
Generate a test interrupt. | |
#define | CAN_IOC_MODE _IOW(CAN_MAGIC, 0x82, unsigned char) |
Set the operational mode of the CAN chip. | |
#define | CAN_IOC_REG _IOW(CAN_MAGIC, 0x86, unsigned char) |
Read a CAN chip register. | |
#define | CAN_IOC_RESET _IO(CAN_MAGIC, 0x84) |
Reset the chip. | |
#define | CAN_IOC_RXQUEUE_SIZE _IO(CAN_MAGIC, 0x8B) |
Get number of messages of the receive queue. | |
#define | CAN_IOC_SAMPLE _IOW(CAN_MAGIC, 0x81, unsigned char) |
Set the bit sampling mode of the CAN chip. | |
#define | CAN_IOC_SET_REG _IOW(CAN_MAGIC, 0x89, struct reg_t *) |
Write a value to a CAN chip register. | |
#define | CAN_IOC_SET_RETRY _IOW(CAN_MAGIC, 0x8E, unsigned long) |
Set the maximum number of retries in case of erroneous transmit (default: 10). | |
#define | CAN_IOC_STATUS _IO(CAN_MAGIC, 0x83) |
Write out the CAN chip status via printk(KERN_INFO . . . ) => (use dmesg to view). | |
#define | CAN_IOC_TXQUEUE_FULL _IO(CAN_MAGIC, 0x8A) |
Test wether the transmit queue is full. | |
General defines (DON'T edit) | |
#define | CAN_MAGIC 'z' |
magic for ioctl access => DON'T edit | |
#define | MAJOR_NUM 100 |
The major device number. | |
#define | SAVE_TIMESTAMP |
used ONLY for internal time measurement => DON'T edit | |
CAN driver structures and macros | |
#define | CANMSG_SET_EID(eid, p_canmsg) {p_canmsg->ident = (eid >> 18) & 0x7FF; p_canmsg->eident = eid & 0x3FFFF; p_canmsg->bits |= 0x1;} |
Macro to set the extended identifier (EID) of a can message. | |
#define | CANMSG_SET_ID(id, p_canmsg) {p_canmsg->ident = id & 0x7FF; p_canmsg->bits &= 0xFE;} |
Macro to set the (normal) identifier (ID) of a can message. | |
Driver test ioctl-commands (i.g. not used) | |
#define | IOCTL_GET_MSG _IOR(MAJOR_NUM, 1, char *) |
Get the message of the device driver. | |
#define | IOCTL_GET_NTH_BYTE _IOWR(MAJOR_NUM, 2, int) |
Get the n'th byte of the message. | |
#define | IOCTL_SET_MSG _IOR(MAJOR_NUM, 0, char *) |
Set the message of the device driver. |
#define CAN_CONFIG_MODE 4 |
CONFIG mode used with ioctl CAN_IOC_MODE.
#define CAN_IOC_BAUDRATE _IOW(CAN_MAGIC, 0x80, unsigned long) |
Set the baudrate for CAN communication.
(Internal change to config mode!!) According to the selected baudrate the internal (message lost arbitration) MLOA timeout is set if the chip is in one shot mode (OSM, default).
CAN_MAGIC | number that identifies our CAN device | |
0x80 | number of the command | |
unsigned_long | new baudrate[bits/second] to be set The following baudrates are supported:
|
#define CAN_IOC_FILTER _IOW(CAN_MAGIC, 0x87, struct mcp2515_filter_t *) |
Set CAN chip filter values for CAN message receive.
(Internal change to config mode!!)
CAN_MAGIC | number that identifies our CAN device | |
0x87 | number of the command | |
mcp2515_filter_t | new filter value |
#define CAN_IOC_GET_BUS_OFF_ERR _IO(CAN_MAGIC, 0x93) |
Get the number of bus off errors.
CAN_MAGIC | number that identifies our CAN device | |
0x93 | number of the command |
#define CAN_IOC_GET_FILTER _IOR(CAN_MAGIC, 0x88, struct mcp2515_filter_t *) |
Retrieve current CAN chip filter settings.
CAN_MAGIC | number that identifies our CAN device | |
0x88 | number of the command | |
mcp2515_filter_t | read out filter settings of the CAN chip |
#define CAN_IOC_GET_RX_ERR _IO(CAN_MAGIC, 0x91) |
Get the number of errors receiving a message.
CAN_MAGIC | number that identifies our CAN device | |
0x91 | number of the command |
#define CAN_IOC_GET_RX_OK _IO(CAN_MAGIC, 0x92) |
Get the number of successfull received messages.
CAN_MAGIC | number that identifies our CAN device | |
0x92 | number of the command |
#define CAN_IOC_GET_TX_ERR _IO(CAN_MAGIC, 0x8F) |
Get the number of the actual lost transmit messages.
CAN_MAGIC | number that identifies our CAN device | |
0x8F | number of the command |
#define CAN_IOC_GET_TX_OK _IO(CAN_MAGIC, 0x90) |
Get the number successfull transmitted messages.
CAN_MAGIC | number that identifies our CAN device | |
0x90 | number of the command |
#define CAN_IOC_INT _IO(CAN_MAGIC, 0x85) |
Generate a test interrupt.
CAN_MAGIC | number that identifies our CAN device | |
0x85 | number of the command |
#define CAN_IOC_MODE _IOW(CAN_MAGIC, 0x82, unsigned char) |
Set the operational mode of the CAN chip.
CAN_MAGIC | number that identifies our CAN device | |
0x82 | number of the command | |
unsigned_char | type of new sampling 0 ... NORMAL mode => use define CAN_NORMAL_MODE 1 ... SLEEP mode => use define CAN_SLEEP_MODE 2 ... LOOPBACK mode => use define CAN_LOOPBACK_MODE 3 ... LISTEN ONLY mode => use define CAN_LISTEN_MODE 4 ... CONFIGURATION mode => use define CAN_CONFIG_MODE |
#define CAN_IOC_REG _IOW(CAN_MAGIC, 0x86, unsigned char) |
Read a CAN chip register.
CAN_MAGIC | number that identifies our CAN device | |
0x86 | number of the command | |
unsigned_char | offset of CAN chip register |
#define CAN_IOC_RESET _IO(CAN_MAGIC, 0x84) |
Reset the chip.
CAN_MAGIC | number that identifies our CAN device | |
0x84 | number of the command |
#define CAN_IOC_RXQUEUE_SIZE _IO(CAN_MAGIC, 0x8B) |
Get number of messages of the receive queue.
CAN_MAGIC | number that identifies our CAN device | |
0x8B | number of the command |
#define CAN_IOC_SAMPLE _IOW(CAN_MAGIC, 0x81, unsigned char) |
Set the bit sampling mode of the CAN chip.
(Internal change to config mode!!)
CAN_MAGIC | number that identifies our CAN device | |
0x81 | number of the command | |
unsigned_char | type of new sampling 0 ... one sample per bit 1 ... three samples per bit |
#define CAN_IOC_SET_REG _IOW(CAN_MAGIC, 0x89, struct reg_t *) |
Write a value to a CAN chip register.
CAN_MAGIC | number that identifies our CAN device | |
0x89 | number of the command | |
struct_reg_t | register offset and value to be written |
#define CAN_IOC_SET_RETRY _IOW(CAN_MAGIC, 0x8E, unsigned long) |
Set the maximum number of retries in case of erroneous transmit (default: 10).
Used if the CAN chip is in ONE SHOT MODE (default).
CAN_MAGIC | number that identifies our CAN device | |
0x8E | number of the command | |
unsigned_long | number of retries, default: 10 |
#define CAN_IOC_STATUS _IO(CAN_MAGIC, 0x83) |
Write out the CAN chip status via printk(KERN_INFO . . . ) => (use dmesg to view).
CAN_MAGIC | number that identifies our CAN device | |
0x83 | number of the command |
#define CAN_IOC_TXQUEUE_FULL _IO(CAN_MAGIC, 0x8A) |
Test wether the transmit queue is full.
CAN_MAGIC | number that identifies our CAN device | |
0x8A | number of the command |
#define CAN_LISTEN_MODE 3 |
LISTEN mode used with ioctl CAN_IOC_MODE.
#define CAN_LOOPBACK_MODE 2 |
LOOPBACK mode used with ioctl CAN_IOC_MODE.
#define CAN_MAGIC 'z' |
magic for ioctl access => DON'T edit
#define CAN_NORMAL_MODE 0 |
NORMAL mode used with ioctl CAN_IOC_MODE.
#define CAN_SLEEP_MODE 1 |
SLEEP mode used with ioctl CAN_IOC_MODE.
#define CANMSG_SET_EID | ( | eid, | |||
p_canmsg | ) | {p_canmsg->ident = (eid >> 18) & 0x7FF; p_canmsg->eident = eid & 0x3FFFF; p_canmsg->bits |= 0x1;} |
Macro to set the extended identifier (EID) of a can message.
eid | 29-bit extended identifier for CAN message | |
p_canmsg | address of CAN message structure (struct msg_t*) |
#define CANMSG_SET_ID | ( | id, | |||
p_canmsg | ) | {p_canmsg->ident = id & 0x7FF; p_canmsg->bits &= 0xFE;} |
Macro to set the (normal) identifier (ID) of a can message.
id | 11-bit identifier for CAN message | |
p_canmsg | address of CAN message structure (struct msg_t*) |
#define IOCTL_GET_MSG _IOR(MAJOR_NUM, 1, char *) |
Get the message of the device driver.
This IOCTL is used for output, to get the message of the device driver. However, we still need the buffer to place the message in to be input, as it is allocated by the process.
MAJOR_NUM | is the major device number we're using | |
1 | The second argument is the number of the command (there could be several with different meanings). | |
char* | The third argument is the type we want to get from the kernel to the process. |
#define IOCTL_GET_NTH_BYTE _IOWR(MAJOR_NUM, 2, int) |
Get the n'th byte of the message.
The IOCTL is used for both input and output. It receives from the user a number, n, and returns Message[n].
MAJOR_NUM | is the major device number we're using | |
2 | The second argument is the number of the command (there could be several with different meanings). | |
int | The third argument n is the index for the message we want to get. |
#define IOCTL_SET_MSG _IOR(MAJOR_NUM, 0, char *) |
Set the message of the device driver.
_IOR means that we're creating an ioctl command number for passing information from a user process to the kernel module.
MAJOR_NUM | is the major device number we're using | |
0 | The second argument is the number of the command (there could be several with different meanings). | |
char* | The third argument is the type we want to get from the process to the kernel. |
#define MAJOR_NUM 100 |
The major device number.
We can't rely on dynamic registration any more, because ioctls need to know it. DON'T edit
#define SAVE_TIMESTAMP |
used ONLY for internal time measurement => DON'T edit