资源简介
一种是使用read and write的方式读写。一种是使用构造i2c_msg结构体的方式并利用ioctl的方式读写
代码片段和文件信息
#include
#include
#include
#include
#include
#include
#include
#include
#define I2C_RETRIES 0x0701
#define I2C_TIMEOUT 0x0702
#define I2C_RDWR 0x0707
/*********定义struct i2c_rdwr_ioctl_data和struct i2c_msg,要和内核一致*******/
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;
/* nmsgs这个数量决定了有多少开始信号,对于“单开始时序”,取1*/
};
/***********主程序***********/
int main()
{
int fdretk;
struct i2c_rdwr_ioctl_data e2prom_data;
fd=open(“/dev/i2c-0“O_RDWR);
/*
dev/i2c-0是在注册i2c-dev.c后产生的,代表一个可操作的适配器。如果不使用i2c-dev.c
的方式,就没有,也不需要这个节点。
*/
if(fd<0)
{
perror(“open error“);
}
e2prom_data.nmsgs=2;
/*
*因为操作时序中,最多是用到2个开始信号(字节读操作中),所以此将
*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“);
exit(1);
}
ioctl(fdI2C_TIMEOUT2);/*超时时间*/
ioctl(fdI2C_RETRIES2);/*重复次数*/
/***write data to e2prom**/
e2prom_data.nmsgs=1;
(e2prom_data.msgs[0]).len=27; //1个 e2prom 写入目标的地址和1个数据
(e2prom_data.msgs[0]).addr=0x51;//e2prom 设备地址
(e2prom_data.msgs[0]).flags=0; //write
(e2prom_data.msgs[0]).buf=(unsigned char*)malloc(27);
(e2prom_data.msgs[0]).buf[0]=0x00;// e2prom 写入目标的地址
for (k=1;k<27;k++)
(e2prom_data.msgs[0]).buf[k]=(unsigned char)k;//the data to write
ret=ioctl(fdI2C_RDWR(unsigned long)&e2prom_data);
if(ret<0)
{
perror(“write error“);
}
free((e2prom_data.msgs[0]).buf);
sleep(1);
/******read data from e2prom*******/
e2prom_data.nmsgs=2;
(e2prom_data.msgs[0]).len=1; //e2prom 目标数据的地址
(e2prom_data.msgs[0]).addr=0x51; // e2prom 设备地址
(e2prom_data.msgs[0]).flags=0;//write
(e2prom_data.msgs[0]).buf=(unsigned char*)malloc(1);
(e2prom_data.msgs[0]).buf[0]=0x00;//e2prom数据地址
(e2prom_data.msgs[1]).len=26;//读出的数据
(e2prom_data.msgs[1]).addr=0x51;// e2prom 设备地址
(e2prom_dat
- 上一篇:用LZW压缩算法实现的编码与解码
- 下一篇:百度外卖用户端APP测试流程及测试点
相关资源
- nmon工具linux性能32位的
- linux网络设备驱动
- Linux Wi-Fi open source drivers-mac80211 ath9k
- Linux Device Driver 中文版第三版
- 理发师问题Linux,c
- linux下cp2101/2102驱动
- kali linux 2019最新镜像包.txt
- 实战Linux编程精髓源代码
- linux下中断处理程序,含驱动和应用程
- SLE-12-SP1-Server-DVD-x86_64-GM-DVD1
- SLES 11 SP4 x86_64
- 01 [基础] 鸟哥私房菜 - 经典Linux零基础
- 用于jz2440的dht11驱动
- 全套linux运维教程
- linux-tpcc测试
- Centos7.1(1503)国内镜像地址.txt
- linux制作启动u盘工具
- HID 免驱 描述符
- OLED12864显示屏,12位ADC,按键中断,
- linuxredis实战教程视频.txt
- Linux内存管理详解.ppt
- jdk11_linux_64.txt
- linux系统net-tools工具包
- 文件系统源代码
- IBM ILOG CPLEX Optimization Studio Free Editio
- Linux 运维入门到高级 全套系列
- gd-devel-2.0.35-11.el6.x86_64.rpm
- intelfortran2019forWINMACLINUX网盘.txt
- 哈工大 操作系统实验二 linux0.01添加系
- u-boot.bin_tekkaman_2009.11.bz2
评论
共有 条评论