分享

I2C子系统之at24c02读写测试

 kkq292 2014-04-09

I2C子系统之at24c02读写测试

分类: Linux驱动之I2C 627人阅读 评论(0) 收藏 举报

结合ioctl和at24c02的介绍,写了个at24c02的测试程序

测试硬件平台:TQ2440、at24c02

内核版本:linux-2.6.37.1

读写单独分开成两个小程序。源码如下:

写测试程序:

  1. #include <stdio.h>  
  2. #include <fcntl.h>  
  3. #include <stdlib.h>  
  4. #include <string.h>  
  5. #include <linux/i2c-dev.h>  
  6. #include <errno.h>  
  7.   
  8. int main(int argc, char *argv[])  
  9. {  
  10.     int num, err, i, j;  
  11.     int fd, addr;  
  12.     char *buff;  
  13.       
  14.     printf("please input as:");  
  15.     printf("./wat24 [data]\n");  
  16.     fflush(stdout);  
  17.       
  18.     if(argc < 3){  
  19.         printf("arg error\n");  
  20.         return -1;    
  21.     }  
  22.     num = argc - 1;  
  23.   
  24.     buff = malloc(num*sizeof(char));  
  25.     if(buff < 0){  
  26.         printf("alloc failed\n");  
  27.         return -1;  
  28.     }  
  29.   
  30.     buff[0] = atoi(argv[1]);  
  31.       
  32.     printf("write data:\n");  
  33.     for(i = 1; i < num; i++){  
  34.         buff[i] = atoi(argv[i + 1]);  
  35.         printf("%d\n",buff[i]);  
  36.     }  
  37.     printf("from word addr:%d\n",buff[0]);  
  38.   
  39.     fd = open("/dev/i2c-0",O_RDWR);  
  40.     if(fd < 0){  
  41.         printf("device open failed\n");  
  42.         return -1;    
  43.     }  
  44.       
  45.     err = ioctl(fd, I2C_SLAVE_FORCE, 0x50);  
  46.     if(err < 0){  
  47.         printf("ioctl failed:%d\n",err);  
  48.         return -1;  
  49.     }  
  50.       
  51.     write(fd, buff, num);  
  52.       
  53.     close(fd);  
  54.     return 0;  
  55. }  
at24c02的写操作可以直接调用write()函数实现。

可以直接进行byte write 或者 page write

在进行page write的时候需要计算好页的起始地址。

程序的使用如下:

page write

./wat24 0 1 2 3 4 5 6 7 8

表示从at24c02中word address为0的地址开始写入1~8。

./wat24 1 1 2 3 4 5 6 7 8

表示从at24c02中的word address为1的地址开始写入1~8数据,但是1是page0内的地址,地址非页对齐。

所以最后在page0内的数据是8 1 2 3 4 5 6 7,8被写到page0的首地址处了。

byte write

./wat24 255 1

byte write相对简单。255表示at24c02中的一个地址,1是向此地址中写入的数据。


读测试程序

  1. #include <stdio.h>  
  2. #include <fcntl.h>  
  3. #include <string.h>  
  4. #include <stdlib.h>  
  5. #include <linux/i2c.h>  
  6. #include <linux/i2c-dev.h>  
  7.   
  8. struct i2c_rdwr_ioctl_data rdwrdata;  
  9.   
  10. int main(int argc, char *argv[])  
  11. {  
  12.     int i, err;  
  13.     int fd;  
  14.     char wordaddr;  
  15.     char *rdbuf1;  
  16.     char *rdbuf2;  
  17.     int bytenum;  
  18.       
  19.     if(argc < 3){  
  20.         printf("please input as:");  
  21.         printf("./rat24 [read byte addr] [read num of byte]\n");  
  22.         return -1;  
  23.     }     
  24.   
  25.     wordaddr = atoi(argv[1]);  
  26.     bytenum = atoi(argv[2]);  
  27.     printf("%d\n",bytenum);  
  28.   
  29.     rdwrdata.msgs = (struct i2c_msg *)malloc(2*sizeof(struct i2c_msg));  
  30.     if(!rdwrdata.msgs){  
  31.         printf("rdwrdata.msgs malloc failed!\n");  
  32.         return -1;  
  33.     }     
  34.   
  35.     rdbuf1 = (unsigned char *)malloc(sizeof(char));  
  36.     rdbuf2 = (unsigned char *)malloc(bytenum*sizeof(char));  
  37.     if((!rdbuf1) || (!rdbuf2)){  
  38.         printf("rdbuf malloc failed!\n");  
  39.         return -1;  
  40.     }  
  41.   
  42.     rdwrdata.nmsgs = 2;  
  43.       
  44.     (rdwrdata.msgs[0]).addr = 0x50;  
  45.     (rdwrdata.msgs[0]).len = 1;  
  46.     (rdwrdata.msgs[0]).flags = 0;  
  47.     (rdwrdata.msgs[0]).buf = rdbuf1;  
  48.     (rdwrdata.msgs[0]).buf[0] = wordaddr;  
  49.           
  50.     (rdwrdata.msgs[1]).addr = 0x50;  
  51.     (rdwrdata.msgs[1]).len = bytenum;  
  52.     (rdwrdata.msgs[1]).flags = I2C_M_RD;  
  53.     (rdwrdata.msgs[1]).buf = rdbuf2;  
  54.       
  55.     fd = open("/dev/i2c-0",O_RDWR);  
  56.     if(fd < 0){  
  57.         printf("i2c device open failed!\n");  
  58.         return -1;    
  59.     }  
  60.       
  61.     err = ioctl(fd, I2C_SLAVE_FORCE, 0x50);  
  62.     if(err < 0){  
  63.         printf("ioctl failed\n");  
  64.         return -1;  
  65.     }  
  66.       
  67.     err = ioctl(fd, I2C_RDWR, &rdwrdata);  
  68.     if(err < 0){  
  69.         printf("ioctl msgs error, error number:%d\n", err);  
  70.         return -1;  
  71.     }  
  72.   
  73.     printf("read %d byte data from at24c02 word address 0x%02x:\n",bytenum, wordaddr);  
  74.     for(i = 0; i < bytenum; i++)  
  75.         printf("%d\n", rdbuf2[i]);  
  76.       
  77.     close(fd);  
  78.     return 0;  
  79. }  
at24c02的read总共有三种

1.current read

2.sequential read

3.random read

其中1和2可以直接通过read系统调用实现

3.的话就需要通过ioctl(,I2C_RDWR,)来实现。

上述程序就是实现这个功能的,可以实现at24c02的random read。

random read

./rat24 0 256

表示从at24c02的word address为0的地址开始读取256个字节数据

./rat24 0 8

表示从at24c02的word address为0的地址开始读取8个字节的数据,即读取page0的数据。


    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多