How do I share an I2c bus between tasks?
Nobody has claimed this yet.
Assessment
- Difficulty
- 5/5
- Estimated time
- Over a week
- Newbie friendliness
- 18/100
- Issue type
- Documentation
- Clarity
- Needs clarification
- Activity status
- Stale
- Tech stack
- rust
- Domain
- embedded-iot
Research direction
Start with the issue's RefCellDevice example and the Rust lifetime error, then inspect the embedded-hal-bus I2C sharing documentation and related ownership guidance. Done means documenting a safe, preferred approach for sharing one I2C bus across asynchronous tasks without relying on unsafe statics or leaked allocations.
Written by the indexing model from the issue text.
Description
I'm trying to setup an embedded project with the async/await approach (using a simple executor, no big framework like Embassy). I have two modules which need to asynchronously manage two i2c devices on the same bus, so they need ownership of an I2c instance.
When I try to create two RefCellDevices and move them to the respective modules I incur in the following compilation error:
// 12c0 is the original I2c bus
let i2c_bus = RefCell::new(i2c0);
// First RefCellDevice, to be owned by the main thread
let mut rtc = Rx8010sj::new(embedded_hal_bus::i2c::RefCellDevice::new(&i2c_bus));
log::info!("RTC stopped: {}", rtc.is_stopped().unwrap());
rtc.set_stopped(false).unwrap();
// Second RefCellDevice, to be sent to a different task
let (mut leds, task) = leds::Driver::start(
embedded_hal_bus::i2c::RefCellDevice::new(&i2c_bus),
);
spawner.spawn_local(task).unwrap();
local_executor.run();
error[E0597]: `i2c_bus` does not live long enough
--> src/lib.rs:68:51
|
49 | let i2c_bus = RefCell::new(i2c0);
| ------- binding `i2c_bus` declared here
...
68 | embedded_hal_bus::i2c::RefCellDevice::new(&i2c_bus),
| ------------------------------------------^^^^^^^^-
| | |
| | borrowed value does not live long enough
| argument requires that `i2c_bus` is borrowed for `'static`
...
89 | }
| - `i2c_bus` dropped here while still borrowed
I think understand the problem: i2c_bus is owned by the main function and the compiler thinks it will be dropped at the end even if the main function never returns. Thus I cannot send the i2c_bus reference to the leds task because it doesn't live long enough.
I can create a (late initialized) static instance for i2c_bus and it works but obviously it uses unsafe code.
Alternatively I can Box the RefCellDevice and immediately leak it, but again it doesn't seem a perfect solution.
What is the preferred way to fix this?
- Dominant language
- Rust
- Stars
- 2.7k
- Forks
- 282
- PR merge metrics
- No merged PRs in 30d
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from rust-embedded/embedded-hal
-
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
rust-embedded/embedded-hal#742 · 1 comment ·
-
Difficulty 5/5 Over a week Newbie friendliness 30/100
rust-embedded/embedded-hal#747 · 5 comments · 1 reaction ·
-
Difficulty 5/5 Over a week Newbie friendliness 35/100
rust-embedded/embedded-hal#746 · 2 comments · 1 reaction ·
-
Difficulty 4/5 3-5 days Newbie friendliness 48/100
rust-embedded/embedded-hal#745 · 1 reaction ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 55/100
rust-embedded/embedded-hal#744 · 1 comment ·
All issues in rust-embedded/embedded-hal
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 86/100
kwakseongjae/auto-hwp#319 ·
-
area:cli bug filter-quality good first issue priority:medium
Difficulty 2/5 1-3 hours Newbie friendliness 84/100
-
Difficulty 1/5 Under an hour Newbie friendliness 72/100
bevyengine/bevy#25861 ·
-
comp-datalake
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
ClickHouse/ClickHouse#121222 ·
-
enhancement remote
Difficulty 2/5 1-3 hours Newbie friendliness 68/100