24c02_text.c
#include
#include
#include
#include
#include
#include
#include
#include
#define file_i2c "/dev/i2c-3"
#define BUFF_SIZE 32
#define I2C_SLAVE 0x0703 //IIC从器件的地址设置
#define I2C_RETRIES 0x0701
#define I2C_TIMEOUT 0x0702
#define I2C_RDWR 0x0707
#define I2C_M_TEN 0x0010
#define I2C_M_RD 0x0001
struct i2c_msg{
unsigned short addr;
unsigned short flags;
unsigned short len;
unsigned char *buf;
};
struct i2c_rdwr_ioctl_data{
struct i2c_msg *msgs;
int nmsgs; /* nmsgs这个数量决定了有多少开始信号,对于“单开始时序”,取1*/
};
int main(void)
{
unsigned int fd_i2c,ret;
struct i2c_rdwr_ioctl_data e2prom_data;
fd_i2c = open(file_i2c,O_RDWR);
if(fd_i2c<0){
printf("open error \n");
exit(0);
}
e2prom_data.nmsgs = 2; //这里是以后通信时最大的msgs包 读的时候就需要两个 要给msg分配空间
e2prom_data.msgs=(struct i2c_msg*)malloc(e2prom_data.nmsgs*sizeof(struct i2c_msg));
if(!e2prom_data.msgs){
perror("malloc error");
exit(1);
}
ioctl(fd_i2c,I2C_TIMEOUT,1);
ioctl(fd_i2c,I2C_RETRIES,2);
/***write data to e2prom 器件地址-从地址-数据**/
/*
msgs0通信线上数据有3个字节 器件地址有固定的数据指定 所以msgs0就要分配两个字节单元 发送的长度也要指定为2
*/
e2prom_data.nmsgs=1;
(e2prom_data.msgs[0]).len=2;
(e2prom_data.msgs[0]).addr=0x50;
(e2prom_data.msgs[0]).flags=0; //write
(e2prom_data.msgs[0]).buf=(unsigned char*)malloc(2);
(e2prom_data.msgs[0]).buf[0]=0x10;
(e2prom_data.msgs[0]).buf[1]=0x36;//the data to write
ioctl(fd_i2c,I2C_RDWR,(unsigned long)&e2prom_data);
if(ret<0)
{
perror("ioctl error1");
}
sleep(1);
/******read data from e2prom 器件地址-从地址-器件地址-返回的数据******/
/*
msgs0发送器件地址 从地址
msgs1发送器件地址后就没发送了 等待读入的一个数据
*/
e2prom_data.nmsgs=2;
(e2prom_data.msgs[0]).len=1;
(e2prom_data.msgs[0]).addr=0x50;
(e2prom_data.msgs[0]).flags=0;//write
(e2prom_data.msgs[0]).buf[0]=0x10;
(e2prom_data.msgs[1]).len=1;
(e2prom_data.msgs[1]).addr=0x50;
(e2prom_data.msgs[1]).flags=I2C_M_RD;//read
(e2prom_data.msgs[1]).buf=(unsigned char*)malloc(1);
(e2prom_data.msgs[1]).buf[0]=0;
ret=ioctl(fd_i2c,I2C_RDWR,(unsigned long)&e2prom_data);//arge在i2cdev_ioctl_rdrw被接收成i2c_rdwr_ioctl_data类型
if(ret<0)
{
perror("ioctl error2");
}
printf("buff[0]=%x \n",(e2prom_data.msgs[1]).buf[0]);
close(fd_i2c);
return 0;
}
*************************************************************************************************************
Android.mk
1 LOCAL_PATH:= $(call my-dir)
2
3 include $(CLEAR_VARS)
4
5 LOCAL_MODULE_TAGS := eng
6
7 LOCAL_SRC_FILES:=24c02_text.c
8
9 LOCAL_SHARED_LIBRARIES := \
10 libutils \
11 libhardware
12
13
14 LOCAL_MODULE:=24c02-text
15
16 LOCAL_PRELINK_MODULE := false
17 include $(BUILD_EXECUTABLE)
阅读(2370) | 评论(0) | 转发(0) |