I2C interface problems (iMX6-SABRESDB)...

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

I2C interface problems (iMX6-SABRESDB)...

Jump to solution
5,850 Views
EdSutter
Senior Contributor II

I don't have much experience with linux/i2c (plenty of i2c, just not much on linux).

Anyway, in preparation for a few I2C devices on our custom hardware I wanted to play with the generic

linux i2c driver just to get used to it.  I picked the accelerometer (MMA8451Q, device addr: 0x1C, bus 0).

The device seems simple enough to use, but I keep getting a Input/Output error when I try to write

to it.  Here's the entire snippet of code...

#include <stdio.h>

#include <stdlib.h>

#include <errno.h>

#include <fcntl.h>

#include <linux/i2c-dev.h>

#define SLAVE_ADDR  0x1c    /* SA0 is low, so addr=1c */

int

main(int argc, char *argv[])

{

    int i, j, dev;

    char buf[8];

    if ((dev = open("/dev/i2c-0", O_RDWR)) < 0) {

        perror("open failed");

        exit(1);

    }

    if (ioctl(dev, I2C_SLAVE, SLAVE_ADDR) < 0) {

        perror("ioctl failed");

        close(dev);

        exit(1);

    }

    buf[0] = 0;

    if (write(dev,buf,1) != 1) {

        perror("write failed");

        close(dev);

        exit(1);

    }

    if (read(dev,buf,1) != 1) {

        perror("read failed");

        close(dev);

        exit(1);

    }

    close(dev);

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

        printf("buf[%d]: 0x%02x\n",i,buf[i]);

    exit(0);

}

Is the the correct way to access a device on the I2C bus?

Any hints as to where the problem might be?

Labels (2)
Tags (3)
0 Kudos
Reply
1 Solution
1,887 Views
EdSutter
Senior Contributor II

Resolved...

I just noticed that L20 is removed from my board.. This puts the device in power-down mode, so all bets are off.

View solution in original post

0 Kudos
Reply
4 Replies
1,887 Views
Raybiztech
Contributor V

Hi Ed Sutter,

    In this code you are writing register at address 0 (buf) which is read only status register,so you can't do write operation on that.Try read operation on same register by calling read first instead of write.

Regards,

Sasidhar 

1,887 Views
EdSutter
Senior Contributor II

Sasidhar,

Yea good point (duh).  Bad example above.

Still can't access the device though because of the L20 issue.

No problem, I was able to access a different I2C device, and my primary goal at

this point is just get some experience with the Linux-I2C generic driver.

By the way, just doing a read (without the illegal write above) didn't matter in my case.

The overriding issue is the absence of L20 on my board because we're using some

of the pins on that device in a different iomux mode.

Thanks,

Ed

0 Kudos
Reply
1,887 Views
EdSutter
Senior Contributor II

More...

Just noticed that if I change the above code so that I am accessing the eCompass (SLAVE_ADDR=0x0e, bus=/dev/i2c-2)

i'm able to access that device.

0 Kudos
Reply
1,888 Views
EdSutter
Senior Contributor II

Resolved...

I just noticed that L20 is removed from my board.. This puts the device in power-down mode, so all bets are off.

0 Kudos
Reply