diff --git a/src/lgfx/v1/platforms/esp32/common.cpp b/src/lgfx/v1/platforms/esp32/common.cpp index 5c0e8ba7..0b98ddb2 100644 --- a/src/lgfx/v1/platforms/esp32/common.cpp +++ b/src/lgfx/v1/platforms/esp32/common.cpp @@ -29,6 +29,9 @@ Original Source: #include #include +#include +#include + #include #include #include @@ -712,6 +715,19 @@ namespace lgfx }; cpp::result state; + xSemaphoreHandle mtx = nullptr; + + void lock(uint32_t msec = portMAX_DELAY) { + if (mtx == nullptr) { + mtx = xSemaphoreCreateMutex(); + } + xSemaphoreTake(mtx, msec); + } + + void unlock(void) { + xSemaphoreGive(mtx); + } + gpio_num_t pin_scl = (gpio_num_t)-1; gpio_num_t pin_sda = (gpio_num_t)-1; uint8_t wait_ack_stage = 0; // 0:Not waiting. / 1:Waiting after addressing. / 2:Waiting during data transmission. @@ -954,6 +970,7 @@ namespace lgfx { i2c_context[i2c_port].state = i2c_context_t::state_t::state_disconnect; } + i2c_context[i2c_port].unlock(); } return res; } @@ -1211,6 +1228,7 @@ namespace lgfx //ESP_LOGI("LGFX", "i2c::beginTransaction : port:%d / addr:%02x / freq:%d / rw:%d", i2c_port, i2c_addr, freq, read); auto dev = getDev(i2c_port); + i2c_context[i2c_port].lock(); #if defined ( CONFIG_IDF_TARGET_ESP32C3 ) || defined ( CONFIG_IDF_TARGET_ESP32S3 ) if (dev->sr.bus_busy)