File locking refactored (#631)

Remove FileMutex and wire locking into driver subsystems.
This commit is contained in:
Ken Van Hoeylandt
2026-08-28 01:06:53 +02:00
committed by GitHub
parent 92ca046681
commit 020aa471e2
44 changed files with 766 additions and 993 deletions
+49 -10
View File
@@ -32,6 +32,7 @@
static const uint8_t GAMMA_CURVE_VALUES[4] = { 0x01, 0x04, 0x02, 0x08 };
struct St7789Internal {
Device* spi_controller;
esp_lcd_panel_io_handle_t io_handle;
esp_lcd_panel_handle_t panel_handle;
// Given from ISR context by on_color_trans_done() once a queued SPI transfer physically
@@ -74,6 +75,7 @@ static error_t start(Device* device) {
return ERROR_OUT_OF_MEMORY;
}
internal->spi_controller = parent;
internal->draw_done_semaphore = xSemaphoreCreateBinary();
if (internal->draw_done_semaphore == nullptr) {
free(internal);
@@ -134,6 +136,7 @@ static error_t start(Device* device) {
// Bring-up sequence, order matches EspLcdDisplayV2::applyConfiguration (proven correct on real ST7789 panels).
// Every failure path below must clean up fully: unlike stop_device, this is never retried by the kernel
// if start_device fails (see device_start() in TactilityKernel), so a partial failure here would leak.
spi_controller_lock(internal->spi_controller);
bool ok =
esp_lcd_panel_reset(internal->panel_handle) == ESP_OK &&
esp_lcd_panel_init(internal->panel_handle) == ESP_OK &&
@@ -152,6 +155,7 @@ static error_t start(Device* device) {
ok = ok && (!config->invert_color || esp_lcd_panel_invert_color(internal->panel_handle, true) == ESP_OK);
ok = ok && (config->gamma_curve >= 4 || esp_lcd_panel_io_tx_param(internal->io_handle, LCD_CMD_GAMSET, &GAMMA_CURVE_VALUES[config->gamma_curve], 1) == ESP_OK);
ok = ok && esp_lcd_panel_disp_on_off(internal->panel_handle, true) == ESP_OK;
spi_controller_unlock(internal->spi_controller);
if (!ok) {
LOG_E(TAG, "Failed to bring up panel");
@@ -169,9 +173,11 @@ static error_t start(Device* device) {
static error_t stop(Device* device) {
auto* internal = static_cast<St7789Internal*>(device_get_driver_data(device));
spi_controller_lock(internal->spi_controller);
if (internal->panel_handle != nullptr) {
if (esp_lcd_panel_del(internal->panel_handle) != ESP_OK) {
LOG_E(TAG, "Failed to delete panel");
spi_controller_unlock(internal->spi_controller);
return ERROR_RESOURCE;
}
internal->panel_handle = nullptr;
@@ -180,10 +186,12 @@ static error_t stop(Device* device) {
if (internal->io_handle != nullptr) {
if (esp_lcd_panel_io_del(internal->io_handle) != ESP_OK) {
LOG_E(TAG, "Failed to delete panel IO");
spi_controller_unlock(internal->spi_controller);
return ERROR_RESOURCE;
}
internal->io_handle = nullptr;
}
spi_controller_unlock(internal->spi_controller);
vSemaphoreDelete(internal->draw_done_semaphore);
free(internal);
@@ -197,12 +205,18 @@ static error_t stop(Device* device) {
static error_t st7789_reset(Device* device) {
auto* internal = static_cast<St7789Internal*>(device_get_driver_data(device));
return esp_lcd_panel_reset(internal->panel_handle) == ESP_OK ? ERROR_NONE : ERROR_RESOURCE;
spi_controller_lock(internal->spi_controller);
error_t result = esp_lcd_panel_reset(internal->panel_handle) == ESP_OK ? ERROR_NONE : ERROR_RESOURCE;
spi_controller_unlock(internal->spi_controller);
return result;
}
static error_t st7789_init(Device* device) {
auto* internal = static_cast<St7789Internal*>(device_get_driver_data(device));
return esp_lcd_panel_init(internal->panel_handle) == ESP_OK ? ERROR_NONE : ERROR_RESOURCE;
spi_controller_lock(internal->spi_controller);
error_t result = esp_lcd_panel_init(internal->panel_handle) == ESP_OK ? ERROR_NONE : ERROR_RESOURCE;
spi_controller_unlock(internal->spi_controller);
return result;
}
static error_t st7789_draw_bitmap(Device* device, int32_t x_start, int32_t y_start, int32_t x_end, int32_t y_end, const void* color_data) {
@@ -213,26 +227,39 @@ static error_t st7789_draw_bitmap(Device* device, int32_t x_start, int32_t y_sta
// satisfied by this draw's own transfer completing.
xSemaphoreTake(internal->draw_done_semaphore, 0);
if (esp_lcd_panel_draw_bitmap(internal->panel_handle, x_start, y_start, x_end, y_end, color_data) != ESP_OK) {
spi_controller_lock(internal->spi_controller);
esp_err_t ret = esp_lcd_panel_draw_bitmap(internal->panel_handle, x_start, y_start, x_end, y_end, color_data);
if (ret != ESP_OK) {
spi_controller_unlock(internal->spi_controller);
return ERROR_RESOURCE;
}
// Block until the SPI transfer physically completes: DisplayApi's draw_bitmap is a synchronous
// contract (see lvgl_display.c), so the caller must be able to safely reuse/overwrite
// color_data as soon as this call returns. esp_lcd_panel_draw_bitmap() only queues the
// transfer and returns once it's handed to the SPI peripheral, not once it's finished.
// transfer and returns once it's handed to the SPI peripheral, not once it's finished. Hold the
// bus lock across the wait too, not just the queueing call: the command/data phases aren't
// wrapped in their own acquire_bus by esp_lcd_panel_io_spi, so another bus user could otherwise
// interleave with this transfer while it's still in flight.
xSemaphoreTake(internal->draw_done_semaphore, portMAX_DELAY);
spi_controller_unlock(internal->spi_controller);
return ERROR_NONE;
}
static error_t st7789_mirror(Device* device, bool x_axis, bool y_axis) {
auto* internal = static_cast<St7789Internal*>(device_get_driver_data(device));
return esp_lcd_panel_mirror(internal->panel_handle, x_axis, y_axis) == ESP_OK ? ERROR_NONE : ERROR_RESOURCE;
spi_controller_lock(internal->spi_controller);
error_t result = esp_lcd_panel_mirror(internal->panel_handle, x_axis, y_axis) == ESP_OK ? ERROR_NONE : ERROR_RESOURCE;
spi_controller_unlock(internal->spi_controller);
return result;
}
static error_t st7789_swap_xy(Device* device, bool swap_axes) {
auto* internal = static_cast<St7789Internal*>(device_get_driver_data(device));
return esp_lcd_panel_swap_xy(internal->panel_handle, swap_axes) == ESP_OK ? ERROR_NONE : ERROR_RESOURCE;
spi_controller_lock(internal->spi_controller);
error_t result = esp_lcd_panel_swap_xy(internal->panel_handle, swap_axes) == ESP_OK ? ERROR_NONE : ERROR_RESOURCE;
spi_controller_unlock(internal->spi_controller);
return result;
}
// Reads the devicetree-configured baseline, not live hardware state: swap_xy()/mirror() calls made after
@@ -251,7 +278,10 @@ static bool st7789_get_mirror_y(Device* device) {
static error_t st7789_set_gap(Device* device, int32_t x_gap, int32_t y_gap) {
auto* internal = static_cast<St7789Internal*>(device_get_driver_data(device));
return esp_lcd_panel_set_gap(internal->panel_handle, x_gap, y_gap) == ESP_OK ? ERROR_NONE : ERROR_RESOURCE;
spi_controller_lock(internal->spi_controller);
error_t result = esp_lcd_panel_set_gap(internal->panel_handle, x_gap, y_gap) == ESP_OK ? ERROR_NONE : ERROR_RESOURCE;
spi_controller_unlock(internal->spi_controller);
return result;
}
// Reads the devicetree-configured baseline, not live hardware state - see DisplayApi::get_gap_x().
@@ -271,17 +301,26 @@ static int32_t st7789_get_gap_y(Device* device) {
static error_t st7789_invert_color(Device* device, bool invert_color_data) {
auto* internal = static_cast<St7789Internal*>(device_get_driver_data(device));
return esp_lcd_panel_invert_color(internal->panel_handle, invert_color_data) == ESP_OK ? ERROR_NONE : ERROR_RESOURCE;
spi_controller_lock(internal->spi_controller);
error_t result = esp_lcd_panel_invert_color(internal->panel_handle, invert_color_data) == ESP_OK ? ERROR_NONE : ERROR_RESOURCE;
spi_controller_unlock(internal->spi_controller);
return result;
}
static error_t st7789_disp_on_off(Device* device, bool on_off) {
auto* internal = static_cast<St7789Internal*>(device_get_driver_data(device));
return esp_lcd_panel_disp_on_off(internal->panel_handle, on_off) == ESP_OK ? ERROR_NONE : ERROR_RESOURCE;
spi_controller_lock(internal->spi_controller);
error_t result = esp_lcd_panel_disp_on_off(internal->panel_handle, on_off) == ESP_OK ? ERROR_NONE : ERROR_RESOURCE;
spi_controller_unlock(internal->spi_controller);
return result;
}
static error_t st7789_disp_sleep(Device* device, bool sleep) {
auto* internal = static_cast<St7789Internal*>(device_get_driver_data(device));
return esp_lcd_panel_disp_sleep(internal->panel_handle, sleep) == ESP_OK ? ERROR_NONE : ERROR_RESOURCE;
spi_controller_lock(internal->spi_controller);
error_t result = esp_lcd_panel_disp_sleep(internal->panel_handle, sleep) == ESP_OK ? ERROR_NONE : ERROR_RESOURCE;
spi_controller_unlock(internal->spi_controller);
return result;
}
static enum DisplayColorFormat st7789_get_color_format(Device* device) {