LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-04-2016, 07:46 PM   #1
jwmueckl
LQ Newbie
 
Registered: Mar 2016
Posts: 9

Rep: Reputation: Disabled
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.
 
Old 04-05-2016, 12:53 PM   #2
jwmueckl
LQ Newbie
 
Registered: Mar 2016
Posts: 9

Original Poster
Rep: Reputation: Disabled
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));
 
  


Reply

Tags
ioctl


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
i2c-dev.h missing i2c read/write functions dbrazeau Programming 1 12-17-2015 11:40 PM
Boot linux on Beagle bone black with initrd shashwatsingh87 Linux - Embedded & Single-board computer 2 03-11-2015 09:07 AM
How to forward IOCTL's from i2c-dev.c to adapter driver (i2c-rt3352.c) elonica.pl Linux - Kernel 2 08-23-2011 03:03 AM
In linux platform how to use ioctl function to read/write data to hid device zyroot998 Linux - Software 3 06-28-2011 11:06 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:42 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration