Skip to content

Conversation

@TG-Techie
Copy link

@TG-Techie TG-Techie commented May 3, 2020

I noticed that on some devices (so far only nrf52840s) after many reboots all hardware locking hangs indefinitely. This modification adds timeouts to the bus devices (default of .25 seconds to be generous) where it will only try to lock for up to the specified amount of time. the timeout can be deactivated by passing None as the timeout. if the time runs out an exception of type exception is thrown.
this code was tested on hardware similar to the feather nrf52840

Copy link
Collaborator

@theacodes theacodes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall looks pretty good, just some small comments.

pass

if self.timeout is not None:
lock_start_time = time.monotonic()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: use time.monotonic_ns() throughout as it'll avoid issues with floating point precision when the program runs for a long time.

"""

def __init__(self, i2c, device_address, probe=True):
def __init__(self, i2c, device_address, probe=True, timeout=0.25):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add the timeout parameter to the docstring above here?

lock_start_time = time.monotonic()
while not self.i2c.try_lock():
if self.timeout > (time.monotonic() - lock_start_time):
raise Exception("'I2CDevice' lock timed out")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't raise bare Exceptions. It seems other Adafruit libraries use RuntimeError here. source.

Copy link
Contributor

@dhalbert dhalbert left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure what problem this is trying to solve. If you are seeing hangs on I2C devices, we have an issue open to solve that problem at a lower level. See adafruit/circuitpython#2253. I have plans to fix this at some point.

"""

def __init__(self, i2c, device_address, probe=True, timeout=0.25):
def __init__(self, i2c, device_address, probe=True, timeout=250):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please go back to floating point seconds. We are trying to be uniform and always use float seconds.

@dhalbert
Copy link
Contributor

dhalbert commented Jun 7, 2020

The I2C bus hangs mentioned in that issue cannot be solved with timeouts in Python; timeouts have to be added in the C code at a low level where the bus status is checked.

@kattni
Copy link
Contributor

kattni commented Aug 17, 2020

This issue needs to be fixed in the CircuitPython core, not the bus_device library. I'm closing this in favor of adafruit/circuitpython#2253

@kattni kattni closed this Aug 17, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants