Application layer read/write I²c device can read and write 8-bit, 16-bit device address gadget and source code

Source: Internet
Author: User
Tags goto int size printf set time



#include "standard_i2c.h"
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#if 0
#include <linux/i2c.h>
#include <linux/i2c-dev.h>

#else
#define I2C_M_WT 0x0000/* Write data from master to slave */
#define I2C_M_RD 0x0001/* read data, from slave to master */
#define I2C_ADDR (0x48>>1)

#define I2C_ADDR_16BIT 1//1:16bit i2c_addr 0:8bit i2c_addr

#define I2C_RETRIES 0x0701/* Sets the number of retries when an ACK is not received */
#define I2C_TIMEOUT 0x0702/* Set time-out time-out jiffies */
#define I2C_SLAVE 0x0703/* Set slave address */
#define I2C_SLAVE_FORCE 0x0706/* Force set Slave address */
#define I2C_TENBIT 0x0704/* Select Address bit length: =0 for 7bit,! = 0 for ten bit */
#define I2C_FUNCS 0x0705/* Get the features supported by the adapter */
#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 */
#define I2C_SMBUS_BYTE_DATA 2
#define I2C_SMBUS_READ 1
#define I2C_SMBUS_WRITE 0
#define I2C_SMBUS_BLOCK_DATA 5
#define I2C_SMBUS_I2C_BLOCK_DATA 8
#define I2c_slave_force 0x0706
#define I2C_SMBUS_BLOCK_MAX 32


Exact-width types
typedef char S8;
typedef short S16;
typedef int S32;
typedef unsigned char U8;
typedef unsigned short U16;
typedef unsigned int u32;
typedef float F32;
typedef double F64;


Union I2c_smbus_data {
unsigned char byte;
unsigned short word;
unsigned char Block[i2c_smbus_block_max + 2];
};

struct I2c_smbus_ioctl_data {
Char Read_write;
unsigned char command;
int size;
Union I2c_smbus_data *data;
};

struct I2C_MSG {
U16 Addr; /* Slave Address */
U16 flags;
U16 Len; /* MSG Length */
U8 *buf; /* Pointer to MSG data */
} ;

/* in./include/linux/i2c-dev.h */
struct I2c_rdwr_ioctl_data {
/* Pointers to I2C_MSGS */
struct I2C_MSG *msgs;
/* Number of I2C_MSGS */
U32 nmsgs;
};

#endif

int standard_i2c_open (int index)
{
Char dev_name[32];

sprintf (Dev_name, "/dev/i2c-%d", index);

Return open (Dev_name, O_RDWR);
}

#if i2c_addr_16bit
int write_16bit_i2c (int bus_index, U8 device_addr/*8bits*/, u32 register_addr, unsigned char *pbuffer)
{
struct I2c_rdwr_ioctl_data e2prom_data;
int i2c_dev_fd =-1;
Char reg_buf[3]={0,0,0};
int ret;
/**/
I2C_DEV_FD = Standard_i2c_open (Bus_index);
if (I2C_DEV_FD < 0)
{
printf ("Standard_i2c_read standard_i2c_open failed\n");
Goto _over_;
}

IOCTL (i2c_dev_fd,i2c_timeout,1);/* Timeout time */
IOCTL (i2c_dev_fd,i2c_retries,2);/* Repeat Count */

E2prom_data.nmsgs=1;
e2prom_data.msgs= (struct i2c_msg*) malloc (e2prom_data.nmsgs*sizeof (struct i2c_msg));
if (!E2PROM_DATA.MSGS)
{
Perror ("malloc error");
Goto _over_;
}

Reg_buf[0] = (register_addr >> 8) & 0xFF; E2prom Data Address 16bit
REG_BUF[1] = register_addr & 0xFF; E2prom Data Address
REG_BUF[2] = pbuffer[0]; Write a data

(E2prom_data.msgs[0]). len=3; //
(E2prom_data.msgs[0]). addr=i2c_addr; E2prom Device Address
(E2prom_data.msgs[0]). FLAGS=I2C_M_WT; Write
(E2prom_data.msgs[0]). buf = Reg_buf;

Ret=ioctl (I2c_dev_fd,i2c_rdwr, (unsigned long) &e2prom_data);
if (ret<0)
{
Perror ("IOCTL Error2");
Goto _over_;
}
printf ("* *%x * \ n", pbuffer[0]);

_over_:
if (i2c_dev_fd >= 0)
{
Close (I2C_DEV_FD);
}
if (e2prom_data.msgs! = NULL)
{
Free (E2PROM_DATA.MSGS);
}

}


int read_16bit_i2c (int bus_index, U8 device_addr/*8bits*/, u32 register_addr, unsigned char *pbuffer)
{
struct I2c_rdwr_ioctl_data e2prom_data;
int i2c_dev_fd =-1;
Char reg_buf[2]={0,0},buf[2]={0,0};
int ret;
/**/
I2C_DEV_FD = Standard_i2c_open (Bus_index);
if (I2C_DEV_FD < 0)
{
printf ("Standard_i2c_read standard_i2c_open failed\n");
Goto _over_;
}
IOCTL (i2c_dev_fd,i2c_timeout,1);/* Timeout time */
IOCTL (i2c_dev_fd,i2c_retries,2);/* Repeat Count */

e2prom_data.nmsgs=2;
e2prom_data.msgs= (struct i2c_msg*) malloc (e2prom_data.nmsgs*sizeof (struct i2c_msg));
if (!E2PROM_DATA.MSGS)
{
Perror ("malloc error");
Goto _over_;
}

Reg_buf[0] = (register_addr >> 8) & 0xFF; E2prom Data Address 16bit
REG_BUF[1] = register_addr & 0xFF; E2prom Data Address

 e2prom_data.nmsgs=2;
  (E2prom_data.msgs[0]). len=2;//e2prom number of bytes to write
  (E2prom_data.msgs[0]). addr=i2c_addr;//E2prom device Address
  (E2prom_data.msgs[0]). flags=i2c_m_wt;//write
  (E2prom_data.msgs[0]). buf = Reg_buf;
 
  (e2prom_data.msgs[1]). len=1;//number of data read
  (e2prom_data.msgs[1]). addr=i2c_addr;//E2prom device address
& nbsp (e2prom_data.msgs[1]). Flags=i2c_m_rd;//read
  (e2prom_data.msgs[1]). buf=buf;
 
  Ret=ioctl (I2c_dev_fd,i2c_rdwr, (unsigned long) &e2prom_data);
 if (ret<0)
 {
      perror ("IOCTL Error2");
   Goto _ Over_;
 }
 printf ("***0x%x***\n", (E2prom_data.msgs[1]). Buf[0]);
  
 pbuffer[0] = (e2prom_data.msgs[1]). Buf[0];

_over_:
if (i2c_dev_fd >= 0)
{
Close (I2C_DEV_FD);
}
if (e2prom_data.msgs! = NULL)
{
Free (E2PROM_DATA.MSGS);
}

}

#else

int read_8bit_i2c (int bus_index, unsigned char device_addr/*8bits*/, unsigned char register_addr, unsigned char *pbuffer, int byte_number)
{
struct I2c_smbus_ioctl_data ioctl_data;
Union I2c_smbus_data Smbus_data;
int ret_val =-1;
int i2c_dev_fd =-1;
int index = 0;

/**/
if (Byte_number >= I2c_smbus_block_max)
{
printf ("Standard_i2c_read Invalid parameter (%d >=%d) \ n", Byte_number, I2c_smbus_block_max);
return-1;
}

/**/
I2C_DEV_FD = Standard_i2c_open (index);
if (I2C_DEV_FD < 0)
{
printf ("Standard_i2c_read standard_i2c_open failed\n");
Goto _over_;
}

/*set address*/
IOCTL (I2C_DEV_FD, i2c_tenbit, 0);
if (IOCTL (I2C_DEV_FD, I2c_slave_force, (device_addr >> 1) & 0x7f) < 0)
{
Perror ("I²c ioctl:");
printf ("Standard_i2c_read ioctl (I2c_slave) failed\n");
Goto _over_;
}

/**/
Smbus_data.block[0] = Byte_number;

Ioctl_data.read_write = I2c_smbus_read;
Ioctl_data.command = register_addr;
Ioctl_data.size = I2c_smbus_i2c_block_data;
Ioctl_data.data = &smbus_data;
if (IOCTL (I2C_DEV_FD, I2c_smbus, &ioctl_data)! = 0)
{
printf ("Standard_i2c_read ioctl (I2c_smbus) Failed (%d) \ n", index);
Goto _over_;
}

if (smbus_data.block[0]! = byte_number)
{
printf ("Standard_i2c_read IOCTL read Failed (%d) (%d) \ n", smbus_data.block[0], byte_number);
Goto _over_;
}
for (index = 0; index < byte_number; index + +)
{
Pbuffer[index] = Smbus_data.block[index + 1];
}

Ret_val = 0;

_over_:
if (i2c_dev_fd >= 0)
{
Close (I2C_DEV_FD);
}

return ret_val;
}


int write_8bit_i2c (int bus_index, unsigned char device_addr/*8bits*/, unsigned char register_addr, unsigned char *pbuffer , int byte_number)
{
struct I2c_smbus_ioctl_data ioctl_data;
Union I2c_smbus_data Smbus_data;
int ret_val =-1;
int i2c_dev_fd =-1;
int index = 0;

/**/
if (Byte_number >= I2c_smbus_block_max)
{
printf ("Standard_i2c_write ioctl Invalid parameter (%d) \ n", byte_number);
return-1;
}

I2C_DEV_FD = Standard_i2c_open (Bus_index);
if (I2C_DEV_FD < 0)
{
printf ("Standard_i2c_write standard_i2c_open failed\n");
Goto _over_;
}

/*set address*/
IOCTL (I2C_DEV_FD, i2c_tenbit, 0);
if (IOCTL (I2C_DEV_FD, I2c_slave_force, (device_addr >> 1) & 0x7f) < 0)
{
Perror ("I²c ioctl:");
printf ("Standard_i2c_write ioctl (I2c_slave) failed\n");
Goto _over_;
}

/**/
Smbus_data.block[0] = Byte_number;
for (index = 0; index < byte_number; index + +)
{
Smbus_data.block[index + 1] = Pbuffer[index];
}
/**/
Ioctl_data.read_write = I2c_smbus_write;
Ioctl_data.command = register_addr;
Ioctl_data.size = I2c_smbus_i2c_block_data;
Ioctl_data.data = &smbus_data;
/**/
if (IOCTL (I2C_DEV_FD, I2c_smbus, &ioctl_data)! = 0)
{
printf ("Standard_i2c_write ioctl (I2c_smbus) Failed (%d) \ n", index);
Goto _over_;
}

Ret_val = 0;

_over_:
if (i2c_dev_fd >= 0)
{
Close (I2C_DEV_FD);
}

return ret_val;
}

#endif

int usage (char *pname)
{
printf ("%s <8/16> <dev> <reg> [value]\n", pname);
return 0;
}

/* read./iis 0x24 0x0111 */
/* write./iis 0x24 0x0111 0x3 */

int main (int argc, char *argv[])
{
unsigned char i2c_dev;
unsigned int i2c_reg;
unsigned char value[4];
unsigned int readvalue;
unsigned char reg_bit;
int i;
if (ARGC < 3)
{
Usage (argv[0]);
Exit (1);
}

Reg_bit=strtoll (Argv[1], NULL, 0);
I2c_dev=strtoll (argv[2], NULL, 0);
I2c_reg=strtoll (Argv[3], NULL, 0);

printf ("Reg_bit%d \ n", reg_bit);

if (argc = = 4)
{
if (+ = = reg_bit) {
READ_16BIT_I2C (0, I2c_dev,i2c_reg, &value[0]);
}else{
READ_8BIT_I2C (0, I2c_dev,i2c_reg, &value[0], 1);
}

printf ("read reg 0x%x value:0x%x \ n", i2c_reg,value[0]);
}
else if (argc = = 5)
{
Value[0]=strtoll (Argv[4], NULL, 0);

if (+ = = reg_bit) {
WRITE_16BIT_I2C (0, I2c_dev,i2c_reg, &value[0]);
}else{
WRITE_8BIT_I2C (0, I2c_dev,i2c_reg, &value[0], 1);
}
printf ("Write reg 0x%x value:0x%x \ n", i2c_reg,value[0]);
}

}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.