在线时间7 小时
UID3265153
注册时间2016-4-24
NXP金币0
该用户从未签到
注册会员

- 积分
- 77
- 最后登录
- 2016-8-3
|

楼主 |
发表于 2016-7-28 13:33:46
|
显示全部楼层
#include <stdio.h>
#include <linux/types.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <errno.h>
#define I2C_RETRIES 0X0701
#define I2C_TIMEOUT 0X0702
#define I2C_RDWR 0X0707
#define I2C_SLAVE_ADDR 0x68
struct i2c_msg
{
unsigned short addr;
unsigned short flags;
#define I2C_M_TEN 0X0010
#define I2C_M_RD 0X0001
unsigned short len;
unsigned char *buf;
};
struct i2c_rdwr_ioctl_data
{
struct i2c_msg *msgs;
int nmsgs;
};
int main(int argc ,char* argv[])
{
int fd,ret;
struct i2c_rdwr_ioctl_data e2prom_data;
fd = open("/dev/i2c-2",O_RDWR);
if(fd<0)
{
perror("open error\n");
}
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\n");
}
ret = ioctl(fd,I2C_TIMEOUT,1);
// printf("%d\n",ret);
ret = ioctl(fd,I2C_RETRIES,2);
// printf("%d\n",ret);
e2prom_data.nmsgs = 1;
(e2prom_data.msgs[0]).len = 2;
(e2prom_data.msgs[0]).addr = I2C_SLAVE_ADDR;
(e2prom_data.msgs[0]).flags = 0;
(e2prom_data.msgs[0]).buf = (unsigned char*)malloc(2);
(e2prom_data.msgs[0]).buf[0] = 0x01;
(e2prom_data.msgs[0]).buf[1] = 0x00;
ret = ioctl(fd,I2C_RDWR,(unsigned long)&e2prom_data);
if(ret < 0)
{
perror("ioctl error1\n");
}
//set time
e2prom_data.nmsgs = 1;
(e2prom_data.msgs[0]).len = 2;
(e2prom_data.msgs[0]).addr = I2C_SLAVE_ADDR;
(e2prom_data.msgs[0]).flags = 0;
// (e2prom_data.msgs[0]).buf = (unsigned char*)malloc(2);
(e2prom_data.msgs[0]).buf[0] = 0x02;
(e2prom_data.msgs[0]).buf[1] = 0x04;
/* (e2prom_data.msgs[0]).buf[2] = 0x59;
(e2prom_data.msgs[0]).buf[3] = 0x59;
(e2prom_data.msgs[0]).buf[4] = 0x16;
(e2prom_data.msgs[0]).buf[5] = 0x04;
(e2prom_data.msgs[0]).buf[6] = 0x25;
(e2prom_data.msgs[0]).buf[7] = 0x07;
(e2prom_data.msgs[0]).buf[8] = 0x16;
*/
ret = ioctl(fd,I2C_RDWR,(unsigned long)&e2prom_data);
if(ret < 0)
{
perror("ioctl error2");
}
sleep(5);
//read time
e2prom_data.nmsgs = 2;
(e2prom_data.msgs[0]).len = 1;
(e2prom_data.msgs[0]).addr = I2C_SLAVE_ADDR;
(e2prom_data.msgs[0]).flags = 0;
(e2prom_data.msgs[0]).buf = (unsigned char*)malloc(1);
(e2prom_data.msgs[0]).buf[0] = 0x08;
(e2prom_data.msgs[1]).len = 1;
(e2prom_data.msgs[1]).addr = I2C_SLAVE_ADDR;
(e2prom_data.msgs[1]).flags = I2C_M_RD;
(e2prom_data.msgs[1]).buf = (unsigned char*)malloc(1);
(e2prom_data.msgs[1]).buf[0] = 0;
ret = ioctl(fd,I2C_RDWR,(unsigned long)&e2prom_data);
if(ret<0)
{
perror("ioctl error3");
}
printf("buf[0] = %x\n",(e2prom_data.msgs[1]).buf[0]);
/* printf("buf[1] = %x\n",(e2prom_data.msgs[1]).buf[1]);
printf("buf[2] = %x\n",(e2prom_data.msgs[1]).buf[2]);
printf("buf[3] = %x\n",(e2prom_data.msgs[1]).buf[3]);
printf("buf[4] = %x\n",(e2prom_data.msgs[1]).buf[4]);
printf("buf[5] = %x\n",(e2prom_data.msgs[1]).buf[5]);
printf("buf[6] = %x\n",(e2prom_data.msgs[1]).buf[6]);
printf("buf[7] = %x\n",(e2prom_data.msgs[1]).buf[7]);
*/
free((e2prom_data.msgs[1]).buf);
free(e2prom_data.msgs);
close(fd);
return 0;
} |
|