Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wire: implement STOP sequence to recover I2C bus. #1663

Merged
merged 1 commit into from
Feb 28, 2022

Conversation

ABOSTM
Copy link
Contributor

@ABOSTM ABOSTM commented Feb 22, 2022

Summary
Wire: implement STOP sequence to recover I2C bus.

Useful in case of bus stuck after a reset for example
Fixes #1661

libraries/Wire/src/Wire.cpp Outdated Show resolved Hide resolved
@fpistm fpistm added this to In progress in STM32 core based on ST HAL via automation Feb 22, 2022
@fpistm fpistm added this to the 2.3.0 milestone Feb 22, 2022
STM32 core based on ST HAL automation moved this from In progress to Reviewer approved Feb 22, 2022
@ABOSTM
Copy link
Contributor Author

ABOSTM commented Feb 24, 2022

Reworked implementation:
Recover only when SDA is low.
Clear bus (CLK pulses) inspired from
https://www.nxp.com/docs/en/user-guide/UM10204.pdf
https://bits4device.wordpress.com/2017/07/28/i2c-bus-recovery/

Copy link
Member

@fpistm fpistm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@cparata
Copy link
Contributor

cparata commented Feb 24, 2022

Hi @ABOSTM ,
I tried the fix. It improves the performances, but sometimes I still get the bus stuck. My setup is with F401RE, IKS02A1 and X_NUCLEO_IKS02A1_ISM330DHCX_FIFO example. Putting a cycle with 20 loops and a delay of 10 us seems improving the stability on my side.
Best Regards,
Carlo

void TwoWire::recoverBus(void)
{
  pinMode(pinNametoDigitalPin(_i2c.sda), INPUT);

  if (digitalReadFast(_i2c.sda) == LOW) {
    pinMode(pinNametoDigitalPin(_i2c.scl), OUTPUT);

    for (int i = 0; i < 20; i++) {
      digitalWriteFast(_i2c.scl, LOW);
      delayMicroseconds(10);
      digitalWriteFast(_i2c.scl, HIGH);
      delayMicroseconds(10);
    }
    pinMode(pinNametoDigitalPin(_i2c.scl), INPUT);
  }
}

Source:
https://www.nxp.com/docs/en/user-guide/UM10204.pdf
https://bits4device.wordpress.com/2017/07/28/i2c-bus-recovery/

Useful in case of bus stuck after a reset for example
Fixes stm32duino#1661

Signed-off-by: Alexandre Bourdiol <alexandre.bourdiol@st.com>
@ABOSTM
Copy link
Contributor Author

ABOSTM commented Feb 25, 2022

Hi @cparata,
This is strange as I used the same example, ISM330DHCX_FIFO, but on a B-U585I-IOT02A.
And could not reproduce this issue after more than 100 reset.
But I trust you and I am fine with your proposal.

@fpistm fpistm merged commit 51fc604 into stm32duino:main Feb 28, 2022
STM32 core based on ST HAL automation moved this from Reviewer approved to Done Feb 28, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Development

Successfully merging this pull request may close these issues.

How to avoid I2C bus stuck after a board reset
4 participants