Skip to content

Commit

Permalink
Added exclusive processing at the start and end of I2C communication
Browse files Browse the repository at this point in the history
  • Loading branch information
lovyan03 committed Sep 21, 2023
1 parent f409165 commit b7baca5
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/lgfx/v1/platforms/esp32/common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ Original Source:
#include <string.h>
#include <math.h>

#include <freertos/FreeRTOS.h>
#include <freertos/semphr.h>

#include <driver/i2c.h>
#include <driver/spi_common.h>
#include <driver/spi_master.h>
Expand Down Expand Up @@ -712,6 +715,19 @@ namespace lgfx
};
cpp::result<state_t, error_t> 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.
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit b7baca5

Please sign in to comment.