uboot中i2c读写有2种方式,一种使用uboot驱动模型,通过宏 CONFIG_DM_I2C定义,另一种是传统方式,通过宏CONFIG_SYS_I2C定义。
1.设置总线号
int i2c_set_bus_num(unsigned int bus);
2.读/写
int i2c_read(uint8_t chip, unsigned int addr, int alen, uint8_t *buffer, int len);
int i2c_write(uint8_t chip, unsigned int addr, int alen, uint8_t *buffer, int len);
uint8_t i2c_reg_read(uint8_t addr, uint8_t reg);
void i2c_reg_write(uint8_t addr, uint8_t reg, uint8_t val);
函数实现见: drivers/i2c/i2c_core.c
1.根据uclass id和总线编号,获取总线udevice
int uclass_get_device_by_seq(enum uclass_id id, int seq, struct udevice **devp);
2.获取设备udevice
int i2c_get_chip(struct udevice *bus, uint chip_addr, uint offset_len, struct udevice **devp);
3.设置设备寄存器地址长度
int i2c_set_chip_offset_len(struct udevice *dev, uint offset_len);
4.读/写
int dm_i2c_read(struct udevice *dev, uint offset, uint8_t *buffer, int len);
int dm_i2c_read(struct udevice *dev, uint offset, uint8_t *buffer, int len);
int dm_i2c_reg_read(struct udevice *dev, uint offset);
int dm_i2c_reg_write(struct udevice *dev, uint offset, uint value);
函数实现见: drivers/i2c/i2c-uclass.c
DM_I2C在drivers/i2c/i2c-uclass-compat.c中,通过定义宏 CONFIG_DM_I2C_COMPAT,实现了SYS_I2C的兼容,从SYS_I2C切换到DM_I2C时,不用修改原来SYS_I2C的i2c读写流程代码。
在uboot命令行中,通过定义宏CONFIG_CMD_I2C,可以打开i2c cmd 子系统。输入i2c查看 usage。
i2c bus - 查看当前总线
i2c dev [dev] - 设置总线号
i2c md chip address[.0, .1, .2] [# of objects] - i2c设备读
i2c mw chip address[.0, .1, .2] value [count] - i2c设备写
=> i2c bus
Bus 2: i2c@48060000 (active 2)
58: generic_58, offset len 1, flags 0
6b: generic_6b, offset len 1, flags 0
6a: generic_6a, offset len 1, flags 0
=> i2c dev 2
Setting bus to 2
=> i2c md 6b d0.1 1
00d0: 14 .
=> i2c mw 6b d0.1 0x15 1
=>