Hi all,
I want to control battery LTC4155 through i2c4 interface.
In LTC4155 datasheet the slave address they have given is.
0b0001001[R/W];
Read = 0x13
Write = 0x12
Through the user space, I want to do program.
Refered Documentation/i2cinterface/dev-interface document. The code given below;
//#include <glib.h>
//#include <glib/gprintf.h>
#include <errno.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <linux/i2c-dev.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#define I2C_SLAVE 0x0703
void
sensors_LTC_init(void) {
int file;
char filename[40];
char *buffer;
int addr_write = 0x12; // The I2C address of the LTC4155
int addr_read = 0x13; // The I2C address of the LTC4155
sprintf(filename,"/dev/i2c-3");
if ((file = open(filename,O_RDWR)) < 0) {
printf("Failed to open the bus.");
/* ERROR HANDLING; you can check errno to see what went wrong */
exit(1);
}
if (ioctl(file,I2C_SLAVE,addr_write) < 0) {
printf("Failed to acquire bus access and/or talk to slave.\n");
/* ERROR HANDLING; you can check errno to see what went wrong */
exit(1);
}
char buf[10] = {0};
float data;
1,1 Top
char channel;
int i;
buf[0] = 0x00;
if (write(file,buf,1) != 1) {
/* ERROR HANDLING: i2c transaction failed */
printf("Failed to write to the i2c bus.\n");
printf("\n\n");
}
if (ioctl(file,I2C_SLAVE,addr_read) < 0) {
printf("Failed to acquire bus access and/or talk to slave.\n");
/* ERROR HANDLING; you can check errno to see what went wrong */
exit(1);
}
for(i = 0; i<4; i++) {
// Using I2C Read
if (read(file,buf,2) != 2) {
/* ERROR HANDLING: i2c transaction failed */
printf("Failed to read from the i2c bus.\n");
printf("\n\n");
} else {
//data = (float)((buf[0] & 0b00001111)<<8)+buf[1];
//data = data/4096*5;
//channel = ((buf[0] & 0b00110000)>>4);
printf("Channel %02d Data: %04f\n",channel,data);
}
}
}
int main () {
sensors_LTC_init();
return 0;
}
I 'm unable to read and write using i2c , what may be the problem Let me know?
Regards,
santosh vastrad