Skip to content

Commit

Permalink
Improved i2c stability for ESP32.
Browse files Browse the repository at this point in the history
  • Loading branch information
lovyan03 committed Sep 2, 2024
1 parent 3e449a9 commit 0e42974
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 23 deletions.
4 changes: 2 additions & 2 deletions src/lgfx/v1/platforms/esp32/Bus_SPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,8 @@ namespace lgfx
}

auto spi_mode = _cfg.spi_mode;
uint32_t pin = 0
uint32_t pin = (spi_mode & 2) ? SPI_CK_IDLE_EDGE : 0;
pin = pin
#if defined ( SPI_CS0_DIS )
| SPI_CS0_DIS
#endif
Expand All @@ -229,7 +230,6 @@ namespace lgfx
| SPI_CS5_DIS
#endif
;
if (spi_mode & 2) pin = SPI_CK_IDLE_EDGE;

if (_cfg.use_lock) spi::beginTransaction(_cfg.spi_host);

Expand Down
36 changes: 15 additions & 21 deletions src/lgfx/v1/platforms/esp32/common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -580,7 +580,8 @@ namespace lgfx

uint32_t user = SPI_USR_MOSI | SPI_USR_MISO | SPI_DOUTDIN;
if (spi_mode == 1 || spi_mode == 2) user |= SPI_CK_OUT_EDGE;
uint32_t pin = 0
uint32_t pin = (spi_mode & 2) ? SPI_CK_IDLE_EDGE : 0;
pin = pin
#if defined ( SPI_CS0_DIS )
| SPI_CS0_DIS
#endif
Expand All @@ -600,7 +601,6 @@ namespace lgfx
| SPI_CS5_DIS
#endif
;
if (spi_mode & 2) pin = SPI_CK_IDLE_EDGE;

beginTransaction(spi_host);

Expand Down Expand Up @@ -630,13 +630,7 @@ namespace lgfx

void endTransaction(int spi_host, int spi_cs)
{
if (_spi_dev_handle[spi_host]) {
#if defined (ARDUINO) // Arduino ESP32
spiEndTransaction(_spi_handle[spi_host]);
#else // ESP-IDF
spi_device_release_bus(_spi_dev_handle[spi_host]);
#endif
}
endTransaction(spi_host);
gpio_hi(spi_cs);
}

Expand Down Expand Up @@ -943,9 +937,8 @@ namespace lgfx
#else
uint32_t us_limit = (dev->scl_high_period.period + dev->scl_low_period.period + 16 ) * (1 + dev->status_reg.tx_fifo_cnt);
#endif
if (i2c_context[i2c_port].wait_ack_stage == 2) {
us_limit += 1024;
}
us_limit += 512 << i2c_context[i2c_port].wait_ack_stage;

do
{
taskYIELD();
Expand Down Expand Up @@ -983,6 +976,7 @@ namespace lgfx
i2c_set_cmd(dev, 0, i2c_cmd_stop, 0);
i2c_set_cmd(dev, 1, i2c_cmd_end, 0);
static constexpr uint32_t intmask_ = I2C_ACK_ERR_INT_RAW_M | I2C_TIME_OUT_INT_RAW_M | I2C_END_DETECT_INT_RAW_M | I2C_ARBITRATION_LOST_INT_RAW_M | I2C_TRANS_COMPLETE_INT_RAW_M;
updateDev(dev);
dev->int_clr.val = intmask_;
dev->ctr.trans_start = 1;
uint32_t ms = lgfx::millis();
Expand Down Expand Up @@ -1416,19 +1410,19 @@ namespace lgfx
dev->int_clr.val = intmask;
dev->ctr.trans_start = 1;

uint32_t us = lgfx::micros();
taskYIELD();
int delayus = ((us_limit + 2) >> 2) - (lgfx::micros() - us);
if (delayus > 0) {
delayMicroseconds(delayus);
}
do
{
uint32_t us = lgfx::micros();
taskYIELD();
us = lgfx::micros() - us;
int delayus = ((us_limit + 2) >> 2) - us;
if (delayus > 0) {
delayMicroseconds(delayus);
}
us = lgfx::micros();
do
{
taskYIELD();
} while (0 == getRxFifoCount(dev) && !(dev->int_raw.val & intmask) && ((lgfx::micros() - us) <= us_limit + 1024));
} while ((len>>1) >= getRxFifoCount(dev) && !(dev->int_raw.val & intmask) && ((lgfx::micros() - us) <= us_limit + 1024));

if (0 == getRxFifoCount(dev))
{
Expand All @@ -1439,7 +1433,7 @@ namespace lgfx
i2c_context[i2c_port].wait_ack_stage = 0;
return res;
}
*readdata++ = *fifo_addr; //dev->fifo_data.data;
*readdata++ = *fifo_addr;
} while (--len);
} while (length);

Expand Down

0 comments on commit 0e42974

Please sign in to comment.