免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 930 | 回复: 0
打印 上一主题 下一主题

linux i2c user 读写 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-04-09 11:55 |只看该作者 |倒序浏览
参考代码  
http://blog.chinaunix.net/u3/91468/showart_1798987.html
linux下24c08的i2c读写程序
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define MAX_I2C_MSG 2
#define I2C_RETRIES 0x701
#define I2C_TIMEOUT 0x702
#define I2C_RDWR 0x707
#define    I2C_M_RD 0x1
struct i2c_msg
{
    __u16    addr;
    __u16    flags;
    __u16    len;
    __u8 *buf;
};
struct i2c_rdwr_ioctl_data
{
    struct i2c_msg *msgs;
    int nmsgs;
};
void clear_whole_eeprom();
void set_whole_eeprom();
void read_whole_eeprom();
void print_usage(__u8 *argv)
{
    printf("\nUsage: %s [-c\\-s\\-r]\n",argv);
}
int main(int argc, char **argv)
{
    int cmd;
    if (argc!=2)
    {
        print_usage(argv[0]);
        return 0;
    }
    if(strcmp(argv[1],"-c") == 0)
        cmd=1;
    else if(strcmp(argv[1],"-s")==0)
        cmd=2;
    else if(strcmp(argv[1],"-r")==0)
        cmd=3;
    else
    {
        print_usage(argv[0]);
        return 0;
    }
    int fd;
    fd = open("/dev/i2c/0",O_RDWR);
    if(fd1024)
    {
        printf("write too long than 1024\n");
        return -1;
    }
    work_queue.nmsgs = 1;
    work_queue.msgs = (struct i2c_msg*)malloc(work_queue.nmsgs * sizeof(struct i2c_msg));
    do{
        work_queue.msgs->len=len>(16-offset%16)?(16-offset%16+1):len+1;
        work_queue.msgs->addr=0x50+offset/256;
        work_queue.msgs->flags=0;//write
        tmp[0]=offset%256;
        memcpy(tmp+1,buf,16);
        work_queue.msgs->buf = tmp;
#if 0
        printf("buf=%x,offset=%u,len=%u,addr=%x\n",buf,tmp[0],work_queue.msgs->len,work_queue.msgs->addr);
        printf("tmp=");
        for(i=0;ilen-1);i++)
        {
            printf("%u ",tmp[i+1]);
        }
        printf("\n");
#endif
        ret = ioctl(fd, I2C_RDWR, (unsigned long)&work_queue);
        if(retlen-1);
        buf=buf+16-offset%16;
        offset=offset+(16-offset%16);
//        usleep(1);
    }while(len>0);
    return bytes;
}
__s32 read_eeprom(__s32 fd,__u32 offset,__u32 len,__u8 *buf)
{
    struct i2c_rdwr_ioctl_data work_queue;
    __u8 tmp[24];
    __s32 ret=0,bytes;
    if((offset+len)>1024)
    {
        printf("read too long than 1024\n");
        return -1;
    }
    bytes=len;
    work_queue.nmsgs = 2;
    work_queue.msgs = (struct i2c_msg*)malloc(work_queue.nmsgs * sizeof(struct i2c_msg));
        (work_queue.msgs[0]).len = 1;
        (work_queue.msgs[0]).addr = 0x50 + offset/256;
        (work_queue.msgs[0]).flags=0;
    tmp[0]=offset%256;
        (work_queue.msgs[0]).buf = tmp;
        (work_queue.msgs[1]).len = len;
        (work_queue.msgs[1]).addr = 0x50 + offset/256;
        (work_queue.msgs[1]).flags=1;
        (work_queue.msgs[1]).buf = buf;
    ret = ioctl(fd, I2C_RDWR, (unsigned long)&work_queue);
    if(ret

本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/19273/showart_1895254.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP