How do I share an I2c bus between tasks?

未关闭
#591 1 条评论 2 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
5/5
预计耗时
一周以上
新手友好度
18/100
Issue 类型
文档
描述清晰度
需要澄清
活跃度
停滞
技术栈
rust
领域
embedded-iot

调研方向

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.

由索引模型根据 Issue 内容生成。

描述

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?

主要语言
Rust
星标
2.7k
派生
283
PR 合并指标
30 天内没有已合并 PR

环境准备

这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

rust-embedded/embedded-hal 的其他 Issue

查看 rust-embedded/embedded-hal 的全部 Issue

相似的 Issue

更多 Rust Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。