Skip to content

i2c: share bus recovery across software and hardware backends - #12120

Draft
silencebadwolf wants to merge 2 commits into
RT-Thread:masterfrom
silencebadwolf:codex/i2c-common-bus-recovery
Draft

silencebadwolf wants to merge 2 commits into
RT-Thread:masterfrom
silencebadwolf:codex/i2c-common-bus-recovery

Conversation

@silencebadwolf

@silencebadwolf silencebadwolf commented Dec 4, 2026 •

Copy link
Copy Markdown

拉取/合并请求描述:(PR description)

[

为什么提交这份 PR / Why

A controller can time out while the target is still holding SDA low. Returning the error lets the caller continue, but the next transfer can fail until the wires are released. The bus-clear sequence is common to GPIO bit-banging and hardware controllers that can temporarily hand their pins over to GPIO.

We saw this on a GD32H759IMT6 board running a downstream RT-Thread v5.3.0 firmware with two independent single-controller I2C buses and 4.7 kΩ board-side pull-ups.

  • I2C0 in Fast-mode (400 kHz configuration), Si5351-family clock generator at 0x60. A write reported NACK and BUSY (0x8010). SCL was high and SDA stayed low after shutdown. One recovery clock and STOP released the bus in the natural incident. A later transaction succeeded without resetting or cycling power. A separate interrupted-read experiment needed three clocks.
  • I2C1 in Standard-mode (100 kHz configuration), GT911 touch controller at 0x5d. The driver recorded LOSTARB and BUSY (0x8201). SCL was high and SDA stayed low with the controller stopped. The driver latched the fault, so later touches failed until reboot. The downstream touch recovery change is build-verified only.

These are observations from downstream DMA/IRQ-based drivers. The first electrical trigger remains unknown. The upstream GD32 example in this PR uses its polling backend.

解决方案 / Approach

  • Add a common rt_i2c_bus_clear() helper using the existing rt_i2c_bit_ops line callbacks. With exclusive physical ownership and open-drain pins, sample SCL, try at most nine clocks when SDA is held low, and attempt STOP on each high phase. Finish when the lines release. Report failure if the bus stays low.
  • Reuse RT_I2C_DEV_CTRL_UNLOCK via rt_i2c_control(bus, RT_I2C_DEV_CTRL_UNLOCK, RT_NULL). The bus mutex serializes the command with transfers. Software I2C also uses the helper for its startup check before publishing the bus.
  • Software I2C can attempt recovery automatically at runtime with CONFIG_RT_I2C_BITOPS_BUS_RECOVERY=y. This option is off by default and is for single-controller buses. After a failed transfer that releases the bus, it attempts recovery only when SCL is high and SDA is still low. The failed transfer keeps its error result. The explicit unlock command remains available for maintenance.
  • GD32H7 is one hardware integration of the common helper. Its board option is off by default. When enabled on a single-controller board, the driver stops the peripheral, hands the pins to open-drain GPIO, and restores the peripheral and pin modes afterwards. This supplies explicit recovery and an attempt on eligible transfer errors. The failed transfer keeps its error result. The polling BSP uses short thread sleeps between recovery edges, since it does not require a board-provided microsecond delay.

Bus clear follows NXP UM10204 section 3.1.16.

验证 / Validation

  • BSP bsp/gd32/arm/gd32h759i-eval, ARM GCC 13.2.1, SCons.
  • SCons compile and link passed for three configurations:
    • Hardware and software recovery together with CONFIG_BSP_USING_I2C=y, CONFIG_BSP_USING_HARD_I2C=y, CONFIG_BSP_USING_HARD_I2C0=y, CONFIG_BSP_GD32H7_I2C_BUS_RECOVERY=y, CONFIG_RT_USING_SOFT_I2C=y, CONFIG_RT_USING_SOFT_I2C0=y, and CONFIG_RT_I2C_BITOPS_BUS_RECOVERY=y. Both backends linked the same helper.
    • Rebuilt with RT_I2C_BITOPS_BUS_RECOVERY disabled and software I2C still enabled. ARM object inspection confirmed that the transfer path calls the common helper only in the opt-in build.
    • Hardware recovery with RT_USING_SOFT_I2C and RT_USING_I2C_BITOPS disabled. The hardware driver still linked the common helper.
    • Hardware I2C with recovery disabled. The driver built without its recovery hook.
  • This upstream patch has not run on hardware. The downstream board results above validate the recovery approach in that setup. The upstream adapters still need wire-level verification of STOP and a successful follow-up transfer. SCL held low and SDA held low beyond nine pulses also need board verification.
  • GitHub Actions results are tracked under this draft PR.
    ]

当前拉取/合并请求的状态 Intent for your PR

必须选择一项 Choose one (Mandatory):

  • 本拉取/合并请求是一个草稿版本 This PR is for a code-review and is intended to get feedback
  • 本拉取/合并请求是一个成熟版本 This PR is mature, and ready to be integrated into the repo

代码质量 Code Quality:

我在这个拉取/合并请求中已经考虑了 As part of this pull request, I've considered the following:

  • 已经仔细查看过代码改动的对比 Already check the difference between PR and old code
  • 代码风格正确,包括缩进空格,命名及其他风格 Style guide is adhered to, including spacing, naming and other styles
  • 没有垃圾代码,代码尽量精简,不包含#if 0代码,不包含已经被注释了的代码 All redundant code is removed and cleaned up
  • 所有变更均有原因及合理的,并且不会影响到其他软件组件代码或BSP All modifications are justified and not affect other components or BSP
  • 对难懂代码均提供对应的注释 I've commented appropriately where code is tricky
  • 代码是高质量的 Code in this PR is of high quality
  • 已经使用clang-format 源码格式化工具确保格式符合RT-Thread代码规范 This PR has been formatted with clang-format and complies with RT-Thread code specification
  • 如果是新增bsp, 已经添加ci检查到.github/ALL_BSP_COMPILE.json 详细请参考链接BSP自查

@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@wdfk-prog

Copy link
Copy Markdown
Contributor

支持软、硬 I²C 共用 Bus Clear 算法,但建议这版先只做公共 helper + 显式恢复,把传输失败后的自动恢复拆到后续 PR。

  1. 公共 helper 不建议直接接收 rt_i2c_bit_ops。能否用一个只包含 SCL/SDA 读写、延时和上下文的接口?硬件后端自行负责停用控制器、切换和恢复引脚复用;有原生恢复能力的后端也不必走 GPIO。
  2. 以 GD32H7 为例,目前 ret < 0 && !(msg->flags & RT_I2C_NO_STOP) 只说明传输出错、消息没有禁止 STOP,不代表 STOP 已完成,也不代表 SDA 卡低。当前实现可能先停外设、切 GPIO,再检查 SCL 是否释放。建议把“外设仍报 BUSY”和“物理总线确实卡住”区分开,不用同一个条件触发 GPIO 接管。
    这版可以保留软 I²C 的启动解锁,通过现有 RT_I2C_DEV_CTRL_UNLOCK 提供显式恢复,并在总线锁下返回恢复结果。后续再单独定义自动恢复的触发条件和最长耗时,保留原事务错误,不自动重发。

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants