2011.11.21 星期一
I2c_test.c + i2c_dev.c +i2c_s3c3410.c 构成的i2c设备的调用链。
一、从用户空间程序i2c_test.c开始进行流程
//i2c_test.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/select.h>
#include <sys/time.h>
#include <errno.h>
#include <stdio.h>
#include <linux/types.h>
#include <assert.h>
#include <string.h>
#include <linux/i2c.h>
#include <linux/i2c-dev.h>
#define SLAVE_ADDRESS 0x70
int main(void)
{
struct i2c_rdwr_ioctl_data TP_data;
unsigned int i2c_fd;
int ret;
i2c_fd = open("/dev/i2c/0", O_RDWR);
if (!i2c_fd)
{
printf("Error on opening the device file\n");
return 0;
}
while(1)
{
TP_data.nmsgs = 2;//因为读时序要两次,所以设为2,有几个start nmsgs就设置为几
TP_data.msgs = (struct i2c_msg *)malloc(TP_data.nmsgs * sizeof(struct i2c_msg));
// printf("4");
if (!TP_data.msgs)
{
printf("Memory alloc error\n");
close(i2c_fd);
return 0;
}
ioctl(i2c_fd, I2C_TIMEOUT, 2);//设置超时时间
ioctl(i2c_fd, I2C_RETRIES, 1);//设置重发次数
/*read data from register*/
TP_data.nmsgs = 2;//读时序要两次过程,要发两次I2C消息
TP_data.msgs[0].len = 1;//信息长度为1,第一次要写需要读入的寄存器地址
TP_data.msgs[0].addr = 0x5c;//设备地址
TP_data.msgs[0].flags = 0;//写命令,看读时序理解
TP_data.msgs[0].buf = (unsigned char*)malloc(1);