I2C子系统之at24c02读写测试


结合ioctl和at24c02的介绍,写了个at24c02的测试程序
测试硬件平台:TQ2440、at24c02
内核版本:linux-2.6.37.1
读写单独分开成两个小程序。

相关阅读:

I2C子系统之at24c02读写测试
I2C子系统之ioctl() 
I2C子系统之at24c02简介
I2C子系统之总结
I2C子系统之内核中I2C子系统的结构
I2C子系统之I2C bus初始化——I2C_init()
I2C子系统之platfor_device初始化——smdk2440_machine_init()
I2C子系统之platform_driver初始化——I2C_adap_s3c_init()
I2C子系统之I2C总线时钟频率设置
I2C子系统之adapter device和client device注册——I2C_add_number_adapter()
I2C子系统之__I2C_first_dynamic_bus_num变量的相关分析
I2C子系统之 adapter driver注册——I2C_dev_init()
I2C子系统之write()

源码如下:

写测试程序:

  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. }  
  • 1
  • 2
  • 下一页

相关内容