Skip to content
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

I2c addr #2864

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open

I2c addr #2864

wants to merge 10 commits into from

Conversation

bjoernQ
Copy link
Contributor

@bjoernQ bjoernQ commented Dec 24, 2024

Thank you for your contribution!

We appreciate the time and effort you've put into this pull request.
To help us review it efficiently, please ensure you've gone through the following checklist:

Submission Checklist 📝

  • I have updated existing examples or added new ones (if applicable).
  • I have used cargo xtask fmt-packages command to ensure that all changed code is formatted correctly.
  • My changes were added to the CHANGELOG.md in the proper section.
  • I have added necessary changes to user code to the Migration Guide.
  • My changes are in accordance to the esp-rs API guidelines

Extra:

Pull Request Details 📖

Description

  • preparation for adding 10 bit addressing mode later without breaking changes
  • use an enum to specify the i2c timeout

Fixes #2786

Testing

CI

@bjoernQ bjoernQ marked this pull request as ready for review December 24, 2024 11:37
Copy link
Member

@SergioGasquez SergioGasquez left a comment

Choose a reason for hiding this comment

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

LGTM! Thanks!

Copy link
Contributor

@bugadani bugadani left a comment

Choose a reason for hiding this comment

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

I wish this were 2 PRs but I have comments for both halves :)

esp-hal/src/i2c/master/mod.rs Outdated Show resolved Hide resolved
esp-hal/src/i2c/master/mod.rs Outdated Show resolved Hide resolved
esp-hal/src/i2c/master/mod.rs Outdated Show resolved Hide resolved
Copy link
Contributor

@JurajSadel JurajSadel left a comment

Choose a reason for hiding this comment

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

Thanks!

@@ -521,33 +619,38 @@ where
}

/// Writes bytes to slave with address `address`
pub fn write(&mut self, address: u8, buffer: &[u8]) -> Result<(), Error> {
pub fn write<A: AddressMode>(&mut self, address: A, buffer: &[u8]) -> Result<(), Error> {
Copy link
Member

Choose a reason for hiding this comment

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

So with this way of doing things, when we add 10bit support, won't this break code like this:

i2c.write(0xaa /* <- ambiguous type here, could be a u8 or a u16 */, &[])?;

Copy link
Contributor

Choose a reason for hiding this comment

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

We would be able to implement for i321 and decide what to do with that. But it would probably be unclear what i32 would represent. Instead, I would recommend implementing AddressMode for u8 and TenBit(u16) instead, not for other numeric types.

Footnotes

  1. https://rust.godbolt.org/z/Wfjh7PMMc

Copy link
Contributor Author

Choose a reason for hiding this comment

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

oh yeah - we can't have defaults for type parameters on functions :( which would have been an easy solution

I would recommend implementing AddressMode for u8 and TenBit(u16) instead, not for other numeric types

I like this solution a lot - 10-bit is uncommon enough to have a new-type for it

pub trait AddressMode: _private::AddressModeInternal + Copy + 'static {}

mod _private {
pub trait AddressModeInternal {
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't understand the need for this. I2cAddress can impl From from the possible address types.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

idea is that implementing From extends the public API surface and we would need to expose the internal enum

Copy link
Contributor

Choose a reason for hiding this comment

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

Why is that a problem, since that enum is already marked with a bunch of derives that only make sense if it's public?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

we can ofc remove the unnecessary derives - we just shouldn't make anything public which doesn't need to

}

impl I2cAddress {
pub fn as_u8(&self) -> u8 {
Copy link
Contributor

Choose a reason for hiding this comment

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

Why is this public, and what will happen when we introduce 10-bit addresses?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

can be pub(super) but since it's in a private module doesn't make much difference

10-bit address support will introduce some more changes - we'll probably have to match on the enum and do a few things differently (and will remove this function)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
5 participants