免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 10187 | 回复: 0
打印 上一主题 下一主题

应用I2C设备,对i2c_msg , i2c_rdwr_ioctl_data 结构的一点理解 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-12-22 23:47 |只看该作者 |倒序浏览
参考: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
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP