diff --git a/firmware/CMakeLists.txt b/firmware/CMakeLists.txt index 5ad5f86..1733075 100644 --- a/firmware/CMakeLists.txt +++ b/firmware/CMakeLists.txt @@ -2,7 +2,7 @@ # CMakeLists in this exact order for cmake to work correctly cmake_minimum_required(VERSION 3.16) -set(PROJECT_VER "1.4.3") +set(PROJECT_VER "1.5.1") add_definitions(-DFIRMWARE_VERSION=\"${PROJECT_VER}\") # Add this line to disable the specific warning diff --git a/firmware/main/hal/board/stackchan.cc b/firmware/main/hal/board/stackchan.cc index 7349b14..88963ff 100644 --- a/firmware/main/hal/board/stackchan.cc +++ b/firmware/main/hal/board/stackchan.cc @@ -24,6 +24,51 @@ #define XPOWERS_AXP2101_ICC_CHG_SET (0x62) +namespace { + +struct LcdInitCommand { + uint8_t command; + uint8_t data[15]; + uint8_t data_size; + uint16_t delay_ms; +}; + +constexpr LcdInitCommand kIli9342eInitCommands[] = { + {0xDD, {0x01}, 1, 0}, + {0x3A, {0x55}, 1, 0}, + {0x21, {}, 0, 0}, + {0x36, {0x08}, 1, 0}, + {0xD5, {0x00}, 1, 0}, + {0xB1, {0x22}, 1, 0}, + {0xC8, {0x38}, 1, 0}, + {0xCB, {0x1C}, 1, 0}, + {0xC9, {0x1A}, 1, 0}, + {0xCA, {0x1A}, 1, 0}, + {0xB7, {0x5A, 0x41, 0x11, 0x19}, 4, 0}, + {0xE4, {0x04, 0x08, 0x11, 0x06, 0x12, 0x07, 0x3A, 0x76, 0x47, 0x07, 0x0F, 0x0A, 0x11, 0x19, 0x05}, 15, 0}, + {0xE5, {0x02, 0x03, 0x07, 0x06, 0x12, 0x07, 0x36, 0x5F, 0x48, 0x06, 0x10, 0x0C, 0x16, 0x14, 0x09}, 15, 0}, + {0x11, {}, 0, 120}, + {0x29, {}, 0, 120}, +}; + +esp_err_t ApplyIli9342eInit(esp_lcd_panel_io_handle_t panel_io) +{ + for (const auto& init_command : kIli9342eInitCommands) { + const void* data = init_command.data_size == 0 ? nullptr : init_command.data; + esp_err_t err = esp_lcd_panel_io_tx_param(panel_io, init_command.command, data, init_command.data_size); + if (err != ESP_OK) { + ESP_LOGE(TAG, "ILI9342E command 0x%02X failed: %s", init_command.command, esp_err_to_name(err)); + return err; + } + if (init_command.delay_ms != 0) { + vTaskDelay(pdMS_TO_TICKS(init_command.delay_ms)); + } + } + return ESP_OK; +} + +} // namespace + class Pmic : public Axp2101 { public: /** @@ -177,6 +222,12 @@ public: class Ft6336 : public I2cDevice { public: + struct ControllerVersion { + uint8_t cipher = 0; + uint8_t firm_id = 0; + uint8_t vendor = 0; + }; + struct TouchPoint_t { int num = 0; int x = -1; @@ -185,8 +236,6 @@ public: Ft6336(i2c_master_bus_handle_t i2c_bus, uint8_t addr) : I2cDevice(i2c_bus, addr) { - uint8_t chip_id = ReadReg(0xA3); - ESP_LOGI(TAG, "Get chip ID: 0x%02X", chip_id); read_buffer_ = new uint8_t[6]; } @@ -195,6 +244,34 @@ public: delete[] read_buffer_; } + bool ReadControllerVersion(ControllerVersion& version) + { + constexpr int kMaxAttempts = 5; + constexpr int kRetryDelayMs = 20; + + version = {}; + for (int attempt = 0; attempt < kMaxAttempts; ++attempt) { + const uint8_t work_mode[] = {0x00, 0x00}; + uint8_t info[6] = {}; + esp_err_t err = i2c_master_transmit(i2c_device_, work_mode, sizeof(work_mode), 100); + if (err == ESP_OK) { + err = TryReadRegs(0xA3, info, sizeof(info)); + } + + if (err == ESP_OK && info[0] != 0 && info[5] != 0) { + version.cipher = info[0]; + version.firm_id = info[3]; + version.vendor = info[5]; + return true; + } + + if (attempt + 1 < kMaxAttempts) { + vTaskDelay(pdMS_TO_TICKS(kRetryDelayMs)); + } + } + return false; + } + bool UpdateTouchPoint() { auto err = TryReadRegs(0x02, read_buffer_, 6); @@ -371,11 +448,14 @@ private: hal_bridge::set_touch_point(touch_point.num, touch_point.x, touch_point.y); } - void InitializeFt6336TouchPad() + void InitializeFt6336() { ESP_LOGI(TAG, "Init FT6336"); ft6336_ = new Ft6336(i2c_bus_, 0x38); + } + void StartTouchpadTimer() + { // 创建定时器,20ms 间隔 esp_timer_create_args_t timer_args = { .callback = @@ -435,6 +515,22 @@ private: aw9523_->ResetIli9342(); esp_lcd_panel_init(panel); + + Ft6336::ControllerVersion touch_version; + const bool touch_version_valid = ft6336_->ReadControllerVersion(touch_version); + const bool is_ili9342e = touch_version_valid && touch_version.firm_id == 0x12 && touch_version.vendor == 0x11; + if (touch_version_valid) { + ESP_LOGI(TAG, "CIPHER:0x%02X / FIRMID:0x%02X / VENDID:0x%02X, panel:%s", touch_version.cipher, + touch_version.firm_id, touch_version.vendor, is_ili9342e ? "ILI9342E" : "ILI9342C"); + } else { + ESP_LOGW(TAG, "FT6336 version read failed, panel:ILI9342C"); + } + + if (is_ili9342e) { + ESP_LOGI(TAG, "Apply ILI9342E panel initialization"); + ESP_ERROR_CHECK(ApplyIli9342eInit(panel_io)); + } + esp_lcd_panel_invert_color(panel, true); esp_lcd_panel_swap_xy(panel, DISPLAY_SWAP_XY); esp_lcd_panel_mirror(panel, DISPLAY_MIRROR_X, DISPLAY_MIRROR_Y); @@ -498,10 +594,11 @@ public: InitializePowerSaveTimer(); InitializeAw9523(); I2cDetect(); + InitializeFt6336(); InitializeSpi(); InitializeIli9342Display(); InitializeCamera(); - InitializeFt6336TouchPad(); + StartTouchpadTimer(); GetBacklight()->RestoreBrightness(); }