Hardware Reference
In-Depth Information
ioctl(2,I2C_RDWR)
While it is possible to use
ioctl(2,I2C_SLAVE)
and then use
read(2)
and
write(2)
calls,
this tends not to be practical. Consequently, the use of the
ioctl(2,I2C_RDWR)
system
call will be promoted here instead. This system call allows considerable flexibility in
carrying out complex I2C I/O transactions.
The general API for any
ioctl(2)
call is as follows:
#include <sys/ioctl.h>
int ioctl(int fd, int request, argp);
where
fd
is the open file descriptor.
request
is the I/O command to perform.
argp
is an argument related to the command (type varies
according to
request
).
returns
-1
(error code in
errno
), number of msgs completed
(when
request = I2C_RDWR
).
Error
Description
EBADF
fd is not a valid descriptor.
EFAULT
argp references an inaccessible memory area.
EINVAL
request or argp is not valid.
When the
request
argument is provided as
I2C_RDWR
, the
argp
argument is a pointer
to
struct i2c_rdwr_ioctl_data
. This structure points to a list of messages and indicates
how many of them are involved.
struct i2c_rdwr_ioctl_data {
struct i2c_msg
msgs; /
ptr to array of simple messages
/
∗
∗
∗
int nmsgs; /
number of messages to exchange
/
∗
∗
};
The individual I/O messages referenced by the preceding structure are described by
struct i2c_msg
:
struct i2c_msg {
__u16 addr; /
7/10 bit slave address
/
∗
∗
__u16 flags; /
Read/Write & options
/
∗
∗
__u16 len; /
No. of bytes in buf
/
∗
∗
__u8
buf; /
Data buffer
/
∗
∗
∗
};