在用户态应用程序中读取I2C

时间:2014-01-02 11:34:35

标签: linux linux-kernel embedded-linux i2c

I2c设备与OMAP L138连接,并应用于读取i2c数据。其中OMAP L138为主设备,i2c设备为从设备,i2c设备发送2字节数据。我可以使用ioctl()和read函数读取两个i2c数据字节。在第一次尝试中,我能够读取前两个字节,但在第二次尝试时,丢弃一个字节,然后读取接下来的两个字节。每次连续读取i2c设备时都会重复此结果。如何确保每个连续读取应该读取每个字节而不丢弃任何一个字节?

代码:

void i2cread()
{  
   int i2c_fd;  
   ssize_t byte;  
   char buffer[32];  

   i2c_fd=open("/dev/i2c-1",O_RDWR);  
   if(i2c_fd<0) {  
      printf("File I2C Device Can't be Opened\n");  
      return -1;  
   }  
   else {  
      printf("File I2C Device is Opened\n");  
   }  
   if(ioctl(i2c_fd,I2C_SLAVE,0x48)<0) {    
      printf("I2C Device Can't be Opened\n");    
      close(i2c_fd);    
      return -1;   
   }
   else {
      printf("I2C Device is Opened\n");
   }  
   byte=read(i2c_fd,buffer,2);  
   printf("%d. ",byte);  
   if(byte<0){  
      printf("\n");  
   }  
   else  {  
     for(byte=0;byte<2;byte++)  
        printf("%d ",buffer[byte]);  
     printf("\n");  
   }
}  

问候
学习者。

0 个答案:

没有答案
相关问题