Skip to content

How to avoid I2C bus stuck after a board reset #1661

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

Closed
cparata opened this issue Feb 16, 2022 · 3 comments · Fixed by #1663
Closed

How to avoid I2C bus stuck after a board reset #1661

cparata opened this issue Feb 16, 2022 · 3 comments · Fixed by #1663
Assignees
Labels
enhancement New feature or request

Comments

@cparata
Copy link
Contributor

cparata commented Feb 16, 2022

I noticed that sometimes if you reset the board during an I2C transaction the I2C bus remains stuck.
A possible solution that I found is to send a bit-banged STOP sequence before the I2C begin. Maybe it would be better to include this procedure in the Wire begin implementation or in a dedicated Wire API to improve the stability of the library.
You can find below the bit-banged STOP sequence that normally I send to unlock the I2C bus before the I2C begin:

pinMode(SCL_PIN, OUTPUT);
pinMode(SDA_PIN, OUTPUT);

for (int i = 0; i<10; i++) {
  digitalWrite(SDA_PIN, LOW);
  delay(3);
  digitalWrite(SCL_PIN, HIGH);
  delay(3);
  digitalWrite(SDA_PIN, HIGH);
  delay(3);
  digitalWrite(SCL_PIN, LOW);
  delay(3);
}

pinMode(SDA_PIN, INPUT);
pinMode(SCL_PIN, INPUT);

Best Regards,
Carlo

@cparata cparata added the enhancement New feature or request label Feb 16, 2022
@ABOSTM
Copy link
Contributor

ABOSTM commented Feb 17, 2022

Hi Carlo, thanks for this sharing.
Any reason to send STOP 10 times in loop ?

@cparata
Copy link
Contributor Author

cparata commented Feb 17, 2022

Hi @ABOSTM ,
nothing in particular. I just try to send the "STOP" sequence 10 times to be sure that all the devices on the I2C bus are able to "see" it. Sometimes the devices take some time to be alive on the bus. But it is just an example. Maybe we can reduce the number of loops.
Best Regards,
Carlo

ABOSTM added a commit to ABOSTM/Arduino_Core_STM32 that referenced this issue Feb 22, 2022
Usefull in case of bus stuck after a reset for example
Fixes stm32duino#1661

Signed-off-by: Alexandre Bourdiol <[email protected]>
ABOSTM added a commit to ABOSTM/Arduino_Core_STM32 that referenced this issue Feb 22, 2022
Useful in case of bus stuck after a reset for example
Fixes stm32duino#1661

Signed-off-by: Alexandre Bourdiol <[email protected]>
ABOSTM added a commit to ABOSTM/Arduino_Core_STM32 that referenced this issue Feb 22, 2022
Useful in case of bus stuck after a reset for example
Fixes stm32duino#1661

Signed-off-by: Alexandre Bourdiol <[email protected]>
@cparata
Copy link
Contributor Author

cparata commented Feb 22, 2022

Hi all,
I found also this implementation on the web:

https://www.forward.com.au/pfod/ArduinoProgramming/I2C_ClearBus/I2C_ClearBus.ino.txt

The implementation is described in this article:

https://github-wiki-see.page/m/Koepel/How-to-use-the-Arduino-Wire-library/wiki/How-to-unlock-a-stuck-I2C-bus

They suggest to do 20 clock loops (> 16) to address also devices that manage 16 bits registers. Besides they do not advice to put high the SDA and SCL because I2C bus is open collector

ABOSTM added a commit to ABOSTM/Arduino_Core_STM32 that referenced this issue Feb 24, 2022
ABOSTM added a commit to ABOSTM/Arduino_Core_STM32 that referenced this issue Feb 25, 2022
fpistm pushed a commit that referenced this issue Feb 28, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants