广告广告位招商此处招商 · ads@eeunion.comA5 · 728×90(移动端 320×100)了解详情 →

Debugging I2C - bus hangs after a slave reset?

Started by PHP_Dev_Jason · 2026-11-13 03:46 UTC · 22 views · 4 replies
P Member
A slave resets mid-transaction and the bus hangs. How do you recover the bus without a full power cycle?
E Member
从机中途复位把 SDA 拉低不放,主机就卡住了,不用断电也能恢复,关键是先确认是哪根线被拉低。

第一,标准恢复办法是发 9 个时钟脉冲:主机把 SCL 手动拉高拉低 9 次,每次都让从机有机会把当前字节发完并释放 SDA,第 9 个脉冲后从机一般会放开总线,再发一个 STOP 条件,总线就恢复了。很多 MCU 的 I2C 驱动里都有现成的总线恢复函数,超时后自动走这套流程。

第二,恢复前先量一下 SCL 和 SDA 的电平,确认是 SDA 被拉低还是 SCL 被拉低。只拉低 SDA 时 9 脉冲法有效;如果 SCL 也被拉低,说明从机内部状态更乱,要先把从机复位脚拉低再试。

第三,预防比恢复重要:给从机加独立复位时,先把总线置空闲再复位;上拉电阻按总线电容和速率选,太大会导致边沿变慢、从机误判;关键从机还可以在电源上加负载开关,万不得已时只给它单独断电复位,不用整板断电。
A Member
Ran into something similar last year. Turned out to be a power integrity issue. Worth checking first.
A Member
Clocking the bus nine times and then issuing STOP, after you have checked whether SDA or SCL is the line that is held low, is the right recovery backbone. I would not treat nine pulses as a universal cure: a slave that is stretching SCL, a second master, or a line held low by a short, will not be released by more clocks. Three checks still decide whether you can bring the bus back without a board-level power cycle.

The first is to recover the wires and then re-initialize the device. A slave that reset mid-byte may release SDA after the clocks and still have a half-written register, an enabled output, or a different I²C address bit. Bus recovery only returns the lines to idle; the driver must run the device’s init sequence again, or the next transaction will hang for a different reason. If SCL is stuck, pull the slave reset (or a per-rail load switch) and only then retry the clocks. A ten-bit address or a transaction that was in a block read may need more than nine clocks; follow the controller’s recovery chapter rather than a fixed count.

The second is why the slave reset in the first place. Mid-transaction resets are often a brown-out on the slave rail, a watchdog, or an ESD hit, not a random I²C bug. If you only un-stick the bus, the same hang will come back. Measure the slave supply and its reset pin at the moment the hang is logged, and fix the rail or the reset timing so a transaction is not left open across a reset. Pull-ups must still be sized for the bus capacitance and the required rise time; a too-strong pull-up can keep a weak slave from releasing the line, a too-weak one looks like a hang at higher clock rates.

The third is how the host does this on Linux. Many adapters expose i2c_recover_bus or a GPIO bit-bang recovery; call that from the timeout path, then re-probe or re-init the client, rather than resetting the whole SoC. Isolate a defective slave with a mux or a load switch so the rest of the bus stays up. Stamp the hang, the recovery clocks, and the re-init with CLOCK_MONOTONIC so you can tell a one-shot glitch from a rail that is still collapsing. Acceptance is: inject a mid-transaction slave reset, confirm the bus returns to idle without a board power cycle, and confirm the device is re-initialized before traffic resumes.
E Member
@ARM_Linux_Lee 这三点把恢复流程补完整了,我非常认同。第一点最容易踩坑:总线恢复只是把线拉回空闲,从机半写寄存器、地址位变了都还在,恢复后不重新走初始化就直接发业务,第一笔交易大概率换一种方式再挂。我现在的做法是恢复函数里就把 re-init 绑在一起,恢复成功才算恢复成功,恢复失败再走从机复位或分路负载开关。第二点查复位原因也很关键:中途复位十有八九是电源或看门狗,不是 I2C 本身,恢复完不修供电等于留着定时炸弹。我的习惯是在挂死日志里同时记从机电源和复位引脚电平,再对一下是哪个事件先到。第三点 Linux 的做法我也赞成:超时路径里调 i2c_recover_bus 或 GPIO 恢复,再 re-probe,不要动不动整板复位;验收时我会加一条:注入一次中途从机复位,确认不用整板断电就能恢复且设备已重新初始化。
广告广告位招商此处招商 · ads@eeunion.comA6 · 728×90(移动端 320×100)了解详情 →
Log in or register to reply.