0

我需要编写一个 I2C 程序,该程序可以使用 I2C_RDWR ioctl 将数据写入 32 位和 16 位寄存器地址。

据我所知,设备地址和数据使用 linux i2c 驱动程序中定义的 i2c_msg 结构中的相同缓冲区 buf 一起传递到设备中:

struct i2c_msg {
        __u16 addr;      slave address
        __u16 flags;
#define I2C_M_TEN       0x10     we have a ten bit chip address
#define I2C_M_RD        0x01
#define I2C_M_NOSTART   0x4000
#define I2C_M_REV_DIR_ADDR      0x2000
#define I2C_M_IGNORE_NAK        0x1000
#define I2C_M_NO_RD_ACK         0x0800
        __u16 len;               msg length
        __u8 *buf;               pointer to msg data
};

目前我正在传递 32 位寄存器地址,如下所示:

    buf[3] = reg_addr & 0xff;
    buf[2] = (reg_addr >> 8) & 0xff;
    buf[1] = (reg_addr >> 16) & 0xff;
    buf[0] = (reg_addr >>24) & 0xff;
 //buf[4,5...] = data;
    msg.buf = buf;

如果寄存器地址为 16 位,则执行以下代码:

    buf[1] = (reg_addr & 0xff);
    buf[0] = (reg_addr >> 8);
    buf[2] = data;
 // buf[2,3...] = data;

我如何让设备知道有多少缓冲区数组包含地址?

另外,这段代码正确吗?

不幸的是,我目前没有任何 I2C 设备可供测试。

欢迎任何帮助。

谢谢。

4

1 回答 1

2

I2C 命令将向由 I2C 地址选择的设备发送字节流。如何解释这些完全取决于设备,您需要阅读设备数据表才能找到答案。

For example, consider Atmel I2C EEPROMs. Devices 512kBit or less use a fixed 16 bit address, and the 1MBit AT24C1024B uses a 17 bit address. 16 bits of this appear in the I2C command stream, and the MSB is encoded into the I2C device address.

于 2012-03-26T12:31:39.897 回答