資源簡介
一種是使用read and write的方式讀寫。一種是使用構(gòu)造i2c_msg結(jié)構(gòu)體的方式并利用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,要和內(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這個數(shù)量決定了有多少開始信號,對于“單開始時序”,取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后產(chǎn)生的,代表一個可操作的適配器。如果不使用i2c-dev.c
????????的方式,就沒有,也不需要這個節(jié)點。
????????*/
????????????????if(fd<0)
????????????????{
????????????????????????perror(“open?error“);
????????????????}
????????????????e2prom_data.nmsgs=2;?
????????/*
????????*因為操作時序中,最多是用到2個開始信號(字節(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(fdI2C_TIMEOUT2);/*超時時間*/
????????????????ioctl(fdI2C_RETRIES2);/*重復次數(shù)*/
????????????????/***write?data?to?e2prom**/
????????????????e2prom_data.nmsgs=1;
????????????????(e2prom_data.msgs[0]).len=27;?//1個?e2prom?寫入目標的地址和1個數(shù)據(jù)?
????????????????(e2prom_data.msgs[0]).addr=0x51;//e2prom?設(shè)備地址
????????????????(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?目標數(shù)據(jù)的地址
????????????????(e2prom_data.msgs[0]).addr=0x51;?//?e2prom?設(shè)備地址
????????????????(e2prom_data.msgs[0]).flags=0;//write
?(e2prom_data.msgs[0]).buf=(unsigned?char*)malloc(1);
????????????????(e2prom_data.msgs[0]).buf[0]=0x00;//e2prom數(shù)據(jù)地址
????????????????(e2prom_data.msgs[1]).len=26;//讀出的數(shù)據(jù)
????????????????(e2prom_data.msgs[1]).addr=0x51;//?e2prom?設(shè)備地址?
????????????????(e2prom_dat
評論
共有 條評論