Topic: i2c device

Hi everybody,

I'm trying to use the i2c accelerometer on my hackerboard, but the device (/dev/i2c-0) doesn't exists.
I listed the devices:

cat /proc/devices

The major of my i2c is 89 then I tried to create the device by using:

mknod /dev/i2c-0 c 89 0

Then I run the program (on the hacker board page) who say:
./i2c

Unable to send data: Invalid argument
Unable to read register!

Has somebody an idee on how to proceed to use the i2c ?

Thank you all,

Miloo

Re: i2c device

The accelerometer is supported by a shared memory structure which in turn utilizes a ioctl. The source is contained in the src-1.044 file. The demo application graphs to the LCD which is not present on the hackerboard. How do you plan to use the accelerometer?

George

Re: i2c device

Hi,

I plan to use the accelerometer to control some servos on an helicopter smile
But it is written on the wiki  that the accelerometter is on i2C. Maybe my firmware is not up to date. I'll check that tonight, I dont have an internet connection right now on the board.

#include <stdio.h>
#include <linux/i2c.h>
#include <linux/i2c-dev.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <string.h>

// The 'raw' 7 bit address shifted up
#define MMA7455_I2CADDR (0x1D * 2)

// The registers to read!
#define MMA7455_XOUT8 6
#define MMA7455_YOUT8 7
#define MMA7455_ZOUT8 8


#define I2C_FILE_NAME "/dev/i2c-0"

static int get_i2c_register(int file,
                            unsigned char addr,
                            unsigned char reg,
                            unsigned char *val) {
    unsigned char inbuf, outbuf;
    struct i2c_rdwr_ioctl_data packets;
    struct i2c_msg messages[2];

    /*
     * In order to read a register, we first do a "dummy write" by writing
     * 0 bytes to the register we want to read from.  This is similar to
     * the packet in set_i2c_register, except it's 1 byte rather than 2.
     */
    outbuf = reg;
    messages[0].addr  = addr;
    messages[0].flags = 0;
    messages[0].len   = sizeof(outbuf);
    messages[0].buf   = &outbuf;

    /* The data will get returned in this structure */
    messages[1].addr  = addr;
    messages[1].flags = I2C_M_RD/* | I2C_M_NOSTART*/;
    messages[1].len   = sizeof(inbuf);
    messages[1].buf   = &inbuf;

    /* Send the request to the kernel and get the result back */
    packets.msgs      = messages;
    packets.nmsgs     = 2;
    if(ioctl(file, I2C_RDWR, &packets) < 0) {
        perror("Unable to send data");
        return 1;
    }
    *val = inbuf;

    return 0;
}


int main(int argc, char **argv) {
    int i2c_file;
    int8_t x, y, z;  // the readings are 8 bits and signed!

    // Open a connection to the I2C userspace control file.
    if ((i2c_file = open(I2C_FILE_NAME, O_RDWR)) < 0) {
        perror("Unable to open i2c control file");
        exit(1);
    }

    // ignore arguments!

    while (1) {
     
      // read X and Y and Z from the register
      if( get_i2c_register(i2c_file, MMA7455_I2CADDR, MMA7455_XOUT8, &x) ||
      get_i2c_register(i2c_file, MMA7455_I2CADDR, MMA7455_YOUT8, &y) ||
      get_i2c_register(i2c_file, MMA7455_I2CADDR, MMA7455_ZOUT8, &z) ) {

    printf("Unable to read register!\n");
    return -1;
      }

      printf("X = %d\tY = %d\tZ = %d\n", x, y, z);
    }

    close(i2c_file);
    return 0;
}

I try to use this program:

4 (edited by miloo 2011-07-24 11:01:04)

Re: i2c device

Hi!

I found the problem, and it was a simple problem between chair and keyboard. The paste I made for the program was not complete due to the buffer size. I pasted the missing piece of the code in a second time and I forgot an other piece. Unfortunately the bad program was compiling without any error.

Thanks