Wire (nRF52): sync with adafruit#873 head (d5ec47a5)
Build / build (cluenrf52840) (push) Failing after 9s
Build / build (feather52832) (push) Failing after 21s
Build / build (feather52840sense) (push) Failing after 21s
Build / build (feather52840) (push) Failing after 22s
Build / build (feather_nrf52840_sense_tft) (push) Failing after 24s
Build / build (cplaynrf52840) (push) Failing after 25s
Build / build (itsybitsy52840) (push) Failing after 29s
Build / build (ledglasses_nrf52840) (push) Failing after 29s
Build / build (cluenrf52840) (push) Failing after 9s
Build / build (feather52832) (push) Failing after 21s
Build / build (feather52840sense) (push) Failing after 21s
Build / build (feather52840) (push) Failing after 22s
Build / build (feather_nrf52840_sense_tft) (push) Failing after 24s
Build / build (cplaynrf52840) (push) Failing after 25s
Build / build (itsybitsy52840) (push) Failing after 29s
Build / build (ledglasses_nrf52840) (push) Failing after 29s
The PR took our timeout/recovery fix (fca5ff3c) and added: recovery also
after a NACK latched in the same call, twim_recover() clears the START/
LAST events and ERRORSRC, requestFrom() clears ERRORSRC, and the waits
count FreeRTOS ticks instead of millis(). Bench x4 (XIAO + INA226): free
bus, stuck SCL and recovery without reboot as before; requestFrom after
a NACKed probe reads the INA's ID.
This commit is contained in:
1 parent
fca5ff3c71
commit
926a22c847
1 file changed
+50
-16
@@ -39,15 +39,39 @@ extern "C" {
|
||||
#define I2C_TWIM_TIMEOUT_MS 100
|
||||
#endif
|
||||
|
||||
// Deadline check on raw FreeRTOS ticks. millis() is tick2ms(ticks) and wraps at
|
||||
// 4194304000 ms (2^32 ticks at 1024 Hz), not at 2^32, so a millis() difference is
|
||||
// wrong across that wrap (a healthy transfer would be reported as timed out). The
|
||||
// 32-bit tick difference is correct across the tick counter's own wrap.
|
||||
static inline bool twim_wait_expired(uint32_t start_tick)
|
||||
{
|
||||
return (uint32_t)(xTaskGetTickCount() - start_tick) > ms2tick(I2C_TWIM_TIMEOUT_MS);
|
||||
}
|
||||
|
||||
// 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.
|
||||
//
|
||||
// Note: the PS asks software to wait for EVENTS_STOPPED after TASKS_STOP before
|
||||
// writing ENABLE=0. That holds here whenever the STOP wait completed. When the
|
||||
// STOP/SUSPEND wait itself timed out, STOPPED never arrived (SCL held low), so the
|
||||
// condition cannot be met and the PS documents no abort sequence for that state.
|
||||
// The alternative was a TWIM that stayed wedged until reboot. EasyDMA only targets
|
||||
// this driver's own txBuffer/rxBuffer (reset at the start of every transaction),
|
||||
// never caller memory.
|
||||
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;
|
||||
// LASTTX/LASTRX can be raised after the caller cleared them (bus released during
|
||||
// the STOP wait); a stale one would cut the next transfer short.
|
||||
twim->EVENTS_TXSTARTED = 0x0UL;
|
||||
twim->EVENTS_RXSTARTED = 0x0UL;
|
||||
twim->EVENTS_LASTTX = 0x0UL;
|
||||
twim->EVENTS_LASTRX = 0x0UL;
|
||||
twim->ERRORSRC = twim->ERRORSRC; // write-1-to-clear
|
||||
twim->ENABLE = (TWIM_ENABLE_ENABLE_Enabled << TWIM_ENABLE_ENABLE_Pos);
|
||||
}
|
||||
|
||||
@@ -184,16 +208,16 @@ uint8_t TwoWire::requestFrom(uint8_t address, size_t quantity, bool stopBit)
|
||||
_p_twim->TASKS_STARTRX = 0x1UL;
|
||||
|
||||
bool timed_out = false;
|
||||
uint32_t wait_start = millis();
|
||||
uint32_t wait_start = xTaskGetTickCount();
|
||||
while(!_p_twim->EVENTS_RXSTARTED && !_p_twim->EVENTS_ERROR) {
|
||||
if (millis() - wait_start > I2C_TWIM_TIMEOUT_MS) { timed_out = true; break; }
|
||||
if (twim_wait_expired(wait_start)) { timed_out = true; break; }
|
||||
}
|
||||
_p_twim->EVENTS_RXSTARTED = 0x0UL;
|
||||
|
||||
if (!timed_out) {
|
||||
wait_start = millis();
|
||||
wait_start = xTaskGetTickCount();
|
||||
while(!_p_twim->EVENTS_LASTRX && !_p_twim->EVENTS_ERROR) {
|
||||
if (millis() - wait_start > I2C_TWIM_TIMEOUT_MS) { timed_out = true; break; }
|
||||
if (twim_wait_expired(wait_start)) { timed_out = true; break; }
|
||||
}
|
||||
}
|
||||
_p_twim->EVENTS_LASTRX = 0x0UL;
|
||||
@@ -201,18 +225,18 @@ uint8_t TwoWire::requestFrom(uint8_t address, size_t quantity, bool stopBit)
|
||||
if (stopBit || _p_twim->EVENTS_ERROR || timed_out)
|
||||
{
|
||||
_p_twim->TASKS_STOP = 0x1UL;
|
||||
wait_start = millis();
|
||||
wait_start = xTaskGetTickCount();
|
||||
while(!_p_twim->EVENTS_STOPPED) {
|
||||
if (millis() - wait_start > I2C_TWIM_TIMEOUT_MS) { timed_out = true; break; }
|
||||
if (twim_wait_expired(wait_start)) { timed_out = true; break; }
|
||||
}
|
||||
_p_twim->EVENTS_STOPPED = 0x0UL;
|
||||
}
|
||||
else
|
||||
{
|
||||
_p_twim->TASKS_SUSPEND = 0x1UL;
|
||||
wait_start = millis();
|
||||
wait_start = xTaskGetTickCount();
|
||||
while(!_p_twim->EVENTS_SUSPENDED) {
|
||||
if (millis() - wait_start > I2C_TWIM_TIMEOUT_MS) { timed_out = true; break; }
|
||||
if (twim_wait_expired(wait_start)) { timed_out = true; break; }
|
||||
}
|
||||
_p_twim->EVENTS_SUSPENDED = 0x0UL;
|
||||
}
|
||||
@@ -220,6 +244,9 @@ uint8_t TwoWire::requestFrom(uint8_t address, size_t quantity, bool stopBit)
|
||||
if (_p_twim->EVENTS_ERROR)
|
||||
{
|
||||
_p_twim->EVENTS_ERROR = 0x0UL;
|
||||
// clear the sticky error bits too, or a stale one makes a later
|
||||
// endTransmission() misclassify ANACK/DNACK (it compares with ==)
|
||||
_p_twim->ERRORSRC = _p_twim->ERRORSRC;
|
||||
}
|
||||
|
||||
// on timeout nothing was actually clocked in - report 0 bytes rather than
|
||||
@@ -270,16 +297,16 @@ uint8_t TwoWire::endTransmission(bool stopBit)
|
||||
_p_twim->TASKS_STARTTX = 0x1UL;
|
||||
|
||||
bool timed_out = false;
|
||||
uint32_t wait_start = millis();
|
||||
uint32_t wait_start = xTaskGetTickCount();
|
||||
while(!_p_twim->EVENTS_TXSTARTED && !_p_twim->EVENTS_ERROR) {
|
||||
if (millis() - wait_start > I2C_TWIM_TIMEOUT_MS) { timed_out = true; break; }
|
||||
if (twim_wait_expired(wait_start)) { timed_out = true; break; }
|
||||
}
|
||||
_p_twim->EVENTS_TXSTARTED = 0x0UL;
|
||||
|
||||
if (!timed_out && txBuffer.available()) {
|
||||
wait_start = millis();
|
||||
wait_start = xTaskGetTickCount();
|
||||
while(!_p_twim->EVENTS_LASTTX && !_p_twim->EVENTS_ERROR) {
|
||||
if (millis() - wait_start > I2C_TWIM_TIMEOUT_MS) { timed_out = true; break; }
|
||||
if (twim_wait_expired(wait_start)) { timed_out = true; break; }
|
||||
}
|
||||
}
|
||||
_p_twim->EVENTS_LASTTX = 0x0UL;
|
||||
@@ -287,20 +314,20 @@ uint8_t TwoWire::endTransmission(bool stopBit)
|
||||
if (stopBit || _p_twim->EVENTS_ERROR || timed_out)
|
||||
{
|
||||
_p_twim->TASKS_STOP = 0x1UL;
|
||||
wait_start = millis();
|
||||
wait_start = xTaskGetTickCount();
|
||||
while(!_p_twim->EVENTS_STOPPED) {
|
||||
// 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; }
|
||||
if (twim_wait_expired(wait_start)) { timed_out = true; break; }
|
||||
}
|
||||
_p_twim->EVENTS_STOPPED = 0x0UL;
|
||||
}
|
||||
else
|
||||
{
|
||||
_p_twim->TASKS_SUSPEND = 0x1UL;
|
||||
wait_start = millis();
|
||||
wait_start = xTaskGetTickCount();
|
||||
while(!_p_twim->EVENTS_SUSPENDED) {
|
||||
if (millis() - wait_start > I2C_TWIM_TIMEOUT_MS) { timed_out = true; break; }
|
||||
if (twim_wait_expired(wait_start)) { timed_out = true; break; }
|
||||
}
|
||||
_p_twim->EVENTS_SUSPENDED = 0x0UL;
|
||||
}
|
||||
@@ -313,6 +340,13 @@ uint8_t TwoWire::endTransmission(bool stopBit)
|
||||
|
||||
_p_twim->ERRORSRC = error;
|
||||
|
||||
// a real error was seen, but a wait also timed out: the TWIM may be wedged,
|
||||
// so recover before returning the NACK code (error is already latched above)
|
||||
if (timed_out)
|
||||
{
|
||||
twim_recover(_p_twim);
|
||||
}
|
||||
|
||||
if (error == TWIM_ERRORSRC_ANACK_Msk)
|
||||
{
|
||||
return 2;
|
||||
|
||||
Reference in new issue
Block a user