日本黄色一级经典视频|伊人久久精品视频|亚洲黄色色周成人视频九九九|av免费网址黄色小短片|黄色Av无码亚洲成年人|亚洲1区2区3区无码|真人黄片免费观看|无码一级小说欧美日免费三级|日韩中文字幕91在线看|精品久久久无码中文字幕边打电话

當(dāng)前位置:首頁 > 單片機(jī) > 單片機(jī)
[導(dǎo)讀]VMware虛擬機(jī)+Fedora10, 硬件平臺(tái)TQ2440, 內(nèi)核2.6.30.4最近學(xué)習(xí)linux I2C驅(qū)動(dòng), 用劉洪濤老師的測試程序測試內(nèi)核自帶的驅(qū)動(dòng), 打開調(diào)試語句dev_dbg后(具體參考我的另一篇博客),發(fā)現(xiàn)應(yīng)用程序?qū)?yīng)的驅(qū)動(dòng)程序豁然開朗, 然

VMware虛擬機(jī)+Fedora10, 硬件平臺(tái)TQ2440, 內(nèi)核2.6.30.4
最近學(xué)習(xí)linux I2C驅(qū)動(dòng), 用劉洪濤老師的測試程序測試內(nèi)核自帶的驅(qū)動(dòng), 打開調(diào)試語句dev_dbg后(具體參考我的另一篇博客),發(fā)現(xiàn)應(yīng)用程序
對(duì)應(yīng)的驅(qū)動(dòng)程序豁然開朗, 然后自己添加了一些dev_dbg后, 對(duì)于不理解的地方也有了一定的參考提示, 記錄下來與大家分享.
測試程序如下:
-----------------------------------------------------------------------------
/*i2c_test.c
* hongtao_liu
*/
#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,要和內(nèi)核一致*******/

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這個(gè)數(shù)量決定了有多少開始信號(hào),對(duì)于“單開始時(shí)序”,取1*/
};

/***********主程序***********/
int main()
{
int fd,ret;
struct i2c_rdwr_ioctl_data e2prom_data;

fd=open("/dev/i2c-0",O_RDWR);
/*
*dev/i2c-0是在注冊(cè)i2c-dev.c后產(chǎn)生的,代表一個(gè)可操作的適配器。如果不使用i2c-dev.c
*的方式,就沒有,也不需要這個(gè)節(jié)點(diǎn)。
*/
if(fd<0)
{
perror("open error");
}
e2prom_data.nmsgs=2;
/*
*因?yàn)椴僮鲿r(shí)序中,最多是用到2個(gè)開始信號(hào)(字節(jié)讀操作中),所以此將
*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,1);/*超時(shí)時(shí)間*/
ioctl(fd,I2C_RETRIES,2);/*重復(fù)次數(shù)*/

/***write data to e2prom**/
e2prom_data.nmsgs=1;
(e2prom_data.msgs[0]).len=2; //1個(gè) e2prom 寫入目標(biāo)的地址和1個(gè)數(shù)據(jù)
(e2prom_data.msgs[0]).addr=0x50;//e2prom 設(shè)備地址
(e2prom_data.msgs[0]).flags=0; //write
(e2prom_data.msgs[0]).buf=(unsigned char*)malloc(2);
(e2prom_data.msgs[0]).buf[0]=0x10;// e2prom 寫入目標(biāo)的地址
(e2prom_data.msgs[0]).buf[1]=0x58;//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*******/
e2prom_data.nmsgs=2;
(e2prom_data.msgs[0]).len=1; //e2prom 目標(biāo)數(shù)據(jù)的地址
(e2prom_data.msgs[0]).addr=0x50; // e2prom 設(shè)備地址
(e2prom_data.msgs[0]).flags=0;//write
(e2prom_data.msgs[0]).buf[0]=0x10;//e2prom數(shù)據(jù)地址
(e2prom_data.msgs[1]).len=1;//讀出的數(shù)據(jù)
(e2prom_data.msgs[1]).addr=0x50;// e2prom 設(shè)備地址
(e2prom_data.msgs[1]).flags=I2C_M_RD;//read
(e2prom_data.msgs[1]).buf=(unsigned char*)malloc(1);//存放返回值的地址。
(e2prom_data.msgs[1]).buf[0]=0;//初始化讀緩沖

ret=ioctl(fd,I2C_RDWR,(unsigned long)&e2prom_data);
if(ret<0)
{
perror("ioctl error2");
}
printf("buff[0]=%xn",(e2prom_data.msgs[1]).buf[0]);
/***打印讀出的值,沒錯(cuò)的話,就應(yīng)該是前面寫的0x58了***/
close(fd);
return 0;
}
------------------------------------------------------------------------------------------------------
從UART口打印的調(diào)試信息如下:

i2c-adapter i2c-0: ioctl, cmd=0x702, arg=0x01

i2c-adapter i2c-0: ioctl, cmd=0x701, arg=0x02

i2c-adapter i2c-0: ioctl, cmd=0x707, arg=0xbeb95d28

i2c-adapt

本站聲明: 本文章由作者或相關(guān)機(jī)構(gòu)授權(quán)發(fā)布,目的在于傳遞更多信息,并不代表本站贊同其觀點(diǎn),本站亦不保證或承諾內(nèi)容真實(shí)性等。需要轉(zhuǎn)載請(qǐng)聯(lián)系該專欄作者,如若文章內(nèi)容侵犯您的權(quán)益,請(qǐng)及時(shí)聯(lián)系本站刪除( 郵箱:macysun@21ic.com )。
換一批
延伸閱讀
關(guān)閉