Wire (nRF52): a timed-out STOP is a failure, and the TWIM is reset after a timeout
Build / build (feather_nrf52840_sense_tft) (push) Failing after 13s
Build / build (cluenrf52840) (push) Failing after 22s
Build / build (feather52840) (push) Failing after 23s
Build / build (itsybitsy52840) (push) Failing after 25s
Build / build (ledglasses_nrf52840) (push) Failing after 25s
Build / build (feather52832) (push) Failing after 25s
Build / build (feather52840sense) (push) Failing after 25s
Build / build (cplaynrf52840) (push) Failing after 26s

On top of #873, tested on a XIAO nRF52840 with SCL tied to GND:
- the STOP/SUSPEND waits broke out without setting timed_out, so an empty
  probe on a stuck bus returned 0, "device ACKed" (every RTC address of an
  autodiscovery scan looked present); requestFrom() alike;
- after a timeout the TWIM stayed wedged once the bus was released: every
  call timed out until a reboot. A disable/enable cycle recovers it.
Result: stuck bus -> error 4 after ~100 ms per call, no hang; bus released
-> NACK 2 / ACK 0 again in under 1 ms, without a reboot.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
tthandClaude Opus 5.5 committed 2026-12-02 13:16:18 +01:00
1 parent 45188a647f
commit fca5ff3c71
1 file changed
+20 -4
+20 -4
View File
@@ -39,6 +39,18 @@ extern "C" {
#define I2C_TWIM_TIMEOUT_MS 100
#endif
// After a timeout the TWIM can stay wedged even once the bus is released (seen with
// SCL held low, then let go: every later call timed out until a reboot). A disable/
// enable cycle resets its state machine; PSEL and FREQUENCY are kept.
static void twim_recover(NRF_TWIM_Type* twim)
{
twim->ENABLE = (TWIM_ENABLE_ENABLE_Disabled << TWIM_ENABLE_ENABLE_Pos);
twim->EVENTS_STOPPED = 0x0UL;
twim->EVENTS_SUSPENDED = 0x0UL;
twim->EVENTS_ERROR = 0x0UL;
twim->ENABLE = (TWIM_ENABLE_ENABLE_Enabled << TWIM_ENABLE_ENABLE_Pos);
}
static volatile uint32_t* pincfg_reg(uint32_t pin)
{
NRF_GPIO_Type * port = nrf_gpio_pin_port_decode(&pin);
@@ -191,7 +203,7 @@ uint8_t TwoWire::requestFrom(uint8_t address, size_t quantity, bool stopBit)
_p_twim->TASKS_STOP = 0x1UL;
wait_start = millis();
while(!_p_twim->EVENTS_STOPPED) {
if (millis() - wait_start > I2C_TWIM_TIMEOUT_MS) break;
if (millis() - wait_start > I2C_TWIM_TIMEOUT_MS) { timed_out = true; break; }
}
_p_twim->EVENTS_STOPPED = 0x0UL;
}
@@ -200,7 +212,7 @@ uint8_t TwoWire::requestFrom(uint8_t address, size_t quantity, bool stopBit)
_p_twim->TASKS_SUSPEND = 0x1UL;
wait_start = millis();
while(!_p_twim->EVENTS_SUSPENDED) {
if (millis() - wait_start > I2C_TWIM_TIMEOUT_MS) break;
if (millis() - wait_start > I2C_TWIM_TIMEOUT_MS) { timed_out = true; break; }
}
_p_twim->EVENTS_SUSPENDED = 0x0UL;
}
@@ -213,6 +225,7 @@ uint8_t TwoWire::requestFrom(uint8_t address, size_t quantity, bool stopBit)
// on timeout nothing was actually clocked in - report 0 bytes rather than
// whatever stale RXD.AMOUNT is left over from a previous transaction
if (timed_out) {
twim_recover(_p_twim);
return 0;
}
@@ -276,7 +289,9 @@ uint8_t TwoWire::endTransmission(bool stopBit)
_p_twim->TASKS_STOP = 0x1UL;
wait_start = millis();
while(!_p_twim->EVENTS_STOPPED) {
if (millis() - wait_start > I2C_TWIM_TIMEOUT_MS) break;
// a STOP that never completes (SCL held low) is a failed transaction too:
// without this an empty probe on a stuck bus returned 0, "device ACKed"
if (millis() - wait_start > I2C_TWIM_TIMEOUT_MS) { timed_out = true; break; }
}
_p_twim->EVENTS_STOPPED = 0x0UL;
}
@@ -285,7 +300,7 @@ uint8_t TwoWire::endTransmission(bool stopBit)
_p_twim->TASKS_SUSPEND = 0x1UL;
wait_start = millis();
while(!_p_twim->EVENTS_SUSPENDED) {
if (millis() - wait_start > I2C_TWIM_TIMEOUT_MS) break;
if (millis() - wait_start > I2C_TWIM_TIMEOUT_MS) { timed_out = true; break; }
}
_p_twim->EVENTS_SUSPENDED = 0x0UL;
}
@@ -317,6 +332,7 @@ uint8_t TwoWire::endTransmission(bool stopBit)
// bus for a device that ACKed. See adafruit/Adafruit_nRF52_Arduino#771.
if (timed_out)
{
twim_recover(_p_twim);
return 4;
}