Skip to content

Commit

Permalink
prevent device block in SPI_MUTEX_LOCK() at least on ESP32
Browse files Browse the repository at this point in the history
ESP32 uses a MUTEX to handle SPI transactions.
So calling beginTransaction twice in a row will block the device.
This happens in current code, if SRAM is used or a display (like 2,7" tricolor IL91874) which uses singleByteTxns = true
Both will call beginTransaction twice in a row and then the device will block for ever in
cores\esp32\esp32-hal-spi.h SPI_MUTEX_LOCK();

To prevent this, I made some changes in Adafruit_EPD.cpp
to track _isInTransaction properly, which was not used before.

See also adafruit/Adafruit-PN532#53 for a similar bug.
  • Loading branch information
Hutch67 authored Dec 30, 2020
1 parent b5b6fcf commit 7833cf1
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions src/Adafruit_EPD.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,7 @@ void Adafruit_EPD::writeSRAMFramebufferToEPD(uint16_t SRAM_buffer_addr,

// use SRAM
sram.csLow();
_isInTransaction = true;
// send read command
SPItransfer(MCPSRAM_READ);
// send address
Expand All @@ -334,6 +335,7 @@ void Adafruit_EPD::writeSRAMFramebufferToEPD(uint16_t SRAM_buffer_addr,
}
csHigh();
sram.csHigh();
_isInTransaction = false;
}

/**************************************************************************/
Expand Down Expand Up @@ -646,8 +648,10 @@ void Adafruit_EPD::csHigh() {
digitalWrite(_cs_pin, HIGH);
#endif

spi_dev->endTransaction();
_isInTransaction = false;
if (_isInTransaction) {
spi_dev->endTransaction();
_isInTransaction = false;
}
}

/**************************************************************************/
Expand All @@ -656,8 +660,11 @@ void Adafruit_EPD::csHigh() {
*/
/**************************************************************************/
void Adafruit_EPD::csLow() {
spi_dev->beginTransaction();
_isInTransaction = true;

if (!_isInTransaction) {
spi_dev->beginTransaction();
_isInTransaction = true;
}

#ifdef BUSIO_USE_FAST_PINIO
*csPort &= ~csPinMask;
Expand Down

0 comments on commit 7833cf1

Please sign in to comment.