-
Notifications
You must be signed in to change notification settings - Fork 201
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
Make I2C compatible with multiple address sizes #230
Conversation
r? @ryankurte (rust_highfive has picked a reviewer for you, use r? to override) |
d5ad656
to
0ae9f66
Compare
I have now also added the |
0ae9f66
to
e6b10e5
Compare
seems generally good to me! the trait indirection approach is clever, though, feels a touch complex (and i have been bitten by trying to do overly clever things with the type system 🙃) does this give us a strong benefit over providing a couple of public type aliases and documentation of the convention ( (i'm not opposed to this approach, but, would like to know we have considered all possibilities) |
263a5bf
to
7b88d29
Compare
Yes, technically without a trait bound it would be possible to misuse the I2C traits and pass some random object through the address parameter if both HAL implementation and driver agree on a complex "address" type. It would also be possible to do this simplification: pub trait AddressMode: private::Sealed {}
pub type SevenBitAddress = u8;
pub type TenBitAddress = u16;
impl AddressMode for SevenBitAddress {}
impl AddressMode for TenBitAddress {} I think this would be fine, since we probably will not need any additional type other than Would you prefer that? I have added some documentation now on how this would work for a HAL implementation and device drivers. I could also write some documentation about how to write a driver for a device compatible with both modes but that might be too much. Opinions? |
Looks good to me. 👍 |
ooh, i was under the impression you can't implement traits on type aliases? though, this may be mitigated by the trait being defined in the same place.
hmm i think so? it seems a bit simpler (which is excellent imo), neat that the |
7b88d29
to
bb76b54
Compare
Done! |
bb76b54
to
15cb0e6
Compare
15cb0e6
to
d8187c3
Compare
i think this looks good to me, @therealprof any thoughts? |
Haven't had time to play with it in person but looks and sounds great. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
well, let's get it out and in the alpha eh? i guess we should look to another (alpha) release shortly
bors r+
This adds I2C 7-bit and 10-bit address mode compatibility as roughly described here.
Discussion issue: #147
I have also added the
SevenBitAddress
as the default address mode to the traits so this is not even a breaking change.Usage broken down per use case:
The driver looks exactly the same as now since the default address mode is 7-bit.
The only difference to a 7-bit-address-only driver is one additional parameter in the I2C trait bound.
Complexity can be abstracted away into additional internal traits which can handle the addressing stuff. Driver code stays clean.
This is nothing new. We already do this on drivers for devices compatible with both I2C and SPI. No need for duplicated code.
Here a real example: usage, traits
Bus controller impl supporting only 7-bit addressing mode:
Code stays almost the same, just adding one addressing mode parameter. Additionally, if desired:
Emulate by extending and copying payload in separate
TenBitAddress
implementation. Total flexibility to do whatever is necessary in this case since the code is independent.Implementation does not offer implementation for
TenBitAddress
variant. The user gets a compilation error and everything is clear.Bus controller impl supporting both addressing modes:
No problem. Two separate implementations guarantee as much flexibility as necessary. At the same time, sharing generic code is possible.
Additional benefits:
See here for a comparison to other alternatives.
I have also sealed the trait.
Proof
For demonstration purposes, explicitly including the
SevenBitAddress
would look like this: OPT300x. code changes.This would be similar to the case of a 10-bit-only device driver.