I2C test application from userspace

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

I2C test application from userspace

Jump to solution
19,806 Views
srinivasanshanm
Contributor III

Dear All,


I'm trying to write the eeprom userspace test application,


when I run the below test app, I see the below messages

BusyBox v1.15.0 () built-in shell (ash)

Enter 'help' for a list of built-in commands.

root@freescale ~$ MY_TEST_APPLICATION_EEPROM

SUCCESS: open(3) passed

ERROR: ioctl(fd, I2C_SLAVE, 0x50) failed

root@freescale ~$

I even tried changing it to   "status = ioctl(fd, I2C_SLAVE_FORCE, i2c_addr);"  , but still the same issue, could you please let me know how to get rid of this issue, as am trying this from since 2 days, not able to succeed further

Any help would be greatly appreciated

Thanks in advance,

#include <stdio.h>

#include <unistd.h>

#include <stdlib.h>

#include <sys/types.h>

#include <sys/stat.h>

#include <sys/ioctl.h>

#include <fcntl.h>

#include <linux/fs.h>

#include <errno.h>

#include <string.h>

#include <linux/i2c-dev.h>

#include <linux/i2c.h>

    /* chmod 666 /dev/i2c-0 */

int main(void)

{

       // Data to be written to eeprom

unsigned char wbuf[17] =  {   0x00, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F };                

unsigned char rbuf[16];

int i2c_addr = 0xa0;

int status;

unsigned short int i;

// OPENING I2C DEVICE

int fd = open("/dev/i2c-0", O_RDWR);

if (fd < 0) {

printf("ERROR: open(%d) failed\n", fd);

return -1;

}

  printf("\nSUCCESS: open(%d) passed\n", fd);

// SETTING EEPROM ADDR

    status = ioctl(fd, I2C_SLAVE, i2c_addr>>1);

    if (status < 0)

    {

printf("ERROR: ioctl(fd, I2C_SLAVE, 0x%02X) failed\n", i2c_addr);

close(fd);

return -1;

        }

    printf("\nSUCCESS: ioctl(fd, I2C_SLAVE, 0x%02X>>1) passed\n", i2c_addr);

// WRITING TO EEPROM

printf ("\nPerforming EEPROM Write operation\n");

write(fd,wbuf,16);

sleep(10); //till eeprom completes writes

if (write(fd,wbuf,16) != 16) {

printf("ERROR: write() failed\n");

close(fd);

return -1;

}

printf("\nSUCCESS: Data written to the EEPROM\n");

// READING FROM EEPROM

printf ("\nPerforming EEPROM Read operation\n");

wbuf[0]=0;

if(write(fd,wbuf,1)!=1){

printf("ERROR: buffer pointer initialization of read() failed\n");

}

  if (read(fd,rbuf,16) != 16) {

printf("ERROR: read() failed\n");

close(fd);

return -1;

}

for(i = 0; i< 16; i++)

printf("ReadBuffer[%d] %d \r\n", i, rbuf[i]);

printf("\nSUCCESS: Data READ from the EEPROM\n");

printf("\neerprom test successfull \n");

close(fd);

}

Labels (2)
0 Kudos
Reply
1 Solution
4,343 Views
raymondwang
Senior Contributor I

You can refer to http://www.lm-sensors.org/browser/i2c-tools/trunk/eepromer?rev=5202

There are some examples of i2c utilities to access EEPROM (also including 24Cxx serial eeprom).

View solution in original post

0 Kudos
Reply
7 Replies
4,343 Views
YixingKong
Senior Contributor IV

Srinivasan

Had your issue got resolved? If yes, we are going to close the discussion in 3 days. If you still need help please feel

free to contact Freescale.

Thanks,
Yixing

0 Kudos
Reply
4,343 Views
srinivasanshanm
Contributor III

Please close this issue it is resolved, thanks a lot for your support

0 Kudos
Reply
4,344 Views
raymondwang
Senior Contributor I

You can refer to http://www.lm-sensors.org/browser/i2c-tools/trunk/eepromer?rev=5202

There are some examples of i2c utilities to access EEPROM (also including 24Cxx serial eeprom).

0 Kudos
Reply
4,343 Views
srinivasanshanm
Contributor III

Could anybody please do the needful in addressing the above thread, as early as possible,

Any help would be greatly appreciated,

Many Many thanks in advance again...

0 Kudos
Reply
4,343 Views
Raybiztech
Contributor V

Hi Srinivasan,

     There can be 3 different possibilities,

1) Each registered i2c adapter gets a number starting from 0 ,check eeprom is connected to which adaptor.  

     see /sys/class/i2c-dev/ to see what number corresponds to which adapter.

2) verify eeprom address in datasheet.

3) I think Right shift operation is not necessary as generally driver is take care of sending 7 bit device address in upper bits of byte.

If you share eeprom datasheet as well as board specification it will be better.

Regards,

sasidhar

0 Kudos
Reply
4,343 Views
srinivasanshanm
Contributor III

Hi sasidhar

Thanks a lot for your response,

Am using eeprom 24c08, whose slave address is 0x50

Please find the attached datasheet, evn I tried giving seven bit address this didnt work for me

Thanks in advance

0 Kudos
Reply
4,343 Views
Raybiztech
Contributor V

Hi Srinivasan,

      If you look into data sheet the first 4 bits of device address always be 1010 ie. 0xa while last 4 bits can be changed based on "Table 3. Device Select Code" on page no 3 of datasheet. check for chip enable pin(3) is connected to vcc or gnd ie 1 or 0. if that pin is connected to 1 then b3 bit of address should be 1 & if pin is connected to 0 then b3 bit of address should be 0.otherwise device wont response.


if chip enable pin is 1 then device address is 10101XXX

if chip enable pin is 0 then device address is 10100XXX.

Please read "write operation" on page no 7 of data sheet carefully.

also refer following link

https://www.kernel.org/doc/Documentation/i2c/dev-interface

regards,

Sasidhar

0 Kudos
Reply