-
Couldn't load subscription status.
- Fork 77
Add timeouts to bus devices. #51
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
There was a problem hiding this 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() |
There was a problem hiding this comment.
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): |
There was a problem hiding this comment.
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?
adafruit_bus_device/i2c_device.py
Outdated
| 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") |
There was a problem hiding this comment.
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.
There was a problem hiding this 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.
adafruit_bus_device/i2c_device.py
Outdated
| """ | ||
|
|
||
| def __init__(self, i2c, device_address, probe=True, timeout=0.25): | ||
| def __init__(self, i2c, device_address, probe=True, timeout=250): |
There was a problem hiding this comment.
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.
|
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. |
|
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 |
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
Noneas the timeout. if the time runs out an exception of typeexceptionis thrown.this code was tested on hardware similar to the feather nrf52840