Skip to content

I2C clock-stretching bug #4884

Open
Open

Description

@kaedros

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

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

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

  1. Configure a I2C de hardware by editing /boot/config.txt:
    dtparam=i2c=on,i2c_baudrate=100000
  2. 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)
  3. Sometimes we get the return without errors but many times we get errors as shown in the image:
    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.
    image

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

  1. pelwell commented on Feb 11, 2022

    @pelwell
    Contributor

    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 adding i2c-bcm2835.clk_tout_ms=0 to cmdline.txt change the behaviour?

  2. kaedros commented on Feb 11, 2022

    @kaedros
    Author

    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?

  3. 6by9 commented on Feb 11, 2022

    @6by9
    Contributor

    This may well be me misremembering, but did all the I2C controllers get the fix on BCM2711, or only the new ones? (i2c3-6)

  4. pelwell commented on Feb 11, 2022

    @pelwell
    Contributor

    That's not a detail I've picked up on, but it sounds like the kind of thing Broadcom would do.

  5. rewolff commented on Feb 12, 2022

    @rewolff

    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.

  6. 6by9 commented on Feb 14, 2022

    @6by9
    Contributor

    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.

  7. rewolff commented on Feb 14, 2022

    @rewolff

    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.

  8. kaedros commented on Feb 14, 2022

    @kaedros
    Author

    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 adding i2c-bcm2835.clk_tout_ms=0 to cmdline.txt change the behaviour?

    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.

  9. kaedros commented on Feb 14, 2022

    @kaedros
    Author

    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.

    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.

  10. kaedros commented on Feb 14, 2022

    @kaedros
    Author

    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.

    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.

  11. JamesH65 commented on Feb 14, 2022

    @JamesH65
    Contributor

    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.

  12. rewolff commented on Feb 14, 2022

    @rewolff

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

  13. kaedros commented on Feb 14, 2022

    @kaedros
    Author

    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.

    I understand. Can you share with everyone how this new driver can be muxed on the same pins?

  14. 6by9 commented on Feb 14, 2022

    @6by9
    Contributor

    I understand. Can you share with everyone how this new driver can be muxed on the same pins?

    https://github.com/raspberrypi/linux/blob/rpi-5.15.y/arch/arm/boot/dts/overlays/README#L1806

    Name:   i2c3
    Info:   Enable the i2c3 bus. BCM2711 only.
    Load:   dtoverlay=i2c3,<param>
    Params: pins_2_3                Use GPIOs 2 and 3
            pins_4_5                Use GPIOs 4 and 5 (default)
            baudrate                Set the baudrate for the interface (default
                                    "100000")
    

    https://github.com/raspberrypi/linux/blob/rpi-5.15.y/arch/arm/boot/dts/overlays/README#L1833

    Name:   i2c6
    Info:   Enable the i2c6 bus. BCM2711 only.
    Load:   dtoverlay=i2c6,<param>
    Params: pins_0_1                Use GPIOs 0 and 1
            pins_22_23              Use GPIOs 22 and 23 (default)
            baudrate                Set the baudrate for the interface (default
                                    "100000")
    

    So

    dtoverlay=i2c3,pins_2_3
    dtoverlay=i2c6,pins_0_1
    

    and then use /dev/i2c-3 and /dev/i2c-6.

  15. lategoodbye commented on Feb 14, 2022

    @lategoodbye
    Contributor

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

    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. 16 remaining items

  17. rewolff commented on Nov 5, 2023

    @rewolff

    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.

  18. TomasRa1 commented on Nov 6, 2023

    @TomasRa1

    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:

    # import the library (replace "import smbus" by:)
    from adafruit_extended_bus import ExtendedI2C as I2C
    
    # inside py-code in init of i2c replace "self.bus = smbus.SMBus(bus=1)" by:
    self.bus = I2C(4)
    
    # and then I can send (under "def write(self, address, values, isFatal = False):" data to new i2c bus by this way:
    self.bus.write_i2c_block_data(self.address, address, values)
    

    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!

  19. rewolff commented on Nov 6, 2023

    @rewolff

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

  20. troxel commented on Nov 13, 2023

    @troxel

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

  21. Ircama commented on Nov 13, 2023

    @Ircama

    "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

    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.

  22. TomasRa1 commented on Nov 19, 2023

    @TomasRa1

    "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

    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.

    Could I ask you please to try release GPIO 2,3 before you load dtoverlay?

    1. Try to comment with # all other dtparam commands in /boot/config.txt, connected to i2c (contents i2c). Check in /boot/config.txt if there is active any other i2c command, if you find comment it with #. Save config.txt, reboot and try communicate via SOFTWARE I2C. (Regarding i2c it must remains active only: dtoverlay=i2c-gpio,i2c_gpio_sda=2,i2c_gpio_scl=3,i2c_gpio_delay_us=2,bus=3)
      If i2c communication still not functions sucessfully:
    2. Try to add on the beginning of /boot/config.txt (somewhere upper of dtoverlay=i2c-gpio,i2c_gpio_sda=2,i2c_gpio_scl=3,i2c_gpio_delay_us=2,bus=3):
    # Select Alt2 (releasing) for GPIO pins 2 to 3
    gpio=2-3=a2
    

    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!

  23. Ircama commented on Nov 20, 2023

    @Ircama

    Please, write here result of your tests, because it's just only my theory, unproven in practice.

    I did all the tests again (Raspberry Pi 4 Model B Rev 1.1 interfacing MH-Tiny ATTINY88).

    I also added gpio=2-3=a2 before the overlay.

    Notice that the /dev/i2c-1 device driver is missing, as not configured:

    pi@raspberrypi:~ $ ls -l /dev/i2c-*
    crw-rw---- 1 root i2c 89, 20 Nov 20 20:47 /dev/i2c-20
    crw-rw---- 1 root i2c 89, 21 Nov 20 20:47 /dev/i2c-21
    crw-rw---- 1 root i2c 89,  3 Nov 20 20:47 /dev/i2c-3
    

    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:

    dtoverlay=i2c-gpio,i2c_gpio_sda=2,i2c_gpio_scl=23,i2c_gpio_delay_us=1,bus=3
    
  24. wschopohl commented on Nov 21, 2023

    @wschopohl

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

  25. troxel commented on Nov 21, 2023

    @troxel
  26. wschopohl commented on Nov 21, 2023

    @wschopohl
  27. TomasRa1 commented on Dec 29, 2023

    @TomasRa1

    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:
    Software_i2c
    It is also still working under very hard RF interferencies. With SMBUS this condition did not work at all:
    Software_i2c_RF_noise
    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

  28. rewolff commented on Dec 29, 2023

    @rewolff

    Thanks for your feedback. :-)

  29. shashwatjnawali-amd commented on Mar 19, 2024

    @shashwatjnawali-amd

    Do we know if RPi5 still has this issue?

  30. pelwell commented on Mar 19, 2024

    @pelwell
    Contributor

    Pi 5 uses RP1 which has a completely different I2C design from Synopsys that supports clock stretching correctly.

  31. added a commit that references this issue on Jul 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions