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 };
/*flags for the client struct: */ #define I2C_CLIENT_PEC 0x04 /* Use Packet Error Checking */ #define I2C_CLIENT_TEN 0x10 /* we have a ten bit chip address */ /* Must equal I2C_M_TEN below */ #define I2C_CLIENT_SLAVE 0x20 /* we are the slave */ #define I2C_CLIENT_WAKE 0x80 /* for board_info; true iff can wake */ #define I2C_CLIENT_SCCB 0x9000 /* Use Omnivision SCCB protocol */ /* Must match I2C_M_STOP|IGNORE_NAK */ structi2c_client { unsignedshort flags; /* div., see below */ unsignedshort addr; /* chip address - NOTE: 7bit */ /* addresses are stored in the */ /* _LOWER_ 7 bits */ char name[I2C_NAME_SIZE]; structi2c_adapter *adapter;/* the adapter we sit on */ structdevicedev;/* the device structure */ int irq; /* irq issued by device */ structlist_headdetected; #if IS_ENABLED(CONFIG_I2C_SLAVE) i2c_slave_cb_t slave_cb; /* callback for slave mode */ #endif };
在该结构体中最重要的成员:
addr:表示该I2C设备的设备地址
adapter:表示该I2C设备挂载在哪个I2C Controller下
结构体i2c_msg封装了需要传输的数据:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
structi2c_msg { __u16 addr; /* slave address */ __u16 flags; /* 表示读或写 */ #define I2C_M_RD 0x0001 /* read data, from slave to master */ /* I2C_M_RD is guaranteed to be 0x0001! */ #define I2C_M_TEN 0x0010 /* this is a ten bit chip address */ #define I2C_M_RECV_LEN 0x0400 /* length will be first received byte */ #define I2C_M_NO_RD_ACK 0x0800 /* if I2C_FUNC_PROTOCOL_MANGLING */ #define I2C_M_IGNORE_NAK 0x1000 /* if I2C_FUNC_PROTOCOL_MANGLING */ #define I2C_M_REV_DIR_ADDR 0x2000 /* if I2C_FUNC_PROTOCOL_MANGLING */ #define I2C_M_NOSTART 0x4000 /* if I2C_FUNC_NOSTART */ #define I2C_M_STOP 0x8000 /* if I2C_FUNC_PROTOCOL_MANGLING */ __u16 len; /* msg length */ __u8 *buf; /* pointer to msg data */ };
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, or '?') 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)
使用例子:
1 2 3 4 5 6 7 8 9 10 11 12
// Example (bus 0, read 8 byte at offset 0x64 from EEPROM at 0x50): # i2ctransfer -f -y 0 w1@0x50 0x64 r8
// Example (bus 0, write 3 byte at offset 0x64 from EEPROM at 0x50): # i2ctransfer -f -y 0 w9@0x50 0x64 val1 val2 val3
// Example:first: (bus 0, write 3 byte at offset 0x64 from EEPROM at 0x50) // and then: (bus 0, read 3 byte at offset 0x64 from EEPROM at 0x50) # i2ctransfer -f -y 0 w9@0x50 0x64 val1 val2 val3 r3@0x50 # i2ctransfer -f -y 0 w9@0x50 0x64 val1 val2 val3 r3 //如果设备地址不变,后面的设备地址可 省略