From 7f652337ce380ef9e49e8225c19e9cd52bebe0de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Procha=CC=81zka?= <90197375+P-R-O-C-H-Y@users.noreply.github.com> Date: Thu, 21 Apr 2022 16:59:02 +0200 Subject: [PATCH 1/3] Separated init for touch / channel called by touchRead() --- cores/esp32/esp32-hal-touch.c | 35 ++++++++++++++++++++++++----------- 1 file changed, 24 insertions(+), 11 deletions(-) diff --git a/cores/esp32/esp32-hal-touch.c b/cores/esp32/esp32-hal-touch.c index e5eaf497b5e..dc2ea0ab517 100644 --- a/cores/esp32/esp32-hal-touch.c +++ b/cores/esp32/esp32-hal-touch.c @@ -119,11 +119,6 @@ static void __touchInit() if (err != ESP_OK) { goto err; } - // Initial no Threshold and setup - for (int i = 0; i < SOC_TOUCH_SENSOR_NUM; i++) { - __touchInterruptHandlers[i].fn = NULL; - touch_pad_config(i, SOC_TOUCH_PAD_THRESHOLD_MAX); // returns ESP_OK - } // keep ISR activated - it can run all together (ISR + touchRead()) err = touch_pad_isr_register(__touchISR, NULL); if (err != ESP_OK) { @@ -148,12 +143,6 @@ static void __touchInit() // Touch Sensor Timer initiated touch_pad_set_fsm_mode(TOUCH_FSM_MODE_TIMER); // returns ESP_OK touch_pad_fsm_start(); // returns ESP_OK - - // Initial no Threshold and setup - TOUCH0 is internal denoise channel - for (int i = 1; i < SOC_TOUCH_SENSOR_NUM; i++) { - __touchInterruptHandlers[i].fn = NULL; - touch_pad_config(i); // returns ESP_OK - } // keep ISR activated - it can run all together (ISR + touchRead()) err = touch_pad_isr_register(__touchISR, NULL, TOUCH_PAD_INTR_MASK_ACTIVE | TOUCH_PAD_INTR_MASK_INACTIVE); if (err != ESP_OK) { @@ -170,13 +159,37 @@ static void __touchInit() return; } +static void __touchChannelInit(int pad) +{ + static bool channels_initialized[SOC_TOUCH_SENSOR_NUM] = { false }; + if(channels_initialized[pad]){ + return; + } + +#if SOC_TOUCH_VERSION_1 // ESP32 + // Initial no Threshold and setup + __touchInterruptHandlers[pad].fn = NULL; + touch_pad_config(pad, SOC_TOUCH_PAD_THRESHOLD_MAX); // returns ESP_OK +#elif SOC_TOUCH_VERSION_2 // ESP32S2, ESP32S3 + // Initial no Threshold and setup - TOUCH0 is internal denoise channel + __touchInterruptHandlers[pad].fn = NULL; + touch_pad_config(pad); // returns ESP_OK + } +#endif + + channels_initialized[pad] = true; + delay(20); //delay needed before reading from touch channel after config +} + static touch_value_t __touchRead(uint8_t pin) { int8_t pad = digitalPinToTouchChannel(pin); if(pad < 0){ return 0; } + __touchInit(); + __touchChannelInit(pad); touch_value_t touch_value; touch_pad_read_raw_data(pad, &touch_value); From 93bd4281bddf8f578014c34c1f1e35353ee84a9a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Procha=CC=81zka?= <90197375+P-R-O-C-H-Y@users.noreply.github.com> Date: Thu, 21 Apr 2022 17:09:14 +0200 Subject: [PATCH 2/3] compile error --- cores/esp32/esp32-hal-touch.c | 1 - 1 file changed, 1 deletion(-) diff --git a/cores/esp32/esp32-hal-touch.c b/cores/esp32/esp32-hal-touch.c index dc2ea0ab517..9858f1262a0 100644 --- a/cores/esp32/esp32-hal-touch.c +++ b/cores/esp32/esp32-hal-touch.c @@ -174,7 +174,6 @@ static void __touchChannelInit(int pad) // Initial no Threshold and setup - TOUCH0 is internal denoise channel __touchInterruptHandlers[pad].fn = NULL; touch_pad_config(pad); // returns ESP_OK - } #endif channels_initialized[pad] = true; From 4098dcda82de3f93fc3eb5432adbea1e40e875b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Procha=CC=81zka?= <90197375+P-R-O-C-H-Y@users.noreply.github.com> Date: Thu, 21 Apr 2022 21:48:00 +0200 Subject: [PATCH 3/3] Fixed touch_V2 + ISR --- cores/esp32/esp32-hal-touch.c | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/cores/esp32/esp32-hal-touch.c b/cores/esp32/esp32-hal-touch.c index 9858f1262a0..539c0004f98 100644 --- a/cores/esp32/esp32-hal-touch.c +++ b/cores/esp32/esp32-hal-touch.c @@ -143,12 +143,7 @@ static void __touchInit() // Touch Sensor Timer initiated touch_pad_set_fsm_mode(TOUCH_FSM_MODE_TIMER); // returns ESP_OK touch_pad_fsm_start(); // returns ESP_OK - // keep ISR activated - it can run all together (ISR + touchRead()) - err = touch_pad_isr_register(__touchISR, NULL, TOUCH_PAD_INTR_MASK_ACTIVE | TOUCH_PAD_INTR_MASK_INACTIVE); - if (err != ESP_OK) { - goto err; - } - touch_pad_intr_enable(TOUCH_PAD_INTR_MASK_ACTIVE | TOUCH_PAD_INTR_MASK_INACTIVE); // returns ESP_OK + //ISR setup moved to __touchChannelInit #endif initialized = true; @@ -168,12 +163,19 @@ static void __touchChannelInit(int pad) #if SOC_TOUCH_VERSION_1 // ESP32 // Initial no Threshold and setup - __touchInterruptHandlers[pad].fn = NULL; - touch_pad_config(pad, SOC_TOUCH_PAD_THRESHOLD_MAX); // returns ESP_OK -#elif SOC_TOUCH_VERSION_2 // ESP32S2, ESP32S3 - // Initial no Threshold and setup - TOUCH0 is internal denoise channel - __touchInterruptHandlers[pad].fn = NULL; - touch_pad_config(pad); // returns ESP_OK + __touchInterruptHandlers[pad].fn = NULL; + touch_pad_config(pad, SOC_TOUCH_PAD_THRESHOLD_MAX); // returns ESP_OK +#elif SOC_TOUCH_VERSION_2 // ESP32S2, ESP32S3 + // Initial no Threshold and setup + __touchInterruptHandlers[pad].fn = NULL; + touch_pad_config(pad); // returns ESP_OK + // keep ISR activated - it can run all together (ISR + touchRead()) + esp_err_t err = touch_pad_isr_register(__touchISR, NULL, TOUCH_PAD_INTR_MASK_ACTIVE | TOUCH_PAD_INTR_MASK_INACTIVE); + if (err != ESP_OK) { + log_e(" Touch sensor initialization error."); + return; + } + touch_pad_intr_enable(TOUCH_PAD_INTR_MASK_ACTIVE | TOUCH_PAD_INTR_MASK_INACTIVE); // returns ESP_OK #endif channels_initialized[pad] = true; @@ -210,6 +212,9 @@ static void __touchConfigInterrupt(uint8_t pin, void (*userFunc)(void), void *Ar } else { // attach ISR User Call __touchInit(); + #if SOC_TOUCH_VERSION_2 // ESP32S2, ESP32S3 + __touchChannelInit(pad); + #endif __touchInterruptHandlers[pad].fn = userFunc; __touchInterruptHandlers[pad].callWithArgs = callWithArgs; __touchInterruptHandlers[pad].arg = Args;