@@ -119,11 +119,6 @@ static void __touchInit()
119
119
if (err != ESP_OK ) {
120
120
goto err ;
121
121
}
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
- }
127
122
// keep ISR activated - it can run all together (ISR + touchRead())
128
123
err = touch_pad_isr_register (__touchISR , NULL );
129
124
if (err != ESP_OK ) {
@@ -148,18 +143,7 @@ static void __touchInit()
148
143
// Touch Sensor Timer initiated
149
144
touch_pad_set_fsm_mode (TOUCH_FSM_MODE_TIMER ); // returns ESP_OK
150
145
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
163
147
#endif
164
148
165
149
initialized = true;
@@ -170,13 +154,43 @@ static void __touchInit()
170
154
return ;
171
155
}
172
156
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
+
173
185
static touch_value_t __touchRead (uint8_t pin )
174
186
{
175
187
int8_t pad = digitalPinToTouchChannel (pin );
176
188
if (pad < 0 ){
177
189
return 0 ;
178
190
}
191
+
179
192
__touchInit ();
193
+ __touchChannelInit (pad );
180
194
181
195
touch_value_t touch_value ;
182
196
touch_pad_read_raw_data (pad , & touch_value );
@@ -198,6 +212,9 @@ static void __touchConfigInterrupt(uint8_t pin, void (*userFunc)(void), void *Ar
198
212
} else {
199
213
// attach ISR User Call
200
214
__touchInit ();
215
+ #if SOC_TOUCH_VERSION_2 // ESP32S2, ESP32S3
216
+ __touchChannelInit (pad );
217
+ #endif
201
218
__touchInterruptHandlers [pad ].fn = userFunc ;
202
219
__touchInterruptHandlers [pad ].callWithArgs = callWithArgs ;
203
220
__touchInterruptHandlers [pad ].arg = Args ;
0 commit comments