/* read the data from max6633 */ #include #include #include #include #include #include #include "i2c-dev.h" #include "version.h" static __inline__ unsigned short swap_bytes(unsigned short val) { return(val >> 8) | (val << 8); } int main(void) { int res,address,file; char filename[20]; unsigned short temp; long funcs; int i2cbus; int errorCount,successfullCount; double errorRate; errorCount=0; successfullCount=0; errorRate=0; i2cbus=1; address=0x40; sprintf(filename,"/dev/i2c-%d",i2cbus); if((file=open(filename,O_RDWR))<0) { fprintf(stderr,"Error: could not open file %s: %s\n",filename,strerror(errno)); exit(1); } /* check adapter functionality */ if(ioctl(file,I2C_FUNCS,&funcs) < 0) { fprintf(stderr, "Error: could not get the adapter functionality matrix: %s\n",strerror(errno)); goto ERROR0; } if(!(funcs & I2C_FUNC_SMBUS_READ_BYTE_DATA)) { fprintf(stderr,"Error: Adapter for i2cbus %d",i2cbus); fprintf(stderr," does not have byte read capability\n"); goto ERROR0; } if(!(funcs & I2C_FUNC_SMBUS_READ_WORD_DATA)) { fprintf(stderr,"Error: Adapter for i2cbus %d",i2cbus); fprintf(stderr," does not have word read capability\n"); goto ERROR0; } /* use FORCE so that we can look at registers even when a driver is also running */ if(ioctl(file,I2C_SLAVE,address) <0) { fprintf(stderr,"Error: Could not set address to %d: %s\n",address, strerror(errno)); goto ERROR0; } fprintf(stderr,"Read with word_data\n"); while(1) { temp=res=i2c_smbus_read_word_data(file,0); if(res<0) { fprintf(stderr,"Error: Could not read the value from the i2c device\n"); errorCount++; }else { successfullCount++; } temp=swap_bytes(temp); temp=temp >> 3; if(errorCount>0 && successfullCount>0) { errorRate=(double)errorCount/(double)successfullCount*(double)100; } printf("Success: %i, Errorcount: %i, Errorrate: %f%, Temp: %f\n",successfullCount,errorCount, errorRate,(float)(temp*0.0625)); sleep(1); } printf("\nCalculate temp\n"); close(file); exit(0); ERROR0: close(file); exit(1); }