Wire (nRF52): add timeout to endTransmission()/requestFrom() poll loops
endTransmission() and requestFrom() both spin on unbounded while(!EVENT) loops waiting on the TWIM peripheral. Several of these loops - notably the EVENTS_STOPPED wait - have no EVENTS_ERROR escape at all, so if the hardware never raises the expected event (e.g. probing an I2C address on a bus with nothing attached and no pull-ups), the call never returns and the whole single-threaded firmware hangs forever. Reported in #771 with the same root cause already identified in the issue thread. Also affects boards that probe for an optional/absent I2C device at boot before anything else initializes - e.g. MeshCore's Xiao_nrf52 target, which runs an RTC autodiscovery scan in radio_init() and hangs on every cold boot when no RTC is wired up (no serial, no BLE - see meshcore-dev/MeshCore#2068). Adds a bounded I2C_TWIM_TIMEOUT_MS (default 100ms, overridable) to every wait loop in both functions. A timeout that occurs before the hardware transaction actually completed is surfaced as a proper error return (endTransmission: 4 "other error"; requestFrom: 0 bytes read) rather than silently falling through to a success path - a timeout on the address/data phase must not be reported as if a device ACKed. Tested on a Seeed XIAO nRF52840 + Wio-SX1262 board that reliably hung at boot (via MeshCore's RTC autodiscovery probe) after every power cycle. With this patch the same probe now completes (with a timeout) in well under a second per address instead of hanging indefinitely, and the board boots and runs normally.
This commit is contained in:
1 parent
780820646f
commit
45188a647f
1 file changed
+61
-11
@@ -31,6 +31,14 @@ extern "C" {
|
||||
|
||||
#include <Adafruit_TinyUSB.h> // for Serial
|
||||
|
||||
// Guards against a permanent MCU hang in the EVENTS_* poll loops below: on some
|
||||
// boards, probing an I2C address with nothing attached (or without pull-ups)
|
||||
// can leave the TWIM peripheral never raising the expected event or EVENTS_ERROR,
|
||||
// so an unbounded while(!event) spins forever. See adafruit/Adafruit_nRF52_Arduino#771.
|
||||
#ifndef I2C_TWIM_TIMEOUT_MS
|
||||
#define I2C_TWIM_TIMEOUT_MS 100
|
||||
#endif
|
||||
|
||||
static volatile uint32_t* pincfg_reg(uint32_t pin)
|
||||
{
|
||||
NRF_GPIO_Type * port = nrf_gpio_pin_port_decode(&pin);
|
||||
@@ -163,22 +171,37 @@ uint8_t TwoWire::requestFrom(uint8_t address, size_t quantity, bool stopBit)
|
||||
_p_twim->RXD.MAXCNT = quantity;
|
||||
_p_twim->TASKS_STARTRX = 0x1UL;
|
||||
|
||||
while(!_p_twim->EVENTS_RXSTARTED && !_p_twim->EVENTS_ERROR);
|
||||
bool timed_out = false;
|
||||
uint32_t wait_start = millis();
|
||||
while(!_p_twim->EVENTS_RXSTARTED && !_p_twim->EVENTS_ERROR) {
|
||||
if (millis() - wait_start > I2C_TWIM_TIMEOUT_MS) { timed_out = true; break; }
|
||||
}
|
||||
_p_twim->EVENTS_RXSTARTED = 0x0UL;
|
||||
|
||||
while(!_p_twim->EVENTS_LASTRX && !_p_twim->EVENTS_ERROR);
|
||||
if (!timed_out) {
|
||||
wait_start = millis();
|
||||
while(!_p_twim->EVENTS_LASTRX && !_p_twim->EVENTS_ERROR) {
|
||||
if (millis() - wait_start > I2C_TWIM_TIMEOUT_MS) { timed_out = true; break; }
|
||||
}
|
||||
}
|
||||
_p_twim->EVENTS_LASTRX = 0x0UL;
|
||||
|
||||
if (stopBit || _p_twim->EVENTS_ERROR)
|
||||
if (stopBit || _p_twim->EVENTS_ERROR || timed_out)
|
||||
{
|
||||
_p_twim->TASKS_STOP = 0x1UL;
|
||||
while(!_p_twim->EVENTS_STOPPED);
|
||||
wait_start = millis();
|
||||
while(!_p_twim->EVENTS_STOPPED) {
|
||||
if (millis() - wait_start > I2C_TWIM_TIMEOUT_MS) break;
|
||||
}
|
||||
_p_twim->EVENTS_STOPPED = 0x0UL;
|
||||
}
|
||||
else
|
||||
{
|
||||
_p_twim->TASKS_SUSPEND = 0x1UL;
|
||||
while(!_p_twim->EVENTS_SUSPENDED);
|
||||
wait_start = millis();
|
||||
while(!_p_twim->EVENTS_SUSPENDED) {
|
||||
if (millis() - wait_start > I2C_TWIM_TIMEOUT_MS) break;
|
||||
}
|
||||
_p_twim->EVENTS_SUSPENDED = 0x0UL;
|
||||
}
|
||||
|
||||
@@ -187,6 +210,12 @@ uint8_t TwoWire::requestFrom(uint8_t address, size_t quantity, bool stopBit)
|
||||
_p_twim->EVENTS_ERROR = 0x0UL;
|
||||
}
|
||||
|
||||
// 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) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
byteRead = rxBuffer._iHead = _p_twim->RXD.AMOUNT;
|
||||
|
||||
return byteRead;
|
||||
@@ -227,24 +256,37 @@ uint8_t TwoWire::endTransmission(bool stopBit)
|
||||
|
||||
_p_twim->TASKS_STARTTX = 0x1UL;
|
||||
|
||||
while(!_p_twim->EVENTS_TXSTARTED && !_p_twim->EVENTS_ERROR);
|
||||
bool timed_out = false;
|
||||
uint32_t wait_start = millis();
|
||||
while(!_p_twim->EVENTS_TXSTARTED && !_p_twim->EVENTS_ERROR) {
|
||||
if (millis() - wait_start > I2C_TWIM_TIMEOUT_MS) { timed_out = true; break; }
|
||||
}
|
||||
_p_twim->EVENTS_TXSTARTED = 0x0UL;
|
||||
|
||||
if (txBuffer.available()) {
|
||||
while(!_p_twim->EVENTS_LASTTX && !_p_twim->EVENTS_ERROR);
|
||||
if (!timed_out && txBuffer.available()) {
|
||||
wait_start = millis();
|
||||
while(!_p_twim->EVENTS_LASTTX && !_p_twim->EVENTS_ERROR) {
|
||||
if (millis() - wait_start > I2C_TWIM_TIMEOUT_MS) { timed_out = true; break; }
|
||||
}
|
||||
}
|
||||
_p_twim->EVENTS_LASTTX = 0x0UL;
|
||||
|
||||
if (stopBit || _p_twim->EVENTS_ERROR)
|
||||
if (stopBit || _p_twim->EVENTS_ERROR || timed_out)
|
||||
{
|
||||
_p_twim->TASKS_STOP = 0x1UL;
|
||||
while(!_p_twim->EVENTS_STOPPED);
|
||||
wait_start = millis();
|
||||
while(!_p_twim->EVENTS_STOPPED) {
|
||||
if (millis() - wait_start > I2C_TWIM_TIMEOUT_MS) break;
|
||||
}
|
||||
_p_twim->EVENTS_STOPPED = 0x0UL;
|
||||
}
|
||||
else
|
||||
{
|
||||
_p_twim->TASKS_SUSPEND = 0x1UL;
|
||||
while(!_p_twim->EVENTS_SUSPENDED);
|
||||
wait_start = millis();
|
||||
while(!_p_twim->EVENTS_SUSPENDED) {
|
||||
if (millis() - wait_start > I2C_TWIM_TIMEOUT_MS) break;
|
||||
}
|
||||
_p_twim->EVENTS_SUSPENDED = 0x0UL;
|
||||
}
|
||||
|
||||
@@ -270,6 +312,14 @@ uint8_t TwoWire::endTransmission(bool stopBit)
|
||||
}
|
||||
}
|
||||
|
||||
// hardware never raised EVENTS_ERROR but also never completed the transaction -
|
||||
// report it as a failure so callers (e.g. an I2C bus scan) don't mistake a dead
|
||||
// bus for a device that ACKed. See adafruit/Adafruit_nRF52_Arduino#771.
|
||||
if (timed_out)
|
||||
{
|
||||
return 4;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user