User avatar
Grumpy Mike
Posts: 936
Joined: Sat Sep 10, 2011 7:49 pm
Location: Manchester (England England)
Contact: Website

I2C not reliable on a Pi3

Mon Mar 20, 2017 6:04 pm

I have an I2C interface to a DJ Hero turntable. I have written an application in Python and run it on a Pi2 and all is well. However when I run the same code and same hardware on a Pi3 it works for a time and then gives an error

Code: Select all

    bus.write_byte(0x52,0)
IOError: [Errno 5] Input/output error
I have used the smbus drivers and the function it is failing on is simply

Code: Select all

def readDJ():
    bus.write_byte(0x52,0)
    dj = [(bus.read_byte(0x52)) for i in range(6)]
    return dj
There are no other GPIO pins being used or set to anything.

As I said it works fine on a Pi2, I can use it for hours but on a Pi3 it fails after about a minute or less of using it.
Is there a fundamental difference between the two systems?

User avatar
joan
Posts: 14993
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: I2C not reliable on a Pi3

Mon Mar 20, 2017 9:26 pm

No difference that I know of.

Usually I2C I/O errors on the Pi are down to loose wires.

A difference between the Pi2 and Pi3 is the power requirements. I suppose a marginal power supply might exhibit problems with I2C as well.

User avatar
Grumpy Mike
Posts: 936
Joined: Sat Sep 10, 2011 7:49 pm
Location: Manchester (England England)
Contact: Website

Re: I2C not reliable on a Pi3

Mon Mar 20, 2017 9:53 pm

Thanks for that however:-
1) New Power supply 2.5A from Pimoroni, voltage checked at 5V pin and 3V3 pin on both P2 and P3 and it is the same. Also tried an Apple 2.5A psu from my iPad and the Pi3 still falls over.

2) The wires soldered onto a header and plugged into the GPIO socket, very solid.

lambcutlet
Posts: 14
Joined: Mon Aug 18, 2014 7:56 pm

Re: I2C not reliable on a Pi3

Thu Mar 23, 2017 8:09 pm

Mike, i have to agree with you something is different with the I2C on a rpi3 and it changed recently.
i have a rpi3 reading a PIC that had Packet Error Correction implemented SMBus style.
The PIC would calculate the PEC value between byte transmissions everything worked fine until a software update at the beginning of February. To restore function i had to remove the PEC + speed up the I2C routine on the PIC. I wasted a weekend resolving this.
I believe its a timing issue, as in the timings have become more critical. I am not familiar enough with linux's low level drivers to confirm this.
It is possible your turntable has a timing abnormality that grinds the rpi3 to a halt.

jamesh
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 26842
Joined: Sat Jul 30, 2011 7:41 pm

Re: I2C not reliable on a Pi3

Thu Mar 23, 2017 8:20 pm

If you have a way of replicating the issue, you need to file a bug report on the Raspberry Pi github. If you can do git bisect and determine the exact kernel update that causes the problem that would be even better.
Principal Software Engineer at Raspberry Pi (Trading) Ltd.
Contrary to popular belief, humorous signatures are allowed.
I've been saying "Mucho" to my Spanish friend a lot more lately. It means a lot to him.

notro
Posts: 696
Joined: Tue Oct 16, 2012 6:21 pm
Location: Drammen, Norway

Re: I2C not reliable on a Pi3

Thu Mar 23, 2017 8:39 pm

Phil recently added a commit to fix the i2c bus clock on 4.9:
clk-bcm2835: Read max core clock from firmware
https://github.com/raspberrypi/linux/co ... 6f53d697f6

The change was present in rpi-update three commits ago:
https://github.com/raspberrypi/firmware ... 00c9ac5576

User avatar
Grumpy Mike
Posts: 936
Joined: Sat Sep 10, 2011 7:49 pm
Location: Manchester (England England)
Contact: Website

Re: I2C not reliable on a Pi3

Fri Mar 24, 2017 7:13 pm

The change was present in rpi-update three commits ago:
Thanks.

So I did a rpi-update but sadly it did not fix the problem. It did however change the error message, now I get:-

Code: Select all

[Errno 121] Remote I/O Error

notro
Posts: 696
Joined: Tue Oct 16, 2012 6:21 pm
Location: Drammen, Norway

Re: I2C not reliable on a Pi3

Fri Mar 24, 2017 9:15 pm

EREMOTEIO=121 is returned by the i2c-bcm2835 driver when the ERR bit is set.
The bit is described like this in the datasheet:
ERR ACK Error
0 = No errors detected. 1 = Slave has not
acknowledged its address. Cleared by writing 1
to the field.

The driver has a debug option that might aid in debugging, described in this commit:
https://github.com/raspberrypi/linux/co ... 3205fd9100

Driver source: https://github.com/raspberrypi/linux/bl ... -bcm2835.c

In Linux 4.9 we switched from i2c-bcm2708 to i2c-bcm2835 kernel driver.
You can switch back to the old driver with:
/boot/config.txt
dtoverlay=i2c-bcm2708

Source: https://github.com/raspberrypi/linux/bl ... verlay.dts

You can determine which driver is loaded by using: lsmod

User avatar
Grumpy Mike
Posts: 936
Joined: Sat Sep 10, 2011 7:49 pm
Location: Manchester (England England)
Contact: Website

Re: I2C not reliable on a Pi3

Sat Mar 25, 2017 9:27 pm

Thanks a lot of that.
You can switch back to the old driver with:
/boot/config.txt
dtoverlay=i2c-bcm2708
Sadly the only thing that was achieved is that the error number went back to being 5.

As this only happens after a minute or so there must be thousands of correct accesses so to me it sounds like either the timing is right on the edge, or there is something maybe in the threading that is causing this. It is definitely a new thing.

notro
Posts: 696
Joined: Tue Oct 16, 2012 6:21 pm
Location: Drammen, Norway

Re: I2C not reliable on a Pi3

Sun Mar 26, 2017 1:14 am

Looking at i2c-bcm2708, it returns EIO=5 on both the clock stretch error and the ACK error.
So errno 121 and 5 is probaly the same ACK error.

User avatar
Grumpy Mike
Posts: 936
Joined: Sat Sep 10, 2011 7:49 pm
Location: Manchester (England England)
Contact: Website

Re: I2C not reliable on a Pi3

Tue Apr 04, 2017 10:47 pm

Just to wrap things up.
I put a sticking plaster over the problem to trap the error and re initialise the I2C when ever this error occurred, using a try: ... except.. structure. The code then did not crash.

Now today upgrading to NOBOS V 2.3.0 fixed the problem, and one created I think by applying the to the rpi-update to the older OS version, as a result of this thread.

See this thread for the gory details of that if you are curious:-
viewtopic.php?f=32&t=179537&p=1143165#p1143165

Thanks to all for the assistance.

Schorschi
Posts: 239
Joined: Thu Nov 22, 2012 9:38 pm

Re: I2C not reliable on a Pi3

Sat Dec 30, 2017 9:59 pm

Can you post the code you used to reset the i2c bus via python? I found a odd but infrequent issue where the i2c bus slows down or hangs, and once this happens, the only fix appears to be a reboot of the Pi. I have tried dropping and reloading the various drivers, but this does not seem to work. I am thinking that if I can get the i2c bus to reset, this might give me a work-around to slow down issue.

User avatar
Grumpy Mike
Posts: 936
Joined: Sat Sep 10, 2011 7:49 pm
Location: Manchester (England England)
Contact: Website

Re: I2C not reliable on a Pi3

Sun Dec 31, 2017 6:14 pm

I just used this:-

Code: Select all

def init():
    global bus
    if rpi.RPI_REVISION == 1:
      i2c_bus = 0
    else :
      i2c_bus = 1
    bus = SMBus(i2c_bus)
    bus.write_byte_data(0x52,0xF0,0x55)

def readDJ(): # Currently an issue with the I2C drivers or something that throws up an occasional error - this is the sticking plaster
    try:
       bus.write_byte(0x52,0)
    except:
       print"bus restart" 
       init()
       bus.write_byte(0x52,0)
    dj = [(bus.read_byte(0x52)) for i in range(6)]
    return dj

Return to “Interfacing (DSI, CSI, I2C, etc.)”