[i2c] I2C User Application

Satendra Pratap spgroups at gmail.com
Sat Oct 28 11:58:00 CEST 2006


Hi Jean,

On 10/20/06, Jean Delvare <khali at linux-fr.org> wrote:
>
>
> > i2c-dev: i2c-0 reading 1 bytes.
> > i2c_adapter i2c-0: master_recv: reading 1 bytes.
> > i2c_adapter i2c-0: master_recv: return:-1 (count:1, addr:0x00)
> >
> > I would be thankfull If I could get a solution asap.
>
> What do you offer? You can't ask us to help you "ASAP" on our free time
> without some compensation, can you?


My Mistake !! :)

Well, I am using kernel 2.6.11 . Now i have modified the program just for
reading 2 bytes
and its write call is successfull but on reading, it gives "Operation not
permitted" Error. Following is the output:

/home/sp # ./i2cnew
Device File: /dev/i2c-0
Device Address(I2C): 0x50
Device File Opened Successfully !!
 res Outside = 0

 buf[0] = 16
buf[1] = 67
buf[2] = 101
After Write: buf[0] = 16
read ERROR: Error Reason: : Operation not permitted
After read: buf[0] = 16
After read: buf[1] = 0
After read: buf[2] = 0


/home/sp # dmesg
i2c_adapter i2c-0: i2c-0 ioctl, cmd: 0x703, arg: 50.
i2c-dev: i2c-0 writing 3 bytes.
i2c_adapter i2c-0: master_send: writing 3 bytes.
Doing write 3 bytes to 0x50 - 1 of 1 messages
i2c-dev: i2c-0 reading 2 bytes.
i2c_adapter i2c-0: master_recv: reading 2 bytes.
Doing read 2 bytes to 0x50 - 1 of 1 messages
I2C: No RXAK
i2c_adapter i2c-0: master_recv: return:-1 (count:2, addr:0x50)
/home/sp #

from dmesg's output, its clear that RXAK is not received. (I don't know
why?)

I have doubt on a point that when the slave address is using ioctl (
ioctl(file,I2C_SLAVE,addr); Say device addres is 0x50 )

and after it we have written the data using write ( write(file,buf,3); )
then if one wants to read the data back using read then should one need to
pass the device read 0x51 or the Driver will take care of it ?

following is the program:

int main()
{
        int file;
        int adapter_nr = 0; /* probably dynamically determined */
        unsigned long addr = 0x50; /* The I2C address */
        char filename[20] = {0};
        unsigned int size;
        char buf[5] = {0};
        int res;
        int reg = 0x10;

        sprintf(filename,"/dev/i2c-%d",adapter_nr);
        printf("Device File: %s\n",filename);
        printf("Device Address(I2C): 0x%x\n",addr);

        if ((file = open(filename,O_RDWR)) < 0) {
                /* ERROR HANDLING; you can check errno to see what went
wrong */
                printf("Error Occured while opening Device File.!!\n");
                exit(1);
        } else {
                printf("Device File Opened Successfully !!\n");
        }

        addr = 0x50;
        res = ioctl(file,I2C_SLAVE,addr);
        if (res < 0) {
                printf("Error Occrued inside IOCTL!! : res = %d\n\n",res);
                printf("error no= %d\n\n",errno);
                perror("Error Reason: ");
                /* ERROR HANDLING; you can check errno to see what went
wrong */
                exit(1);
        }
                printf(" res Outside = %d\n\n ",res);

        /* Using I2C Write, equivalent of
i2c_smbus_write_word_data(file,register,0x6543) */
        buf[0] = reg;
        buf[1] = 0x43;
        buf[2] = 0x65;
        printf("buf[0] = %d\n",buf[0]);
        printf("buf[1] = %d\n",buf[1]);
        printf("buf[2] = %d\n",buf[2]);
        if ( write(file,buf,3) != 3) {
        /* ERROR HANDLING: i2c transaction failed */
                perror("write ERROR: Error Reason: ");
        }
        buf[1] = 0x0;
        buf[2] = 0x0;
        printf("After Write: buf[0] = %d\n",buf[0]);
        /* Using I2C Read, equivalent of i2c_smbus_read_byte(file) */
        if (read(file,buf,2) != 2) {
                perror("read ERROR: Error Reason: ");
        /* ERROR HANDLING: i2c transaction failed */
        } else {
        /* buf[0] contains the read byte */
        }
        printf("After read: buf[0] = %d\n",buf[0]);
        printf("After read: buf[1] = %d\n",buf[1]);
        printf("After read: buf[2] = %d\n",buf[2]);

        close(filename);
        return 0;
}

Thanks,

Satendra
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.lm-sensors.org/pipermail/i2c/attachments/20061028/9d31058f/attachment-0001.html 


More information about the i2c mailing list