// SPDX-License-Identifier: Apache-2.0 #ifdef ESP_PLATFORM #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define TAG "lvgl_esp32" extern struct LvglModuleConfig lvgl_module_config; extern void lvgl_devices_attach(); extern void lvgl_devices_detach(); static bool initialized = false; /** Read by every fopen() */ static TaskHandle_t lvgl_task = NULL; FILE* __real_fopen(const char* path, const char* mode); /** * Warns about files that are opened on the LVGL task: a file access blocks the UI, and the task's stack can be in external RAM, * which can't be used while the flash cache is off. Linked with --wrap=fopen. */ FILE* __wrap_fopen(const char* path, const char* mode) { if (lvgl_task != NULL && xTaskGetCurrentTaskHandle() == lvgl_task) { LOG_E(TAG, "+----------------------------------------------------------------------+"); LOG_E(TAG, "| File opened on the LVGL task: |"); LOG_E(TAG, "| %-68s |", path); LOG_E(TAG, "| This might crash in the future: move file access to file_ops. |"); LOG_E(TAG, "+----------------------------------------------------------------------+"); } return __real_fopen(path, mode); } void lvgl_lock(void) { if (!initialized) { return; } lvgl_port_lock(portMAX_DELAY); } bool lvgl_try_lock(uint32_t timeoutTicks) { if (!initialized) { return false; } // lvgl_port_lock expects milliseconds return lvgl_port_lock(timeoutTicks * portTICK_PERIOD_MS); } void lvgl_unlock(void) { if (!initialized) { return; } lvgl_port_unlock(); } void lvgl_run_on_task(void (*function)(void*), void* argument) { lvgl_lock(); function(argument); lvgl_unlock(); } error_t lvgl_arch_start() { // The LVGL task must not read files: its stack in external RAM can't be used while the flash cache is off const bool has_external_ram = heap_caps_get_total_size(MALLOC_CAP_SPIRAM) > 0; const lvgl_port_cfg_t lvgl_cfg = { .task_priority = lvgl_module_config.task_priority, .task_stack = lvgl_module_config.task_stack_size, .task_stack_caps = has_external_ram ? (MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT) : (MALLOC_CAP_INTERNAL | MALLOC_CAP_DEFAULT), .task_affinity = lvgl_module_config.task_affinity, .task_max_sleep_ms = 500, .timer_period_ms = 5 }; if (lvgl_port_init(&lvgl_cfg) != ESP_OK) { return ERROR_RESOURCE; } // The port doesn't expose its task, so it is found by the name that the port gives it lvgl_task = xTaskGetHandle("taskLVGL"); // We must have the state set to "initialized" so that the lvgl lock works // when we call listener functions. These functions could create new // devices and services. The latter might start adding widgets immediately. initialized = true; lvgl_lock(); lvgl_png_decoder_init(); lvgl_unlock(); // Must exist before devices/services are attached below, since those can // immediately try to assign an indev to this group (e.g. USB HID input). lvgl_keyboard_on_start_lvgl(); lvgl_devices_attach(); if (lvgl_module_config.on_start) lvgl_module_config.on_start(); return ERROR_NONE; } error_t lvgl_arch_stop() { if (lvgl_module_config.on_stop) lvgl_module_config.on_stop(); lvgl_devices_detach(); lvgl_keyboard_on_stop_lvgl(); // The port deletes its mutex while deinitializing, so other tasks must stop locking first initialized = false; lvgl_task = NULL; if (lvgl_port_deinit() != ESP_OK) { initialized = true; lvgl_task = xTaskGetHandle("taskLVGL"); // Recreate what stop() above tore down, then call on_start again to recover lvgl_keyboard_on_start_lvgl(); if (lvgl_module_config.on_start) lvgl_module_config.on_start(); return ERROR_RESOURCE; } return ERROR_NONE; } #endif