write i2c with DCMD_I2C_SEND

hi,

I want to write in my I2C slave, I guess that it’s with DCMD_I2C_SEND, so where I shall do the data ?

I put the register address of the slave on index 0 of the the data buffer and the data in the index 1, but it doesn’t work.

The following code should work. It is a stripped version of a more complex function I use (I removed specific code dedicated to my specific usage).

int  I2cWrite_(int fd, uint8_t Address , uint8_t *pBuffer, uint32_t NbData)
{
    i2c_send_t hdr;
    iov_t sv[2];
    int status;

    hdr.slave.addr = Address >> 1;
    hdr.slave.fmt = I2C_ADDRFMT_7BIT;
    hdr.len = NbData;
    hdr.stop = 1;

    SETIOV(&sv[0], &hdr, sizeof(hdr));
    SETIOV(&sv[1], pBuffer, NbData);
    status = devctlv(fd, DCMD_I2C_SEND, 2, 0, sv, NULL, NULL);

    return status;
}

where I shall put the address register of the slave and the data to write.

me i’m using this but it doesn’t work

[code]
struct dataBuf {
i2c_send_t header;
unsigned char data[256];
};

struct dataBuf dev_data;
int ret;

	memset(dev_data.data, 0, sizeof(dev_data.data));
			dev_data.header.slave.addr = addr; // address of factory config information
			dev_data.header.slave.fmt = I2C_ADDRFMT_7BIT;
			dev_data.header.len = lng; //  1
			dev_data.header.stop = 1;
			dev_data.data[0] = 0x20; //register address 
			dev_data.data[1] = 143; //the data to write

	ret = devctl(fd, DCMD_I2C_SEND, &dev_data, sizeof(dev_data), NULL );[/code]

Put the register address in MemoryAddress of the following function.

static int                  I2cWriteMemory_(int fd, uint8_t DeviceId, uint8_t Address, uint32_t NbData, uint8_t MemoryAddress)
{
    i2c_send_t hdr;
    int status;
    iov_t sv[3];


    hdr.slave.addr = Address >> 1;
    hdr.slave.fmt = I2C_ADDRFMT_7BIT;
    hdr.len = NbData + sizeof(MemoryAddress);
    hdr.stop = 1;

    SETIOV(&sv[0], &hdr, sizeof(hdr));
    SETIOV(&sv[1], &MemoryAddress, sizeof(MemoryAddress));
    SETIOV(&sv[2], pBuffer, NbData);
    status = devctlv(fd, DCMD_I2C_SEND, 3, 0, sv, NULL, NULL);

    return status;
}

thank you

the mine is working, I just forgot to modify the “lng” to 2 when I want to send one byte

Great !

You also have to correct the size passed to devctl(). Passing sizeof(dev_data) is incorrect. You should pass sizeof(dev_data.header)+lng .