Skip navigation.
Home
The QNX Community Portal

View topic - write i2c with DCMD_I2C_SEND

write i2c with DCMD_I2C_SEND

For discussion of realtime and/or embedded programming.

write i2c with DCMD_I2C_SEND

Postby djidji » Tue Aug 02, 2016 8:32 am

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.
djidji
Active Member
 
Posts: 21
Joined: Tue Jun 28, 2016 1:17 pm

Re: write i2c with DCMD_I2C_SEND

Postby nico04 » Tue Aug 02, 2016 8:52 am

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).
Code: Select all
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;
}
nico04
Active Member
 
Posts: 77
Joined: Wed Sep 29, 2010 9:59 am
Location: France

Re: write i2c with DCMD_I2C_SEND

Postby djidji » Tue Aug 02, 2016 9:17 am

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: Select all
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 );
djidji
Active Member
 
Posts: 21
Joined: Tue Jun 28, 2016 1:17 pm

Re: write i2c with DCMD_I2C_SEND

Postby nico04 » Tue Aug 02, 2016 9:37 am

Put the register address in MemoryAddress of the following function.
Code: Select all
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;
}
nico04
Active Member
 
Posts: 77
Joined: Wed Sep 29, 2010 9:59 am
Location: France

Re: write i2c with DCMD_I2C_SEND

Postby djidji » Tue Aug 02, 2016 5:20 pm

thank you

the mine is working, I just forgot to modify the "lng" to 2 when I want to send one byte
Last edited by djidji on Wed Aug 03, 2016 4:23 pm, edited 1 time in total.
djidji
Active Member
 
Posts: 21
Joined: Tue Jun 28, 2016 1:17 pm

Re: write i2c with DCMD_I2C_SEND

Postby nico04 » Wed Aug 03, 2016 6:28 am

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 .
nico04
Active Member
 
Posts: 77
Joined: Wed Sep 29, 2010 9:59 am
Location: France


Return to Realtime and Embedded

Who is online

Users browsing this forum: No registered users and 0 guests

cron