#include <stdio.h>
#include <linux/i2c.h>
#include <linux/i2c-dev.h>
#include <fcntl.h>
#define msleep (udelay *1000)
int device_write(int *file, unsigned char reg, unsigned char *val, unsigned int length)
{
char buf[3];
int msgsExec = 0;
buf[0] = reg;
buf[1] = val[0];
buf[2] = val[1];
struct i2c_msg msg[1] = {
{ .addr = 0x08,
.flags = 0, .buf = &buf[0], .len = length},
};
struct i2c_rdwr_ioctl_data ioctl_data = {
.msgs = (struct i2c_msg *)&msg, /* ptr to array of simple messages */
.nmsgs = 0x01 /* number of messages to exchange */
};
msgsExec = ioctl(*file, I2C_RDWR, &ioctl_data);
printf("Executed messages : %d\n", msgsExec);
printf("write : 0x%x,0x%x to :0x%x\n", buf[1], buf[2], buf[0]);
return msgsExec;
}
int device_read(int *file, unsigned char reg, unsigned char *val, unsigned char len)
{
char buf[2];
int msgsExec = 0;
char bval[4] = { 0, 0, 0, 0 };
buf[0] = reg;
struct i2c_msg msg[2] = {
{ .addr = 0x08,
.flags = 0, .buf = &buf[0], .len = 1 },
{ .addr = 0x08,
.flags = I2C_M_RD, .buf = &bval[0], .len = len },
};
struct i2c_rdwr_ioctl_data ioctl_data = {
.msgs = (struct i2c_msg *)&msg, /* ptr to array of simple messages */
.nmsgs = 0x02 /* number of messages to exchange */
};
msgsExec = ioctl(*file, I2C_RDWR, &ioctl_data);
printf("Executed messages : %d\n", msgsExec);
printf("read : 0x%x from :0x%x\n", bval[0], buf[0]);
val = bval;
return msgsExec;
}
int main()
{
int file;
int ret ,i;
int adapter_nr = 1; /* probably dynamically determined */
char filename[20];
int addr = 0x08; /* The I2C address */
char buf[2];
unsigned int regVal;
snprintf(filename, 19, "/dev/i2c-%d", adapter_nr);
int msgsExec = 0;
char bval[2] = { 0, 0 };
file = open(filename, O_RDWR);
if (file < 0) {
/* ERROR HANDLING; you can check errno to see what went wrong */
printf("Failed to open i2c device \n");
return 1;
}
if (ioctl(file, I2C_SLAVE, addr) < 0) {
/* ERROR HANDLING; you can check errno to see what went wrong */
printf("Failed to get i2c slave device \n");
return;
}
buf[0] = 0x78;
buf[1] = 0x48;
msgsExec = device_write(&file, 0x01, buf, 3);
msgsExec = device_read(&file, 0x01, &bval[0],1);
#if 0
buf[0] =0x20;
ret = write(file, buf, 1);
if(ret < 0)
printf("Failed to write device \n");
/* ERROR HANDLING: i2c transaction failed */
if (read(file, buf, 1) != 1) {
printf("Failed to read device \n");
/* ERROR HANDLING: i2c transaction failed */
} else {
printf("read byte %x at add %x \n",buf[0],i);
/* buf[0] contains the read byte */
}
#endif
close(file);
}
We are using above application to communicate with an i2c slave over dm6467T. Can we get some pointers on the below issues we are facing
- When we write 1 byte data on the i2c device, we get a read terminated error "RDR read not requested"
- when we read 1 byte data from the i2c device we always read 0x00 data
- when we repeat the same steps in ccs application we get correct data without and errors and warnings.
Can we get some pointers as in why we are seeing this different behavior?
Thanks in advance
Sushant