diff --git a/dependencies.lock b/dependencies.lock index 39ce76c..31a32a4 100644 --- a/dependencies.lock +++ b/dependencies.lock @@ -13,72 +13,6 @@ dependencies: registry_url: https://components.espressif.com/ type: service version: 1.0.6 - espp/base_component: - component_hash: 044472522a07bb4f03f50885ad0f3684d26254ef7cb0ea73f21d11241cfe4fdd - dependencies: - - name: espp/logger - registry_url: https://components.espressif.com - require: private - version: '>=1.0' - - name: idf - require: private - version: '>=5.0' - source: - registry_url: https://components.espressif.com - type: service - version: 1.1.3 - espp/base_peripheral: - component_hash: f1b5be84a6f1e3150d1b3d4a6ca5c81be0da06d3be391cc869b795ac4d5b7b92 - dependencies: - - name: espp/base_component - registry_url: https://components.espressif.com - require: private - version: '>=1.0' - - name: idf - require: private - version: '>=5.0' - source: - registry_url: https://components.espressif.com - type: service - version: 1.1.3 - espp/cst816: - component_hash: a7c153be65c384213e80c83fec5a5466b172dd821021eb32d99b6752ad8f5f62 - dependencies: - - name: espp/base_peripheral - registry_url: https://components.espressif.com - require: private - version: '>=0.22.0' - - name: idf - require: private - version: '>=5.0' - source: - registry_url: https://components.espressif.com/ - type: service - version: 0.22.1 - espp/format: - component_hash: 997bc557fbb73549999699407f27856ad3c3cd9207e2e5f4af523e8dfc9ae659 - dependencies: - - name: idf - require: private - version: '>=5.0' - source: - registry_url: https://components.espressif.com - type: service - version: 1.1.3 - espp/logger: - component_hash: 2a636c84d987a20b98cabbdcc65b92d9b6725d1c12cf2b3487d6ba60e452ef13 - dependencies: - - name: espp/format - registry_url: https://components.espressif.com - require: private - version: '>=1.0' - - name: idf - require: private - version: '>=5.0' - source: - registry_url: https://components.espressif.com - type: service - version: 1.1.3 espressif/esp_lcd_touch: component_hash: 3f85a7d95af876f1a6ecca8eb90a81614890d0f03a038390804e5a77e2caf862 dependencies: @@ -89,6 +23,20 @@ dependencies: registry_url: https://components.espressif.com type: service version: 1.2.1 + espressif/esp_lcd_touch_cst816s: + component_hash: a562ca3238756cca575908ed2cc4da4bab95f3815180b4f01f4c949b658bdd6d + dependencies: + - name: espressif/esp_lcd_touch + registry_url: https://components.espressif.com + require: public + version: ^1.2.0 + - name: idf + require: private + version: '>=5.2' + source: + registry_url: https://components.espressif.com/ + type: service + version: 1.1.1~1 espressif/esp_lcd_touch_stmpe610: component_hash: d8adce47eabe3f8caa23c5acf03cfefe78012628abd974673ccb9a577c3d738b dependencies: @@ -116,9 +64,9 @@ dependencies: version: 9.3.0 direct_dependencies: - atanisoft/esp_lcd_touch_xpt2046 -- espp/cst816 +- espressif/esp_lcd_touch_cst816s - espressif/esp_lcd_touch_stmpe610 - lvgl/lvgl -manifest_hash: 10c0c694b19d739cb3a8b361aa783292ec04b0f26fdbd54a5e06e72dd9b7b37c +manifest_hash: 2b6db917e70b8d2f517a68c9019f4be93d8a03d820ef9c487259330151bd4a4f target: esp32s3 version: 3.0.0 diff --git a/main/bsp/board_config.h b/main/bsp/board_config.h index c3b79c4..80d9869 100644 --- a/main/bsp/board_config.h +++ b/main/bsp/board_config.h @@ -1,14 +1,17 @@ #pragma once -#include "driver/i2c.h" +#include "driver/i2c_types.h" #include "driver/ledc.h" #include "driver/spi_master.h" /* LCD SPI (ST7789) */ #define BOARD_LCD_HOST SPI2_HOST #define BOARD_LCD_PIXEL_CLOCK_HZ (20 * 1000 * 1000) -#define BOARD_LCD_H_RES 240 +#define BOARD_LCD_H_RES 170 #define BOARD_LCD_V_RES 320 +#define BOARD_LCD_PANEL_H_RES 240 +#define BOARD_LCD_GAP_X ((BOARD_LCD_PANEL_H_RES - BOARD_LCD_H_RES) / 2) +#define BOARD_LCD_GAP_Y 0 #define BOARD_LCD_CMD_BITS 8 #define BOARD_LCD_PARAM_BITS 8 @@ -35,7 +38,7 @@ #define BOARD_TOUCH_I2C_NUM I2C_NUM_0 #define BOARD_TOUCH_I2C_FREQ_HZ 400000 -#define BOARD_TOUCH_ENABLED 0 +#define BOARD_TOUCH_ENABLED 1 /* User IO */ #define BOARD_PIN_KEY 4 diff --git a/main/bsp/display.c b/main/bsp/display.c index 85387e1..acd3ab5 100644 --- a/main/bsp/display.c +++ b/main/bsp/display.c @@ -87,6 +87,7 @@ esp_err_t display_init(esp_lcd_panel_io_handle_t *io_handle_out, ESP_ERROR_CHECK(esp_lcd_panel_reset(panel_handle)); ESP_ERROR_CHECK(esp_lcd_panel_init(panel_handle)); ESP_ERROR_CHECK(esp_lcd_panel_mirror(panel_handle, false, false)); + ESP_ERROR_CHECK(esp_lcd_panel_set_gap(panel_handle, BOARD_LCD_GAP_X, BOARD_LCD_GAP_Y)); ESP_ERROR_CHECK(esp_lcd_panel_disp_on_off(panel_handle, true)); ESP_LOGI(TAG, "Turn on LCD backlight"); diff --git a/main/bsp/touch.c b/main/bsp/touch.c index 022fc61..ed6da62 100644 --- a/main/bsp/touch.c +++ b/main/bsp/touch.c @@ -3,30 +3,47 @@ #include "board_config.h" #include "driver/gpio.h" -#include "driver/i2c.h" +#include "driver/i2c_master.h" +#include "esp_lcd_io_i2c.h" +#include "esp_lcd_panel_io.h" #include "esp_lcd_touch.h" +#include "esp_lcd_touch_cst816s.h" #include "esp_log.h" +#include "freertos/FreeRTOS.h" +#include "freertos/semphr.h" +#include "freertos/task.h" static const char *TAG = "touch"; #if BOARD_TOUCH_ENABLED -#include "esp_lcd_touch_cst816.h" +static i2c_master_bus_handle_t s_touch_i2c_bus; +static esp_lcd_touch_handle_t s_touch_handle; +static SemaphoreHandle_t s_touch_mux; + +static void touch_isr_cb(esp_lcd_touch_handle_t tp) +{ + BaseType_t task_woken = pdFALSE; + (void)tp; + xSemaphoreGiveFromISR(s_touch_mux, &task_woken); + if (task_woken) { + portYIELD_FROM_ISR(); + } +} static void lvgl_touch_read_cb(lv_indev_t *indev, lv_indev_data_t *data) { - uint16_t touchpad_x[1] = {0}; - uint16_t touchpad_y[1] = {0}; - uint8_t touchpad_cnt = 0; + esp_lcd_touch_handle_t tp = lv_indev_get_user_data(indev); - esp_lcd_touch_handle_t touch_pad = lv_indev_get_user_data(indev); - esp_lcd_touch_read_data(touch_pad); - bool touchpad_pressed = esp_lcd_touch_get_coordinates( - touch_pad, touchpad_x, touchpad_y, NULL, &touchpad_cnt, 1); + if (xSemaphoreTake(s_touch_mux, 0) == pdTRUE) { + esp_lcd_touch_read_data(tp); + } - if (touchpad_pressed && touchpad_cnt > 0) { - data->point.x = touchpad_x[0]; - data->point.y = touchpad_y[0]; + esp_lcd_touch_point_data_t points[1]; + uint8_t point_cnt = 0; + if (esp_lcd_touch_get_data(tp, points, &point_cnt, 1) == ESP_OK && point_cnt > 0) { + data->point.x = points[0].x; + data->point.y = points[0].y; data->state = LV_INDEV_STATE_PRESSED; } else { data->state = LV_INDEV_STATE_RELEASED; @@ -36,36 +53,51 @@ static void lvgl_touch_read_cb(lv_indev_t *indev, lv_indev_data_t *data) esp_err_t touch_init(lv_display_t *display) { ESP_LOGI(TAG, "Initialize I2C for touch"); - i2c_config_t i2c_conf = { - .mode = I2C_MODE_MASTER, + i2c_master_bus_config_t bus_config = { + .clk_source = I2C_CLK_SRC_DEFAULT, + .i2c_port = BOARD_TOUCH_I2C_NUM, .sda_io_num = BOARD_PIN_TOUCH_SDA, .scl_io_num = BOARD_PIN_TOUCH_SCL, - .sda_pullup_en = GPIO_PULLUP_ENABLE, - .scl_pullup_en = GPIO_PULLUP_ENABLE, - .master.clk_speed = BOARD_TOUCH_I2C_FREQ_HZ, + .glitch_ignore_cnt = 7, + .flags.enable_internal_pullup = true, }; - ESP_ERROR_CHECK(i2c_param_config(BOARD_TOUCH_I2C_NUM, &i2c_conf)); - ESP_ERROR_CHECK(i2c_driver_install(BOARD_TOUCH_I2C_NUM, I2C_MODE_MASTER, 0, 0, 0)); + ESP_ERROR_CHECK(i2c_new_master_bus(&bus_config, &s_touch_i2c_bus)); - ESP_LOGI(TAG, "Initialize touch controller CST816"); - esp_lcd_touch_handle_t tp = NULL; + s_touch_mux = xSemaphoreCreateBinary(); + if (s_touch_mux == NULL) { + ESP_LOGE(TAG, "Failed to create touch semaphore"); + return ESP_ERR_NO_MEM; + } + + ESP_LOGI(TAG, "Initialize touch panel IO (CST816S)"); + esp_lcd_panel_io_handle_t tp_io_handle = NULL; + esp_lcd_panel_io_i2c_config_t tp_io_config = ESP_LCD_TOUCH_IO_I2C_CST816S_CONFIG(); + tp_io_config.scl_speed_hz = BOARD_TOUCH_I2C_FREQ_HZ; + ESP_ERROR_CHECK(esp_lcd_new_panel_io_i2c(s_touch_i2c_bus, &tp_io_config, &tp_io_handle)); + + ESP_LOGI(TAG, "Initialize touch controller CST816S"); esp_lcd_touch_config_t tp_cfg = { .x_max = BOARD_LCD_H_RES, .y_max = BOARD_LCD_V_RES, .rst_gpio_num = BOARD_PIN_TOUCH_RST, .int_gpio_num = BOARD_PIN_TOUCH_INT, + .levels = { + .reset = 0, + .interrupt = 0, + }, .flags = { .swap_xy = 0, .mirror_x = 0, .mirror_y = 0, }, + .interrupt_callback = touch_isr_cb, }; - ESP_ERROR_CHECK(esp_lcd_touch_new_i2c_cst816(BOARD_TOUCH_I2C_NUM, &tp_cfg, &tp)); + ESP_ERROR_CHECK(esp_lcd_touch_new_i2c_cst816s(tp_io_handle, &tp_cfg, &s_touch_handle)); lv_indev_t *indev = lv_indev_create(); lv_indev_set_type(indev, LV_INDEV_TYPE_POINTER); lv_indev_set_display(indev, display); - lv_indev_set_user_data(indev, tp); + lv_indev_set_user_data(indev, s_touch_handle); lv_indev_set_read_cb(indev, lvgl_touch_read_cb); return ESP_OK; diff --git a/main/idf_component.yml b/main/idf_component.yml index 04a3d2c..74ed57b 100644 --- a/main/idf_component.yml +++ b/main/idf_component.yml @@ -1,5 +1,6 @@ dependencies: lvgl/lvgl: 9.3.0 + espressif/esp_lcd_touch_cst816s: ^1.1.1 esp_lcd_touch_stmpe610: ^1.0.0 atanisoft/esp_lcd_touch_xpt2046: 1.0.6 espressif/esp_wifi_remote: @@ -10,4 +11,3 @@ dependencies: version: ~2 rules: - if: target in [esp32p4, esp32h2] - espp/cst816: ^0.22.0 diff --git a/sdkconfig.defaults b/sdkconfig.defaults index 42b9e74..24b6396 100644 --- a/sdkconfig.defaults +++ b/sdkconfig.defaults @@ -1 +1,2 @@ CONFIG_ESP_WIFI_SOFTAP_SUPPORT=n +CONFIG_ESP_LCD_TOUCH_CST816S_DISABLE_READ_ID=y