diff --git a/src/lgfx/v1/platforms/esp32/Bus_SPI.cpp b/src/lgfx/v1/platforms/esp32/Bus_SPI.cpp index 33f480de..15c5070b 100644 --- a/src/lgfx/v1/platforms/esp32/Bus_SPI.cpp +++ b/src/lgfx/v1/platforms/esp32/Bus_SPI.cpp @@ -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 @@ -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); diff --git a/src/lgfx/v1/platforms/esp32/common.cpp b/src/lgfx/v1/platforms/esp32/common.cpp index ff7f7713..b6511a07 100644 --- a/src/lgfx/v1/platforms/esp32/common.cpp +++ b/src/lgfx/v1/platforms/esp32/common.cpp @@ -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 @@ -600,7 +601,6 @@ namespace lgfx | SPI_CS5_DIS #endif ; - if (spi_mode & 2) pin = SPI_CK_IDLE_EDGE; beginTransaction(spi_host); @@ -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); } @@ -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(); @@ -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(); @@ -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)) { @@ -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);