How do I share an I2c bus between tasks?

Open
#591 1 comment 2 reactions 0 assignees View on GitHub

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

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from rust-embedded/embedded-hal

All issues in rust-embedded/embedded-hal

Similar issues

More Rust issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.