Skip to content

Add transactional I2C interface #223

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
merged 7 commits into from
Oct 30, 2020
Merged

Conversation

eldruin
Copy link
Member

@eldruin eldruin commented Jun 17, 2020

An example where a transactional I2C interface would be an improvement is when facing the problem of sending an array of data where the first item is the destination address.
At the moment this requires copying the data into a bigger array and assigning the first item to the address. With a transactional I2C two write operations could be chained into one transaction so that it is possible to send the address and data without an extra copy.
This is specially problematic if the data length is unknown e.g. because it comes from the user.

You can see the problem here in the eeprom24x driver. In this case I am lucky enough to have the page upper limit so that the copy workaround is possible.

With this PR a bunch of code and macros could be replaced by doing something similar to this:

let user_data = [0x12, 0x34, ...]; // defined somewhere else. length may be unknown.
let target_address = 0xAB;
let mut ops = [
  Operation::Write(&[target_address]),
  Operation::Write(&user_data),
];
i2cdev.try_exec(DEV_ADDR, &ops)

I added a PoC here in linux-embedded-hal including an example of a driver where a classical combined write/read is performed through the transactional interface.

Note: A default implementation of the Transactional trait like in #191 is not possible because STOPs would always be sent after each operation. What is possible is to do is the other way around. This includes an implementation of the Write, Read and WriteRead traits for Transactional implementers.

This is based on previous work from #178 by @ryankurte and it is similar to #191

TODO:

  • Add changelog entry

@rust-highfive
Copy link

r? @ryankurte

(rust_highfive has picked a reviewer for you, use r? to override)

@eldruin eldruin force-pushed the transactional-i2c branch from f8c1d08 to b22c88c Compare June 18, 2020 12:11
@therealprof
Copy link
Contributor

Doesn't the trait need an entry in the prelude?

@eldruin
Copy link
Member Author

eldruin commented Jun 20, 2020

Doesn't the trait need an entry in the prelude?

Done.

@eldruin eldruin force-pushed the transactional-i2c branch 3 times, most recently from 0de9610 to bcaa268 Compare July 16, 2020 06:48
@eldruin
Copy link
Member Author

eldruin commented Oct 30, 2020

Is there anything left to do here? Since we have merged transactional SPI already, adding also transactional I2C to the same release would be nice.
I have also removed an entry about transactional SPI which was also present for the alpha.2 entry in the changelog.
However, #191 was only merged after the alpha.2 release.

Copy link
Contributor

@therealprof therealprof left a comment

Choose a reason for hiding this comment

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

Excellent, let's get this in then.

bors r+

@bors
Copy link
Contributor

bors bot commented Oct 30, 2020

👎 Rejected by code reviews

@therealprof
Copy link
Contributor

... after @ryankurte removed his concern...

@therealprof
Copy link
Contributor

bors r+

@bors bors bot merged commit 95de0e0 into rust-embedded:master Oct 30, 2020
@eldruin eldruin deleted the transactional-i2c branch October 31, 2020 08:21
@eldruin eldruin restored the transactional-i2c branch October 31, 2020 08:21
bors bot added a commit to rust-embedded/linux-embedded-hal that referenced this pull request Nov 12, 2020
44: Implement transactional I2C interface and add example r=ryankurte a=eldruin

I implemented the transactional I2C interface from rust-embedded/embedded-hal#223 and added an example with a driver which does a combined Write/Read transaction.
This is based on previous work from #33 by @ryankurte, rust-embedded/rust-i2cdev#51 and is similar to #35.

Co-authored-by: Diego Barrios Romero <eldruin@gmail.com>
bors bot added a commit to rust-embedded/linux-embedded-hal that referenced this pull request Nov 14, 2020
44: Implement transactional I2C interface and add example r=ryankurte a=eldruin

I implemented the transactional I2C interface from rust-embedded/embedded-hal#223 and added an example with a driver which does a combined Write/Read transaction.
This is based on previous work from #33 by @ryankurte, rust-embedded/rust-i2cdev#51 and is similar to #35.

Co-authored-by: Diego Barrios Romero <eldruin@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants