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.
|