Chinaunix

标题: 应用I2C设备,对i2c_msg , i2c_rdwr_ioctl_data 结构的一点理解 [打印本页]

作者: nn1300    时间: 2008-12-22 23:47
标题: 应用I2C设备,对i2c_msg , i2c_rdwr_ioctl_data 结构的一点理解
参考:Distribution Guide
Device drivers
Inter-IC (I2C)
http://www.stlinux.com/docs/manual/distribution/distribution_guide6.php
http://www.kernel.org/doc/htmldocs/kernel-api/re1209.html

/* In ./include/linux/i2c.h */
struct i2c_msg {   
/* slave address                        */   
__u16 addr;   
/* flags == 0 means write operations      
   flags == 1 means read  operations    */   
__u16 flags;   
/* msg length                    */   
__u16 len;   
/* pointer to msg data            */  
__u8 *buf;
};

/* In ./include/linux/i2c-dev.h */
struct i2c_rdwr_ioctl_data {   
/* pointers to i2c_msgs                 */
   struct i2c_msg __user *msgs;   
/* number of i2c_msgs                   */
   __u32 nmsgs;
};

大概用在2.6以上内核应用
用ioctl 访问i2c步骤(例如写设备0x42 ,地址0x9b, 写入0x81)
1.打开i2c总线,即设备/dev/i2c/0
fd=open(i2c_p->i2c_bus,O_RDWR);

2.对i2c_msg结构赋值
sbuf[0] = addrt[1];           //要写入的地址 addrt[1] = 0x9b
memcpy(sbuf+1, data, nBytes); //要写入的数据 *data = 0x81

//器件地址0x42,右移是因为读写操作用msg[0].flags表示,不用器件地址的最低位
msg[0].addr=i2c_p->dev_address>>1;

//要写入的数据个数,这里sbuf里是2个
  msg[0].len=i2c_p->subaddr_size+nBytes;
  
  //表示读写操作
  msg[0].flags=flags | (read?I2C_M_RD:I2C_M_NOSTART);
   
  //要传的数据
  msg[0].buf=sbuf;   
  
3.系统调用访问总线
//msgtable 既是i2c_rdwr_ioctl_data 结构,
//可用来携带同时向总线上几个器件所发送的数据
//例如:struct i2c_msg msg[2];
//     struct i2c_rdwr_ioctl_data msgtable={msg,2};
//可以向总线上的两个器件发数据,赋值操作同上
if(ioctl(fd,I2C_RDWR,&msgtable)



本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u2/85787/showart_1735838.html




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2