Hi,
I am trying to test i2c read and write on am62x.
Here is the code that i am using.
#include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <sys/stat.h> #include <sys/ioctl.h> #include <unistd.h> #include <fcntl.h> #define I2C_SLAVE 0x51 typedef struct { /* * The I2C Bus number that the device is connected to */ int bus; /* * The slave address of the I2C device */ unsigned int slave_addr; /* * The address of the register we want to access */ char *reg; /* * Number of bytes in the register address */ int reg_size; /* * Data buffer to transfer with the register */ char *buf; /* * Number of data bytes to transfer */ int buf_size; } i2c_test_st; int main(int argc, char **argv) { i2c_test_st i2c_test; int i2c_file; char reg[1]; char buf[2]; int fail = 0; printf("\nI2C test!\n"); i2c_file = open("/dev/i2c-0", O_RDWR); if (i2c_file < 0) { printf("Open failed\n"); exit(-1); } /* Enable the CSI clock */ ioctl(i2c_file, 3, &i2c_test); i2c_test.bus = 0; i2c_test.slave_addr = 0x51; printf("Slave address=0x%x\n\n", i2c_test.slave_addr); reg[0] = (0x100 >> 8) & 0xff; reg[1] = 0x100 & 0xff; buf[1] = 0x4; buf[0] = 0x0; i2c_test.reg = reg; i2c_test.reg_size = 2; i2c_test.buf = buf; i2c_test.buf_size = 2; printf("Data write: buf[0]=%x, buf[1]=%x to reg[0]=%x reg[1]=%x\n", buf[0], buf[1], reg[0], reg[1]); ioctl(i2c_file, 2, &i2c_test); buf[0] = 0; buf[1] = 0; i2c_test.buf = buf; ioctl(i2c_file, 1, &i2c_test); if (i2c_test.buf[0] != 0x0) { fail = 1; } if ((i2c_test.buf[1] & 0x4)!= 0x4) { fail = 1; } printf("Data read: buf[0]=%x, buf[1]=%x from reg=%x\n", buf[0], buf[1], reg[0]); if (fail == 1) { printf("\nI2C TEST FAILED\n\n"); } else { printf("\nI2C TEST PASSED\n\n"); } /* Disable the CSI clock */ ioctl(i2c_file, 4, &i2c_test); close(i2c_file); return 0; }
But when i read back, i keep getting 0.
All the kernel configurations are correct as i have tested with shell script and was able to read back.
Following is the script.
address=0x01 address2=4 temp=0 data=0x01 bus=0 eeprom=0x51 #Loop through the addresses for (( i=0; i<10; i++ )) do #Increment the address by 1 int_value=$address2 int_value=$((int_value + 1)) address2=$int_value temp=$(printf "%x" $int_value) i2cset -y $bus $eeprom $address 0x$temp $data i i2ctransfer -y 0 w2@0x51 $address 0x$temp r1 >/dev/null