Skip to content

Commit

Permalink
fix(LVGL port): Wait for stop task when deinit.
Browse files Browse the repository at this point in the history
  • Loading branch information
espzav committed May 9, 2024
1 parent 1fa92b6 commit 57f01d4
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 6 deletions.
1 change: 1 addition & 0 deletions components/esp_lvgl_port/include/esp_lvgl_port.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ esp_err_t lvgl_port_init(const lvgl_port_cfg_t *cfg);
*
* @return
* - ESP_OK on success
* - ESP_ERR_TIMEOUT when stopping the LVGL task times out
*/
esp_err_t lvgl_port_deinit(void);

Expand Down
31 changes: 28 additions & 3 deletions components/esp_lvgl_port/src/lvgl8/esp_lvgl_port.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,15 @@

static const char *TAG = "LVGL";

#define ESP_LVGL_PORT_TASK_MUX_DELAY_MS 10000

/*******************************************************************************
* Types definitions
*******************************************************************************/

typedef struct lvgl_port_ctx_s {
SemaphoreHandle_t lvgl_mux;
SemaphoreHandle_t task_mux;
esp_timer_handle_t tick_timer;
bool running;
int task_max_sleep_ms;
Expand Down Expand Up @@ -63,8 +66,12 @@ esp_err_t lvgl_port_init(const lvgl_port_cfg_t *cfg)
if (lvgl_port_ctx.task_max_sleep_ms == 0) {
lvgl_port_ctx.task_max_sleep_ms = 500;
}
/* LVGL semaphore */
lvgl_port_ctx.lvgl_mux = xSemaphoreCreateRecursiveMutex();
ESP_GOTO_ON_FALSE(lvgl_port_ctx.lvgl_mux, ESP_ERR_NO_MEM, err, TAG, "Create LVGL mutex fail!");
/* Task semaphore */
lvgl_port_ctx.task_mux = xSemaphoreCreateMutex();
ESP_GOTO_ON_FALSE(lvgl_port_ctx.task_mux, ESP_ERR_NO_MEM, err, TAG, "Create LVGL task sem fail!");

BaseType_t res;
if (cfg->task_affinity < 0) {
Expand Down Expand Up @@ -118,10 +125,17 @@ esp_err_t lvgl_port_deinit(void)
/* Stop running task */
if (lvgl_port_ctx.running) {
lvgl_port_ctx.running = false;
} else {
lvgl_port_task_deinit();
}

/* Wait for stop task */
if (xSemaphoreTake(lvgl_port_ctx.task_mux, pdMS_TO_TICKS(ESP_LVGL_PORT_TASK_MUX_DELAY_MS)) != pdTRUE) {
ESP_LOGE(TAG, "Failed to stop LVGL task");
return ESP_ERR_TIMEOUT;
}
ESP_LOGI(TAG, "Stopped LVGL task");

lvgl_port_task_deinit();

return ESP_OK;
}

Expand All @@ -147,6 +161,13 @@ static void lvgl_port_task(void *arg)
{
uint32_t task_delay_ms = lvgl_port_ctx.task_max_sleep_ms;

/* Take the task semaphore */
if (xSemaphoreTake(lvgl_port_ctx.task_mux, 0) != pdTRUE) {
ESP_LOGE(TAG, "Failed to take LVGL task sem");
lvgl_port_task_deinit();
vTaskDelete( NULL );
}

ESP_LOGI(TAG, "Starting LVGL task");
lvgl_port_ctx.running = true;
while (lvgl_port_ctx.running) {
Expand All @@ -162,7 +183,8 @@ static void lvgl_port_task(void *arg)
vTaskDelay(pdMS_TO_TICKS(task_delay_ms));
}

lvgl_port_task_deinit();
/* Give semaphore back */
xSemaphoreGive(lvgl_port_ctx.task_mux);

/* Close task */
vTaskDelete( NULL );
Expand All @@ -173,6 +195,9 @@ static void lvgl_port_task_deinit(void)
if (lvgl_port_ctx.lvgl_mux) {
vSemaphoreDelete(lvgl_port_ctx.lvgl_mux);
}
if (lvgl_port_ctx.task_mux) {
vSemaphoreDelete(lvgl_port_ctx.task_mux);
}
memset(&lvgl_port_ctx, 0, sizeof(lvgl_port_ctx));
#if LV_ENABLE_GC || !LV_MEM_CUSTOM
/* Deinitialize LVGL */
Expand Down
31 changes: 28 additions & 3 deletions components/esp_lvgl_port/src/lvgl9/esp_lvgl_port.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,15 @@

static const char *TAG = "LVGL";

#define ESP_LVGL_PORT_TASK_MUX_DELAY_MS 10000

/*******************************************************************************
* Types definitions
*******************************************************************************/

typedef struct lvgl_port_ctx_s {
SemaphoreHandle_t lvgl_mux;
SemaphoreHandle_t task_mux;
esp_timer_handle_t tick_timer;
bool running;
int task_max_sleep_ms;
Expand Down Expand Up @@ -63,8 +66,12 @@ esp_err_t lvgl_port_init(const lvgl_port_cfg_t *cfg)
if (lvgl_port_ctx.task_max_sleep_ms == 0) {
lvgl_port_ctx.task_max_sleep_ms = 500;
}
/* LVGL semaphore */
lvgl_port_ctx.lvgl_mux = xSemaphoreCreateRecursiveMutex();
ESP_GOTO_ON_FALSE(lvgl_port_ctx.lvgl_mux, ESP_ERR_NO_MEM, err, TAG, "Create LVGL mutex fail!");
/* Task semaphore */
lvgl_port_ctx.task_mux = xSemaphoreCreateMutex();
ESP_GOTO_ON_FALSE(lvgl_port_ctx.task_mux, ESP_ERR_NO_MEM, err, TAG, "Create LVGL task sem fail!");

BaseType_t res;
if (cfg->task_affinity < 0) {
Expand Down Expand Up @@ -118,10 +125,17 @@ esp_err_t lvgl_port_deinit(void)
/* Stop running task */
if (lvgl_port_ctx.running) {
lvgl_port_ctx.running = false;
} else {
lvgl_port_task_deinit();
}

/* Wait for stop task */
if (xSemaphoreTake(lvgl_port_ctx.task_mux, pdMS_TO_TICKS(ESP_LVGL_PORT_TASK_MUX_DELAY_MS)) != pdTRUE) {
ESP_LOGE(TAG, "Failed to stop LVGL task");
return ESP_ERR_TIMEOUT;
}
ESP_LOGI(TAG, "Stopped LVGL task");

lvgl_port_task_deinit();

return ESP_OK;
}

Expand All @@ -147,6 +161,13 @@ static void lvgl_port_task(void *arg)
{
uint32_t task_delay_ms = lvgl_port_ctx.task_max_sleep_ms;

/* Take the task semaphore */
if (xSemaphoreTake(lvgl_port_ctx.task_mux, 0) != pdTRUE) {
ESP_LOGE(TAG, "Failed to take LVGL task sem");
lvgl_port_task_deinit();
vTaskDelete( NULL );
}

ESP_LOGI(TAG, "Starting LVGL task");
lvgl_port_ctx.running = true;
while (lvgl_port_ctx.running) {
Expand All @@ -162,7 +183,8 @@ static void lvgl_port_task(void *arg)
vTaskDelay(pdMS_TO_TICKS(task_delay_ms));
}

lvgl_port_task_deinit();
/* Give semaphore back */
xSemaphoreGive(lvgl_port_ctx.task_mux);

/* Close task */
vTaskDelete( NULL );
Expand All @@ -173,6 +195,9 @@ static void lvgl_port_task_deinit(void)
if (lvgl_port_ctx.lvgl_mux) {
vSemaphoreDelete(lvgl_port_ctx.lvgl_mux);
}
if (lvgl_port_ctx.task_mux) {
vSemaphoreDelete(lvgl_port_ctx.task_mux);
}
memset(&lvgl_port_ctx, 0, sizeof(lvgl_port_ctx));
#if LV_ENABLE_GC || !LV_MEM_CUSTOM
/* Deinitialize LVGL */
Expand Down

0 comments on commit 57f01d4

Please sign in to comment.