/********************************************************************** * Linux tmp75/dev/i2c-* get Data demo * Description: * Previous attempts to read the EEPROM in this way, the result failed, and did not find the reason, * today, the problem is fixed due to i2c_slave, I2c_slave_force caused by the previous attempt * i2c_slave, today to locate the problem is the I²C bus Busy, change to use I2c_slave_force to solve. * There is the test procedure, unexpectedly tmp75 the continuous conversion to turn off, resulting in access to the data is always fixed, once doubted life. * 2016-3-26 Shenzhen Nanshan Ping Shan village Zengjianfeng *********************************************************** **********///Reference article://MX6 i²c Linux driver// https://community.freescale.com/thread/315690#include<stdio.h>#include<stdlib.h>#include<sys/types.h>#include<sys/stat.h>#include<fcntl.h>#include<string.h>#include<linux/i2c.h>#include<linux/i2c-dev.h>#include<unistd.h>#include<sys/time.h>#defineI2c_dev "/dev/i2c-3"intMainvoid){ intTMP75FD; intret; unsignedCharSLAVEADDR =0x4c; unsignedCharbuf[4] = {0}; //turn on the deviceTMP75FD =Open (I2c_dev, O_RDWR); if(TMP75FD <0) {printf ("Faile to open the I²c bus:%s.\n", I2c_dev); return-1; } //set 7-bit address if(IOCTL (TMP75FD, I2c_tenbit,0) <0) {printf ("Faile to set bits.\n"); return-1; } //Force set Address//if (IOCTL (TMP75FD, I2c_slave, 0x4c) < 0) { if(IOCTL (TMP75FD, I2c_slave_force,0x4c) <0) {perror ("Faile to set address.\n"); return-1; } //Configuring the Tmp75 controllerbuf[0] =0x01; buf[1] = (1<<6) | (1<<5); if(Write (TMP75FD, buf,2) !=2) {perror ("Faile to write config.\n"); return-1; } //read the values in the TMP75 controller to ensure proper configurationbuf[0] =1; if(Write (TMP75FD, buf,1) !=1) {perror ("Faile to write Pointer register.\n"); return-1; } buf[0] =0; if(Read (TMP75FD, buf,1) !=1) {perror ("Faile to read back configure data.\n"); return-1; } printf ("tmp75 configure:0x%x.\n", buf[0]); //point The Register pointer inside the tmp75 to address 0buf[0] =0; if(Write (TMP75FD, buf,1) !=1) {perror ("Faile to write Pointer register.\n"); return-1; } //cyclic reading of temperature databuf[0] =0; buf[1] =0; while(1 ) { if(Read (TMP75FD, buf,2) !=2) {perror ("Faile to read data.\n"); return-1; } printf ("tmp75 temperature:0x%x%x.\n", buf[0], buf[1]); Usleep (500000); } //it seems to be superfluous.Close (TMP75FD); return 0;}
Linux tmp75/dev/i2c-* get data demo