Skip to content

Conversation

@FoamyGuy
Copy link
Contributor

Only SPI is implemented right now, I'll work on I2C next.

Adds new RegisterAccessor classes which RWBits and RWBit use so that they are agnostic to bus type.

I have successfully tested with a development version of the bmp5xx driver. I'll submit a PR over in that repo soon with the driver that relies on this new functionality.

Marking this draft until I2C is implemented, but wanted to get it opened now for any feedback.

Copy link
Member

@tannewt tannewt left a comment

Choose a reason for hiding this comment

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

Thanks for the draft PR. This is definitely the right direction.

@FoamyGuy
Copy link
Contributor Author

FoamyGuy commented Sep 30, 2025

I found that the OV5640 camera module has I2C interface with 2 byte register addresses. I was able to successfully test the current version from this PR branch with this code.

import board
import busio
import digitalio
from adafruit_bus_device.i2c_device import I2CDevice
from adafruit_register.register_accessor import I2CRegisterAccessor
from adafruit_register.register_bits import ROBits, RWBits

I2C_ADDRESS = 0x3C
REG_CHIP_ID_HIGH = 0x300A

class OV5640Tester():
    chip_id = ROBits(16, REG_CHIP_ID_HIGH, 0, register_width=2, lsb_first=False)

    def __init__(self, i2c):

        try:
            i2c_device = I2CDevice(i2c, I2C_ADDRESS)
            self.register_accessor = I2CRegisterAccessor(i2c_device, address_width=2, lsb_first=False)
        except ValueError:
            raise ValueError(f"No I2C device found.")


if __name__ == '__main__':
    print("construct bus")
    i2c = busio.I2C(board.GP5, board.GP4)
    print("construct camera")
    reset = digitalio.DigitalInOut(board.GP14)

    ov5640 = OV5640Tester(i2c)
    print(hex(ov5640.chip_id))

@FoamyGuy
Copy link
Contributor Author

Thank you. I got honed in on write_then_readinto() using the same buffer for both in/out in the existing implementation, overlooked that it can use different ones. That is quite convenient.

The latest commit removes the buffer concatenation and only uses _full_buffer() for i2c.write(). SPI write is split back into 2 writes now as well without concatenation.

I tested the latest version successfully with I2C & SPI on BMP5x & SPA06-003, and I2C / multi-byte address with OV5640. I submitted the latter as a new example to this repo as well.

Copy link
Member

@tannewt tannewt left a comment

Choose a reason for hiding this comment

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

Thanks for iterating on this! I'm glad multibyte is working too.

"""
RegisterAccessor class for I2C bus transport. Provides interface to read/write
registers over I2C

Copy link
Member

Choose a reason for hiding this comment

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

Same here, explain the protocol over I2C that this supports.

Copy link
Member

Choose a reason for hiding this comment

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

Also talk about the relationship between address and data.

…h comment. use slices instead loop. set end for i2c.write().
@FoamyGuy
Copy link
Contributor Author

FoamyGuy commented Oct 1, 2025

@tannewt All of those changes are made in the latest commit.

Copy link
Member

@tannewt tannewt left a comment

Choose a reason for hiding this comment

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

One more minor doc thing. Do you want to undraft this PR?

"""
RegisterAccessor class for I2C bus transport. Provides interface to read/write
registers over I2C

Copy link
Member

Choose a reason for hiding this comment

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

Also talk about the relationship between address and data.

@FoamyGuy FoamyGuy marked this pull request as ready for review October 1, 2025 19:11
@FoamyGuy
Copy link
Contributor Author

FoamyGuy commented Oct 1, 2025

Added more detail about the order of the addresses and data in the latest commit, and I've marked this ready now.

Thank you

Copy link
Member

@tannewt tannewt left a comment

Choose a reason for hiding this comment

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

Ok, this will work. Thanks!

@FoamyGuy FoamyGuy merged commit 5f991fd into adafruit:main Oct 2, 2025
1 check passed
@FoamyGuy FoamyGuy mentioned this pull request Oct 2, 2025
adafruit-adabot pushed a commit to adafruit/Adafruit_CircuitPython_Bundle that referenced this pull request Oct 3, 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.

2 participants