LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Failing to read the correct data on Beagle bone black using ioctl and file read on I2C bus 0 (https://www.linuxquestions.org/questions/linux-newbie-8/failing-to-read-the-correct-data-on-beagle-bone-black-using-ioctl-and-file-read-on-i2c-bus-0-a-4175576686/)

jwmueckl 04-04-2016 06:46 PM

Failing to read the correct data on Beagle bone black using ioctl and file read on I2C bus 0
 
I wrote the following code based on similar examples to read the STATUS register from the Beagle Bone Black built-in Power Management IC (TPS65217). The TPS65217 is an I2C slave device at address 0x24. The STATUS register value I want to obtain is stored in the 10th register of the thirty registers on the TPS65217.

Although I do obtain thirty bytes of data as a result of the read function, it appears to be incorrect, since the first byte does not match the chip ID and the last 23 bytes of the expected thirty bytes are all zero.

I would appreciate it very much if someone could identify the source of my error. I am leaving out the code that does not pertain to this issue.

#include<sys/ioctl.h>
#include<fcntl.h>
<linux/i2c-dev.h>

char ReadDat[30] = {0};
int ACPWR_On = 0;

if (( fi2c = open("/dev/i2c-0", O_RDONLY)) < 0)
fprintf(stderr, "Failed to communicate with I2C Bus 0\n");

if (ioctl(fi2c, I2C_SLAVE_FORCE, 0x24) < 0)
fprintf(stderr, "i2c_open ioctl error:%s\n", strerror(errno));

if (read (fi2c, ReadDat, sizeof(ReadDat)) < 0 )
frintf(stderr, "Failed to read I2C Bus\n");
else
ACPWR_On = ReadDat[10]; // ACPWR_On = 0

The resulting data in ReadDat[30] is as follows:
0x0, 0x3, 0x15, 0x5F, 0x32, 0x40, 0x20, 0, 0, 0, 0, 0 ...

Using I2C_SLAVE instead of I2C_SLAVE_FORCE results in the return of -1 from ioctl()

rsh'ing to my Beagle Bone Black from a Linux terminal and then executing the command 'sudo i2cget -y -f 0 0x24 0xA' returns the correct value of 0x8C. I therefore expect ReadDat[10] to contain 0x8C.

jwmueckl 04-05-2016 11:53 AM

I should let everyone know that I found a solution for my problem based on another example. I would, however, appreciate further insight on the root cause. If my solution is the right one, why do so few code examples on line follow a similar procedure?

Due to an I2C initialization issue, the address of the PMIC I told my code to read is not the address being read. Before performing the read of the PMIC, I have to write a value of zero to the I2C bus as follows. Then the read function understands what my intended address is.

char tempBuf[1] = {0x00};
if (write(fi2c, tempBuf, 1) != 1)
fprintf(stderr, "Failed to reset I2C Bus address: %s\n", strerror(errno));


All times are GMT -5. The time now is 11:27 PM.