之前尝试过一次用这种方式来读EEPROM,结果以失败告终,也没找到原因,今天定位到问题是由于I2C_SLAVE、I2C_SLAVE_FORCE导致的,之前一直尝试I2C_SLAVE,今天定位到问题是I2C总线忙,改成用I2C_SLAVE_FORCE就解决。还有就是测试程序的时候,竟然把不小心tmp75的连续转换给关了,导致获取到的数据总是固定的,一度怀疑人生。
/**********************************************************************
* linux tmp75 /dev/i2c-* 获取数据 demo
* 说明:
* 之前尝试过一次用这种方式来读EEPROM,结果以失败告终,也没找到原因,
* 今天定位到问题是由于I2C_SLAVE、I2C_SLAVE_FORCE导致的,之前一直尝试
* I2C_SLAVE,今天定位到问题是I2C总线忙,改成用I2C_SLAVE_FORCE就解决。
* 还有就是测试程序的时候,竟然把不小心tmp75的连续转换给关了,导致获取到
* 的数据总是固定的,一度怀疑人生。
*
* 2016-3-26 深圳 南山平山村 曾剑锋
*********************************************************************/
// 参考文章:
// 1. MX6 i2C linux driver
// https://community.freescale.com/thread/315690
// 2. Linux内核学习:I2C_SLAVE_FORCE
// http://m.blog.csdn.net/article/details?id=8245226
//
#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>
#define I2C_DEV "/dev/i2c-3"
int main(void){
int tmp75Fd;
int ret;
unsigned char slaveAddr = 0x4c;
unsigned char buf[4] = {0};
// 打开设备
tmp75Fd = open(I2C_DEV, O_RDWR);
if ( tmp75Fd < 0 ){
printf("faile to open the i2c bus: %s.\n", I2C_DEV);
return -1;
}
// 设置7位地址
if ( ioctl(tmp75Fd, I2C_TENBIT, 0) < 0) {
printf("faile to set bits.\n");
return -1;
}
// 强制设置地址
//if ( ioctl(tmp75Fd, I2C_SLAVE, 0x4c) < 0 ) {
if ( ioctl(tmp75Fd, I2C_SLAVE_FORCE, 0x4c) < 0 ) {
perror("faile to set address.\n");
return -1;
}
// 配置tmp75控制器
buf[0] = 0x01;
buf[1] = (1 << 6) | (1 << 5);
if ( write(tmp75Fd, buf, 2) != 2 ) {
perror("faile to write config.\n");
return -1;
}
// 读取tmp75控制器中的值,保证配置正确
buf[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]);
// 将tmp75内的寄存器指针指向地址0
buf[0] = 0;
if ( write(tmp75Fd, buf, 1) != 1 ) {
perror("faile to write Pointer register.\n");
return -1;
}
// 循环读取温度数据
buf[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);
}
// 貌似是多余的
close(tmp75Fd);
return 0;
}