I2C的内核驱动测试代码
2011-07-18 10:41
465 查看
以上代码是通过我的测试没有问题的
/*
lulu wangyuluyulu@126.com
*/
#include <stdio.h>
#include <linux/types.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <errno.h>
#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 /* this is a ten bit chip address */
#define I2C_M_RD 0x0001 /* read data, from slave to master */
#define I2C_M_NOSTART 0x4000 /* if I2C_FUNC_PROTOCOL_MANGLING */
#define I2C_M_REV_DIR_ADDR 0x2000 /* if I2C_FUNC_PROTOCOL_MANGLING */
#define I2C_M_IGNORE_NAK 0x1000 /* if I2C_FUNC_PROTOCOL_MANGLING */
#define I2C_M_NO_RD_ACK 0x0800 /* if I2C_FUNC_PROTOCOL_MANGLING */
#define I2C_M_RECV_LEN 0x0400 /* length will be first received byte */
unsigned short len;
unsigned char *buf;
};
struct i2c_rdwr_ioctl_data
{
struct i2c_msg *msgs;
int nmsgs;
/* nmsgs这个数量决定了有多少开始信号,对于“单开始时序”,取1*/
};
/***********主程序***********/
int main()
{
int fd,ret,i;
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(fd,I2C_TIMEOUT,5);/*超时时间*/
ioctl(fd,I2C_RETRIES,1);/*重复次数*/
/***write data to e2prom**/
e2prom_data.nmsgs=1;
(e2prom_data.msgs[0]).len=2; //1个 e2prom 写入目标的地址和1个数据
(e2prom_data.msgs[0]).addr=0x45;//e2prom 设备地址
(e2prom_data.msgs[0]).flags=0; //write
(e2prom_data.msgs[0]).buf=(unsigned char*)malloc(2);
(e2prom_data.msgs[0]).buf[0]=0xaa;// e2prom 写入目标的地址
(e2prom_data.msgs[0]).buf[1]=0xf0;//the data to write
ret=ioctl(fd,I2C_RDWR,(unsigned long)&e2prom_data);
if(ret<0)
{
perror("ioctl error1");
}
sleep(1);
/******read data from e2prom*******/
//for(i=0;i<13;i++){
e2prom_data.nmsgs=1;
(e2prom_data.msgs[0]).len=3; //e2prom 目标数据的地址
(e2prom_data.msgs[0]).addr=0x45; // e2prom 设备地址
// (e2prom_data.msgs[0]).flags=0;//write
//(e2prom_data.msgs[0]).buf[0]=0xaa;//e2prom数据地址
/*(e2prom_data.msgs[1]).len=3;//读出的数据
(e2prom_data.msgs[1]).addr=0x45;// e2prom 设备地址
(e2prom_data.msgs[1]).flags=I2C_M_RD;//read
(e2prom_data.msgs[1]).buf=(unsigned char*)malloc(3);//存放返回值的地址。
(e2prom_data.msgs[1]).buf[0]=0;//初始化读缓冲
(e2prom_data.msgs[1]).buf[1]=0;//初始化读缓冲
(e2prom_data.msgs[1]).buf[2]=0;//初始化读缓冲*/
(e2prom_data.msgs[0]).flags=1;//read
(e2prom_data.msgs[0]).buf=(unsigned char*)malloc(3);//存放返回值的地址。
(e2prom_data.msgs[0]).buf[0]=0;//初始化读缓冲
(e2prom_data.msgs[0]).buf[1]=0;//初始化读缓冲
(e2prom_data.msgs[0]).buf[2]=0;//初始化读缓冲
ret=ioctl(fd,I2C_RDWR,(unsigned long)&e2prom_data);
if(ret<0)
{
perror("ioctl error2");
}
printf("buff[0]=%x\n",(e2prom_data.msgs[0]).buf[0]);
printf("buff[1]=%x\n",(e2prom_data.msgs[0]).buf[1]);
printf("buff[2]=%x\n",(e2prom_data.msgs[0]).buf[2]);
/***打印读出的值,没错的话,就应该是前面写的0x58了***/
close(fd);
return 0;
}
/*
lulu wangyuluyulu@126.com
*/
#include <stdio.h>
#include <linux/types.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <errno.h>
#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 /* this is a ten bit chip address */
#define I2C_M_RD 0x0001 /* read data, from slave to master */
#define I2C_M_NOSTART 0x4000 /* if I2C_FUNC_PROTOCOL_MANGLING */
#define I2C_M_REV_DIR_ADDR 0x2000 /* if I2C_FUNC_PROTOCOL_MANGLING */
#define I2C_M_IGNORE_NAK 0x1000 /* if I2C_FUNC_PROTOCOL_MANGLING */
#define I2C_M_NO_RD_ACK 0x0800 /* if I2C_FUNC_PROTOCOL_MANGLING */
#define I2C_M_RECV_LEN 0x0400 /* length will be first received byte */
unsigned short len;
unsigned char *buf;
};
struct i2c_rdwr_ioctl_data
{
struct i2c_msg *msgs;
int nmsgs;
/* nmsgs这个数量决定了有多少开始信号,对于“单开始时序”,取1*/
};
/***********主程序***********/
int main()
{
int fd,ret,i;
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(fd,I2C_TIMEOUT,5);/*超时时间*/
ioctl(fd,I2C_RETRIES,1);/*重复次数*/
/***write data to e2prom**/
e2prom_data.nmsgs=1;
(e2prom_data.msgs[0]).len=2; //1个 e2prom 写入目标的地址和1个数据
(e2prom_data.msgs[0]).addr=0x45;//e2prom 设备地址
(e2prom_data.msgs[0]).flags=0; //write
(e2prom_data.msgs[0]).buf=(unsigned char*)malloc(2);
(e2prom_data.msgs[0]).buf[0]=0xaa;// e2prom 写入目标的地址
(e2prom_data.msgs[0]).buf[1]=0xf0;//the data to write
ret=ioctl(fd,I2C_RDWR,(unsigned long)&e2prom_data);
if(ret<0)
{
perror("ioctl error1");
}
sleep(1);
/******read data from e2prom*******/
//for(i=0;i<13;i++){
e2prom_data.nmsgs=1;
(e2prom_data.msgs[0]).len=3; //e2prom 目标数据的地址
(e2prom_data.msgs[0]).addr=0x45; // e2prom 设备地址
// (e2prom_data.msgs[0]).flags=0;//write
//(e2prom_data.msgs[0]).buf[0]=0xaa;//e2prom数据地址
/*(e2prom_data.msgs[1]).len=3;//读出的数据
(e2prom_data.msgs[1]).addr=0x45;// e2prom 设备地址
(e2prom_data.msgs[1]).flags=I2C_M_RD;//read
(e2prom_data.msgs[1]).buf=(unsigned char*)malloc(3);//存放返回值的地址。
(e2prom_data.msgs[1]).buf[0]=0;//初始化读缓冲
(e2prom_data.msgs[1]).buf[1]=0;//初始化读缓冲
(e2prom_data.msgs[1]).buf[2]=0;//初始化读缓冲*/
(e2prom_data.msgs[0]).flags=1;//read
(e2prom_data.msgs[0]).buf=(unsigned char*)malloc(3);//存放返回值的地址。
(e2prom_data.msgs[0]).buf[0]=0;//初始化读缓冲
(e2prom_data.msgs[0]).buf[1]=0;//初始化读缓冲
(e2prom_data.msgs[0]).buf[2]=0;//初始化读缓冲
ret=ioctl(fd,I2C_RDWR,(unsigned long)&e2prom_data);
if(ret<0)
{
perror("ioctl error2");
}
printf("buff[0]=%x\n",(e2prom_data.msgs[0]).buf[0]);
printf("buff[1]=%x\n",(e2prom_data.msgs[0]).buf[1]);
printf("buff[2]=%x\n",(e2prom_data.msgs[0]).buf[2]);
/***打印读出的值,没错的话,就应该是前面写的0x58了***/
close(fd);
return 0;
}
相关文章推荐
- 国嵌内核驱动进阶班-7-1(Ioctl设备控制)--- 测试代码
- 使用内核定时器的second字符设备驱动及测试代码
- Code maturity level options 代码成熟度选项 [*]Prompt for development and/or incomplete code/drivers 显示尚在开发中或尚未完成的代码与驱动.除非你是测试人员或者开发者,否则请勿选择 我是开发者,所以选[*] Loadable module support 可加载模块支持 [*]Enable loadable module support 内核编译配置选项简介 (2.4.20-8
- i2c设备驱动(ds1337实例:内核代码)
- IIC专题(二)-使用内核原代码提供的at24.c驱动来操作I2C的at24lc04 EEPROM
- 用内核定时器来实现的按键驱动代码分析以及测试代码
- 详解Linux-I2C驱动(硬件原理\驱动分析\测试)
- 展讯智能机平台代码i2c设备驱动解读(上)
- 微型项目实践(2):用测试驱动代码生成
- 测试驱动开发 读书笔记--坏代码的味道
- 【Tiny6410 And Linux】—(5.1)—RamDisk 驱动实现(内核缺省的处理函数 __make_request())——代码
- i2c驱动--代码
- 在内核中新增驱动代码目录(1)
- 应用层对i2c通用驱动接口与eeprom的i2 4000 c驱动接口从应用层到内核层调用区别和联系
- 【Tiny6410 And Linux】—(6.2)—LCD 驱动测试程序(BMP 显示位图)——代码
- fl2440使能linux-3.0内核自带的led驱动,并测试
- 展讯智能机平台代码 i2c设备驱动解读(下)
- linux驱动——内核通知链(探究i2c-dev.c 中的bus_register_notifier函数所得)
- [I2C]pca9555应用层测试代码
- 内核里写i2c client 驱动的两种方式 【转】--未读