Skip to content

Conversation

Ultrawipf
Copy link
Contributor

@Ultrawipf Ultrawipf commented Sep 16, 2025

Regarding issue #27 after the initial reset the device may fail to respond immediately to the next access if there is no delay to allow the chip to start back up.
This raises an uncaught exception instead of properly waiting.

This PR retries the first access after the reset by catching the OSError after the reset until it succeeds to let the chip start back up properly.

If the device is actually not connected the initial poll will still fail properly and raise an exception.

@ladyada
Copy link
Member

ladyada commented Sep 16, 2025

this is ok but a timeout would be preferrable

@Ultrawipf
Copy link
Contributor Author

Ultrawipf commented Sep 16, 2025

It could raise an exception after failing a few times to ensure it can not get stuck and wait a bit between requests.
A constant delay of 1ms instead of the waiting loop also works on my device but a dynamic waiting loop may be more reliable.

Perhaps something like this to wait after each attempt and limit the iterations:

        attempts = 10
        while attempts:
            attempts -= 1
            try:
                if not self._read_u8(_MMA8451_REG_CTRL_REG2) & 0x40 > 0:
                    # Continue if chip is ready
                    break
            # Ignore OSError. After reset the device may fail to respond
            except OSError as e:
                time.sleep(0.001)
                if not attempts:
                    raise e

Copy link
Contributor

@FoamyGuy FoamyGuy left a comment

Choose a reason for hiding this comment

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

Thank you!

This looks good to me now. Latest version added timeout after a few failed attempts at reading the register.

I don't have the breakout to test on, it's from a while ago and is out of stock.

@FoamyGuy FoamyGuy merged commit 707276b into adafruit:main Sep 17, 2025
1 check passed
@FoamyGuy FoamyGuy mentioned this pull request Sep 17, 2025
adafruit-adabot pushed a commit to adafruit/Adafruit_CircuitPython_Bundle that referenced this pull request Sep 18, 2025
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.

3 participants