structi2c_adapter { structmodule *owner; unsignedintclass;/* classes to allow probing for */ conststructi2c_algorithm *algo;/* the algorithm to access the bus */ void *algo_data;
/* data fields that are valid for all devices */ conststructi2c_lock_operations *lock_ops; structrt_mutexbus_lock; structrt_mutexmux_lock;
int timeout; /* in jiffies */ int retries; structdevicedev;/* the adapter device */
int nr; char name[48]; structcompletiondev_released;
structi2c_algorithm { /* If an adapter algorithm can't do I2C-level access, set master_xfer to NULL. If an adapter algorithm can do SMBus access, set smbus_xfer. If set to NULL, the SMBus protocol is simulated using common I2C messages */ /* master_xfer should return the number of messages successfully processed, or a negative value on error */ int (*master_xfer)(struct i2c_adapter *adap, struct i2c_msg *msgs, int num); int (*smbus_xfer) (struct i2c_adapter *adap, u16 addr, unsignedshort flags, char read_write, u8 command, int size, union i2c_smbus_data *data);
/* To determine what the adapter supports */ u32 (*functionality) (struct i2c_adapter *);
#if IS_ENABLED(CONFIG_I2C_SLAVE) int (*reg_slave)(struct i2c_client *client); int (*unreg_slave)(struct i2c_client *client); #endif };
/* Notifies the driver that a new bus has appeared. You should avoid * using this, it will be removed in a near future. */ int (*attach_adapter)(struct i2c_adapter *) __deprecated;
/* Standard driver model interfaces */ int (*probe)(struct i2c_client *, conststruct i2c_device_id *); int (*remove)(struct i2c_client *);
/* driver model interfaces that don't relate to enumeration */ void (*shutdown)(struct i2c_client *);
/* Alert callback, for example for the SMBus alert protocol. * The format and meaning of the data value depends on the protocol. * For the SMBus alert protocol, there is a single bit of data passed * as the alert response's low bit ("event flag"). * For the SMBus Host Notify protocol, the data corresponds to the * 16-bit payload data reported by the slave device acting as master. */ void (*alert)(struct i2c_client *, enum i2c_alert_protocol protocol, unsignedint data);
/* a ioctl like command that can be used to perform specific functions * with the device. */ int (*command)(struct i2c_client *client, unsignedint cmd, void *arg);
[root@100ask:~]# ls /sys/bus/i2c/devices/ 1-001a 1-0039 1-005d i2c-0 i2c-1
使用示例
i2cdetect
i2cdetect 用于扫描 i2c 总线上的设备,并显示地址。
1 2 3 4 5 6 7 8 9 10 11 12 13
Usage: i2cdetect [-y] [-a] [-q|-r] I2CBUS [FIRST LAST] i2cdetect -F I2CBUS i2cdetect -l I2CBUS is an integer or an I2C bus name If provided, FIRST and LAST limit the probing range.
Usage: i2cset [-f] [-y] [-m MASK] [-r] [-a] I2CBUS CHIP-ADDRESS DATA-ADDRESS [VALUE] ... [MODE] I2CBUS is an integer or an I2C bus name ADDRESS is an integer (0x03 - 0x77, or 0x00 - 0x7f if -a is given) MODE is one of: c (byte, no value) b (byte data, default) w (word data) i (I2C block data) s (SMBus block data) Append p for SMBus PEC -f:强制执行,即使在某些情况下可能不安全。 -y:自动应答,不提示用户确认。 -m MASK:对数据应用掩码。只有掩码中为1的位会被写入,其余位保持不变。 -r:如果设置了,并且指定了模式 c(即无值的字节命令),则在写入命令后立即读取一个字节的数据。 -a:允许10位地址的设备。默认情况下,i2cset 只处理7位地址的设备。 I2CBUS:指定I2C总线的编号或名称。 CHIP-ADDRESS:i2c设备地址。 DATA-ADDRESS:i2c寄存器地址 VALUE:要写入的数据值。具体需要根据后面指定的 MODE 来确定数据格式。 MODE:指定数据传输的模式,可以是以下几种之一: c:发送一个无数据的字节命令。 b:发送一个字节的数据(默认模式)。 w:发送一个字(16位)的数据。 i:发送I2C块数据。 s:发送SMBus块数据。
Usage: i2cget [-f] [-y] [-a] I2CBUS CHIP-ADDRESS [DATA-ADDRESS [MODE]] I2CBUS is an integer or an I2C bus name ADDRESS is an integer (0x03 - 0x77, or 0x00 - 0x7f if -a is given) MODE is one of: b (read byte data, default) w (read word data) c (write byte/read byte) Append p for SMBus PEC -f:强制执行,即使在某些情况下可能不安全。 -y:自动应答,不提示用户确认。 -a:允许10位地址的设备。默认情况下,i2cset 只处理7位地址的设备。 I2CBUS:指定I2C总线的编号或名称。 CHIP-ADDRESS:i2c设备地址。 DATA-ADDRESS:i2c寄存器地址 MODE:指定数据传输的模式,可以是以下几种之一: c:发送一个无数据的字节命令。 b:发送一个字节的数据(默认模式)。 w:发送一个字(16位)的数据。 i:发送I2C块数据。 s:发送SMBus块数据。
Usage: i2cdump [-f] [-y] [-r first-last] [-a] I2CBUS ADDRESS [MODE [BANK [BANKREG]]] I2CBUS is an integer or an I2C bus name ADDRESS is an integer (0x03 - 0x77, or 0x00 - 0x7f if -a is given) MODE is one of: b (byte, default) w (word) W (word on even register addresses) s (SMBus block) i (I2C block) c (consecutive byte) Append p for SMBus PEC -f:强制执行,即使可能存在风险的操作也会执行。 -y:自动应答,不提示用户确认。 -r first-last:指定要读取的寄存器地址范围。first 是起始地址,last 是结束地址。 -a:允许使用10位地址的设备。默认情况下,i2cdump 只处理7位地址的设备。 I2CBUS:指定要读取的I2C总线的编号或名称。 ADDRESS:i2c设备地址 MODE:指定读取数据的模式,可以是以下几种之一: b:按字节读取(默认模式)。 w:按字(16位)读取。 W:按字(16位)读取,但只在偶数寄存器地址上读取。 s:SMBus块数据读取。 i:I2C块数据读取。 c:连续字节读取。 BANK:对于需要切换寄存器银行的设备,指定要读取的寄存器银行。 BANKREG:寄存器银行切换寄存器的地址。 PEC:如果模式以 p 结尾,表示启用SMBus数据包错误校正(PEC)。
Usage: i2ctransfer [-f] [-y] [-v] [-V] [-a] I2CBUS DESC [DATA] [DESC [DATA]]... I2CBUS is an integer or an I2C bus name DESC describes the transfer in the form: {r|w}LENGTH[@address] 1) read/write-flag 2) LENGTH (range 0-65535) 3) I2C address (use last one if omitted) DATA are LENGTH bytes for a write message. They can be shortened by a suffix: = (keep value constant until LENGTH) + (increase value by 1 until LENGTH) - (decrease value by 1 until LENGTH) p (use pseudo random generator until LENGTH with value as seed)
Example (bus 0, read 8 byte at offset 0x64 from EEPROM at 0x50): # i2ctransfer 0 w1@0x50 0x64 r8 Example (same EEPROM, at offset 0x42 write 0xff 0xfe ... 0xf0): # i2ctransfer 0 w17@0x50 0x42 0xff-