I2c_test.c + i2c_dev.c +i2c_s3c3410.c 构成的i2c设备的调用链

本文档详细介绍了如何通过i2c_test.c、i2c_dev.c和i2c_s3c3410.c构成的i2c设备调用链进行设备操作。从用户空间程序i2c_test.c开始,涉及设备文件打开、i2c_msg结构体和i2c_rdwr_ioctl_data结构体的使用,以及ioctl函数的调用来实现读写操作。接着探讨了设备驱动程序i2c_dev.c中的关键函数i2c_transfer,以及适配器的注册和使用。
摘要由CSDN通过智能技术生成

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);
      

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值