fix[STM32][I2C]: stabilize async completion and recovery flow for hard i2c#11366
fix[STM32][I2C]: stabilize async completion and recovery flow for hard i2c#11366wdfk-prog wants to merge 1 commit intoRT-Thread:masterfrom
Conversation
…d i2c enable I2C error irq when async transfer paths are used fall back to polling when scheduler or interrupt context cannot wait for completion complete the wait path on HAL error callback and abort the transfer on async timeout or error allow INT and DMA mode flags to coexist and keep recovery behavior aligned across STM32 I2C IPs
|
👋 感谢您对 RT-Thread 的贡献!Thank you for your contribution to RT-Thread! 为确保代码符合 RT-Thread 的编码规范,请在你的仓库中执行以下步骤运行代码格式化工作流(如果格式化CI运行失败)。 🛠 操作步骤 | Steps
完成后,提交将自动更新至 如有问题欢迎联系我们,再次感谢您的贡献!💐 |
📌 Code Review Assignment🏷️ Tag: bsp_stm32Reviewers: @Liang1795 @hamburger-os @wdfk-prog Changed Files (Click to expand)
📊 Current Review Status (Last Updated: 2026-05-06 18:32 CST)
📝 Review Instructions
|
There was a problem hiding this comment.
Pull request overview
This PR updates the STM32 hard I2C driver to make IRQ/DMA async transfers more robust by ensuring error interrupts can wake waiters and by adding a recovery/abort flow when async completion fails.
Changes / 变更:
- Enable both I2C event IRQ and error IRQ when using INT/DMA async paths; wake the waiting thread from
HAL_I2C_ErrorCallback().
启用 I2C event/error 中断并在错误回调中唤醒等待线程,避免等待超时卡死。 - Gate IRQ/DMA usage with
async_allowedto avoid entering async wait paths when scheduler is unavailable or interrupts are disabled.
增加async_allowed限制,避免在不允许调度/中断的上下文进入异步等待路径。 - Add an abort-and-wait-for-READY recovery path when async wait fails or HAL reports an error.
在异步等待失败或 HAL 报错时尝试Abort并等待外设恢复到 READY。
Comments suppressed due to low confidence (2)
bsp/stm32/libraries/HAL_Drivers/drivers/drv_hard_i2c.c:229
- [maintainability/类别]: Misleading RT_UNUSED on used parameters / 对已使用参数的 RT_UNUSED 误用
English: In stm32_i2c_master_receive_start(), RT_UNUSED(i2c_obj), RT_UNUSED(mode), and RT_UNUSED(timeout) are declared, but these parameters are later dereferenced/used in the function. This is misleading for readers and may hide real unused-parameter issues in the future. Please remove RT_UNUSED(...) for parameters that are actually used.
中文:在 stm32_i2c_master_receive_start() 中声明了 RT_UNUSED(i2c_obj) / RT_UNUSED(mode) / RT_UNUSED(timeout),但后续又实际解引用/使用了这些参数。这样会误导阅读者,也可能掩盖未来真正的未使用参数问题。建议删除对已使用参数的 RT_UNUSED(...)。
RT_UNUSED(i2c_obj);
RT_UNUSED(mode);
RT_UNUSED(timeout);
RT_ASSERT(i2c_obj != RT_NULL);
bsp/stm32/libraries/HAL_Drivers/drivers/drv_hard_i2c.c:283
- [maintainability/类别]: Misleading RT_UNUSED on used parameters / 对已使用参数的 RT_UNUSED 误用
English: In stm32_i2c_master_transmit_start(), RT_UNUSED(i2c_obj), RT_UNUSED(mode), and RT_UNUSED(timeout) are marked unused but later used. Please remove these RT_UNUSED(...) lines for the parameters that are actually referenced.
中文:在 stm32_i2c_master_transmit_start() 中把 i2c_obj/mode/timeout 标记为 RT_UNUSED,但后续又实际使用了它们。建议删除这些对已使用参数的 RT_UNUSED(...),避免误导。
RT_UNUSED(i2c_obj);
RT_UNUSED(mode);
RT_UNUSED(timeout);
RT_ASSERT(i2c_obj != RT_NULL);
| * 2024-06-23 wdfk-prog Add blocking modes and distinguish POLL,INT,DMA modes | ||
| * 2024-06-23 wdfk-prog Distinguish STM32 I2C timing semantics by IP generation | ||
| * 2026-04-20 wdfk-prog Stabilize async completion and recovery flow | ||
| */ |
| #if defined(STM32_I2C_TIMINGR_IP) | ||
| timeout = bus->timeout ? bus->timeout : 100U; | ||
| timeout_ms = bus->timeout ? bus->timeout : 100U; | ||
| #else | ||
| timeout = TIMEOUT_CALC(msg); | ||
| timeout_ms = TIMEOUT_CALC(msg); | ||
| #endif | ||
| #if defined(BSP_I2C_USING_IRQ) | ||
| rt_tick_t timeout_tick = rt_tick_from_millisecond(timeout_ms); | ||
| rt_completion_init(completion); |
拉取/合并请求描述:(PR description)
[
为什么提交这份PR (why to submit this PR)
STM32 hard I2C 在使用 IRQ/DMA 异步传输时依赖 HAL 回调唤醒
rt_completion_wait()。当前错误中断未完整启用,且错误回调没有通知等待线程,可能导致 NACK、BERR 或异常状态下等待超时,后续恢复流程不稳定。你的解决方案是什么 (what is your solution)
async_allowed参数,避免在调度器不可用或全局中断关闭时进入 IRQ/DMA 等待路径。ErrorCode,在错误回调中唤醒等待线程。HAL_I2C_Master_Abort_IT(),并等待外设恢复到 READY 状态。请提供验证的bsp和config (provide the config and bsp)
BSP_I2C*_USING_INT、BSP_I2C*_TX_USING_DMA、BSP_I2C*_RX_USING_DMA组合。]
当前拉取/合并请求的状态 Intent for your PR
必须选择一项 Choose one (Mandatory):
代码质量 Code Quality:
我在这个拉取/合并请求中已经考虑了 As part of this pull request, I've considered the following:
#if 0代码,不包含已经被注释了的代码 All redundant code is removed and cleaned up