请问大家有做过mpl3115传感器的吗?我用BBB上写个应用程序去读取mpl3115的WHO_AM_I寄存器,通过smbus读取的,读取的正确结果应该是0xc4,但是一直返回,通过调试是Remote I/O error,但是芯片拿到树莓派上用是可以读取的(网上下载的树莓派的python程序),所以芯片应该没问题,但是同份代码我去调试mag3110是可以读取ID的,也可以读取数据。大家能给个思路调试吗?
代码:
struct eeprom
{
char *dev; // device file i.e. /dev/i2c-N
int addr; // i2c address
int fd; // file descriptor
};
static inline __s32 i2c_smbus_access(int file, char read_write, __u8 command, int size, union i2c_smbus_data *data)
{
struct i2c_smbus_ioctl_data args;
args.read_write = read_write;
args.command = command;
args.size = size;
args.data = data;
return ioctl(file, I2C_SMBUS, &args); //调试到这里出现Remote I/O error
}
int eeprom_read_byte(struct eeprom* e, __u16 mem_addr)
{
int r;
union i2c_smbus_data data;
ioctl(e->fd, BLKFLSBUF); //clear kernel read buffer
__u8 buf = mem_addr & 0x0ff;
r = i2c_smbus_access(e->fd, I2C_SMBUS_WRITE, buf, I2C_SMBUS_BYTE, NULL);
usleep(10);
if (r < 0)
return r;
if (i2c_smbus_access(e->fd, I2C_SMBUS_READ, 0, I2C_SMBUS_BYTE, &data) < 0) {
perror("Error:");
return -1;
} else
return 0x0FF & data.byte;
}
int eeprom_write_byte(struct eeprom *e, __u16 mem_addr, __u8 data)
{
int r;
__u8 command = mem_addr & 0x00ff;
union i2c_smbus_data i2c_data;
i2c_data.byte = data;
r = i2c_smbus_access(e->fd, I2C_SMBUS_WRITE, command, I2C_SMBUS_BYTE_DATA, &i2c_data);
/*
if(r < 0)
printf("IIC write error");
*/
usleep(10);
return r;
}
int eeprom_open(char *dev_name, int addr, struct eeprom* e)
{
int funcs, fd, r;
e->fd = e->addr = 0;
e->dev = 0;
fd = open(dev_name, O_RDWR);
if(fd <= 0)
{
fprintf(stderr, "Error eeprom_open: %s\n", strerror(errno));
return -1;
}
#if 1
// get funcs list
if((r = ioctl(fd, I2C_FUNCS, &funcs) < 0))
{
fprintf(stderr, "Error eeprom_open: %s\n", strerror(errno));
return -1;
}
#endif
// check for req funcs
/*CHECK_I2C_FUNC(funcs, I2C_FUNC_SMBUS_READ_BYTE);
CHECK_I2C_FUNC(funcs, I2C_FUNC_SMBUS_WRITE_BYTE);
CHECK_I2C_FUNC(funcs, I2C_FUNC_SMBUS_READ_BYTE_DATA);
CHECK_I2C_FUNC(funcs, I2C_FUNC_SMBUS_WRITE_BYTE_DATA);
CHECK_I2C_FUNC(funcs, I2C_FUNC_SMBUS_READ_WORD_DATA);
CHECK_I2C_FUNC(funcs, I2C_FUNC_SMBUS_WRITE_WORD_DATA);*/
// set working device
if ((r = ioctl(fd, I2C_SLAVE, addr)) < 0)
{
fprintf(stderr, "Error eeprom_open: %s\n", strerror(errno));
return -1;
}
e->fd = fd;
e->addr = addr;
e->dev = dev_name;
return 0;
}
int eeprom_close(struct eeprom *e)
{
close(e->fd);
e->fd = -1;
e->dev = 0;
return 0;
}
void check_normal(struct eeprom *e)
{
//char ret;
int status = -1;
status = eeprom_read_byte(e, 0xC1);
printf("The status read 0xC1: %d\n", status);
status = eeprom_read_byte(e,WHO_AM_I);
printf("The device id: %d\n", status);
if(0xc4 == status)
{
printf("I2C check is OK\n");
} else {
printf("I2C check is failed\n");
exit(0);
}
}
int main(int argc, char** argv)
{
struct eeprom e;
int ret = 0;
if ((ret = eeprom_open(I2C_DEV, SlaveAddressIIC, &e)) == 0)
{
printf("eeprom open success!\n");
} else {
printf("unable to open eeprom device file\n");
exit(0);
}
check_normal(&e);
eeprom_close(&e);
return 0;
}
? 你这是在linux开发的 还是裸机开发
生活就是油盐酱醋再加一点糖,快活就是一天到晚乐呵呵的忙
===================================
做一个简单的人,踏实而务实,不沉溺幻想,不庸人自扰
你好,我也遇到和你同样的问题,请问你现在解决了没有?谢谢