8 Replies Latest reply on Aug 17, 2015 2:43 AM by JaeHyuk

    I2C Help

    Maaaah

      Hi,

       

      I've been trying to get a host of sensors up on Edison over I2C. I'm using Eclipse and have historical issues for not wanting to use mraa. Right now I am working on the Melexis 90615 (which there is already a UPM project for the 90614). But I can't get it to read with standard I2C calls (open, ioctl, read, write) in C. I think the Arduino sketchs #include <i2cmaster.h> to access i2c_rep_start or other linux distributions can get smbus calls like i2c_smbus_read_word_data to get more control. The device polls with i2ctools, I just need some help getting the same functions easily into a C project for Edison under Eclipse. Can someone walk me through how to get smbus operations in C for Edison?

       

      Thanks,

       

      Jeff

        • 1. Re: I2C Help
          CMata_Intel

          Hi Maaaah

           

          You should have I2Ctools in your board, if you don’t use the next line in your board (be sure to have a WiFi connection working):

          root@edison:/# opkg install http://repo.opkg.net/edison/repo/core2-32/i2c-tools_3.1.0-r0_core2-32.ipk
          

           

          Also, take a look at this thread (Reply 16)  when they suggest this other link https://github.com/edouardrosset/i2c-tools.

           

          Regards;

          CMata

          • 2. Re: I2C Help
            Maaaah

            I installed this i2c_tools package from the repo, but I'm not sure I needed to.

             

            i2c-tools works out of the box and relies on smbus c functions, but the c program reading/writing through the hardware i2c doesn't see the smbus functions in the #include <linux/i2c.h> or #include <linux/i2c-dev.h> included with the vanilla install. When I built a c program with the i2c_smbus_read_word_data function I would get a warning: implicit declaration of function "i2c_smbus_read_word_data". I think it means, that while the library that is built on the device knows these functions (again i2c-tools works and uses them) it isn't declared (or made public) through the .h files pointed to in eclipse. Changing the .h in eclipse to declare the smbus functions and defines (using any other i2c-dev.h from a different linux distro should do) allowed me to use the regular i2c_smbus . . . function calls and use the more prevalent help to get all my i2c connections working.

            • 3. Re: I2C Help
              CMata_Intel

              Hi Maaaah

               

              The libraries linux/i2c-dev.h and linux/i2c.h are in the /usr/include folder you could take a look at them and see the functions in there.

              Could you post a piece of the code that you are using, only to test it and be in the same page. The error that you are having could be related to the libi2c-dev that I think it's not in the Yocto Image as default.

              There is a workaround using Galileo-Python (Re: i2c-tool pgk to be able to use SMBUS in python) , we could try to do it in Edison-Python if you want.

              I found this other example for ioctl functions too: I2C bus interface . Let me know if this last one is useful for you.

               

              Regards;

              CMata

              • 4. Re: I2C Help
                Maaaah

                Hi CMata_Intel

                 

                if I Open Declaration or F3

                #include <linux/i2c.h>

                 

                This is the file I get:

                /* ------------------------------------------------------------------------- */

                /*     */

                /* i2c.h - definitions for the i2c-bus interface     */

                /*     */

                /* ------------------------------------------------------------------------- */

                /*   Copyright (C) 1995-2000 Simon G. Vogl

                 

                 

                    This program is free software; you can redistribute it and/or modify

                    it under the terms of the GNU General Public License as published by

                    the Free Software Foundation; either version 2 of the License, or

                    (at your option) any later version.

                 

                 

                    This program is distributed in the hope that it will be useful,

                    but WITHOUT ANY WARRANTY; without even the implied warranty of

                    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the

                    GNU General Public License for more details.

                 

                 

                    You should have received a copy of the GNU General Public License

                    along with this program; if not, write to the Free Software

                    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,

                    MA 02110-1301 USA.     */

                /* ------------------------------------------------------------------------- */

                 

                 

                /* With some changes from Kyösti Mälkki <kmalkki@cc.hut.fi> and

                   Frodo Looijaard <frodol@dds.nl> */

                 

                 

                #ifndef _LINUX_I2C_H

                #define _LINUX_I2C_H

                 

                 

                #include <linux/types.h>

                 

                 

                /**

                * struct i2c_msg - an I2C transaction segment beginning with START

                * @addr: Slave address, either seven or ten bits.  When this is a ten

                * bit address, I2C_M_TEN must be set in @flags and the adapter

                * must support I2C_FUNC_10BIT_ADDR.

                * @flags: I2C_M_RD is handled by all adapters.  No other flags may be

                * provided unless the adapter exported the relevant I2C_FUNC_*

                * flags through i2c_check_functionality().

                * @len: Number of data bytes in @buf being read from or written to the

                * I2C slave address.  For read transactions where I2C_M_RECV_LEN

                * is set, the caller guarantees that this buffer can hold up to

                * 32 bytes in addition to the initial length byte sent by the

                * slave (plus, if used, the SMBus PEC); and this value will be

                * incremented by the number of block data bytes received.

                * @buf: The buffer into which data is read, or from which it's written.

                *

                * An i2c_msg is the low level representation of one segment of an I2C

                * transaction.  It is visible to drivers in the @i2c_transfer() procedure,

                * to userspace from i2c-dev, and to I2C adapter drivers through the

                * @i2c_adapter.@master_xfer() method.

                *

                * Except when I2C "protocol mangling" is used, all I2C adapters implement

                * the standard rules for I2C transactions.  Each transaction begins with a

                * START.  That is followed by the slave address, and a bit encoding read

                * versus write.  Then follow all the data bytes, possibly including a byte

                * with SMBus PEC.  The transfer terminates with a NAK, or when all those

                * bytes have been transferred and ACKed.  If this is the last message in a

                * group, it is followed by a STOP.  Otherwise it is followed by the next

                * @i2c_msg transaction segment, beginning with a (repeated) START.

                *

                * Alternatively, when the adapter supports I2C_FUNC_PROTOCOL_MANGLING then

                * passing certain @flags may have changed those standard protocol behaviors.

                * Those flags are only for use with broken/nonconforming slaves, and with

                * adapters which are known to support the specific mangling options they

                * need (one or more of IGNORE_NAK, NO_RD_ACK, NOSTART, and REV_DIR_ADDR).

                */

                struct i2c_msg {

                  __u16 addr; /* slave address */

                  __u16 flags;

                #define I2C_M_TEN 0x0010 /* this is a ten bit chip address */

                #define I2C_M_RD 0x0001 /* read data, from slave to master */

                #define I2C_M_STOP 0x8000 /* if I2C_FUNC_PROTOCOL_MANGLING */

                #define I2C_M_NOSTART 0x4000 /* if I2C_FUNC_NOSTART */

                #define I2C_M_REV_DIR_ADDR 0x2000 /* if I2C_FUNC_PROTOCOL_MANGLING */

                #define I2C_M_IGNORE_NAK 0x1000 /* if I2C_FUNC_PROTOCOL_MANGLING */

                #define I2C_M_NO_RD_ACK 0x0800 /* if I2C_FUNC_PROTOCOL_MANGLING */

                #define I2C_M_RECV_LEN 0x0400 /* length will be first received byte */

                  __u16 len; /* msg length */

                  __u8 *buf; /* pointer to msg data */

                };

                 

                 

                /* To determine what functionality is present */

                 

                 

                #define I2C_FUNC_I2C 0x00000001

                #define I2C_FUNC_10BIT_ADDR 0x00000002

                #define I2C_FUNC_PROTOCOL_MANGLING 0x00000004 /* I2C_M_IGNORE_NAK etc. */

                #define I2C_FUNC_SMBUS_PEC 0x00000008

                #define I2C_FUNC_NOSTART 0x00000010 /* I2C_M_NOSTART */

                #define I2C_FUNC_SMBUS_BLOCK_PROC_CALL 0x00008000 /* SMBus 2.0 */

                #define I2C_FUNC_SMBUS_QUICK 0x00010000

                #define I2C_FUNC_SMBUS_READ_BYTE 0x00020000

                #define I2C_FUNC_SMBUS_WRITE_BYTE 0x00040000

                #define I2C_FUNC_SMBUS_READ_BYTE_DATA 0x00080000

                #define I2C_FUNC_SMBUS_WRITE_BYTE_DATA 0x00100000

                #define I2C_FUNC_SMBUS_READ_WORD_DATA 0x00200000

                #define I2C_FUNC_SMBUS_WRITE_WORD_DATA 0x00400000

                #define I2C_FUNC_SMBUS_PROC_CALL 0x00800000

                #define I2C_FUNC_SMBUS_READ_BLOCK_DATA 0x01000000

                #define I2C_FUNC_SMBUS_WRITE_BLOCK_DATA 0x02000000

                #define I2C_FUNC_SMBUS_READ_I2C_BLOCK 0x04000000 /* I2C-like block xfer  */

                #define I2C_FUNC_SMBUS_WRITE_I2C_BLOCK 0x08000000 /* w/ 1-byte reg. addr. */

                 

                 

                #define I2C_FUNC_SMBUS_BYTE (I2C_FUNC_SMBUS_READ_BYTE | \

                  I2C_FUNC_SMBUS_WRITE_BYTE)

                #define I2C_FUNC_SMBUS_BYTE_DATA (I2C_FUNC_SMBUS_READ_BYTE_DATA | \

                  I2C_FUNC_SMBUS_WRITE_BYTE_DATA)

                #define I2C_FUNC_SMBUS_WORD_DATA (I2C_FUNC_SMBUS_READ_WORD_DATA | \

                  I2C_FUNC_SMBUS_WRITE_WORD_DATA)

                #define I2C_FUNC_SMBUS_BLOCK_DATA (I2C_FUNC_SMBUS_READ_BLOCK_DATA | \

                  I2C_FUNC_SMBUS_WRITE_BLOCK_DATA)

                #define I2C_FUNC_SMBUS_I2C_BLOCK (I2C_FUNC_SMBUS_READ_I2C_BLOCK | \

                  I2C_FUNC_SMBUS_WRITE_I2C_BLOCK)

                 

                 

                #define I2C_FUNC_SMBUS_EMUL (I2C_FUNC_SMBUS_QUICK | \

                  I2C_FUNC_SMBUS_BYTE | \

                  I2C_FUNC_SMBUS_BYTE_DATA | \

                  I2C_FUNC_SMBUS_WORD_DATA | \

                  I2C_FUNC_SMBUS_PROC_CALL | \

                  I2C_FUNC_SMBUS_WRITE_BLOCK_DATA | \

                  I2C_FUNC_SMBUS_I2C_BLOCK | \

                  I2C_FUNC_SMBUS_PEC)

                 

                 

                /*

                * Data for SMBus Messages

                */

                #define I2C_SMBUS_BLOCK_MAX 32 /* As specified in SMBus standard */

                union i2c_smbus_data {

                  __u8 byte;

                  __u16 word;

                  __u8 block[I2C_SMBUS_BLOCK_MAX + 2]; /* block[0] is used for length */

                        /* and one more for user-space compatibility */

                };

                 

                 

                /* i2c_smbus_xfer read or write markers */

                #define I2C_SMBUS_READ 1

                #define I2C_SMBUS_WRITE 0

                 

                 

                /* SMBus transaction types (size parameter in the above functions)

                   Note: these no longer correspond to the (arbitrary) PIIX4 internal codes! */

                #define I2C_SMBUS_QUICK    0

                #define I2C_SMBUS_BYTE    1

                #define I2C_SMBUS_BYTE_DATA    2

                #define I2C_SMBUS_WORD_DATA    3

                #define I2C_SMBUS_PROC_CALL    4

                #define I2C_SMBUS_BLOCK_DATA    5

                #define I2C_SMBUS_I2C_BLOCK_BROKEN  6

                #define I2C_SMBUS_BLOCK_PROC_CALL   7 /* SMBus 2.0 */

                #define I2C_SMBUS_I2C_BLOCK_DATA    8

                 

                 

                #endif /* _LINUX_I2C_H */

                 

                if I do the same for

                #include <linux/i2c-dev.h>

                /*

                    i2c-dev.h - i2c-bus driver, char device interface

                 

                 

                    Copyright (C) 1995-97 Simon G. Vogl

                    Copyright (C) 1998-99 Frodo Looijaard <frodol@dds.nl>

                 

                 

                    This program is free software; you can redistribute it and/or modify

                    it under the terms of the GNU General Public License as published by

                    the Free Software Foundation; either version 2 of the License, or

                    (at your option) any later version.

                 

                 

                    This program is distributed in the hope that it will be useful,

                    but WITHOUT ANY WARRANTY; without even the implied warranty of

                    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the

                    GNU General Public License for more details.

                 

                 

                    You should have received a copy of the GNU General Public License

                    along with this program; if not, write to the Free Software

                    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,

                    MA 02110-1301 USA.

                */

                 

                 

                #ifndef _LINUX_I2C_DEV_H

                #define _LINUX_I2C_DEV_H

                 

                 

                #include <linux/types.h>

                 

                 

                 

                 

                /* /dev/i2c-X ioctl commands.  The ioctl's parameter is always an

                * unsigned long, except for:

                * - I2C_FUNCS, takes pointer to an unsigned long

                * - I2C_RDWR, takes pointer to struct i2c_rdwr_ioctl_data

                * - I2C_SMBUS, takes pointer to struct i2c_smbus_ioctl_data

                */

                #define I2C_RETRIES 0x0701 /* number of times a device address should

                    be polled when not acknowledging */

                #define I2C_TIMEOUT 0x0702 /* set timeout in units of 10 ms */

                 

                 

                /* NOTE: Slave address is 7 or 10 bits, but 10-bit addresses

                * are NOT supported! (due to code brokenness)

                */

                #define I2C_SLAVE 0x0703 /* Use this slave address */

                #define I2C_SLAVE_FORCE 0x0706 /* Use this slave address, even if it

                    is already in use by a driver! */

                #define I2C_TENBIT 0x0704 /* 0 for 7 bit addrs, != 0 for 10 bit */

                 

                 

                #define I2C_FUNCS 0x0705 /* Get the adapter functionality mask */

                 

                 

                #define I2C_RDWR 0x0707 /* Combined R/W transfer (one STOP only) */

                 

                 

                #define I2C_PEC 0x0708 /* != 0 to use PEC with SMBus */

                #define I2C_SMBUS 0x0720 /* SMBus transfer */

                 

                 

                 

                 

                /* This is the structure as used in the I2C_SMBUS ioctl call */

                struct i2c_smbus_ioctl_data {

                  __u8 read_write;

                  __u8 command;

                  __u32 size;

                  union i2c_smbus_data *data;

                };

                 

                 

                /* This is the structure as used in the I2C_RDWR ioctl call */

                struct i2c_rdwr_ioctl_data {

                  struct i2c_msg *msgs; /* pointers to i2c_msgs */

                  __u32 nmsgs; /* number of i2c_msgs */

                };

                 

                 

                #define  I2C_RDRW_IOCTL_MAX_MSGS 42

                 

                 

                 

                 

                #endif /* _LINUX_I2C_DEV_H */

                 

                These are the same files as in:

                iotdk-ide-linux/devkit-x86/sysroots/i586-poky-linux/usr/include/linux

                 

                These are what was installed with the Eclipse IDE before the one on Friday. They should look like:

                /*

                    i2c-dev.h - i2c-bus driver, char device interface

                 

                 

                    Copyright (C) 1995-97 Simon G. Vogl

                    Copyright (C) 1998-99 Frodo Looijaard <frodol@dds.nl>

                 

                 

                    This program is free software; you can redistribute it and/or modify

                    it under the terms of the GNU General Public License as published by

                    the Free Software Foundation; either version 2 of the License, or

                    (at your option) any later version.

                 

                 

                    This program is distributed in the hope that it will be useful,

                    but WITHOUT ANY WARRANTY; without even the implied warranty of

                    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the

                    GNU General Public License for more details.

                 

                 

                    You should have received a copy of the GNU General Public License

                    along with this program; if not, write to the Free Software

                    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,

                    MA 02110-1301 USA.

                */

                 

                 

                /* $Id: i2c-dev.h 5361 2008-10-19 09:47:02Z khali $ */

                 

                 

                #ifndef LIB_I2CDEV_H

                #define LIB_I2CDEV_H

                 

                 

                #include <linux/types.h>

                #include <sys/ioctl.h>

                 

                 

                 

                 

                /* -- i2c.h -- */

                 

                 

                 

                 

                /*

                * I2C Message - used for pure i2c transaction, also from /dev interface

                */

                struct i2c_msg {

                  __u16 addr; /* slave address */

                  unsigned short flags;

                #define I2C_M_TEN 0x10 /* we have a ten bit chip address */

                #define I2C_M_RD 0x01

                #define I2C_M_NOSTART 0x4000

                #define I2C_M_REV_DIR_ADDR 0x2000

                #define I2C_M_IGNORE_NAK 0x1000

                #define I2C_M_NO_RD_ACK 0x0800

                  short len; /* msg length */

                  char *buf; /* pointer to msg data */

                };

                 

                 

                /* To determine what functionality is present */

                 

                 

                #define I2C_FUNC_I2C 0x00000001

                #define I2C_FUNC_10BIT_ADDR 0x00000002

                #define I2C_FUNC_PROTOCOL_MANGLING 0x00000004 /* I2C_M_{REV_DIR_ADDR,NOSTART,..} */

                #define I2C_FUNC_SMBUS_PEC 0x00000008

                #define I2C_FUNC_SMBUS_BLOCK_PROC_CALL 0x00008000 /* SMBus 2.0 */

                #define I2C_FUNC_SMBUS_QUICK 0x00010000

                #define I2C_FUNC_SMBUS_READ_BYTE 0x00020000

                #define I2C_FUNC_SMBUS_WRITE_BYTE 0x00040000

                #define I2C_FUNC_SMBUS_READ_BYTE_DATA 0x00080000

                #define I2C_FUNC_SMBUS_WRITE_BYTE_DATA 0x00100000

                #define I2C_FUNC_SMBUS_READ_WORD_DATA 0x00200000

                #define I2C_FUNC_SMBUS_WRITE_WORD_DATA 0x00400000

                #define I2C_FUNC_SMBUS_PROC_CALL 0x00800000

                #define I2C_FUNC_SMBUS_READ_BLOCK_DATA 0x01000000

                #define I2C_FUNC_SMBUS_WRITE_BLOCK_DATA 0x02000000

                #define I2C_FUNC_SMBUS_READ_I2C_BLOCK 0x04000000 /* I2C-like block xfer  */

                #define I2C_FUNC_SMBUS_WRITE_I2C_BLOCK 0x08000000 /* w/ 1-byte reg. addr. */

                 

                 

                #define I2C_FUNC_SMBUS_BYTE (I2C_FUNC_SMBUS_READ_BYTE | \

                                             I2C_FUNC_SMBUS_WRITE_BYTE)

                #define I2C_FUNC_SMBUS_BYTE_DATA (I2C_FUNC_SMBUS_READ_BYTE_DATA | \

                                                  I2C_FUNC_SMBUS_WRITE_BYTE_DATA)

                #define I2C_FUNC_SMBUS_WORD_DATA (I2C_FUNC_SMBUS_READ_WORD_DATA | \

                                                  I2C_FUNC_SMBUS_WRITE_WORD_DATA)

                #define I2C_FUNC_SMBUS_BLOCK_DATA (I2C_FUNC_SMBUS_READ_BLOCK_DATA | \

                                                   I2C_FUNC_SMBUS_WRITE_BLOCK_DATA)

                #define I2C_FUNC_SMBUS_I2C_BLOCK (I2C_FUNC_SMBUS_READ_I2C_BLOCK | \

                                                  I2C_FUNC_SMBUS_WRITE_I2C_BLOCK)

                 

                 

                /* Old name, for compatibility */

                #define I2C_FUNC_SMBUS_HWPEC_CALC I2C_FUNC_SMBUS_PEC

                 

                 

                /*

                * Data for SMBus Messages

                */

                #define I2C_SMBUS_BLOCK_MAX 32 /* As specified in SMBus standard */

                #define I2C_SMBUS_I2C_BLOCK_MAX 32 /* Not specified but we use same structure */

                union i2c_smbus_data {

                  __u8 byte;

                  __u16 word;

                  __u8 block[I2C_SMBUS_BLOCK_MAX + 2]; /* block[0] is used for length */

                                                             /* and one more for PEC */

                };

                 

                 

                /* smbus_access read or write markers */

                #define I2C_SMBUS_READ 1

                #define I2C_SMBUS_WRITE 0

                 

                 

                /* SMBus transaction types (size parameter in the above functions)

                   Note: these no longer correspond to the (arbitrary) PIIX4 internal codes! */

                #define I2C_SMBUS_QUICK    0

                #define I2C_SMBUS_BYTE    1

                #define I2C_SMBUS_BYTE_DATA    2

                #define I2C_SMBUS_WORD_DATA    3

                #define I2C_SMBUS_PROC_CALL    4

                #define I2C_SMBUS_BLOCK_DATA    5

                #define I2C_SMBUS_I2C_BLOCK_BROKEN  6

                #define I2C_SMBUS_BLOCK_PROC_CALL   7 /* SMBus 2.0 */

                #define I2C_SMBUS_I2C_BLOCK_DATA    8

                 

                 

                 

                 

                /* ----- commands for the ioctl like i2c_command call:

                * note that additional calls are defined in the algorithm and hw

                * dependent layers - these can be listed here, or see the

                * corresponding header files.

                */

                  /* -> bit-adapter specific ioctls */

                #define I2C_RETRIES 0x0701 /* number of times a device address      */

                  /* should be polled when not            */

                                                /* acknowledging */

                #define I2C_TIMEOUT 0x0702 /* set timeout - call with int */

                 

                 

                 

                 

                /* this is for i2c-dev.c */

                #define I2C_SLAVE 0x0703 /* Change slave address */

                  /* Attn.: Slave address is 7 or 10 bits */

                #define I2C_SLAVE_FORCE 0x0706 /* Change slave address */

                  /* Attn.: Slave address is 7 or 10 bits */

                  /* This changes the address, even if it */

                  /* is already taken! */

                #define I2C_TENBIT 0x0704 /* 0 for 7 bit addrs, != 0 for 10 bit */

                 

                 

                #define I2C_FUNCS 0x0705 /* Get the adapter functionality */

                #define I2C_RDWR 0x0707 /* Combined R/W transfer (one stop only)*/

                #define I2C_PEC 0x0708 /* != 0 for SMBus PEC                   */

                 

                 

                #define I2C_SMBUS 0x0720 /* SMBus-level access */

                 

                 

                /* -- i2c.h -- */

                 

                 

                 

                 

                /* Note: 10-bit addresses are NOT supported! */

                 

                 

                /* This is the structure as used in the I2C_SMBUS ioctl call */

                struct i2c_smbus_ioctl_data {

                  char read_write;

                  __u8 command;

                  int size;

                  union i2c_smbus_data *data;

                };

                 

                 

                /* This is the structure as used in the I2C_RDWR ioctl call */

                struct i2c_rdwr_ioctl_data {

                  struct i2c_msg *msgs; /* pointers to i2c_msgs */

                  int nmsgs; /* number of i2c_msgs */

                };

                 

                 

                 

                 

                static inline __s32 i2c_smbus_access(int file, char read_write, __u8 command,

                                                     int size, union i2c_smbus_data *data)

                {

                  struct i2c_smbus_ioctl_data args;

                 

                 

                  args.read_write = read_write;

                  args.command = command;

                  args.size = size;

                  args.data = data;

                  return ioctl(file,I2C_SMBUS,&args);

                }

                 

                 

                 

                 

                static inline __s32 i2c_smbus_write_quick(int file, __u8 value)

                {

                  return i2c_smbus_access(file,value,0,I2C_SMBUS_QUICK,NULL);

                }

                 

                static inline __s32 i2c_smbus_read_byte(int file)

                {

                  union i2c_smbus_data data;

                  if (i2c_smbus_access(file,I2C_SMBUS_READ,0,I2C_SMBUS_BYTE,&data))

                  return -1;

                  else

                  return 0x0FF & data.byte;

                }

                 

                 

                static inline __s32 i2c_smbus_write_byte(int file, __u8 value)

                {

                  return i2c_smbus_access(file,I2C_SMBUS_WRITE,value,

                                         I2C_SMBUS_BYTE,NULL);

                }

                 

                 

                static inline __s32 i2c_smbus_read_byte_data(int file, __u8 command)

                {

                  union i2c_smbus_data data;

                  if (i2c_smbus_access(file,I2C_SMBUS_READ,command,

                                      I2C_SMBUS_BYTE_DATA,&data))

                  return -1;

                  else

                  return 0x0FF & data.byte;

                }

                 

                 

                static inline __s32 i2c_smbus_write_byte_data(int file, __u8 command,

                                                              __u8 value)

                {

                  union i2c_smbus_data data;

                  data.byte = value;

                  return i2c_smbus_access(file,I2C_SMBUS_WRITE,command,

                                         I2C_SMBUS_BYTE_DATA, &data);

                }

                 

                 

                static inline __s32 i2c_smbus_read_word_data(int file, __u8 command)

                {

                  union i2c_smbus_data data;

                  if (i2c_smbus_access(file,I2C_SMBUS_READ,command,

                                      I2C_SMBUS_WORD_DATA,&data))

                  return -1;

                  else

                  return 0x0FFFF & data.word;

                }

                 

                 

                static inline __s32 i2c_smbus_write_word_data(int file, __u8 command,

                                                              __u16 value)

                {

                  union i2c_smbus_data data;

                  data.word = value;

                  return i2c_smbus_access(file,I2C_SMBUS_WRITE,command,

                                         I2C_SMBUS_WORD_DATA, &data);

                }

                 

                 

                static inline __s32 i2c_smbus_process_call(int file, __u8 command, __u16 value)

                {

                  union i2c_smbus_data data;

                  data.word = value;

                  if (i2c_smbus_access(file,I2C_SMBUS_WRITE,command,

                                      I2C_SMBUS_PROC_CALL,&data))

                  return -1;

                  else

                  return 0x0FFFF & data.word;

                }

                 

                 

                 

                 

                /* Returns the number of read bytes */

                static inline __s32 i2c_smbus_read_block_data(int file, __u8 command,

                                                              __u8 *values)

                {

                  union i2c_smbus_data data;

                  int i;

                  if (i2c_smbus_access(file,I2C_SMBUS_READ,command,

                                      I2C_SMBUS_BLOCK_DATA,&data))

                  return -1;

                  else {

                  for (i = 1; i <= data.block[0]; i++)

                  values[i-1] = data.block[i];

                  return data.block[0];

                  }

                }

                 

                 

                static inline __s32 i2c_smbus_write_block_data(int file, __u8 command,

                                                               __u8 length, __u8 *values)

                {

                  union i2c_smbus_data data;

                  int i;

                  if (length > 32)

                  length = 32;

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

                  data.block[i] = values[i-1];

                  data.block[0] = length;

                  return i2c_smbus_access(file,I2C_SMBUS_WRITE,command,

                                         I2C_SMBUS_BLOCK_DATA, &data);

                }

                 

                 

                /* Returns the number of read bytes */

                /* Until kernel 2.6.22, the length is hardcoded to 32 bytes. If you

                   ask for less than 32 bytes, your code will only work with kernels

                   2.6.23 and later. */

                static inline __s32 i2c_smbus_read_i2c_block_data(int file, __u8 command,

                                                                  __u8 length, __u8 *values)

                {

                  union i2c_smbus_data data;

                  int i;

                 

                 

                  if (length > 32)

                  length = 32;

                  data.block[0] = length;

                  if (i2c_smbus_access(file,I2C_SMBUS_READ,command,

                                      length == 32 ? I2C_SMBUS_I2C_BLOCK_BROKEN :

                                       I2C_SMBUS_I2C_BLOCK_DATA,&data))

                  return -1;

                  else {

                  for (i = 1; i <= data.block[0]; i++)

                  values[i-1] = data.block[i];

                  return data.block[0];

                  }

                }

                 

                 

                static inline __s32 i2c_smbus_write_i2c_block_data(int file, __u8 command,

                                                               __u8 length, __u8 *values)

                {

                  union i2c_smbus_data data;

                  int i;

                  if (length > 32)

                  length = 32;

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

                  data.block[i] = values[i-1];

                  data.block[0] = length;

                  return i2c_smbus_access(file,I2C_SMBUS_WRITE,command,

                                         I2C_SMBUS_I2C_BLOCK_BROKEN, &data);

                }

                 

                 

                /* Returns the number of read bytes */

                static inline __s32 i2c_smbus_block_process_call(int file, __u8 command,

                                                                 __u8 length, __u8 *values)

                {

                  union i2c_smbus_data data;

                  int i;

                  if (length > 32)

                  length = 32;

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

                  data.block[i] = values[i-1];

                  data.block[0] = length;

                  if (i2c_smbus_access(file,I2C_SMBUS_WRITE,command,

                                      I2C_SMBUS_BLOCK_PROC_CALL,&data))

                  return -1;

                  else {

                  for (i = 1; i <= data.block[0]; i++)

                  values[i-1] = data.block[i];

                  return data.block[0];

                  }

                }

                 

                 

                 

                 

                #endif /* LIB_I2CDEV_H */

                 

                 

                On another note: I have inconsistent behaviour when I open declarations like linux/i2c.h so it might be a IDE/toolchain problem to figure out and I have just done a workaround.

                 

                j

                • 5. Re: I2C Help
                  CMata_Intel

                  Hi Maaaah

                   

                  Which are the inconsistent behaviors that you are having when trying to use i2c.h?

                  Also I'm not sure if you were able to work with this because of:

                  .....and I have just done a workaround.

                  If you are still having problems with this, did you try with ioctl functions or the workaround with python?

                   

                  Regards;

                  CMata

                  • 6. Re: I2C Help
                    Maaaah

                    The file copied in above are from opening declaration of i2c.h from eclipse does not have the smb inline declarations.

                     

                    I used a i2c-usr.h that did which is also copied in above. I'd expect that the devkit would install with the smb inline declarations available which it did not on my computer. I use the IOCtl functions and the i2c-usr.h that I've copied in above with the full declaration.

                     

                    j

                    • 7. Re: I2C Help
                      DiegoV_Intel

                      Hi, Maaaah,

                       

                      From your last post I understand that the workaround resolved the issue, is this right? If so, could you please mark it as correct?

                       

                      Regards,

                      Diego.

                      • 8. Re: I2C Help
                        JaeHyuk

                        Hi Maaaah. I've faced same problem that you had. But when I insert code you uploaded, I solved my problem! So thank you for sharing that code.