How do I share an I2c bus between tasks?
还没有人认领这个 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,通用步骤见我们的新手贡献指南。
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
rust-embedded/embedded-hal 的其他 Issue
-
难度 2/5 1-3 小时 新手友好度 68/100
rust-embedded/embedded-hal#750 ·
-
难度 2/5 1-3 小时 新手友好度 75/100
rust-embedded/embedded-hal#742 · 1 条评论 ·
-
难度 5/5 一周以上 新手友好度 30/100
rust-embedded/embedded-hal#747 · 5 条评论 · 1 个 reaction ·
-
难度 5/5 一周以上 新手友好度 35/100
rust-embedded/embedded-hal#746 · 2 条评论 · 1 个 reaction ·
-
难度 4/5 3-5 天 新手友好度 48/100
rust-embedded/embedded-hal#745 · 1 个 reaction ·
查看 rust-embedded/embedded-hal 的全部 Issue
相似的 Issue
-
binding:python bug
难度 2/5 1-3 小时 新手友好度 82/100
维护者通常 1 天内回复
-
enhancement
难度 2/5 1-3 小时 新手友好度 78/100
coollabsio/orbit#72 ·
维护者通常 1 天内回复
-
Synth suite fails unless run from the repo root: signal_flow's default machine path is cwd-relative未关闭bug
难度 2/5 1-3 小时 新手友好度 80/100
Nimblesite/osprey#370 ·
维护者通常 1 天内回复
-
enhancement
难度 2/5 1-3 小时 新手友好度 70/100
-
bug order-flow studies ui
难度 2/5 1-3 小时 新手友好度 88/100
userFRM/egui-charts#2417 ·
维护者通常 1 天内回复