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. |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
03-02-2011, 10:37 AM
|
#1
|
LQ Newbie
Registered: Mar 2011
Posts: 1
Rep:
|
i2c-dev.h Issues
Okay,
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.
//CODE
//TEST BEGIN
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");
}
else
{
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);
}
else
{
printf("Open chip %d Succeeded file %d\n",0x4b, file);
}
//CODE END
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.
|
|
|
03-24-2011, 12:20 PM
|
#2
|
Moderator
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,955
|
Read ioctl(2) manpage, not to say that this is your problem:
Note
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.
|
|
|
All times are GMT -5. The time now is 02:11 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|