Best way to detect and recover from stuck i2c bus

Which system do you use? Android, Ubuntu, OOWOW or others?

Khadas vim3 Ubuntu

Which version of system do you use? Please provide the version of the system here:

uname -a output:

Linux Khadas 4.9.241 #2 SMP PREEMPT Fri Nov 18 03:18:35 UTC 2022 aarch64 aarch64 aarch64 GNU/Linux

Please describe your issue below:

I am using the i2c pins 22 and 23 and I am trying to make my python code more robust by detecting a stuck bus and then recovering from it. For recovery: I've seen suggestions to turn SCL into a gpio pin and manually bit banging 10 pulses. Someone also suggested its possible to enable kernel-internal i2c bus recovery.

I am currently using smbus for the i2c and wiringpi-python for GPIO pins. 

If I was doing it manually by bit banging the SCL, how would I unlock the SCL pin from i2c to bit bang during runtime? 

And is a kernel-internal i2c bus recovery possible or are there other options?

Also how would I detect if the bus is stuck? Ideally I could just read the state of the SDA line while it is idle and check if it is low or not but I can't since it's not a GPIO pin.

Thank you.

It used as I2C so you can’t do this.

We havent tested, but you can have a try.