i2c error recovery

The official pyboard running MicroPython.
This is the reference design and main target board for MicroPython.
You can buy one at the store.
Target audience: Users with a pyboard.
Post Reply
shaoziyang
Posts: 303
Joined: Sun Apr 17, 2016 1:55 pm

i2c error recovery

Post by shaoziyang » Mon Jul 08, 2019 7:32 am

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?

User avatar
jimmo
Posts: 1440
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: i2c error recovery

Post by jimmo » Tue Jul 09, 2019 12:56 am

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.

shaoziyang
Posts: 303
Joined: Sun Apr 17, 2016 1:55 pm

Re: i2c error recovery

Post by shaoziyang » Tue Jul 09, 2019 2:31 am

re-init I2C has no effect,

Jonwalter
Posts: 11
Joined: Mon Apr 09, 2018 12:41 pm
Contact:

Re: i2c error recovery

Post by Jonwalter » Wed Sep 25, 2019 8:25 am

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.

microcarl
Posts: 1
Joined: Thu Mar 05, 2020 6:32 pm

Re: i2c error recovery

Post by microcarl » Thu Mar 05, 2020 6:40 pm

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
#********************************************************************#

Post Reply