Repository navigation
Description
Describe the bug
I'm having intermittent problems with the RPi4 when it performs I2C reads on devices (like UCD9081 texas) that use clock stretch.
This apparently affects the Raspberry Pi and was documented in a lot of detail, back in 2013 by someone at Advamation. Here is that post.

Also have an open issue #3064 for RPi3 which apparently has not been resolved. Everything indicates that the SoCs (BCM2835, BCM2836 e BCM2837) do not have hardware support for clock stretch, but some interventions were made to stabilize the software i2c.
Although I found evidence on the datasheet BCM2711 on the chapter 3 that it had been fixed on the hardware RPi4, this is apparently not the case as it was a RPi4 I was getting the problem on.

By setting a baud rate of 10,000 the readings are more stable but still occur in smaller amounts.
dtparam=i2c_baudrate=10000
An alternative solution would be to use the software I2C dtoverlay=i2c-gpio,bus=3, however with the UCD9081 device this is not an option as it implements a maximum time for a byte transition and is something that cannot be ensure with software I2C that it can be interrupted for brief moments to process other interruptions.
Steps to reproduce the behaviour
- Configure a I2C de hardware by editing /boot/config.txt:
dtparam=i2c=on,i2c_baudrate=100000 - Send an I2C request using python:
python import smbus import time bus = smbus.SMBus(1) time.sleep(1) read = bus.read_i2c_block_data(0x65, 0x0, 16) - Sometimes we get the return without errors but many times we get errors as shown in the image:

We can see that there was a stretch and soon after the RPi4 did not guarantee the adequate clock width, resulting in a chain error.
In the image below we can see the correct communication where the clock at the same moment is with its correct width, probably because there was no stretch request by the device.

Device (s)
Raspberry Pi 4 Mod. B
System
OS: Raspberry Pi reference 2021-10-30 Generated using pi-gen, https://github.com/RPi-Distro/pi-gen, 88b21fc27e128ea6b330777aca68e0061ebf4fe, stage4
Firmware: Jan 20 2022 13:56:48 Copyright (c) 2012 Broadcom version bd88f66f8952d34e4e0613a85c7a6d3da49e13e2
Kernel: Linux JigaBrise 5.10.92-v7l+ #1514 SMP Mon Jan 17 17:38:03 GMT 2022 armv7l GNU/Linux
Soc: BCM2711 b03112
Logs
No response
Additional context
No response
Activity
pelwell commentedon Feb 11, 2022
Your "Steps to reproduce the behaviour" does not include the step "find some hardware that uses I2C clock stretching. We're not about to go and spend significant money on a UDC9081 EVM kit, so something else is required.
A recent kernel commit (available in 5.10.95 via
sudo rpi-update 39821d33) adds a module property that allows the timeout to be set. Does updating to that version and addingi2c-bcm2835.clk_tout_ms=0to cmdline.txt change the behaviour?kaedros commentedon Feb 11, 2022
This issue was very well described a few years ago #254 on the old SoC BCM2835, but the description is still exactly the same as the new SoC BCM2711 used on the RPi4 which claimed to have implemented the fix.
The suggestions made by @rewolff to fix through the driver would be a great start. Although not eradicating at least it would make possible the use of hardware i2c by slaves that use stretch clock.
The recommended solution for devices that definitely don't work with I2C hardware is to use the i2c-gpio overlay, which bitbashs the pins. But for many devices like the Texas UCD9081 this is not possible because it implements transition timeout and we know that with bitbang there can be big stops depending on what the MCU needs to process.
People we are talking about a simple I2C and the issue is very clear.
@rewolff are you test de BCM2711?
6by9 commentedon Feb 11, 2022
This may well be me misremembering, but did all the I2C controllers get the fix on BCM2711, or only the new ones? (i2c3-6)
pelwell commentedon Feb 11, 2022
That's not a detail I've picked up on, but it sounds like the kind of thing Broadcom would do.
rewolff commentedon Feb 12, 2022
Guys, If you need testing-hardware, let me know and I'll ship you a module. You're not having to spend a cent on that. I'll sponsor "quick testing by those who can do that". Just get in contact and get me an addres. Or find my shop, pick out a module (that doesn't name a chip), and at the checkout say: "bank payment" and include the remark that I promised you a free one here.
I'm a bit swamped to find time to test this. Sorry.
6by9 commentedon Feb 14, 2022
I've just checked with @P33M as the person with probably the most knowledge about this (hopefully he'll correct me if I report this wrong).
Recollection is that only the new controllers are fixed in case it broke something else. In each case there is a pin muxing option to map a new controller onto the pins of BSC0 or BSC1. (BSC6 in the case of GPIOs 0&1 alt5, or BSC3 for GPIOs 2&3 alt5).
It's going on memory, but clock stretching is only supported in the address ACK phase, not on all bytes, and the stretch has to be for at least 1/2 an I2C clock cycle.
rewolff commentedon Feb 14, 2022
OK then.
For my software-implemented I2C-device (with hardware clock stretching in the hardware, allowing the software any amount of time to prepare the data) that's still useless.
My protocol specifies that after the address phase, you specify the register number and at that moment, I'll have to go out and gather the data that needs to be sent. I have some "abstract" registers that have 8 bits that come from different places. That might take a bit more than half an i2c clock cycle.
So: BCM i2C is still not reasonably compliant with the protocol, I'll continue to recommend using SPI then.
kaedros commentedon Feb 14, 2022
Thanks for the contribution, but as I tried to make it clear any chip that uses stretch and respects the I2C protocol must present the bug, however I believe it is always important to describe and specify exactly how to reproduce and that includes citing the hardware used.
As for the kernel update indicated where there were changes regarding the stretch timeout, which is clearly not the problem as the stretch is not happening for a very long time, it is actually quite short. Anyway I followed your suggestions and as I predicted unfortunately the bug is still present.
kaedros commentedon Feb 14, 2022
Thank you for your willingness! I am available to test and I have everything needed (hardware and knowledge) to run any test.
I've already invested a lot of time in this problem and I have no doubt that there is a serious bug in the system, whether in the BCM2711 or drive.
I'm taking a close look at the drive to rule out errors in this one or even perform a workaround as you initially suggested.
It would be very opportune to have someone responsible at the Raspberry Foundation to accompany and assist us.
kaedros commentedon Feb 14, 2022
What would this new controller be? Wouldn't it be the BCM2711 you're quoting? Do you have more than one version of BCM2711? How do I know if mine is new?
It would be extremely important to confirm this information, this problem has been going on for many years.
If in fact it was implemented only in the address phase, stretch is not fully implemented and I understand that there is not much use in such an implementation.
Clock stretching pauses a transaction by holding the SCL line LOW after acknowloedgment. The transaction cannot continue until the line is released HIGH again.
On the byte level, a device may be able to receive bytes of data at a fast rate, but needs more time to store a received byte or prepare another byte to be transmitted. Targets can then hold the SCL line LOW after reception and acknowledgment of a byte to force the controller into a wait state until the target is ready for the next byte transfer in a type of handshake procedure.
JamesH65 commentedon Feb 14, 2022
AIUI, these are new I2C controllers on the 2711, above what is present on earlier SoC's. The older controllers with the faults are still there, but the additional ones have the fixes as specified. The older ones were not fixed, as there was a worry that the fix may introduce other bugs, and leaving things worse. You can mux the new controllers on to the same pins as the old ones.
rewolff commentedon Feb 14, 2022
@kaedros it is not "after ack" it is inside the bit-cycle meant for "ack" that the clock stretching usually happens. The slave can clock stretch to "buy time" to decide weather or not to ACK the byte.....
This made me think. If someone misunderstood the "only in the ack" to mean "only in addresss-ack" then maybe it's usable: There is an ACK after every byte, and that's where my hardware needs the time to process the data.
My offer for hardware still stands. Just go to https://bitwizard.nl/shop and chose a module that you might like (e.g. dual relay https://bitwizard.nl/shop/Relay ) and select bank transfer. Then add the remark who you are on github and that I promised you free hardware.
kaedros commentedon Feb 14, 2022
I understand. Can you share with everyone how this new driver can be muxed on the same pins?
6by9 commentedon Feb 14, 2022
https://github.com/raspberrypi/linux/blob/rpi-5.15.y/arch/arm/boot/dts/overlays/README#L1806
https://github.com/raspberrypi/linux/blob/rpi-5.15.y/arch/arm/boot/dts/overlays/README#L1833
So
and then use /dev/i2c-3 and /dev/i2c-6.
lategoodbye commentedon Feb 14, 2022
Thanks for clarification. So the bcm2711 dtsi file needs to be fixed:
https://github.com/torvalds/linux/blob/d567f5db412ed52de0b3b3efca4a451263de6108/arch/arm/boot/dts/bcm2711.dtsi#L1048
But this won't have any affect on this issue.
16 remaining items
rewolff commentedon Nov 5, 2023
You cannot change the timing of the SDA and SCL signals coming from RPI. So your only option would be to use software I2C. I'm told there is such a feature.
TomasRa1 commentedon Nov 6, 2023
Thank you for new informations.
In my Python SW-case is used SMBus for i2c communication, I do not know if SMbus is optimal for reliable for communication (with QN8066) without aborts. My osciloscope measurements are done with this communication via to use SMBus.
Regarding mentioned "software I2C" which I found on https://learn.adafruit.com/raspberry-pi-i2c-clock-stretching-fixes/software-i2c how to set alternative i2c way:
/boot/config.txt add: dtoverlay=i2c-gpio,i2c_gpio_sda=2,i2c_gpio_scl=3,i2c_gpio_delay_us=4,bus=4
Then I would see in $ ls /dev/i2c* : /dev/i2c-1 /dev/i2c-4
Then I must install to Rpi3B+: pip3 install adafruit-extended-bus
Then I must add to py code:
Could you please check my ideas and if you found smth. incorrect or incomprehensible, please help me by your comment or question. Thank you very much!
rewolff commentedon Nov 6, 2023
I really don't have much experience in "software I2C". I've never used it.
I really don't have much experience in using I2C from Python. I've never done I2C in python. (I've written a few lines in python, I've done I2C, but just not together).
"looks good to me".
troxel commentedon Nov 13, 2023
"The only way I have found to reliably read data from the BNO055 is to use the software implementation of the i2c protocol, that is, adding the line dtoverlay=i2c-gpio,i2c_gpio_sda=2,i2c_gpio_scl=3,i2c_gpio_delay_us=2,bus=3 to the /boot/config.txt" - rillbert
This comment saved me countless hours. Getting unstable results with Rpi4 on bookworm trying to communicate with a Bno085. The chip kept random resetting itself after a few hundred cycles. Now communicating on /dev/i2c-3 without problems. Thank you.
Ircama commentedon Nov 13, 2023
Even trying to disable hw I2C on its bus 1 in /boot/config.txt (
dtparam=i2c1=off,dtparam=i2c_arm=off), using software I2C (i2c-gpio) with another bus (e.g, bus=3) on the same pins of the hw I2C (2/SDA and 3/SCL), I get periodic bus errors which I haven't been able to figure out how they are generated and how to remove them. No problem when using other GPIO pins.TomasRa1 commentedon Nov 19, 2023
Could I ask you please to try release GPIO 2,3 before you load dtoverlay?
If i2c communication still not functions sucessfully:
save /boot/config.txt than reboot and test i2c communication.
Please, write here result of your tests, because it's just only my theory, unproven in practice.
Thank you very much!
Ircama commentedon Nov 20, 2023
I did all the tests again (Raspberry Pi 4 Model B Rev 1.1 interfacing MH-Tiny ATTINY88).
I also added
gpio=2-3=a2before the overlay.Notice that the /dev/i2c-1 device driver is missing, as not configured:
The load&stress test (loop of different I2C synchronous requests/responses) fails rather quickly, while no error is found when using other I2C pins.
In addition, I verified that the conflict is on SCL (GPIO3, pin 5).
So, this works:
wschopohl commentedon Nov 21, 2023
Just wanted to add: If you are using i2c-gpio (software), make sure the corresponding module is loaded otherwise it won't show under /dev/i2c-
modprobe i2c-gpiotroxel commentedon Nov 21, 2023
wschopohl commentedon Nov 21, 2023
TomasRa1 commentedon Dec 29, 2023
I would like to send many thanks here to Mr. Rewolff for help with solution unreliable i2c communication between RPi 3B+ and QN8066 chip. When we replace "SMBUS" to "SOFTWARE i2c" (thanks the best and also so friendly programmer Nick Anderson) it is much more reliable now.


There is sometimes problem when SDA edge is on the SCL edge in almost the same time, but SDA-SCL timing of all other pulses are correct now:
It is also still working under very hard RF interferencies. With SMBUS this condition did not work at all:
I'm so glad that Mr. Wolff is so knowledgeable and so willing to help!
I tried to find a Linux ALSA expert for help with configuration of ALSAEQUAL between FPP (FalconPlayer) and HiFiBerry DAC. I have only found "ALSA experts" who only look like that on the outside. In fact they were not willing or able to give any advice. I respect Mr. Rewolff all the more and I don't know how I can repay him.
Best regards and many thanks! Tomas
rewolff commentedon Dec 29, 2023
Thanks for your feedback. :-)
shashwatjnawali-amd commentedon Mar 19, 2024
Do we know if RPi5 still has this issue?
pelwell commentedon Mar 19, 2024
Pi 5 uses RP1 which has a completely different I2C design from Synopsys that supports clock stretching correctly.
fixed LS word bit 7 issue