From d7c5d51661d6126524033ce6234caaef766b5db9 Mon Sep 17 00:00:00 2001 From: Gustavo Cateim Date: Fri, 10 Jul 2026 16:19:11 -0300 Subject: [PATCH] input: release touch handles before deleting the i2c bus Since ESP-IDF v5.5 (espressif/esp-idf@e73d78ba) i2c_del_master_bus() refuses to delete a bus that still has devices attached and returns ESP_ERR_INVALID_STATE. The touchscreen task registers a panel IO device on the bus but never releases it, so under IDF 5.5.4 the first touchscreen_deinit() fails and aborts via ESP_ERROR_CHECK. On boards that panic with PRINT_HALT, such as the Waveshare S3 Touch LCD 2, this freezes the device on the boot splash screen while gathering camera entropy, and also hangs the QR scanner and the camera exit path, since the touchscreen is restarted around every camera start/stop (see #306). Releasing the touch handle and the panel IO device (which removes it from the bus) before deleting the bus restores the deinit/init cycle, and also fixes a heap leak present under IDF 5.4 where both handles leaked on every cycle. Co-authored-by: oroderico --- main/input/touchscreen.inc | 3 +++ 1 file changed, 3 insertions(+) diff --git a/main/input/touchscreen.inc b/main/input/touchscreen.inc index 7607db01..4f1540f1 100644 --- a/main/input/touchscreen.inc +++ b/main/input/touchscreen.inc @@ -91,6 +91,9 @@ static void touchscreen_task(void* ignored) } vTaskDelay(20 / portTICK_PERIOD_MS); } + // Since IDF 5.5 the i2c bus cannot be deleted while devices are still attached + ESP_ERROR_CHECK(esp_lcd_touch_del(ret_touch)); + ESP_ERROR_CHECK(esp_lcd_panel_io_del(tp_io_handle)); ESP_ERROR_CHECK(_i2c_deinit(touch_i2c_handle)); shutdown_finished = true; vTaskDelete(NULL);