@@ -119,11 +119,6 @@ static void __touchInit()
119119 if (err != ESP_OK ) {
120120 goto err ;
121121 }
122- // Initial no Threshold and setup
123- for (int i = 0 ; i < SOC_TOUCH_SENSOR_NUM ; i ++ ) {
124- __touchInterruptHandlers [i ].fn = NULL ;
125- touch_pad_config (i , SOC_TOUCH_PAD_THRESHOLD_MAX ); // returns ESP_OK
126- }
127122 // keep ISR activated - it can run all together (ISR + touchRead())
128123 err = touch_pad_isr_register (__touchISR , NULL );
129124 if (err != ESP_OK ) {
@@ -148,18 +143,7 @@ static void __touchInit()
148143 // Touch Sensor Timer initiated
149144 touch_pad_set_fsm_mode (TOUCH_FSM_MODE_TIMER ); // returns ESP_OK
150145 touch_pad_fsm_start (); // returns ESP_OK
151-
152- // Initial no Threshold and setup - TOUCH0 is internal denoise channel
153- for (int i = 1 ; i < SOC_TOUCH_SENSOR_NUM ; i ++ ) {
154- __touchInterruptHandlers [i ].fn = NULL ;
155- touch_pad_config (i ); // returns ESP_OK
156- }
157- // keep ISR activated - it can run all together (ISR + touchRead())
158- err = touch_pad_isr_register (__touchISR , NULL , TOUCH_PAD_INTR_MASK_ACTIVE | TOUCH_PAD_INTR_MASK_INACTIVE );
159- if (err != ESP_OK ) {
160- goto err ;
161- }
162- touch_pad_intr_enable (TOUCH_PAD_INTR_MASK_ACTIVE | TOUCH_PAD_INTR_MASK_INACTIVE ); // returns ESP_OK
146+ //ISR setup moved to __touchChannelInit
163147#endif
164148
165149 initialized = true;
@@ -170,13 +154,43 @@ static void __touchInit()
170154 return ;
171155}
172156
157+ static void __touchChannelInit (int pad )
158+ {
159+ static bool channels_initialized [SOC_TOUCH_SENSOR_NUM ] = { false };
160+ if (channels_initialized [pad ]){
161+ return ;
162+ }
163+
164+ #if SOC_TOUCH_VERSION_1 // ESP32
165+ // Initial no Threshold and setup
166+ __touchInterruptHandlers [pad ].fn = NULL ;
167+ touch_pad_config (pad , SOC_TOUCH_PAD_THRESHOLD_MAX ); // returns ESP_OK
168+ #elif SOC_TOUCH_VERSION_2 // ESP32S2, ESP32S3
169+ // Initial no Threshold and setup
170+ __touchInterruptHandlers [pad ].fn = NULL ;
171+ touch_pad_config (pad ); // returns ESP_OK
172+ // keep ISR activated - it can run all together (ISR + touchRead())
173+ esp_err_t err = touch_pad_isr_register (__touchISR , NULL , TOUCH_PAD_INTR_MASK_ACTIVE | TOUCH_PAD_INTR_MASK_INACTIVE );
174+ if (err != ESP_OK ) {
175+ log_e (" Touch sensor initialization error." );
176+ return ;
177+ }
178+ touch_pad_intr_enable (TOUCH_PAD_INTR_MASK_ACTIVE | TOUCH_PAD_INTR_MASK_INACTIVE ); // returns ESP_OK
179+ #endif
180+
181+ channels_initialized [pad ] = true;
182+ delay (20 ); //delay needed before reading from touch channel after config
183+ }
184+
173185static touch_value_t __touchRead (uint8_t pin )
174186{
175187 int8_t pad = digitalPinToTouchChannel (pin );
176188 if (pad < 0 ){
177189 return 0 ;
178190 }
191+
179192 __touchInit ();
193+ __touchChannelInit (pad );
180194
181195 touch_value_t touch_value ;
182196 touch_pad_read_raw_data (pad , & touch_value );
@@ -198,6 +212,9 @@ static void __touchConfigInterrupt(uint8_t pin, void (*userFunc)(void), void *Ar
198212 } else {
199213 // attach ISR User Call
200214 __touchInit ();
215+ #if SOC_TOUCH_VERSION_2 // ESP32S2, ESP32S3
216+ __touchChannelInit (pad );
217+ #endif
201218 __touchInterruptHandlers [pad ].fn = userFunc ;
202219 __touchInterruptHandlers [pad ].callWithArgs = callWithArgs ;
203220 __touchInterruptHandlers [pad ].arg = Args ;
0 commit comments