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?
i2c error recovery
Re: i2c error recovery
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.
I notice that in the STM32 port that doing this will explicitly reset the I2C peripheral.
-
- Posts: 363
- Joined: Sun Apr 17, 2016 1:55 pm
Re: i2c error recovery
re-init I2C has no effect,
Re: i2c error recovery
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
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
#********************************************************************#
#********************************************************************#
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
#********************************************************************#