i2c read failed

Hi,

I am trying to read two bytes of data from a board(slave) using BBB(master) through i2c interface.

I connected my board’s SDA and SCL lines to P9_18 and P9_17 of BBB respectively.

My code is

#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <linux/i2c-dev.h>
#include <sys/ioctl.h>

int main()
{
int fd,i;
int addr = 0x43;
char buffer[10] = {0};

fd = open("/dev/i2c-1", O_RDWR); // opening i2c-1 file. returns a file descriptor used by read
if(fd < 0)
{
printf(“i2c failed to open\n”);
}

if(ioctl(fd, I2C_SLAVE, addr) < 0)
printf(“failed to acquire bus\n”);

if(read(fd,buffer,2) != 2) //reading 2 bytes of data
{
printf(“i2c read failed\n”);

}
else
{
for(i=0;i<2;i++)
printf(“the data is %d \n”,buffer[i]);
}

return 0;
}

I got output — “i2c read failed”

What is the problem in my code? Or any problem in hardware? When I checked the following command, I got output like

root@beaglebone:~# i2cdetect -l
i2c-0 i2c OMAP I2C adapter I2C adapter
i2c-1 i2c OMAP I2C adapter I2C adapter
root@beaglebone:~# ls -l /sys/bus/i2c/devices/i2c-*
lrwxrwxrwx 1 root root 0 Jan 1 2000 /sys/bus/i2c/devices/i2c-0 → …/…/…/devices/ocp.3/44e0b000.i2c/i2c-0
lrwxrwxrwx 1 root root 0 Jan 1 2000 /sys/bus/i2c/devices/i2c-1 → …/…/…/devices/ocp.3/4819c000.i2c/i2c-1

Can anyone find problem? Please reply.

Does i2cget work?

I tried i2cget command but it also showing Error: Read failed.

What it means? Any hardware problem? How i trace the problem?

Why don’t you show is what command your running?

Like this:

I am using i2cset just fine on my BBB of P9 19 and 20, it is currently Bus1.

My notes:

i2c detect on Bus1, see my address at 4C and 4D:

i2cdetect -r -y 1

0 1 2 3 4 5 6 7 8 9 a b c d e f

00: – -- – -- – -- – -- – -- – -- –

10: – -- – -- – -- – -- UU – -- – -- – -- –

20: – -- – -- – -- – -- – -- – -- – -- – --

30: – -- – -- – -- – -- – -- – -- – -- – --

40: – -- – -- – -- – -- – -- – -- 4c 4d – --

50: – -- – -- UU UU UU UU – -- – -- – -- – --

60: – -- – -- – -- – -- – -- – -- – -- – --

Dump the 4C device registers, register 0x14 is 0x00:

i2cdump -y 1 0x4c

No size specified (using byte-data access)

0 1 2 3 4 5 6 7 8 9 a b c d e f 0123456789abcdef

00: 00 00 00 00 11 31 00 00 20 00 00 01 7c 10 00 00 …?1… …?|?..

10: 00 00 00 10 00 10 00 00 01 00 80 01 0f 03 07 04 …?.?..?.???

20: 00 00 00 04 00 08 f3 04 13 00 11 1f 00 00 00 00 …?.???.??..

30: 00 00 00 00 00 00 00 00 00 00 00 00 00 30 30 22 …00"

40: 02 04 14 05 00 00 00 00 55 00 00 00 00 00 00 00 ???..U…

50: 00 00 00 00 00 07 00 00 81 00 00 00 00 40 78 15 …?..?..@x?

60: 01 10 00 00 00 01 0f 03 07 11 00 00 00 00 06 03 ??..???..??

70: 02 83 03 00 04 00 80 01 00 00 00 00 00 00 00 00 ???.?.??..

80: 00 00 00 00 11 31 00 00 20 00 00 01 7c 10 00 00 …?1… …?|?..

90: 00 00 00 10 00 10 00 00 01 00 80 01 0f 03 07 04 …?.?..?.???

a0: 00 00 00 04 00 08 f3 04 13 00 11 1f 00 00 00 00 …?.???.??..

b0: 00 00 00 00 00 00 00 00 00 00 00 00 00 30 30 22 …00"

c0: 02 04 14 05 00 00 00 00 55 00 00 00 00 00 00 00 ???..U…

d0: 00 00 00 00 00 07 00 00 81 00 00 00 00 40 78 15 …?..?..@x?

e0: 01 10 00 00 00 01 0f 03 07 11 00 00 00 00 06 03 ??..???..??

f0: 02 83 03 00 04 00 80 01 00 00 00 00 00 00 00 00 ???.?.??..

Write 0x01 to register 0x14:

i2cset -y 1 0x4c 0x14 0x01

Dump the registers and not the change to 0x01:

i2cdump -y 1 0x4c

No size specified (using byte-data access)

0 1 2 3 4 5 6 7 8 9 a b c d e f 0123456789abcdef

00: 00 00 00 00 11 31 00 00 20 00 00 01 7c 10 00 00 …?1… …?|?..

10: 00 00 00 10 01 10 00 00 01 00 80 01 0f 03 07 04 …???..?.???

20: 00 00 00 04 00 08 f3 04 13 00 11 1f 00 00 00 00 …?.???.??..

30: 00 00 00 00 00 00 00 00 00 00 00 00 00 30 30 22 …00"

40: 02 04 14 05 00 00 00 00 55 00 00 00 00 00 00 00 ???..U…

50: 00 00 00 00 00 07 00 00 81 00 00 00 00 40 78 15 …?..?..@x?

60: 01 10 00 00 00 01 0f 03 07 11 00 00 00 00 06 03 ??..???..??

70: 02 83 03 00 04 00 80 01 00 00 00 00 00 00 00 00 ???.?.??..

80: 00 00 00 00 11 31 00 00 20 00 00 01 7c 10 00 00 …?1… …?|?..

90: 00 00 00 10 01 10 00 00 01 00 80 01 0f 03 07 04 …???..?.???

a0: 00 00 00 04 00 08 f3 04 13 00 11 1f 00 00 00 00 …?.???.??..

b0: 00 00 00 00 00 00 00 00 00 00 00 00 00 30 30 22 …00"

c0: 02 04 14 05 00 00 00 00 55 00 00 00 00 00 00 00 ???..U…

d0: 00 00 00 00 00 07 00 00 81 00 00 00 00 40 78 15 …?..?..@x?

e0: 01 10 00 00 00 01 0f 03 07 11 00 00 00 00 06 03 ??..???..??

f0: 02 83 03 00 04 00 80 01 00 00 00 00 00 00 00 00 ???.?.??..

Works for me!

If i2c tools are not working your script won’t work either.

Your really need to show more info to troubleshoot this…

Chip address conflict perhaps?

Have you connected 4k7 (optional: 1k to 10k) resistors from 3.3V to SDA and to SCL? That helps many times.

Pin 17 and 18 belong to /dev/i2c-0 in my opinion.

So try to change your code to
fd = open("/dev/i2c-0", O_RDWR);

or use pin 19 and 20.

To find your device use the i2cdetect command.
For i2c-0 use:
sudo i2cdetect -r 0

and for i2c-1 use
sudo i2cdetect -r 1

Hi everyone,

I tried i2c communication with p9_17 and p9_18 with /dev/i2c-0 and /dev/i2c-1.
I got same error in both cases: Remote I/O error.
But my ioctl() function is working and i got ‘0’ return value(success).
Now i am confused with pull up resistors. Is it necessary to pull up p9_17 and p9_18 line to Vdd?
I know that there is internal pull up resistors present in BBB. Is this not available for two enabled i2c buses?
How to enable all the three i2c buses?
Can I choose i2c1 and i2c2 for communication?
When I connected my slave board to BBB(p9_17 and p9_18) and checked the command ‘i2cdetect -r 1’
I got :

root@beaglebone:~# i2cdetect -r 1
WARNING! This program can confuse your I2C bus, cause data loss and worse!
I will probe file /dev/i2c-1 using read byte commands.
I will probe address range 0x03-0x77.
Continue? [Y/n] y
0 1 2 3 4 5 6 7 8 9 a b c d e f
00: – -- – -- – -- – -- – -- – -- –
10: – -- – -- – -- – -- – -- – -- – -- – --
20: – -- – -- – -- – -- – -- – -- – -- – --
30: – -- – -- – -- – -- – -- – -- – -- – --
40: – -- – -- – -- – -- – -- – -- – -- – --
50: – -- – -- UU UU UU UU – -- – -- – -- – --
60: – -- – -- – -- – -- – -- – -- – -- – --
70: – -- – -- – -- – --

for 'i2cdetect -r 0 ’ i got

root@beaglebone:~# i2cdetect -r 0
WARNING! This program can confuse your I2C bus, cause data loss and worse!
I will probe file /dev/i2c-0 using read byte commands.
I will probe address range 0x03-0x77.
Continue? [Y/n] y
0 1 2 3 4 5 6 7 8 9 a b c d e f
00: – -- – -- – -- – -- – -- – -- –
10: – -- – -- – -- – -- – -- – -- – -- – --
20: – -- – -- UU – -- – -- – -- – -- – -- –
30: – -- – -- UU – -- – -- – -- – -- – -- –
40: – -- – -- – -- – -- – -- – -- – -- – --
50: UU – -- – -- – -- – -- – -- – -- – -- –
60: – -- – -- – -- – -- – -- – -- – -- – --
70: UU – -- – -- – -- –

I am waiting for your great opinions.

What device are you using? Is it 3.3V kompatible?

Check the following:-Do you use the right pins? Pin 1 is located near the 5V jack and not on the USB side. (see my photo for i2c-1)
-Don’t mess up with SCL and SDA
-Is your device 3.3V compatible? If not you need a level shifter.
-Put two pull up resistors to SCL and SDA if your device doesn’t provide them.

i2c.jpg

Good morning everyone,

My slave board consists of 16 bit I/O expander IC-- PCF8575. It has two PCF8575. According to the datasheet the address of PCF is 0x0100A2A1A0R/W. In the board for first PCF which i am interested, the A2 is ground, A1 is ground and A0 is connected to Vcc. I want to read from PCF, so i used R/W=1. Thus my address is 0x43.
The board is powered with two supply, 12V and 5V. But when checked the SDA and SCL voltage using a digital multimeter, i found they are at 3.3V. So i didn’t go for a level shifter.

I will try with pull up resistors and tell the results.

from tech specs:

22 19 SCL Serial clock line. Connect to VCC through a pullup resistor
23 20 SDA Serial data line. Connect to VCC through a pullup resistor.

should work

Hi everyone,

today i got it. i have read two bytes of data from my slave device through i2c interface.
the problem was in pull up . I have pulled up the SDA and SCL line to Vdd in my slave board.
i connected my board to P9_19 and P9_20 of BBB and i2cdetect command realized the slave device and it showed the slave address.

Thank you everyone who helped to solve i2c interfacing problem in my BBB.