From c81cb49608af28489c4ef5cc80a0f68dcbcd4a77 Mon Sep 17 00:00:00 2001 From: Malko Gindrat Date: Sat, 1 Aug 2026 00:01:28 +0200 Subject: [PATCH] First working C64 READY screen --- .gitignore | 2 + README.md | 15 + platformio.ini | 21 ++ src/axs15231b/esp_lcd_axs15231b.c | 550 ++++++++++++++++++++++++++++++ src/axs15231b/esp_lcd_axs15231b.h | 207 +++++++++++ src/c64_ui.c | 6 + src/c64_ui.h | 8 + src/i2c_bsp.c | 112 ++++++ src/i2c_bsp.h | 23 ++ src/lcd_bl_bsp/lcd_bl_pwm_bsp.c | 47 +++ src/lcd_bl_bsp/lcd_bl_pwm_bsp.h | 30 ++ src/lvgl_port.c | 268 +++++++++++++++ src/lvgl_port.h | 30 ++ src/main.cpp | 6 + src/touch/esp_lcd_touch.c | 266 +++++++++++++++ src/touch/esp_lcd_touch.h | 433 +++++++++++++++++++++++ src/user_config.h | 69 ++++ 17 files changed, 2093 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 platformio.ini create mode 100644 src/axs15231b/esp_lcd_axs15231b.c create mode 100644 src/axs15231b/esp_lcd_axs15231b.h create mode 100644 src/c64_ui.c create mode 100644 src/c64_ui.h create mode 100644 src/i2c_bsp.c create mode 100644 src/i2c_bsp.h create mode 100644 src/lcd_bl_bsp/lcd_bl_pwm_bsp.c create mode 100644 src/lcd_bl_bsp/lcd_bl_pwm_bsp.h create mode 100644 src/lvgl_port.c create mode 100644 src/lvgl_port.h create mode 100644 src/main.cpp create mode 100644 src/touch/esp_lcd_touch.c create mode 100644 src/touch/esp_lcd_touch.h create mode 100644 src/user_config.h diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..968a41b --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.pio/ +.vscode/ diff --git a/README.md b/README.md new file mode 100644 index 0000000..125a1e0 --- /dev/null +++ b/README.md @@ -0,0 +1,15 @@ +# Waveshare ESP32-S3-Touch-LCD-3.49 - C64 READY + +PlatformIO starter showing Commodore 64-style READY. with a blinking cursor. + +Hardware: ESP32-S3R8, 16 MB flash, 8 MB PSRAM, 172x640 AXS15231B QSPI LCD. + +Build: `pio run` +Upload: `pio run -t upload` +Monitor: 115200 baud + +C64 palette: background `#40318D`; text/cursor `#7869C4`. + +Sources: +- https://www.waveshare.com/wiki/ESP32-S3-Touch-LCD-3.49 +- https://github.com/waveshareteam/ESP32-S3-Touch-LCD-3.49 diff --git a/platformio.ini b/platformio.ini new file mode 100644 index 0000000..3275d77 --- /dev/null +++ b/platformio.ini @@ -0,0 +1,21 @@ +[env:waveshare-c64-ready] +platform = https://github.com/pioarduino/platform-espressif32/releases/download/stable/platform-espressif32.zip +board = esp32-s3-devkitc-1 +framework = arduino +monitor_speed = 115200 +upload_speed = 921600 +board_build.flash_size = 16MB +board_build.flash_mode = qio +board_upload.maximum_size = 16777216 +board_build.arduino.memory_type = qio_opi +board_build.partitions = default_16MB.csv +build_flags = + -DBOARD_HAS_PSRAM + -DARDUINO_USB_MODE=1 + -DARDUINO_USB_CDC_ON_BOOT=1 + -DLV_CONF_SKIP + -DLV_COLOR_DEPTH=16 + -DLV_COLOR_16_SWAP=1 + -DLV_FONT_MONTSERRAT_48=1 +lib_deps = + lvgl/lvgl@8.4.0 diff --git a/src/axs15231b/esp_lcd_axs15231b.c b/src/axs15231b/esp_lcd_axs15231b.c new file mode 100644 index 0000000..a6f08b6 --- /dev/null +++ b/src/axs15231b/esp_lcd_axs15231b.c @@ -0,0 +1,550 @@ +/* + * SPDX-FileCopyrightText: 2021-2024 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" +#include "esp_lcd_panel_interface.h" +#include "esp_lcd_panel_io.h" +#include "esp_lcd_panel_vendor.h" +#include "esp_lcd_panel_ops.h" +#include "esp_lcd_panel_commands.h" +#include "driver/gpio.h" +#include "esp_log.h" +#include "esp_check.h" +#include "../touch/esp_lcd_touch.h" + +#include "esp_lcd_axs15231b.h" + +/*max point num*/ +#define AXS_MAX_TOUCH_NUMBER (1) + +#define LCD_OPCODE_WRITE_CMD (0x02ULL) +#define LCD_OPCODE_READ_CMD (0x0BULL) +#define LCD_OPCODE_WRITE_COLOR (0x32ULL) + +static const char *TAG = "lcd_panel.axs15231b"; + +static esp_err_t panel_axs15231b_del(esp_lcd_panel_t *panel); +static esp_err_t panel_axs15231b_reset(esp_lcd_panel_t *panel); +static esp_err_t panel_axs15231b_init(esp_lcd_panel_t *panel); +static esp_err_t panel_axs15231b_draw_bitmap(esp_lcd_panel_t *panel, int x_start, int y_start, int x_end, int y_end, const void *color_data); +static esp_err_t panel_axs15231b_invert_color(esp_lcd_panel_t *panel, bool invert_color_data); +static esp_err_t panel_axs15231b_mirror(esp_lcd_panel_t *panel, bool mirror_x, bool mirror_y); +static esp_err_t panel_axs15231b_swap_xy(esp_lcd_panel_t *panel, bool swap_axes); +static esp_err_t panel_axs15231b_set_gap(esp_lcd_panel_t *panel, int x_gap, int y_gap); +static esp_err_t panel_axs15231b_disp_off(esp_lcd_panel_t *panel, bool off); + +static esp_err_t touch_axs15231b_read_data(esp_lcd_touch_handle_t tp); +static bool touch_axs15231b_get_xy(esp_lcd_touch_handle_t tp, uint16_t *x, uint16_t *y, uint16_t *strength, uint8_t *point_num, uint8_t max_point_num); +static esp_err_t touch_axs15231b_del(esp_lcd_touch_handle_t tp); +static esp_err_t touch_axs15231b_reset(esp_lcd_touch_handle_t tp); + +static esp_err_t i2c_read_bytes(esp_lcd_touch_handle_t tp, int reg, uint8_t *data, uint8_t len); +static esp_err_t i2c_write_bytes(esp_lcd_touch_handle_t tp, int reg, const uint8_t *data, uint8_t len); + +typedef struct { + esp_lcd_panel_t base; + esp_lcd_panel_io_handle_t io; + int reset_gpio_num; + int x_gap; + int y_gap; + uint8_t fb_bits_per_pixel; + uint8_t madctl_val; // save current value of LCD_CMD_MADCTL register + uint8_t colmod_val; // save surrent value of LCD_CMD_COLMOD register + const axs15231b_lcd_init_cmd_t *init_cmds; + uint16_t init_cmds_size; + struct { + unsigned int use_qspi_interface: 1; + unsigned int reset_level: 1; + } flags; +} axs15231b_panel_t; + +esp_err_t esp_lcd_new_panel_axs15231b(const esp_lcd_panel_io_handle_t io, const esp_lcd_panel_dev_config_t *panel_dev_config, esp_lcd_panel_handle_t *ret_panel) +{ + esp_err_t ret = ESP_OK; + axs15231b_panel_t *axs15231b = NULL; + ESP_GOTO_ON_FALSE(io && panel_dev_config && ret_panel, ESP_ERR_INVALID_ARG, err, TAG, "invalid argument"); + axs15231b = calloc(1, sizeof(axs15231b_panel_t)); + ESP_GOTO_ON_FALSE(axs15231b, ESP_ERR_NO_MEM, err, TAG, "no mem for axs15231b panel"); + + if (panel_dev_config->reset_gpio_num >= 0) { + gpio_config_t io_conf = { + .mode = GPIO_MODE_OUTPUT, + .pin_bit_mask = 1ULL << panel_dev_config->reset_gpio_num, + }; + ESP_GOTO_ON_ERROR(gpio_config(&io_conf), err, TAG, "configure GPIO for RST line failed"); + } + + switch (panel_dev_config->color_space) { + case LCD_RGB_ELEMENT_ORDER_RGB: + axs15231b->madctl_val = 0; + break; + case LCD_RGB_ELEMENT_ORDER_BGR: + axs15231b->madctl_val |= LCD_CMD_BGR_BIT; + break; + default: + ESP_GOTO_ON_FALSE(false, ESP_ERR_NOT_SUPPORTED, err, TAG, "unsupported RGB element order"); + break; + } + + uint8_t fb_bits_per_pixel = 0; + switch (panel_dev_config->bits_per_pixel) { + case 16: // RGB565 + axs15231b->colmod_val = 0x55; + fb_bits_per_pixel = 16; + break; + case 18: // RGB666 + axs15231b->colmod_val = 0x66; + // each color component (R/G/B) should occupy the 6 high bits of a byte, which means 3 full bytes are required for a pixel + fb_bits_per_pixel = 24; + break; + default: + ESP_GOTO_ON_FALSE(false, ESP_ERR_NOT_SUPPORTED, err, TAG, "unsupported pixel width"); + break; + } + + axs15231b->io = io; + axs15231b->fb_bits_per_pixel = fb_bits_per_pixel; + axs15231b->reset_gpio_num = panel_dev_config->reset_gpio_num; + axs15231b->flags.reset_level = panel_dev_config->flags.reset_active_high; + if (panel_dev_config->vendor_config) { + axs15231b->init_cmds = ((axs15231b_vendor_config_t *)panel_dev_config->vendor_config)->init_cmds; + axs15231b->init_cmds_size = ((axs15231b_vendor_config_t *)panel_dev_config->vendor_config)->init_cmds_size; + axs15231b->flags.use_qspi_interface = ((axs15231b_vendor_config_t *)panel_dev_config->vendor_config)->flags.use_qspi_interface; + } + axs15231b->base.del = panel_axs15231b_del; + axs15231b->base.reset = panel_axs15231b_reset; + axs15231b->base.init = panel_axs15231b_init; + axs15231b->base.draw_bitmap = panel_axs15231b_draw_bitmap; + axs15231b->base.invert_color = panel_axs15231b_invert_color; + axs15231b->base.set_gap = panel_axs15231b_set_gap; + axs15231b->base.mirror = panel_axs15231b_mirror; + axs15231b->base.swap_xy = panel_axs15231b_swap_xy; + axs15231b->base.disp_on_off = panel_axs15231b_disp_off; + *ret_panel = &(axs15231b->base); + ESP_LOGD(TAG, "new axs15231b panel @%p", axs15231b); + //ESP_LOGI(TAG, "LCD panel create success, version: %d.%d.%d", ESP_LCD_AXS15231B_VER_MAJOR, ESP_LCD_AXS15231B_VER_MINOR, + // ESP_LCD_AXS15231B_VER_PATCH); + + return ESP_OK; + +err: + if (axs15231b) { + if (panel_dev_config->reset_gpio_num >= 0) { + gpio_reset_pin(panel_dev_config->reset_gpio_num); + } + free(axs15231b); + } + return ret; +} + +static esp_err_t tx_param(axs15231b_panel_t *axs15231b, esp_lcd_panel_io_handle_t io, int lcd_cmd, const void *param, size_t param_size) +{ + if (axs15231b->flags.use_qspi_interface) { + lcd_cmd &= 0xff; + lcd_cmd <<= 8; + lcd_cmd |= LCD_OPCODE_WRITE_CMD << 24; + } + return esp_lcd_panel_io_tx_param(io, lcd_cmd, param, param_size); +} + +static esp_err_t tx_color(axs15231b_panel_t *axs15231b, esp_lcd_panel_io_handle_t io, int lcd_cmd, const void *param, size_t param_size) +{ + if (axs15231b->flags.use_qspi_interface) { + lcd_cmd &= 0xff; + lcd_cmd <<= 8; + lcd_cmd |= LCD_OPCODE_WRITE_COLOR << 24; + } + return esp_lcd_panel_io_tx_color(io, lcd_cmd, param, param_size); +} + +static esp_err_t panel_axs15231b_del(esp_lcd_panel_t *panel) +{ + axs15231b_panel_t *axs15231b = __containerof(panel, axs15231b_panel_t, base); + + if (axs15231b->reset_gpio_num >= 0) { + gpio_reset_pin(axs15231b->reset_gpio_num); + } + ESP_LOGD(TAG, "del axs15231b panel @%p", axs15231b); + free(axs15231b); + return ESP_OK; +} + +static esp_err_t panel_axs15231b_reset(esp_lcd_panel_t *panel) +{ + axs15231b_panel_t *axs15231b = __containerof(panel, axs15231b_panel_t, base); + esp_lcd_panel_io_handle_t io = axs15231b->io; + + // perform hardware reset + if (axs15231b->reset_gpio_num >= 0) { + gpio_set_level(axs15231b->reset_gpio_num, !axs15231b->flags.reset_level); + vTaskDelay(pdMS_TO_TICKS(10)); + gpio_set_level(axs15231b->reset_gpio_num, axs15231b->flags.reset_level); + vTaskDelay(pdMS_TO_TICKS(10)); + gpio_set_level(axs15231b->reset_gpio_num, !axs15231b->flags.reset_level); + vTaskDelay(pdMS_TO_TICKS(120)); + } else { // perform software reset + tx_param(axs15231b, io, LCD_CMD_SWRESET, NULL, 0); + vTaskDelay(pdMS_TO_TICKS(120)); // spec, wait at least 5m before sending new command + } + + return ESP_OK; +} + +static const axs15231b_lcd_init_cmd_t vendor_specific_init_default[] = { + {0xBB, (uint8_t[]){0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x5A, 0xA5}, 8, 0}, + {0xA0, (uint8_t[]){0x00, 0x10, 0x00, 0x02, 0x00, 0x00, 0x64, 0x3F, 0x20, 0x05, 0x3F, 0x3F, 0x00, 0x00, 0x00, 0x00, 0x00}, 17, 0}, + {0xA2, (uint8_t[]){0x30, 0x04, 0x0A, 0x3C, 0xEC, 0x54, 0xC4, 0x30, 0xAC, 0x28, 0x7F, 0x7F, 0x7F, 0x20, 0xF8, 0x10, 0x02, 0xFF, 0xFF, 0xF0, 0x90, 0x01, 0x32, 0xA0, 0x91, 0xC0, 0x20, 0x7F, 0xFF, 0x00, 0x54}, 31, 0}, + {0xD0, (uint8_t[]){0x30, 0xAC, 0x21, 0x24, 0x08, 0x09, 0x10, 0x01, 0xAA, 0x14, 0xC2, 0x00, 0x22, 0x22, 0xAA, 0x03, 0x10, 0x12, 0x40, 0x14, 0x1E, 0x51, 0x15, 0x00, 0x40, 0x10, 0x00, 0x03, 0x3D, 0x12}, 30, 0}, + {0xA3, (uint8_t[]){0xA0, 0x06, 0xAA, 0x08, 0x08, 0x02, 0x0A, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x00, 0x55, 0x55}, 22, 0}, + {0xC1, (uint8_t[]){0x33, 0x04, 0x02, 0x02, 0x71, 0x05, 0x24, 0x55, 0x02, 0x00, 0x41, 0x00, 0x53, 0xFF, 0xFF, 0xFF, 0x4F, 0x52, 0x00, 0x4F, 0x52, 0x00, 0x45, 0x3B, 0x0B, 0x02, 0x0D, 0x00, 0xFF, 0x40}, 30, 0}, + {0xC3, (uint8_t[]){0x00, 0x00, 0x00, 0x50, 0x03, 0x00, 0x00, 0x00, 0x01, 0x80, 0x01}, 11, 0}, + {0xC4, (uint8_t[]){0x00, 0x24, 0x33, 0x90, 0x50, 0xea, 0x64, 0x32, 0xC8, 0x64, 0xC8, 0x32, 0x90, 0x90, 0x11, 0x06, 0xDC, 0xFA, 0x04, 0x03, 0x80, 0xFE, 0x10, 0x10, 0x00, 0x0A, 0x0A, 0x44, 0x50}, 29, 0}, + {0xC5, (uint8_t[]){0x18, 0x00, 0x00, 0x03, 0xFE, 0x78, 0x33, 0x20, 0x30, 0x10, 0x88, 0xDE, 0x0D, 0x08, 0x0F, 0x0F, 0x01, 0x78, 0x33, 0x20, 0x10, 0x10, 0x80}, 23, 0}, + {0xC6, (uint8_t[]){0x05, 0x0A, 0x05, 0x0A, 0x00, 0xE0, 0x2E, 0x0B, 0x12, 0x22, 0x12, 0x22, 0x01, 0x00, 0x00, 0x3F, 0x6A, 0x18, 0xC8, 0x22}, 20, 0}, + {0xC7, (uint8_t[]){0x50, 0x32, 0x28, 0x00, 0xa2, 0x80, 0x8f, 0x00, 0x80, 0xff, 0x07, 0x11, 0x9F, 0x6f, 0xff, 0x26, 0x0c, 0x0d, 0x0e, 0x0f}, 20, 0}, + {0xC9, (uint8_t[]){0x33, 0x44, 0x44, 0x01}, 4, 0}, + {0xCF, (uint8_t[]){0x34, 0x1E, 0x88, 0x58, 0x13, 0x18, 0x56, 0x18, 0x1E, 0x68, 0xF7, 0x00, 0x65, 0x0C, 0x22, 0xC4, 0x0C, 0x77, 0x22, 0x44, 0xAA, 0x55, 0x04, 0x04, 0x12, 0xA0, 0x08}, 27, 0}, + {0xD5, (uint8_t[]){0x3E, 0x3E, 0x88, 0x00, 0x44, 0x04, 0x78, 0x33, 0x20, 0x78, 0x33, 0x20, 0x04, 0x28, 0xD3, 0x47, 0x03, 0x03, 0x03, 0x03, 0x86, 0x00, 0x00, 0x00, 0x30, 0x52, 0x3f, 0x40, 0x40, 0x96}, 30, 0}, + {0xD6, (uint8_t[]){0x10, 0x32, 0x54, 0x76, 0x98, 0xBA, 0xDC, 0xFE, 0x95, 0x00, 0x01, 0x83, 0x75, 0x36, 0x20, 0x75, 0x36, 0x20, 0x3F, 0x03, 0x03, 0x03, 0x10, 0x10, 0x00, 0x04, 0x51, 0x20, 0x01, 0x00}, 30, 0}, + {0xD7, (uint8_t[]){0x0a, 0x08, 0x0e, 0x0c, 0x1E, 0x18, 0x19, 0x1F, 0x00, 0x1F, 0x1A, 0x1F, 0x3E, 0x3E, 0x04, 0x00, 0x1F, 0x1F, 0x1F}, 19, 0}, + {0xD8, (uint8_t[]){0x0B, 0x09, 0x0F, 0x0D, 0x1E, 0x18, 0x19, 0x1F, 0x01, 0x1F, 0x1A, 0x1F}, 12, 0}, + {0xD9, (uint8_t[]){0x00, 0x0D, 0x0F, 0x09, 0x0B, 0x1F, 0x18, 0x19, 0x1F, 0x01, 0x1E, 0x1A, 0x1F}, 13, 0}, + {0xDD, (uint8_t[]){0x0C, 0x0E, 0x08, 0x0A, 0x1F, 0x18, 0x19, 0x1F, 0x00, 0x1E, 0x1A, 0x1F}, 12, 0}, + {0xDF, (uint8_t[]){0x44, 0x73, 0x4B, 0x69, 0x00, 0x0A, 0x02, 0x90}, 8, 0}, + {0xE0, (uint8_t[]){0x19, 0x20, 0x0A, 0x13, 0x0E, 0x09, 0x12, 0x28, 0xD4, 0x24, 0x0C, 0x35, 0x13, 0x31, 0x36, 0x2f, 0x03}, 17, 0}, + {0xE1, (uint8_t[]){0x38, 0x20, 0x09, 0x12, 0x0E, 0x08, 0x12, 0x28, 0xC5, 0x24, 0x0C, 0x34, 0x12, 0x31, 0x36, 0x2f, 0x27}, 17, 0}, + {0xE2, (uint8_t[]){0x19, 0x20, 0x0A, 0x11, 0x09, 0x06, 0x11, 0x25, 0xD4, 0x22, 0x0B, 0x33, 0x12, 0x2D, 0x32, 0x2f, 0x03}, 17, 0}, + {0xE3, (uint8_t[]){0x38, 0x20, 0x0A, 0x11, 0x09, 0x06, 0x11, 0x25, 0xC4, 0x21, 0x0A, 0x32, 0x11, 0x2C, 0x32, 0x2f, 0x27}, 17, 0}, + {0xE4, (uint8_t[]){0x19, 0x20, 0x0D, 0x14, 0x0D, 0x08, 0x12, 0x2A, 0xD4, 0x26, 0x0E, 0x35, 0x13, 0x34, 0x39, 0x2f, 0x03}, 17, 0}, + {0xE5, (uint8_t[]){0x38, 0x20, 0x0D, 0x13, 0x0D, 0x07, 0x12, 0x29, 0xC4, 0x25, 0x0D, 0x35, 0x12, 0x33, 0x39, 0x2f, 0x27}, 17, 0}, + {0xBB, (uint8_t[]){0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 8, 0}, + {0x13, (uint8_t[]){0x00}, 0, 0}, + {0x11, (uint8_t[]){0x00}, 0, 200}, + {0x29, (uint8_t[]){0x00}, 0, 200}, + {0x2C, (uint8_t[]){0x00, 0x00, 0x00, 0x00}, 4, 0}, + {0x22, (uint8_t[]){0x00}, 0, 200},//All Pixels off +}; + +static esp_err_t panel_axs15231b_init(esp_lcd_panel_t *panel) +{ + axs15231b_panel_t *axs15231b = __containerof(panel, axs15231b_panel_t, base); + esp_lcd_panel_io_handle_t io = axs15231b->io; + + // LCD goes into sleep mode and display will be turned off after power on reset, exit sleep mode first + ESP_RETURN_ON_ERROR(tx_param(axs15231b, io, LCD_CMD_SLPOUT, NULL, 0), TAG, "send command failed"); + vTaskDelay(pdMS_TO_TICKS(100)); + ESP_RETURN_ON_ERROR(tx_param(axs15231b, io, LCD_CMD_MADCTL, (uint8_t[]) { + axs15231b->madctl_val, + }, 1), TAG, "send command failed"); + ESP_RETURN_ON_ERROR(tx_param(axs15231b, io, LCD_CMD_COLMOD, (uint8_t[]) { + axs15231b->colmod_val, + }, 1), TAG, "send command failed"); + + const axs15231b_lcd_init_cmd_t *init_cmds = NULL; + uint16_t init_cmds_size = 0; + if (axs15231b->init_cmds) { + init_cmds = axs15231b->init_cmds; + init_cmds_size = axs15231b->init_cmds_size; + } else { + init_cmds = vendor_specific_init_default; + init_cmds_size = sizeof(vendor_specific_init_default) / sizeof(axs15231b_lcd_init_cmd_t); + } + + bool is_cmd_overwritten = false; + for (int i = 0; i < init_cmds_size; i++) { + // Check if the command has been used or conflicts with the internal + switch (init_cmds[i].cmd) { + case LCD_CMD_MADCTL: + is_cmd_overwritten = true; + axs15231b->madctl_val = ((uint8_t *)init_cmds[i].data)[0]; + break; + case LCD_CMD_COLMOD: + is_cmd_overwritten = true; + axs15231b->colmod_val = ((uint8_t *)init_cmds[i].data)[0]; + break; + default: + is_cmd_overwritten = false; + break; + } + + if (is_cmd_overwritten) { + ESP_LOGW(TAG, "The %02Xh command has been used and will be overwritten by external initialization sequence", init_cmds[i].cmd); + } + ESP_RETURN_ON_ERROR(tx_param(axs15231b, io, init_cmds[i].cmd, init_cmds[i].data, init_cmds[i].data_bytes), TAG, "send command failed"); + vTaskDelay(pdMS_TO_TICKS(init_cmds[i].delay_ms)); + } + ESP_LOGI(TAG, "send init commands success"); + + return ESP_OK; +} + +static esp_err_t panel_axs15231b_draw_bitmap(esp_lcd_panel_t *panel, int x_start, int y_start, int x_end, int y_end, const void *color_data) +{ + axs15231b_panel_t *axs15231b = __containerof(panel, axs15231b_panel_t, base); + assert((x_start < x_end) && (y_start < y_end) && "start position must be smaller than end position"); + esp_lcd_panel_io_handle_t io = axs15231b->io; + + x_start += axs15231b->x_gap; + x_end += axs15231b->x_gap; + y_start += axs15231b->y_gap; + y_end += axs15231b->y_gap; + + // define an area of frame memory where MCU can access + tx_param(axs15231b, io, LCD_CMD_CASET, (uint8_t[]) { + (x_start >> 8) & 0xFF, + x_start & 0xFF, + ((x_end - 1) >> 8) & 0xFF, + (x_end - 1) & 0xFF, + }, 4); + + if (0 == axs15231b->flags.use_qspi_interface) { + tx_param(axs15231b, io, LCD_CMD_RASET, (uint8_t[]) { + (y_start >> 8) & 0xFF, + y_start & 0xFF, + ((y_end - 1) >> 8) & 0xFF, + (y_end - 1) & 0xFF, + }, 4); + } + + // transfer frame buffer + size_t len = (x_end - x_start) * (y_end - y_start) * axs15231b->fb_bits_per_pixel / 8; + if (y_start == 0) { + tx_color(axs15231b, io, LCD_CMD_RAMWR, color_data, len);//2C + } else { + tx_color(axs15231b, io, LCD_CMD_RAMWRC, color_data, len);//3C + } + + return ESP_OK; +} + +static esp_err_t panel_axs15231b_invert_color(esp_lcd_panel_t *panel, bool invert_color_data) +{ + axs15231b_panel_t *axs15231b = __containerof(panel, axs15231b_panel_t, base); + esp_lcd_panel_io_handle_t io = axs15231b->io; + int command = 0; + if (invert_color_data) { + command = LCD_CMD_INVON; + } else { + command = LCD_CMD_INVOFF; + } + tx_param(axs15231b, io, command, NULL, 0); + return ESP_OK; +} + +static esp_err_t panel_axs15231b_mirror(esp_lcd_panel_t *panel, bool mirror_x, bool mirror_y) +{ + axs15231b_panel_t *axs15231b = __containerof(panel, axs15231b_panel_t, base); + esp_lcd_panel_io_handle_t io = axs15231b->io; + if (mirror_x) { + axs15231b->madctl_val |= LCD_CMD_MX_BIT; + } else { + axs15231b->madctl_val &= ~LCD_CMD_MX_BIT; + } + if (mirror_y) { + axs15231b->madctl_val |= LCD_CMD_MY_BIT; + } else { + axs15231b->madctl_val &= ~LCD_CMD_MY_BIT; + } + tx_param(axs15231b, io, LCD_CMD_MADCTL, (uint8_t[]) { + axs15231b->madctl_val + }, 1); + return ESP_OK; +} + +static esp_err_t panel_axs15231b_swap_xy(esp_lcd_panel_t *panel, bool swap_axes) +{ + axs15231b_panel_t *axs15231b = __containerof(panel, axs15231b_panel_t, base); + esp_lcd_panel_io_handle_t io = axs15231b->io; + if (swap_axes) { + axs15231b->madctl_val |= LCD_CMD_MV_BIT; + } else { + axs15231b->madctl_val &= ~LCD_CMD_MV_BIT; + } + tx_param(axs15231b, io, LCD_CMD_MADCTL, (uint8_t[]) { + axs15231b->madctl_val + }, 1); + return ESP_OK; +} + +static esp_err_t panel_axs15231b_set_gap(esp_lcd_panel_t *panel, int x_gap, int y_gap) +{ + axs15231b_panel_t *axs15231b = __containerof(panel, axs15231b_panel_t, base); + axs15231b->x_gap = x_gap; + axs15231b->y_gap = y_gap; + return ESP_OK; +} + +static esp_err_t panel_axs15231b_disp_off(esp_lcd_panel_t *panel, bool off) +{ + axs15231b_panel_t *axs15231b = __containerof(panel, axs15231b_panel_t, base); + esp_lcd_panel_io_handle_t io = axs15231b->io; + int command = 0; + if (off) { + command = LCD_CMD_DISPOFF; + } else { + command = LCD_CMD_DISPON; + } + tx_param(axs15231b, io, command, NULL, 0); + return ESP_OK; +} + +esp_err_t esp_lcd_touch_new_i2c_axs15231b(const esp_lcd_panel_io_handle_t io, const esp_lcd_touch_config_t *config, esp_lcd_touch_handle_t *tp) +{ + ESP_RETURN_ON_FALSE(io, ESP_ERR_INVALID_ARG, TAG, "Invalid io"); + ESP_RETURN_ON_FALSE(config, ESP_ERR_INVALID_ARG, TAG, "Invalid config"); + ESP_RETURN_ON_FALSE(tp, ESP_ERR_INVALID_ARG, TAG, "Invalid touch handle"); + + /* Prepare main structure */ + esp_err_t ret = ESP_OK; + esp_lcd_touch_handle_t axs15231b = calloc(1, sizeof(esp_lcd_touch_t)); + ESP_GOTO_ON_FALSE(axs15231b, ESP_ERR_NO_MEM, err, TAG, "Touch handle malloc failed"); + + /* Communication interface */ + axs15231b->io = io; + /* Only supported callbacks are set */ + axs15231b->read_data = touch_axs15231b_read_data; + axs15231b->get_xy = touch_axs15231b_get_xy; + axs15231b->del = touch_axs15231b_del; + /* Mutex */ + axs15231b->data.lock.owner = portMUX_FREE_VAL; + /* Save config */ + memcpy(&axs15231b->config, config, sizeof(esp_lcd_touch_config_t)); + + /* Prepare pin for touch interrupt */ + if (axs15231b->config.int_gpio_num != GPIO_NUM_NC) { + const gpio_config_t int_gpio_config = { + .mode = GPIO_MODE_INPUT, + .intr_type = GPIO_INTR_NEGEDGE, + .pin_bit_mask = BIT64(axs15231b->config.int_gpio_num) + }; + ESP_GOTO_ON_ERROR(gpio_config(&int_gpio_config), err, TAG, "GPIO intr config failed"); + + /* Register interrupt callback */ + if (axs15231b->config.interrupt_callback) { + esp_lcd_touch_register_interrupt_callback(axs15231b, axs15231b->config.interrupt_callback); + } + } + /* Prepare pin for touch controller reset */ + if (axs15231b->config.rst_gpio_num != GPIO_NUM_NC) { + const gpio_config_t rst_gpio_config = { + .mode = GPIO_MODE_OUTPUT, + .pin_bit_mask = BIT64(axs15231b->config.rst_gpio_num) + }; + ESP_GOTO_ON_ERROR(gpio_config(&rst_gpio_config), err, TAG, "GPIO reset config failed"); + } + /* Reset controller */ + ESP_GOTO_ON_ERROR(touch_axs15231b_reset(axs15231b), err, TAG, "Reset failed"); + *tp = axs15231b; + + return ESP_OK; +err: + if (axs15231b) { + touch_axs15231b_del(axs15231b); + } + ESP_LOGE(TAG, "Initialization failed!"); + return ret; +} + +static esp_err_t touch_axs15231b_read_data(esp_lcd_touch_handle_t tp) +{ + typedef struct { + uint8_t gesture; //AXS_TOUCH_GESTURE_POS:0 + uint8_t num; //AXS_TOUCH_POINT_NUM:1 + uint8_t x_h : 4; //AXS_TOUCH_X_H_POS:2 + uint8_t : 2; + uint8_t event : 2; //AXS_TOUCH_EVENT_POS:2 + uint8_t x_l; //AXS_TOUCH_X_L_POS:3 + uint8_t y_h : 4; //AXS_TOUCH_Y_H_POS:4 + uint8_t : 4; + uint8_t y_l; //AXS_TOUCH_Y_L_POS:5 + } __attribute__((packed)) touch_record_struct_t; + + touch_record_struct_t *p_touch_data = NULL; + + uint8_t data[AXS_MAX_TOUCH_NUMBER * 6 + 2] = {0}; /*1 Point:8; 2 Point: 14 */ + const uint8_t read_cmd[11] = {0xb5, 0xab, 0xa5, 0x5a, 0x00, 0x00, (AXS_MAX_TOUCH_NUMBER * 6 + 2) >> 8, (AXS_MAX_TOUCH_NUMBER * 6 + 2) & 0xff, 0x00, 0x00, 0x00}; + + ESP_RETURN_ON_ERROR(i2c_write_bytes(tp, -1, read_cmd, sizeof(read_cmd)), TAG, "I2C write failed"); + ESP_RETURN_ON_ERROR(i2c_read_bytes(tp, -1, data, sizeof(data)), TAG, "I2C read failed"); + + p_touch_data = (touch_record_struct_t *) data; + if (p_touch_data->num && (AXS_MAX_TOUCH_NUMBER >= p_touch_data->num)) { + portENTER_CRITICAL(&tp->data.lock); + tp->data.points = p_touch_data->num; + /* Fill all coordinates */ + for (int i = 0; i < tp->data.points; i++) { + tp->data.coords[i].x = ((p_touch_data->x_h & 0x0F) << 8) | p_touch_data->x_l; + tp->data.coords[i].y = ((p_touch_data->y_h & 0x0F) << 8) | p_touch_data->y_l; + } + portEXIT_CRITICAL(&tp->data.lock); + } + + return ESP_OK; +} + +static bool touch_axs15231b_get_xy(esp_lcd_touch_handle_t tp, uint16_t *x, uint16_t *y, uint16_t *strength, uint8_t *point_num, uint8_t max_point_num) +{ + portENTER_CRITICAL(&tp->data.lock); + /* Count of points */ + *point_num = (tp->data.points > max_point_num ? max_point_num : tp->data.points); + for (size_t i = 0; i < *point_num; i++) { + x[i] = tp->data.coords[i].x; + y[i] = tp->data.coords[i].y; + + if (strength) { + strength[i] = tp->data.coords[i].strength; + } + } + /* Invalidate */ + tp->data.points = 0; + portEXIT_CRITICAL(&tp->data.lock); + + return (*point_num > 0); +} + +static esp_err_t touch_axs15231b_del(esp_lcd_touch_handle_t tp) +{ + /* Reset GPIO pin settings */ + if (tp->config.int_gpio_num != GPIO_NUM_NC) { + gpio_reset_pin(tp->config.int_gpio_num); + } + if (tp->config.rst_gpio_num != GPIO_NUM_NC) { + gpio_reset_pin(tp->config.rst_gpio_num); + } + /* Release memory */ + free(tp); + + return ESP_OK; +} + +static esp_err_t touch_axs15231b_reset(esp_lcd_touch_handle_t tp) +{ + if (tp->config.rst_gpio_num != GPIO_NUM_NC) { + ESP_RETURN_ON_ERROR(gpio_set_level(tp->config.rst_gpio_num, tp->config.levels.reset), TAG, "GPIO set level failed"); + vTaskDelay(pdMS_TO_TICKS(200)); + ESP_RETURN_ON_ERROR(gpio_set_level(tp->config.rst_gpio_num, !tp->config.levels.reset), TAG, "GPIO set level failed"); + vTaskDelay(pdMS_TO_TICKS(200)); + } + + return ESP_OK; +} + +static esp_err_t i2c_read_bytes(esp_lcd_touch_handle_t tp, int reg, uint8_t *data, uint8_t len) +{ + ESP_RETURN_ON_FALSE(data, ESP_ERR_INVALID_ARG, TAG, "Invalid data"); + + return esp_lcd_panel_io_rx_param(tp->io, reg, data, len); +} + +static esp_err_t i2c_write_bytes(esp_lcd_touch_handle_t tp, int reg, const uint8_t *data, uint8_t len) +{ + ESP_RETURN_ON_FALSE(data, ESP_ERR_INVALID_ARG, TAG, "Invalid data"); + + return esp_lcd_panel_io_tx_param(tp->io, reg, data, len); +} diff --git a/src/axs15231b/esp_lcd_axs15231b.h b/src/axs15231b/esp_lcd_axs15231b.h new file mode 100644 index 0000000..038b10d --- /dev/null +++ b/src/axs15231b/esp_lcd_axs15231b.h @@ -0,0 +1,207 @@ +/* + * SPDX-FileCopyrightText: 2022-2024 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ +/** + * @file + * @brief ESP LCD & Touch: AXS15231B + */ + +#pragma once + +#include "hal/spi_ll.h" +#include "../touch/esp_lcd_touch.h" +#include "esp_lcd_panel_vendor.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @brief LCD panel initialization commands. + * + */ +typedef struct { + int cmd; /* +#define C64_BLUE lv_color_make(64,49,141) +#define C64_LIGHT_BLUE lv_color_make(120,105,196) +static void cursor_blink(void*o,int32_t v){lv_obj_set_style_opa((lv_obj_t*)o,v?LV_OPA_COVER:LV_OPA_TRANSP,LV_PART_MAIN);} +void c64_ui_create(void){lv_obj_t*s=lv_scr_act();lv_obj_set_style_bg_color(s,C64_BLUE,LV_PART_MAIN);lv_obj_set_style_bg_opa(s,LV_OPA_COVER,LV_PART_MAIN);lv_obj_clear_flag(s,LV_OBJ_FLAG_SCROLLABLE);lv_obj_t*r=lv_label_create(s);lv_label_set_text(r,"READY.");lv_obj_set_style_text_color(r,C64_LIGHT_BLUE,LV_PART_MAIN);lv_obj_set_style_text_font(r,&lv_font_montserrat_48,LV_PART_MAIN);lv_obj_align(r,LV_ALIGN_TOP_LEFT,12,44);lv_obj_t*c=lv_obj_create(s);lv_obj_remove_style_all(c);lv_obj_set_size(c,29,8);lv_obj_set_style_bg_color(c,C64_LIGHT_BLUE,LV_PART_MAIN);lv_obj_set_style_bg_opa(c,LV_OPA_COVER,LV_PART_MAIN);lv_obj_align_to(c,r,LV_ALIGN_OUT_BOTTOM_LEFT,0,10);lv_anim_t a;lv_anim_init(&a);lv_anim_set_var(&a,c);lv_anim_set_values(&a,1,0);lv_anim_set_time(&a,500);lv_anim_set_playback_time(&a,500);lv_anim_set_repeat_count(&a,LV_ANIM_REPEAT_INFINITE);lv_anim_set_exec_cb(&a,cursor_blink);lv_anim_start(&a);} diff --git a/src/c64_ui.h b/src/c64_ui.h new file mode 100644 index 0000000..0e1146b --- /dev/null +++ b/src/c64_ui.h @@ -0,0 +1,8 @@ +#pragma once +#ifdef __cplusplus +extern "C" { +#endif +void c64_ui_create(void); +#ifdef __cplusplus +} +#endif diff --git a/src/i2c_bsp.c b/src/i2c_bsp.c new file mode 100644 index 0000000..5cae425 --- /dev/null +++ b/src/i2c_bsp.c @@ -0,0 +1,112 @@ +#include +#include "i2c_bsp.h" +#include "user_config.h" +#include "freertos/FreeRTOS.h" + +static i2c_master_bus_handle_t user_i2c_port0_handle = NULL; +static i2c_master_bus_handle_t user_i2c_port1_handle = NULL; +i2c_master_dev_handle_t disp_touch_dev_handle = NULL; +i2c_master_dev_handle_t rtc_dev_handle = NULL; +i2c_master_dev_handle_t imu_dev_handle = NULL; + + +static uint32_t i2c_data_pdMS_TICKS = 0; +static uint32_t i2c_done_pdMS_TICKS = 0; + + +void i2c_master_Init(void) +{ + i2c_data_pdMS_TICKS = pdMS_TO_TICKS(5000); + i2c_done_pdMS_TICKS = pdMS_TO_TICKS(1000); + /*i2c_port 0 init*/ + i2c_master_bus_config_t i2c_bus_config = + { + .clk_source = I2C_CLK_SRC_DEFAULT, + .i2c_port = I2C_NUM_0, + .scl_io_num = ESP_SCL_NUM, + .sda_io_num = ESP_SDA_NUM, + .glitch_ignore_cnt = 7, + .flags = { + .enable_internal_pullup = true, + }, + }; + ESP_ERROR_CHECK(i2c_new_master_bus(&i2c_bus_config, &user_i2c_port0_handle)); + i2c_bus_config.scl_io_num = Touch_SCL_NUM; + i2c_bus_config.sda_io_num = Touch_SDA_NUM; + i2c_bus_config.i2c_port = I2C_NUM_1; + ESP_ERROR_CHECK(i2c_new_master_bus(&i2c_bus_config, &user_i2c_port1_handle)); + + i2c_device_config_t dev_cfg = + { + .dev_addr_length = I2C_ADDR_BIT_LEN_7, + .scl_speed_hz = 300000, + }; + dev_cfg.device_address = EXAMPLE_RTC_ADDR; + ESP_ERROR_CHECK(i2c_master_bus_add_device(user_i2c_port0_handle, &dev_cfg, &rtc_dev_handle)); + + dev_cfg.device_address = EXAMPLE_IMU_ADDR; + ESP_ERROR_CHECK(i2c_master_bus_add_device(user_i2c_port0_handle, &dev_cfg, &imu_dev_handle)); + + dev_cfg.device_address = I2C_TOUCH_ADDR; + ESP_ERROR_CHECK(i2c_master_bus_add_device(user_i2c_port1_handle, &dev_cfg, &disp_touch_dev_handle)); + +} + +uint8_t i2c_write_buff(i2c_master_dev_handle_t dev_handle,int reg,uint8_t *buf,uint8_t len) +{ + uint8_t ret; + uint8_t *pbuf = NULL; + ret = i2c_master_bus_wait_all_done(user_i2c_port0_handle,i2c_done_pdMS_TICKS); + if(ret != ESP_OK) + return ret; + if(reg == -1) + { + ret = i2c_master_transmit(dev_handle,buf,len,i2c_data_pdMS_TICKS); + } + else + { + pbuf = (uint8_t*)malloc(len+1); + pbuf[0] = reg; + for(uint8_t i = 0; i +#include "lcd_bl_pwm_bsp.h" +#include "esp_err.h" +#include "driver/ledc.h" +#include "driver/gpio.h" +#include "user_config.h" + +void gpio_init(void) +{ + gpio_config_t gpio_conf = {}; + gpio_conf.intr_type = GPIO_INTR_DISABLE; + gpio_conf.mode = GPIO_MODE_OUTPUT; + gpio_conf.pin_bit_mask = ((uint64_t)0X01<user_data; + const int flush_coun = (LVGL_SPIRAM_BUFF_LEN / LVGL_DMA_BUFF_LEN); + const int offgap = (LCD_NOROT_VRES / flush_coun); + const int dmalen = (LVGL_DMA_BUFF_LEN / 2); + int offsetx1 = 0; + int offsety1 = 0; + int offsetx2 = LCD_NOROT_HRES; + int offsety2 = offgap; + +#if (Rotated == USER_DISP_ROT_90) + uint16_t *map = (uint16_t *)rotat_ptr; +#else + uint16_t *map = (uint16_t *)color_map; +#endif + + xSemaphoreGive(lvgl_flush_semap); + for(int i = 0; i0 && buff[1]<5) + { + data->state = LV_INDEV_STATE_PR; +#if (Rotated == USER_DISP_ROT_NONO) + if(pointX > EXAMPLE_LCD_V_RES) pointX = EXAMPLE_LCD_V_RES; + if(pointY > EXAMPLE_LCD_H_RES) pointY = EXAMPLE_LCD_H_RES; + data->point.x = pointY; + data->point.y = (EXAMPLE_LCD_V_RES-pointX); +#else + if(pointX > EXAMPLE_LCD_H_RES) pointX = EXAMPLE_LCD_H_RES; + if(pointY > EXAMPLE_LCD_V_RES) pointY = EXAMPLE_LCD_V_RES; + data->point.x = (EXAMPLE_LCD_H_RES - pointX); + data->point.y = (EXAMPLE_LCD_V_RES - pointY); +#endif + } + else + { + data->state = LV_INDEV_STATE_REL; + } +} + +static bool example_lvgl_lock(int timeout_ms) +{ + const TickType_t timeout_ticks = (timeout_ms == -1) ? portMAX_DELAY : pdMS_TO_TICKS(timeout_ms); + return xSemaphoreTake(lvgl_mux, timeout_ticks) == pdTRUE; +} + +static void example_lvgl_unlock(void) +{ + assert(lvgl_mux && "bsp_display_start must be called first"); + xSemaphoreGive(lvgl_mux); +} + +void example_lvgl_port_task(void *arg) +{ + uint32_t task_delay_ms = EXAMPLE_LVGL_TASK_MAX_DELAY_MS; + for(;;) + { + if (example_lvgl_lock(-1)) + { + task_delay_ms = lv_timer_handler(); + //Release the mutex + example_lvgl_unlock(); + } + if (task_delay_ms > EXAMPLE_LVGL_TASK_MAX_DELAY_MS) + { + task_delay_ms = EXAMPLE_LVGL_TASK_MAX_DELAY_MS; + } else if (task_delay_ms < EXAMPLE_LVGL_TASK_MIN_DELAY_MS) + { + task_delay_ms = EXAMPLE_LVGL_TASK_MIN_DELAY_MS; + } + vTaskDelay(pdMS_TO_TICKS(task_delay_ms)); + } +} + +void lvgl_port_init(void) +{ +#if (Rotated == USER_DISP_ROT_90) + rotat_ptr = (uint16_t*)heap_caps_malloc(EXAMPLE_LCD_H_RES * EXAMPLE_LCD_V_RES * sizeof(uint16_t), MALLOC_CAP_SPIRAM); + assert(rotat_ptr); +#endif + lvgl_flush_semap = xSemaphoreCreateBinary(); + + static lv_disp_draw_buf_t disp_buf; // contains internal graphic buffer(s) called draw buffer(s) + static lv_disp_drv_t disp_drv; // contains callback functions + ESP_LOGI(TAG, "Initialize LCD RESET GPIO"); + + gpio_config_t gpio_conf = {}; + gpio_conf.intr_type = GPIO_INTR_DISABLE; + gpio_conf.mode = GPIO_MODE_OUTPUT; + gpio_conf.pin_bit_mask = ((uint64_t)0X01< +#include "i2c_bsp.h" +#include "lvgl_port.h" +#include "lcd_bl_bsp/lcd_bl_pwm_bsp.h" +void setup(){Serial.begin(115200);i2c_master_Init();lvgl_port_init();lcd_bl_pwm_bsp_init(LCD_PWM_MODE_255);Serial.println("READY.");} +void loop(){delay(1000);} diff --git a/src/touch/esp_lcd_touch.c b/src/touch/esp_lcd_touch.c new file mode 100644 index 0000000..2e6034f --- /dev/null +++ b/src/touch/esp_lcd_touch.c @@ -0,0 +1,266 @@ +/* + * SPDX-FileCopyrightText: 2015-2023 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" +#include "driver/gpio.h" +#include "esp_system.h" +#include "esp_err.h" +#include "esp_check.h" +#include "esp_log.h" +#include "esp_lcd_touch.h" + +static const char *TAG = "TP"; + +/******************************************************************************* +* Function definitions +*******************************************************************************/ + +/******************************************************************************* +* Local variables +*******************************************************************************/ + +/******************************************************************************* +* Public API functions +*******************************************************************************/ + +esp_err_t esp_lcd_touch_enter_sleep(esp_lcd_touch_handle_t tp) +{ + assert(tp != NULL); + if (tp->enter_sleep == NULL) { + ESP_LOGE(TAG, "Sleep mode not supported!"); + return ESP_FAIL; + } else { + return tp->enter_sleep(tp); + } +} + +esp_err_t esp_lcd_touch_exit_sleep(esp_lcd_touch_handle_t tp) +{ + assert(tp != NULL); + if (tp->exit_sleep == NULL) { + ESP_LOGE(TAG, "Sleep mode not supported!"); + return ESP_FAIL; + } else { + return tp->exit_sleep(tp); + } +} + +esp_err_t esp_lcd_touch_read_data(esp_lcd_touch_handle_t tp) +{ + assert(tp != NULL); + assert(tp->read_data != NULL); + + return tp->read_data(tp); +} + +bool esp_lcd_touch_get_coordinates(esp_lcd_touch_handle_t tp, uint16_t *x, uint16_t *y, uint16_t *strength, uint8_t *point_num, uint8_t max_point_num) +{ + bool touched = false; + + assert(tp != NULL); + assert(x != NULL); + assert(y != NULL); + assert(tp->get_xy != NULL); + + touched = tp->get_xy(tp, x, y, strength, point_num, max_point_num); + if (!touched) { + return false; + } + + /* Process coordinates by user */ + if (tp->config.process_coordinates != NULL) { + tp->config.process_coordinates(tp, x, y, strength, point_num, max_point_num); + } + + /* Software coordinates adjustment needed */ + bool sw_adj_needed = ((tp->config.flags.mirror_x && (tp->set_mirror_x == NULL)) || + (tp->config.flags.mirror_y && (tp->set_mirror_y == NULL)) || + (tp->config.flags.swap_xy && (tp->set_swap_xy == NULL))); + + /* Adjust all coordinates */ + for (int i = 0; (sw_adj_needed && i < *point_num); i++) { + + /* Mirror X coordinates (if not supported by HW) */ + if (tp->config.flags.mirror_x && tp->set_mirror_x == NULL) { + x[i] = tp->config.x_max - x[i]; + } + + /* Mirror Y coordinates (if not supported by HW) */ + if (tp->config.flags.mirror_y && tp->set_mirror_y == NULL) { + y[i] = tp->config.y_max - y[i]; + } + + /* Swap X and Y coordinates (if not supported by HW) */ + if (tp->config.flags.swap_xy && tp->set_swap_xy == NULL) { + uint16_t tmp = x[i]; + x[i] = y[i]; + y[i] = tmp; + } + } + + return touched; +} + +#if (CONFIG_ESP_LCD_TOUCH_MAX_BUTTONS > 0) +esp_err_t esp_lcd_touch_get_button_state(esp_lcd_touch_handle_t tp, uint8_t n, uint8_t *state) +{ + assert(tp != NULL); + assert(state != NULL); + + *state = 0; + + if (tp->get_button_state) { + return tp->get_button_state(tp, n, state); + } else { + return ESP_ERR_NOT_SUPPORTED; + } + + return ESP_OK; +} +#endif + +esp_err_t esp_lcd_touch_set_swap_xy(esp_lcd_touch_handle_t tp, bool swap) +{ + assert(tp != NULL); + + tp->config.flags.swap_xy = swap; + + /* Is swap supported by HW? */ + if (tp->set_swap_xy) { + return tp->set_swap_xy(tp, swap); + } + + return ESP_OK; +} + +esp_err_t esp_lcd_touch_get_swap_xy(esp_lcd_touch_handle_t tp, bool *swap) +{ + assert(tp != NULL); + assert(swap != NULL); + + /* Is swap supported by HW? */ + if (tp->get_swap_xy) { + return tp->get_swap_xy(tp, swap); + } else { + *swap = tp->config.flags.swap_xy; + } + + return ESP_OK; +} + +esp_err_t esp_lcd_touch_set_mirror_x(esp_lcd_touch_handle_t tp, bool mirror) +{ + assert(tp != NULL); + + tp->config.flags.mirror_x = mirror; + + /* Is mirror supported by HW? */ + if (tp->set_mirror_x) { + return tp->set_mirror_x(tp, mirror); + } + + return ESP_OK; +} + +esp_err_t esp_lcd_touch_get_mirror_x(esp_lcd_touch_handle_t tp, bool *mirror) +{ + assert(tp != NULL); + assert(mirror != NULL); + + /* Is swap supported by HW? */ + if (tp->get_mirror_x) { + return tp->get_mirror_x(tp, mirror); + } else { + *mirror = tp->config.flags.mirror_x; + } + + return ESP_OK; +} + +esp_err_t esp_lcd_touch_set_mirror_y(esp_lcd_touch_handle_t tp, bool mirror) +{ + assert(tp != NULL); + + tp->config.flags.mirror_y = mirror; + + /* Is mirror supported by HW? */ + if (tp->set_mirror_y) { + return tp->set_mirror_y(tp, mirror); + } + + return ESP_OK; +} + +esp_err_t esp_lcd_touch_get_mirror_y(esp_lcd_touch_handle_t tp, bool *mirror) +{ + assert(tp != NULL); + assert(mirror != NULL); + + /* Is swap supported by HW? */ + if (tp->get_mirror_y) { + return tp->get_mirror_y(tp, mirror); + } else { + *mirror = tp->config.flags.mirror_y; + } + + return ESP_OK; +} + +esp_err_t esp_lcd_touch_del(esp_lcd_touch_handle_t tp) +{ + assert(tp != NULL); + + if (tp->del != NULL) { + return tp->del(tp); + } + + return ESP_OK; +} + +esp_err_t esp_lcd_touch_register_interrupt_callback(esp_lcd_touch_handle_t tp, esp_lcd_touch_interrupt_callback_t callback) +{ + esp_err_t ret = ESP_OK; + assert(tp != NULL); + + /* Interrupt pin is not selected */ + if (tp->config.int_gpio_num == GPIO_NUM_NC) { + return ESP_ERR_INVALID_ARG; + } + + tp->config.interrupt_callback = callback; + + if (callback != NULL) { + ret = gpio_install_isr_service(0); + /* ISR service can be installed from user before, then it returns invalid state */ + if (ret != ESP_OK && ret != ESP_ERR_INVALID_STATE) { + ESP_LOGE(TAG, "GPIO ISR install failed"); + return ret; + } + /* Add GPIO ISR handler */ + ret = gpio_intr_enable(tp->config.int_gpio_num); + ESP_RETURN_ON_ERROR(ret, TAG, "GPIO ISR install failed"); + ret = gpio_isr_handler_add(tp->config.int_gpio_num, (gpio_isr_t)tp->config.interrupt_callback, tp); + ESP_RETURN_ON_ERROR(ret, TAG, "GPIO ISR install failed"); + } else { + /* Remove GPIO ISR handler */ + ret = gpio_isr_handler_remove(tp->config.int_gpio_num); + ESP_RETURN_ON_ERROR(ret, TAG, "GPIO ISR remove handler failed"); + ret = gpio_intr_disable(tp->config.int_gpio_num); + ESP_RETURN_ON_ERROR(ret, TAG, "GPIO ISR disable failed"); + } + + return ESP_OK; +} + +esp_err_t esp_lcd_touch_register_interrupt_callback_with_data(esp_lcd_touch_handle_t tp, esp_lcd_touch_interrupt_callback_t callback, void *user_data) +{ + assert(tp != NULL); + + tp->config.user_data = user_data; + return esp_lcd_touch_register_interrupt_callback(tp, callback); +} diff --git a/src/touch/esp_lcd_touch.h b/src/touch/esp_lcd_touch.h new file mode 100644 index 0000000..db075ac --- /dev/null +++ b/src/touch/esp_lcd_touch.h @@ -0,0 +1,433 @@ +/* + * SPDX-FileCopyrightText: 2022-2024 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +/** + * @file + * @brief ESP LCD touch + */ + +#pragma once + +#include +#include "sdkconfig.h" +#include "esp_err.h" +#include "driver/gpio.h" +#include "esp_lcd_panel_io.h" +#include "freertos/FreeRTOS.h" +#include "freertos/semphr.h" + +/*为了避免报错*/ +#define CONFIG_ESP_LCD_TOUCH_MAX_POINTS 5 +#define CONFIG_ESP_LCD_TOUCH_MAX_BUTTONS 1 + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @brief Touch controller type + * + */ +typedef struct esp_lcd_touch_s esp_lcd_touch_t; +typedef esp_lcd_touch_t *esp_lcd_touch_handle_t; + +/** + * @brief Touch controller interrupt callback type + * + */ +typedef void (*esp_lcd_touch_interrupt_callback_t)(esp_lcd_touch_handle_t tp); + +/** + * @brief Touch Configuration Type + * + */ +typedef struct { + uint16_t x_max; /*!< X coordinates max (for mirroring) */ + uint16_t y_max; /*!< Y coordinates max (for mirroring) */ + + gpio_num_t rst_gpio_num; /*!< GPIO number of reset pin */ + gpio_num_t int_gpio_num; /*!< GPIO number of interrupt pin */ + + struct { + unsigned int reset: 1; /*!< Level of reset pin in reset */ + unsigned int interrupt: 1;/*!< Active Level of interrupt pin */ + } levels; + + struct { + unsigned int swap_xy: 1; /*!< Swap X and Y after read coordinates */ + unsigned int mirror_x: 1; /*!< Mirror X after read coordinates */ + unsigned int mirror_y: 1; /*!< Mirror Y after read coordinates */ + } flags; + + /*!< User callback called after get coordinates from touch controller for apply user adjusting */ + void (*process_coordinates)(esp_lcd_touch_handle_t tp, uint16_t *x, uint16_t *y, uint16_t *strength, uint8_t *point_num, uint8_t max_point_num); + /*!< User callback called after the touch interrupt occurred */ + esp_lcd_touch_interrupt_callback_t interrupt_callback; + /*!< User data passed to callback */ + void *user_data; + /*!< User data passed to driver */ + void *driver_data; +} esp_lcd_touch_config_t; + +typedef struct { + uint8_t points; /*!< Count of touch points saved */ + + struct { + uint16_t x; /*!< X coordinate */ + uint16_t y; /*!< Y coordinate */ + uint16_t strength; /*!< Strength */ + } coords[CONFIG_ESP_LCD_TOUCH_MAX_POINTS]; + +#if (CONFIG_ESP_LCD_TOUCH_MAX_BUTTONS > 0) + uint8_t buttons; /*!< Count of buttons states saved */ + + struct { + uint8_t status; /*!< Status of button */ + } button[CONFIG_ESP_LCD_TOUCH_MAX_BUTTONS]; +#endif + + portMUX_TYPE lock; /*!< Lock for read/write */ +} esp_lcd_touch_data_t; + +/** + * @brief Declare of Touch Type + * + */ +struct esp_lcd_touch_s { + + /** + * @brief set touch controller into sleep mode + * + * @note This function is usually blocking. + * + * @param tp: Touch handler + * + * @return + * - ESP_OK on success, otherwise returns ESP_ERR_xxx + */ + esp_err_t (*enter_sleep)(esp_lcd_touch_handle_t tp); + + /** + * @brief set touch controller into normal mode + * + * @note This function is usually blocking. + * + * @param tp: Touch handler + * + * @return + * - ESP_OK on success, otherwise returns ESP_ERR_xxx + */ + esp_err_t (*exit_sleep)(esp_lcd_touch_handle_t tp); + + /** + * @brief Read data from touch controller (mandatory) + * + * @note This function is usually blocking. + * + * @param tp: Touch handler + * + * @return + * - ESP_OK on success, otherwise returns ESP_ERR_xxx + */ + esp_err_t (*read_data)(esp_lcd_touch_handle_t tp); + + /** + * @brief Get coordinates from touch controller (mandatory) + * + * @param tp: Touch handler + * @param x: Array of X coordinates + * @param y: Array of Y coordinates + * @param strength: Array of strengths + * @param point_num: Count of points touched (equals with count of items in x and y array) + * @param max_point_num: Maximum count of touched points to return (equals with max size of x and y array) + * + * @return + * - Returns true, when touched and coordinates readed. Otherwise returns false. + */ + bool (*get_xy)(esp_lcd_touch_handle_t tp, uint16_t *x, uint16_t *y, uint16_t *strength, uint8_t *point_num, uint8_t max_point_num); + + +#if (CONFIG_ESP_LCD_TOUCH_MAX_BUTTONS > 0) + /** + * @brief Get button state (optional) + * + * @param tp: Touch handler + * @param n: Button index + * @param state: Button state + * + * @return + * - Returns true, when touched and coordinates readed. Otherwise returns false. + */ + esp_err_t (*get_button_state)(esp_lcd_touch_handle_t tp, uint8_t n, uint8_t *state); +#endif + + /** + * @brief Swap X and Y after read coordinates (optional) + * If set, then not used SW swapping. + * + * @param tp: Touch handler + * @param swap: Set swap value + * + * @return + * - ESP_OK on success, otherwise returns ESP_ERR_xxx + */ + esp_err_t (*set_swap_xy)(esp_lcd_touch_handle_t tp, bool swap); + + /** + * @brief Are X and Y coordinates swapped (optional) + * + * @param tp: Touch handler + * @param swap: Get swap value + * + * @return + * - ESP_OK on success, otherwise returns ESP_ERR_xxx + */ + esp_err_t (*get_swap_xy)(esp_lcd_touch_handle_t tp, bool *swap); + + /** + * @brief Mirror X after read coordinates + * If set, then not used SW mirroring. + * + * @param tp: Touch handler + * @param mirror: Set X mirror value + * + * @return + * - ESP_OK on success, otherwise returns ESP_ERR_xxx + */ + esp_err_t (*set_mirror_x)(esp_lcd_touch_handle_t tp, bool mirror); + + /** + * @brief Is mirrored X (optional) + * + * @param tp: Touch handler + * @param mirror: Get X mirror value + * + * @return + * - ESP_OK on success, otherwise returns ESP_ERR_xxx + */ + esp_err_t (*get_mirror_x)(esp_lcd_touch_handle_t tp, bool *mirror); + + /** + * @brief Mirror Y after read coordinates + * If set, then not used SW mirroring. + * + * @param tp: Touch handler + * @param mirror: Set Y mirror value + * + * @return + * - ESP_OK on success, otherwise returns ESP_ERR_xxx + */ + esp_err_t (*set_mirror_y)(esp_lcd_touch_handle_t tp, bool mirror); + + /** + * @brief Is mirrored Y (optional) + * + * @param tp: Touch handler + * @param mirror: Get Y mirror value + * + * @return + * - ESP_OK on success, otherwise returns ESP_ERR_xxx + */ + esp_err_t (*get_mirror_y)(esp_lcd_touch_handle_t tp, bool *mirror); + + /** + * @brief Delete Touch + * + * @param tp: Touch handler + * + * @return + * - ESP_OK on success, otherwise returns ESP_ERR_xxx + */ + esp_err_t (*del)(esp_lcd_touch_handle_t tp); + + /** + * @brief Configuration structure + */ + esp_lcd_touch_config_t config; + + /** + * @brief Communication interface + */ + esp_lcd_panel_io_handle_t io; + + /** + * @brief Data structure + */ + esp_lcd_touch_data_t data; +}; + +/** + * @brief Read data from touch controller + * + * @note This function is usually blocking. + * + * @param tp: Touch handler + * + * @return + * - ESP_OK on success + * - ESP_ERR_INVALID_ARG parameter error + * - ESP_FAIL sending command error, slave hasn't ACK the transfer + * - ESP_ERR_INVALID_STATE I2C driver not installed or not in master mode + * - ESP_ERR_TIMEOUT operation timeout because the bus is busy + */ +esp_err_t esp_lcd_touch_read_data(esp_lcd_touch_handle_t tp); + +/** + * @brief Read coordinates from touch controller + * + * @param tp: Touch handler + * @param x: Array of X coordinates + * @param y: Array of Y coordinates + * @param strength: Array of the strengths (can be NULL) + * @param point_num: Count of points touched (equals with count of items in x and y array) + * @param max_point_num: Maximum count of touched points to return (equals with max size of x and y array) + * + * @return + * - Returns true, when touched and coordinates readed. Otherwise returns false. + */ +bool esp_lcd_touch_get_coordinates(esp_lcd_touch_handle_t tp, uint16_t *x, uint16_t *y, uint16_t *strength, uint8_t *point_num, uint8_t max_point_num); + + +#if (CONFIG_ESP_LCD_TOUCH_MAX_BUTTONS > 0) +/** + * @brief Get button state + * + * @param tp: Touch handler + * @param n: Button index + * @param state: Button state + * + * @return + * - ESP_OK on success + * - ESP_ERR_NOT_SUPPORTED if this function is not supported by controller + * - ESP_ERR_INVALID_ARG if bad button index + */ +esp_err_t esp_lcd_touch_get_button_state(esp_lcd_touch_handle_t tp, uint8_t n, uint8_t *state); +#endif + +/** + * @brief Swap X and Y after read coordinates + * + * @param tp: Touch handler + * @param swap: Set swap value + * + * @return + * - ESP_OK on success + */ +esp_err_t esp_lcd_touch_set_swap_xy(esp_lcd_touch_handle_t tp, bool swap); + +/** + * @brief Are X and Y coordinates swapped + * + * @param tp: Touch handler + * @param swap: Get swap value + * + * @return + * - ESP_OK on success + */ +esp_err_t esp_lcd_touch_get_swap_xy(esp_lcd_touch_handle_t tp, bool *swap); + +/** + * @brief Mirror X after read coordinates + * + * @param tp: Touch handler + * @param mirror: Set X mirror value + * + * @return + * - ESP_OK on success + */ +esp_err_t esp_lcd_touch_set_mirror_x(esp_lcd_touch_handle_t tp, bool mirror); + +/** + * @brief Is mirrored X + * + * @param tp: Touch handler + * @param mirror: Get X mirror value + * + * @return + * - ESP_OK on success + */ +esp_err_t esp_lcd_touch_get_mirror_x(esp_lcd_touch_handle_t tp, bool *mirror); + +/** + * @brief Mirror Y after read coordinates + * + * @param tp: Touch handler + * @param mirror: Set Y mirror value + * + * @return + * - ESP_OK on success + */ +esp_err_t esp_lcd_touch_set_mirror_y(esp_lcd_touch_handle_t tp, bool mirror); + +/** + * @brief Is mirrored Y + * + * @param tp: Touch handler + * @param mirror: Get Y mirror value + * + * @return + * - ESP_OK on success + */ +esp_err_t esp_lcd_touch_get_mirror_y(esp_lcd_touch_handle_t tp, bool *mirror); + +/** + * @brief Delete touch (free all allocated memory and restart HW) + * + * @param tp: Touch handler + * + * @return + * - ESP_OK on success + */ +esp_err_t esp_lcd_touch_del(esp_lcd_touch_handle_t tp); + +/** + * @brief Register user callback called after the touch interrupt occurred + * + * @param tp: Touch handler + * @param callback: Interrupt callback + * + * @return + * - ESP_OK on success + */ +esp_err_t esp_lcd_touch_register_interrupt_callback(esp_lcd_touch_handle_t tp, esp_lcd_touch_interrupt_callback_t callback); + +/** + * @brief Register user callback called after the touch interrupt occurred with user data + * + * @param tp: Touch handler + * @param callback: Interrupt callback + * @param user_data: User data passed to callback + * + * @return + * - ESP_OK on success + */ +esp_err_t esp_lcd_touch_register_interrupt_callback_with_data(esp_lcd_touch_handle_t tp, esp_lcd_touch_interrupt_callback_t callback, void *user_data); + +/** + * @brief Enter sleep mode + * + * @param tp: Touch handler + * + * @return + * - ESP_OK on success + * - ESP_ERR_INVALID_ARG if parameter is invalid + */ +esp_err_t esp_lcd_touch_enter_sleep(esp_lcd_touch_handle_t tp); + +/** + * @brief Exit sleep mode + * + * @param tp: Touch handler + * + * @return + * - ESP_OK on success + * - ESP_ERR_INVALID_ARG if parameter is invalid + */ +esp_err_t esp_lcd_touch_exit_sleep(esp_lcd_touch_handle_t tp); + +#ifdef __cplusplus +} +#endif diff --git a/src/user_config.h b/src/user_config.h new file mode 100644 index 0000000..9a1535c --- /dev/null +++ b/src/user_config.h @@ -0,0 +1,69 @@ +#ifndef USER_CONFIG_H +#define USER_CONFIG_H + +//spi & i2c handle +#define LCD_HOST SPI3_HOST + +// touch I2C port +#define Touch_SCL_NUM (GPIO_NUM_18) +#define Touch_SDA_NUM (GPIO_NUM_17) + +// touch esp +#define ESP_SCL_NUM (GPIO_NUM_48) +#define ESP_SDA_NUM (GPIO_NUM_47) + +// DISP +#define EXAMPLE_PIN_NUM_LCD_CS (GPIO_NUM_9) +#define EXAMPLE_PIN_NUM_LCD_PCLK (GPIO_NUM_10) +#define EXAMPLE_PIN_NUM_LCD_DATA0 (GPIO_NUM_11) +#define EXAMPLE_PIN_NUM_LCD_DATA1 (GPIO_NUM_12) +#define EXAMPLE_PIN_NUM_LCD_DATA2 (GPIO_NUM_13) +#define EXAMPLE_PIN_NUM_LCD_DATA3 (GPIO_NUM_14) +#define EXAMPLE_PIN_NUM_LCD_RST (GPIO_NUM_21) +#define EXAMPLE_PIN_NUM_BK_LIGHT (GPIO_NUM_8) + + +#define I2C_TOUCH_ADDR 0x3b +#define EXAMPLE_PIN_NUM_TOUCH_RST (-1) +#define EXAMPLE_PIN_NUM_TOUCH_INT (-1) + + +#define EXAMPLE_LVGL_TICK_PERIOD_MS 5 +#define EXAMPLE_LVGL_TASK_MAX_DELAY_MS 500 +#define EXAMPLE_LVGL_TASK_MIN_DELAY_MS 5 + + +/*bl test*/ +#define Backlight_Testing 0 + +/*ADDR*/ +#define EXAMPLE_RTC_ADDR 0x51 + +#define EXAMPLE_IMU_ADDR 0x6b + + +#define USER_DISP_ROT_90 1 +#define USER_DISP_ROT_NONO 0 +#define Rotated USER_DISP_ROT_NONO //软件实现旋转 + + + + +#if (Rotated == USER_DISP_ROT_NONO) +#define EXAMPLE_LCD_H_RES 172 +#define EXAMPLE_LCD_V_RES 640 +#else +#define EXAMPLE_LCD_H_RES 640 +#define EXAMPLE_LCD_V_RES 172 +#endif + +#define LCD_NOROT_HRES 172 +#define LCD_NOROT_VRES 640 +#define LVGL_DMA_BUFF_LEN (LCD_NOROT_HRES * 64 * 2) +#define LVGL_SPIRAM_BUFF_LEN (EXAMPLE_LCD_H_RES * EXAMPLE_LCD_V_RES * 2) + + + + + +#endif \ No newline at end of file