Skip to content

I2c spi combo driver#2

Closed
FoamyGuy wants to merge 2 commits intoadafruit:mainfrom
FoamyGuy:i2c_spi_combo_driver
Closed

I2c spi combo driver#2
FoamyGuy wants to merge 2 commits intoadafruit:mainfrom
FoamyGuy:i2c_spi_combo_driver

Conversation

@FoamyGuy
Copy link
Contributor

This change proposes a version of the driver that supports both I2C and SPI for communication. @tannewt I'm interested to get feedback from you on this refactor when you have time.

This code goes along with the changes in: adafruit/Adafruit_CircuitPython_Register_SPI#8 it will not work without those.

The main API change with this as compared to a "plain" I2C driver using adafruit_register is having a function that creates the class using dynamic arguments for RWBits, ROBits etc... classes instead of using a class constructor to make an instance.

This means that user code must call the create function like this:

bmp = create_bmp5xx_i2c(i2c)
# or
bmp = create_bmp5xx_spi(spi, cs)

instead of using a class constructor like this:

bmp = BMP5XX_I2C(i2c)

But the resulting object returned is essentially the same, an instance of BMP5XX that is ready to take readings by accessing properties like bmp.temperature etc. And happily it allows us to have unified definitions for the descriptors that don't get duplicated and work with either type of bus.

I tested this version successfully with both the SPI and I2C example scripts using different breakouts connected to each bus. On this device:

Adafruit CircuitPython 10.0.0-beta.3 on 2025-08-29; Adafruit Feather RP2040 with rp2040

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.

I'd prefer to make Register more flexible than make this driver more complicated.

What I'm picturing is one BMP5XX class that takes an I2CDevice or a SPIDevice and adapts internally to the transport. I think it'll need to create a "RegisterAccessor" or something that reads and writes registers and then the data descriptors will not be from i2c_ and instead look for a register_accessor on the given obj. This new accessor layer will allow the register fields to focus on packing and unpacking instead of the transport itself.

(Data descriptor reference: https://docs.python.org/3/howto/descriptor.html)

@FoamyGuy
Copy link
Contributor Author

Closing this PR, I started a new branch for the refactored version

@FoamyGuy FoamyGuy closed this Sep 25, 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