OpenWrt Forum Archive

Topic: i2c-dev.h Not working

The content of this topic has been archived on 25 Apr 2018. There are no obvious gaps in this topic, but there may still be some posts missing at the end.

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 (Meraki Mini). 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.

Maybe is your open function ?
I open i2c bus like this "if ((fd = open ("/dev/i2c-0", I2C_SMBUS)) < 0) ", and never got error (Bifferboard r22299).

I remember, got some open error when time between open and ioctl is too long (like two printf between neutral).
So in my code, i use ioctl just after :

if ((fd = open (bus, I2C_SMBUS)) < 0) { perror(bus); return 1; }
if (ioctl (fd, I2C_SLAVE, addr) < 0) { perror(bus); return 1; }

And it works.

Ps : For i2c-dev.h, i use directly from my src directory, like you.

(Last edited by Niii on 3 Mar 2011, 08:40)

That's interesting. I've seen so many instructions on i2c and I've never seen an open command use "I2C_SMBUS". I will give it a try.

Same with removing my printf statements. But I'm not sure that makes sense, since the work flow of the app is to keep the stream open and do ioctls all the time. It has to switch between different chips after all.

AARCGH.
I decided to make a stub application to test out the application. And
it worked properly when I opened it up inside my main() function. But
when I do the same code elsewhere inside a cpp object it does not
work. So I'm kind of happy that I know how it's not happening, but not
why.
My main() func declares an object that allocates and controls another
object that I want do the ioctl. How is it that this is no possible ?
All the relevant headers are declared in the object's .cpp file. The
functions trigger fine but they fail. I'm also noticing that instead
of file being assigned "0" each time, instead it's being assigned
numbers higher.
So anyway. How can I do this properly within an object?

The discussion might have continued from here.