Skip to content

feat: change sccb APIs for sharing i2c port with other devices #413

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

Merged

Conversation

WangYuxin-esp
Copy link
Contributor

@WangYuxin-esp WangYuxin-esp commented Jul 1, 2022

  • Leave the allocation, configuration and deallocation of the i2c bus under the control of application code.
  • Configure the pin_sccb_sda and pin_sccb_scl pins to -1, then pass the initialized i2c port number to sccb_i2c_port to share the I2C port with other devices.

@WangYuxin-esp WangYuxin-esp force-pushed the feature/better_use_of_sccb_interface branch from ada7c7b to 7dd0708 Compare July 1, 2022 06:57
@jepler
Copy link

jepler commented Aug 4, 2022

I raised a similar issue in #434. However, I think this PR in its current form doesn't meet our needs. Thanks to @igrr for pointing me here.

In CircuitPython, we do not know statically which i2c port number will be associated with the camera. Rather, it is determined at runtime by CircuitPython, which tracks which i2c port number(s) are available itself and assumes no other part of the system allocates i2c ports.

Suppose the product has two I2C buses, one for the camera SCCB and one for a sensor. The application code might say:

camera_bus = busio.I2C(...) # assume this allocates I2C port 0
sensor_bus = busio.I2C(...) # assume this allocates I2C port 1

camera = esp32_camera.Camera(..., i2c=camera_bus, ...) # the camera must use bus 0

but the application must also operate if the I2C buses are constructed in the opposite order:

sensor_bus = busio.I2C(...) # allocates I2C port 0
camera_bus = busio.I2C(...) # allocates I2C port 1

camera = esp32_camera.Camera(..., i2c=camera_bus, ...) # the camera must use bus 1

Furthermore, consider the case where instead of having two separate buses, there's just a single bus with a sensor and a camera:

combined_bus = busio.I2C(...) # allocates I2C port 0

sensor = Sensor(i2c=combined_bus)

camera = esp32_camera.Camera(..., i2c=combined_bus, ...) # so the camera must use bus 0
camera.deinit()

# At this point, `combined_bus` must remain valid so that `sensor` can still be used

So, for CircuitPython we want to leave the allocation, configuration and deallocation of the i2c bus under the control of CircuitPython. The bus number cannot be statically determined, and the camera may never deallocate/deinitialize the i2c bus. In practice, it's OK if constructing the camera configures the bus, since all devices on the bus have to be compatible in settings anyway.

@igrr
Copy link
Member

igrr commented Aug 4, 2022

Thanks for the explanation @jepler!

@WangYuxin-esp could you please look at the solution proposed in #434 and see if it meets your needs? If yes, would you mind updating the PR as suggested there?

@WangYuxin-esp
Copy link
Contributor Author

Thanks for the explanation @jepler!
The current solution can help solve the issue of I2C bus multiplexing. However, the current PR does not address the issue of I2C deactivation after calling cam.deinit() when multiple devices share an I2C bus.

@WangYuxin-esp
Copy link
Contributor Author

I can update this PR, or please @jepler create a new one.

@jepler
Copy link

jepler commented Aug 8, 2022

I can, but my other duties would prevent me from submitting a PR for this for at least 2 weeks.

@WangYuxin-esp WangYuxin-esp force-pushed the feature/better_use_of_sccb_interface branch from 6141152 to ee808dd Compare August 9, 2022 04:00
@WangYuxin-esp WangYuxin-esp changed the title feat: build some sccb APIs to share the bus with other devices feat: change sccb APIs for sharing i2c port with other devices Aug 9, 2022
@WangYuxin-esp
Copy link
Contributor Author

@jepler I have changed the relevant code about sccb. PTAL.

Copy link
Member

@igrr igrr left a comment

Choose a reason for hiding this comment

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

Really good update, thanks @WangYuxin-esp! Have left some mostly minor comments.

@WangYuxin-esp WangYuxin-esp force-pushed the feature/better_use_of_sccb_interface branch from ee808dd to 77d71b0 Compare August 12, 2022 12:18
Copy link
Member

@igrr igrr left a comment

Choose a reason for hiding this comment

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

Latest version looks good to me, thanks for addressing my comments!
Let's wait a bit for @jepler to have a look before we merge this.

@wuyuanyi135
Copy link

I feel lucky when I am currently designing a product with insufficient IO pins and then see this PR. Looking forward to merging this feature!

@me-no-dev
Copy link
Member

@WangYuxin-esp all looks great, but I would like you to make the rename from pin_sscb_* to pin_sccb_* in camera_config_t as a separate pull request. That change is not critical for the functionality that this PR intends to add, but will break a lot of working code. Please do this change and I will merge the code immediately :)

@igrr
Copy link
Member

igrr commented Aug 23, 2022

@me-no-dev the sscb -> sccb rename seems to be a non-breaking one as far as I can tell, due to

    union {
        int pin_sccb_sda;           /*!< GPIO pin for camera SDA line */
        int pin_sscb_sda __attribute__((deprecated("please use pin_sccb_sda instead")));           /*!< GPIO pin for camera SDA line (legacy name) */
    };
    union {
        int pin_sccb_scl;           /*!< GPIO pin for camera SCL line */
        int pin_sscb_scl __attribute__((deprecated("please use pin_sccb_scl instead")));           /*!< GPIO pin for camera SCL line (legacy name) */
    };

(pin_sscb_sda and pin_sscb_scl will still work, will produce deprecation warning)

@me-no-dev
Copy link
Member

Ha! Seems I have misread the changes... 🤷‍♂️ merging now.

@me-no-dev me-no-dev merged commit 5611989 into espressif:master Aug 23, 2022
@WangYuxin-esp WangYuxin-esp deleted the feature/better_use_of_sccb_interface branch August 26, 2022 02:05
ladyada added a commit to adafruit/Adafruit_PyCamera that referenced this pull request Oct 8, 2023
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.

5 participants