Page 1 of 1

i2c error recovery

Posted: Mon Jul 08, 2019 7:32 am
by shaoziyang
I using a i2c sensor in pyboard, and occasionally hardware i2c will timeout. When this happens, i2c will not read sensor anymore, and it can't recover automatic unless reset. If I using software i2c, it will recovery.

Does anyone konw how to recovery hardware i2c when it occur errors?

Re: i2c error recovery

Posted: Tue Jul 09, 2019 12:56 am
by jimmo
Out of curiosity, what happens if you re-initialise the I2C? (e.g. by calling .init())

I notice that in the STM32 port that doing this will explicitly reset the I2C peripheral.

Re: i2c error recovery

Posted: Tue Jul 09, 2019 2:31 am
by shaoziyang
re-init I2C has no effect,

Re: i2c error recovery

Posted: Wed Sep 25, 2019 8:25 am
by Jonwalter
My general startup/recovery procedure is to set the I2C as I/O lines and send out clock pulses while checking the data line. I wait for ten consecutive ones with no intervening zeroes before I switch (back) to the MSSP.

Re: i2c error recovery

Posted: Thu Mar 05, 2020 6:40 pm
by microcarl
I use the foloowing for I2C WRITE() exception handling. Maybe you can modify the basics to help with your issue...

#********************************************************************#
def WriteData(address, data):

n = 0
while n < 3:
try:
i2c.writeto(address, ' ')
time.sleep(0.05) # Wait for slave to process dummy command
except Exception as error:
pass
print('While writing:', error)
try:
result = bytearray(1)
i2c.readfrom_into(address, result)
except Exception as error:
print('While reading:', error)
pass
n += 1
continue
else:
i2c.writeto(address, data)
time.sleep(0.05) # Wait for slave to process valid command
break
#********************************************************************#