在线时间7 小时
UID3265153
注册时间2016-4-24
NXP金币0
该用户从未签到
注册会员

- 积分
- 77
- 最后登录
- 2016-8-3
|
请教一个关于i2c设备驱动的问题
我想通过i2c设备节点操作i2c从设备
#include <stdio.h>
#include <linux/types.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <errno.h>
#define I2C_RETRIES 0X0701
#define I2C_TIMEOUT 0X0702
#define I2C_RDWR 0X0707
struct i2c_msg
{
unsigned short addr;
unsigned short flags;
#define I2C_M_TEN 0X0010
#define I2C_M_RD 0X0001
unsigned short len;
unsigned char *buf;
};
struct i2c_rdwr_ioctl_data
{
struct i2c_msg *msgs;
int nmsgs;
};
int main(int argc ,char* argv[])
{
int fd,ret;
struct i2c_rdwr_ioctl_data e2prom_data;
fd = open("/dev/i2c-2",O_RDWR);
if(fd<0)
{
perror("open error\n");
}
e2prom_data.nmsgs = 2;
e2prom_data.msgs = (struct i2c_msg*)malloc(e2prom_data.nmsgs*sizeof(struct i2c_msg));
if(!e2prom_data.msgs)
{
perror("malloc error\n");
}
ioctl(fd,I2C_TIMEOUT,1);
ioctl(fd,I2C_RETRIES,2);
/* e2prom_data.nmsgs = 1;
(e2prom_data.msgs[0]).len = 2;
(e2prom_data.msgs[0]).addr = 0x50;
(e2prom_data.msgs[0]).flags = 0;
(e2prom_data.msgs[0]).buf = (unsigned char*)malloc(2);
(e2prom_data.msgs[0]).buf[0] = 0x10;
(e2prom_data.msgs[0]).buf[1] = 0x58;
ret = ioctl(fd,I2C_RDWR,(unsigned long)&e2prom_data);
if(ret < 0)
{
perror("ioctl error1");
}
sleep(1);
*/
e2prom_data.nmsgs = 2;
(e2prom_data.msgs[0]).len = 1;
(e2prom_data.msgs[0]).addr = 0x64;
(e2prom_data.msgs[0]).flags = 0;
(e2prom_data.msgs[0]).buf = (unsigned char*)malloc(1);
(e2prom_data.msgs[0]).buf[0] = 0x02;
(e2prom_data.msgs[1]).len = 2;
(e2prom_data.msgs[1]).addr = 0x64;
(e2prom_data.msgs[1]).flags = I2C_M_RD;
(e2prom_data.msgs[1]).buf = (unsigned char*)malloc(2);
(e2prom_data.msgs[1]).buf[0] = 0;
(e2prom_data.msgs[1]).buf[1] = 0;
ret = ioctl(fd,I2C_RDWR,(unsigned long)&e2prom_data);
if(ret<0)
{
perror("ioctl error2");
}
printf("buf[0] = %x\n",(e2prom_data.msgs[1]).buf[0]);
printf("buf[1] = %x\n",(e2prom_data.msgs[1]).buf[1]);
free((e2prom_data.msgs[1]).buf);
free(e2prom_data.msgs);
close(fd);
return 0;
}
这是我的代码
使用的板子是imx6q
要读取的设备是ltc2941库仑计
设备是接到i2c3上面的 设备节点应该就是i2c-2
The 7-bit
hard-coded I2C address of LTC2941 is 1100100.
这个是从设备地址
addr
02h C Accumulated Charge MSB R/W 7Fh
03h D Accumulated Charge LSB R/W FFh
这个是我要读取的设备寄存器的地址
我在板子上面运行之后报 ioctl error2:input/output error
我程序地址有没有什么问题 我现在就是不知道报这个错误 是因为这个设备没接对还是设备已经挂上i2c了我在读写的时候错了
有没有写过的 先帮我看看代码有没有问题 0.0
|
|