10000 i2ctarget: Add deinit() to I2CTargetRequest; remove close() by dhalbert · Pull Request #10366 · adafruit/circuitpython · GitHub
[go: up one dir, main page]

Skip to content

i2ctarget: Add deinit() to I2CTargetRequest; remove close() #10366

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 10000 to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 21, 2025

Conversation

dhalbert
Copy link
Collaborator

Removes visible I2CTargetRequest.close() method. Adds I2CTargetRequest.deinit(). Does close() and deinit() in I2CTargetRequest.__exit__(). Makes I2CTargetRequest have a finalizer: __del__() calls deinit().

I tried testing with the more extensive example in the I2CTarget documentation, which tries to write and then read a value to an I2C register. It did not work, but I could not get that example to work with 8.2.9 or 9.2.3 either.

@Neradoc if you or someone else would like to test this with some previously-working code, I would be grateful.

@dhalbert dhalbert force-pushed the i2ctargetrequest-deinit branch from 17e24b8 to 7d3dd30 Compare May 21, 2025 17:00
@Neradoc
Copy link
Neradoc commented May 21, 2025

The following test code triggers the issue in 9.2.7 and works as expected with this PR.

Controller code (running on a tiny2350)

import board
import random
import time
from adafruit_bus_device.i2c_device import I2CDevice

i2c = board.STEMMA_I2C()
device = I2CDevice(i2c, 0x40)
buffer = bytearray(1)

while True:
    try:
        with device:
            device.readinto(buffer)
            print(f"device responded with #{buffer[0]:02X}")
        time.sleep(1)

        with device:
            buffer[0] = random.randint(0, 255)
            device.write(buffer)
            print(f"wrote to device: #{buffer[0]:02X}")
        time.sleep(1)

    except OSError: # target resetting or in error
        time.sleep(1)

Target code (pro micro 2040) - that's the code being tested.

import board
from i2ctarget import I2CTarget

memory = 0x55
with I2CTarget(board.SCL, board.SDA, (0x40,)) as device:
    while True:
        i2c_target_request = device.request()
        if not i2c_target_request:
            continue

        with i2c_target_request:
            if i2c_target_request.is_read:
                print(f"read request: #{memory:02X}")
                buffer = memory.to_bytes(1)
                i2c_target_request.write(buffer)
            else:
                data = i2c_target_request.read(1)
                print(f"write request: #{data[0]:02X}")
                memory = data[0]

Could we have the call to common_hal_i2ctarget_i2c_target_close in deinit ? It is possible (though ill advised) to not use the context manager and call deinit manually, in which case I believe close wouldn't be called.
And then we could remove the custom __exit__.

@dhalbert
Copy link
Collaborator Author

Could we have the call to common_hal_i2ctarget_i2c_target_close in deinit ? It is possible (though ill advised) to not use the context manager and call deinit manually, in which case I believe close wouldn't be called.
And then we could remove the custom __exit__.

I considered that, but wasn't sure if there was another use case. But I think not. I'll do that, yes, thanks.

@dhalbert dhalbert force-pushed the i2ctargetrequest-deinit branch from 7306e5e to 8da8e31 Compare May 21, 2025 20:46
@dhalbert dhalbert requested a review from Neradoc May 21, 2025 21:05
@dhalbert dhalbert marked this pull request as ready for review May 21, 2025 21:06
Copy link
@Neradoc Neradoc left a comment

Choose a reason for hiding this comment

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

Runs fine, looks good.

@dhalbert
Copy link
Collaborator Author

I will open a separate issue to fix the example.

@dhalbert dhalbert merged commit c6e4d72 into adafruit:main May 21, 2025
197 checks passed
@dhalbert dhalbert deleted the i2ctargetrequest-deinit branch May 21, 2025 22:05
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.

i2ctarget error with missing deinit for request
2 participants
0