Latest LQ Deal: Latest LQ Deals
Go Back > Forums > Linux Forums > Linux - Hardware > Linux - Embedded & Single-board computer
User Name
Linux - Embedded & Single-board computer This forum is for the discussion of Linux on both embedded devices and single-board computers (such as the Raspberry Pi, BeagleBoard and PandaBoard). Discussions involving Arduino, plug computers and other micro-controller like devices are also welcome.


  Search this Thread
Old 03-02-2011, 11:37 AM   #1
LQ Newbie
Registered: Mar 2011
Posts: 1

Rep: Reputation: 0
i2c-dev.h Issues


I've been trying for days to get i2c-dev working properly. I have everything I need according to instructions, but for some reason I keep getting errors at ioctl. I'm using OpenWRT on a atheros platform. Everything is compiling properly without error but I am getting ioctl errors out the wazoo.


int file;

//Open i2c dev
if(file = open( "/dev/i2c-0", O_RDWR )<0)
enabled = -1;
printf("Unable to open file /dev/i2c-0.\n");
printf("Opened file /dev/i2c-0.\n");
enabled = 1;

printf("File is %d\n",file);

if( ioctl( file, I2C_SLAVE, 0x4b ) < 0 )
printf("Error opening file: %s\n", strerror( errno ) );
printf("Open chip %d FAILED file %d\n", 0x4b, file);
printf("Open chip %d Succeeded file %d\n",0x4b, file);


At the level of ioctl(file, i2C_SLAVE,address) I get the following error: "Error opening file: Invalid argument". So I'm successfully opening /dev/i2c-0 but I can't do a ioctl to the specific chip address. I've already verified the chip address using i2cdetect, and I've also verified that I can read/write with the other i2ctools suite.

What am I missing?

I am including the i2c-dev.h file from the the i2c-tools package. I just put it in my src directory and I #include "i2c-dev.h" as my final include in my .cpp file. All the defines and functions are configuring correctly, so I imagine it's included right. But I wonder if I need to compile the i2c-dev.h file separately or in a different way than my other code?

I am compiling with -O2 as I understand that's somehow required. Not sure if there needs to be anything else special in my make file since everything is building correctly without errors.
Old 03-24-2011, 01:20 PM   #2
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,874
Blog Entries: 13

Rep: Reputation: 4928Reputation: 4928Reputation: 4928Reputation: 4928Reputation: 4928Reputation: 4928Reputation: 4928Reputation: 4928Reputation: 4928Reputation: 4928Reputation: 4928
Read ioctl(2) manpage, not to say that this is your problem:

In order to use this call, one needs an open file descriptor. Often the open(2) call has unwanted side effects, that can be avoided under Linux by giving it the O_NONBLOCK flag.

Try the O_NONBLOCK and further, change the output to include the errno value

%d:%s\n", errno, strerror(errno));

Then you'll see the actual number and can correlate that from errno.h to the return value; such as EBADF, EINVAL, etc; which are also noted in the manpage for ioctl.

You say you have examples that work, if that code is available, look at it and copy it. Or search for I2C Linux source code examples.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Issues with /dev/pts & /dev/shm, when LFS boots zer0signal Linux From Scratch 2 12-21-2010 08:14 AM
Unable to open the device /dev/i2c-0 avyadavnitw Linux - Embedded & Single-board computer 3 06-29-2009 04:31 AM
Help! Problems with i2c-dev. No more /dev/i2c-* em1l Fedora 0 10-28-2005 06:48 AM
missing /dev/i2c nodes puremourning Linux - Software 0 02-23-2005 10:10 PM
Unknown symbols in i2c-dev Zoombie Linux - Hardware 0 06-28-2004 03:56 AM > Forums > Linux Forums > Linux - Hardware > Linux - Embedded & Single-board computer

All times are GMT -5. The time now is 07:13 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration