资源简介
一种是使用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测试流程及测试点
相关资源
- uboot到linux logo显示不间断 补丁
- UNIX/LINUX编程实践教程的源码
- Linux任务管理器
- linux应用层的华容道游戏源代码
- DAS1302蜂鸣 独立按键 闹钟设定 IIC存储
- ubuntu9.10 可加载内核模块和字符设备驱
- MP3文件ID3v2ID3v2APEv2标签读取
- 操作系统实验——虚存管理实验
- linux下的发包工具sendip
- 尚观培训linux许巍关于c 的笔记和讲义
- 尚观培训linux董亮老师关于数据结构的
- linux 线程池源码 c 版
- linux C 电梯程序练习
- linux下用多进程同步方法解决生产者
- Linux 操作系统实验(全)
- Linux From Scratch 中文手册
- linux 网络实验 ftp程序
- Linux命令大全离线版&在线版
- 操作系统共享内存实验
- dos 下运行Linux 命令--gnu_utils
- linux 0.12内核源代码
- linux简易shell C实现
- linux实验报告及心得体会
- 基于GTK的Linux环境下的简易任务管理器
- linux扫雷游戏代码
- CAN Linux驱动代码
- Linux系统教材
- intel 82579LM 网卡驱动Linux系统版 v1.9.
- STM32 DS3231驱动.zip
- IIC proteus仿真实验
评论
共有 条评论