14
14
#include "soc/soc_caps.h"
15
15
16
16
#if SOC_TOUCH_SENSOR_SUPPORTED
17
- #if SOC_TOUCH_SENSOR_VERSION == 3 // ESP32P4 for now
17
+ // #if SOC_TOUCH_SENSOR_VERSION == 3 // ESP32P4 for now
18
18
19
- #include "driver/touch_sens.h"
20
19
#include "esp32-hal-touch-ng.h"
21
20
#include "esp32-hal-periman.h"
22
21
@@ -37,11 +36,24 @@ typedef struct {
37
36
static TouchInterruptHandle_t __touchInterruptHandlers [SOC_TOUCH_SENSOR_NUM ] = {
38
37
0 ,
39
38
};
40
-
41
- static uint8_t _sample_num = 1 ;
39
+ #if SOC_TOUCH_SENSOR_VERSION == 1 // ESP32
40
+ static uint8_t _sample_num = 1 ; // only one sample configuration supported
41
+ static float _duration_ms = 5.0f ;
42
+ static touch_volt_lim_l_t _volt_low = TOUCH_VOLT_LIM_L_0V5 ;
43
+ static touch_volt_lim_h_t _volt_high = TOUCH_VOLT_LIM_H_1V7 ;
44
+ static touch_intr_trig_mode_t _intr_trig_mode = TOUCH_INTR_TRIG_ON_BELOW_THRESH ;
45
+ #elif SOC_TOUCH_SENSOR_VERSION == 2 // ESP32S2, ESP32S3
46
+ static uint8_t _sample_num = 1 ; // only one sample configuration supported
47
+ static float _duration_ms = 500.0f ;
48
+ static touch_volt_lim_l_t _volt_low = TOUCH_VOLT_LIM_L_0V5 ;
49
+ static touch_volt_lim_h_t _volt_high = TOUCH_VOLT_LIM_H_2V2 ;
50
+ #elif SOC_TOUCH_SENSOR_VERSION == 3 // ESP32P4
51
+ static uint8_t _sample_num = 1 ; // TODO: can be extended to multiple samples
42
52
static uint32_t _div_num = 1 ;
43
53
static uint8_t _coarse_freq_tune = 1 ;
44
54
static uint8_t _fine_freq_tune = 1 ;
55
+ #endif
56
+
45
57
static uint8_t used_pads = 0 ;
46
58
47
59
static uint32_t __touchSleepTime = 256 ;
@@ -156,15 +168,28 @@ bool touchBenchmarkThreshold(uint8_t pad) {
156
168
157
169
// Reconfigure passed pad with new threshold
158
170
uint32_t benchmark [_sample_num ] = {};
171
+ #if SOC_TOUCH_SUPPORT_BENCHMARK // ESP32S2, ESP32S3,ESP32P4
159
172
if (touch_channel_read_data (touch_channel_handle [pad ], TOUCH_CHAN_DATA_TYPE_BENCHMARK , benchmark ) != ESP_OK ) {
160
173
log_e ("Touch channel read data failed!" );
161
174
return false;
162
175
}
176
+ #else
177
+ if (touch_channel_read_data (touch_channel_handle [pad ], TOUCH_CHAN_DATA_TYPE_SMOOTH , benchmark ) != ESP_OK ) {
178
+ log_e ("Touch channel read data failed!" );
179
+ return false;
180
+ }
181
+ #endif
182
+
163
183
/* Calculate the proper active thresholds regarding the initial benchmark */
164
- touch_channel_config_t chan_cfg = {} ;
184
+ touch_channel_config_t chan_cfg = TOUCH_CHANNEL_DEFAULT_CONFIG () ;
165
185
for (int i = 0 ; i < _sample_num ; i ++ ) {
186
+ #if SOC_TOUCH_SENSOR_VERSION == 1 // ESP32
187
+ chan_cfg .abs_active_thresh [i ] = (uint32_t )(benchmark [i ] * (1 - s_thresh2bm_ratio ));
188
+ log_v ("Configured [CH %d] sample %d: benchmark = %" PRIu32 ", threshold = %" PRIu32 "\t" , pad , i , benchmark [i ], chan_cfg .abs_active_thresh [i ]);
189
+ #else
166
190
chan_cfg .active_thresh [i ] = (uint32_t )(benchmark [i ] * s_thresh2bm_ratio );
167
- log_v ("Configured [CH %d] sample %d: benchmark = %" PRIu32 ", threshold = %" PRIu32 "\t" , pad , i , benchmark [i ], chan_cfg .active_thresh [i ]);
191
+ log_v ("Configured [CH %d] sample %d: benchmark = %" PRIu32 ", threshold = %" PRIu32 "\t" , pad , i , benchmark [i ], chan_cfg .active_thresh [i ]);
192
+ #endif
168
193
}
169
194
/* Update the channel configuration */
170
195
if (touch_sensor_reconfig_channel (touch_channel_handle [pad ], & chan_cfg ) != ESP_OK ) {
@@ -198,21 +223,40 @@ static void __touchInit() {
198
223
return ;
199
224
}
200
225
// Support only one sample configuration for now
201
- touch_sensor_sample_config_t single_sample_cfg = TOUCH_SENSOR_V3_DEFAULT_SAMPLE_CONFIG (_div_num , _coarse_freq_tune , _fine_freq_tune );
226
+ #if SOC_TOUCH_SENSOR_VERSION == 1 // ESP32
227
+ touch_sensor_sample_config_t single_sample_cfg = TOUCH_SENSOR_V1_DEFAULT_SAMPLE_CONFIG (_duration_ms , _volt_low , _volt_high );
228
+ #elif SOC_TOUCH_SENSOR_VERSION == 2 // ESP32S2, ESP32S3
229
+ touch_sensor_sample_config_t single_sample_cfg = TOUCH_SENSOR_V2_DEFAULT_SAMPLE_CONFIG (_duration_ms , _volt_low , _volt_high );
230
+ #elif SOC_TOUCH_SENSOR_VERSION == 3 // ESP32P4
231
+ touch_sensor_sample_config_t single_sample_cfg = TOUCH_SENSOR_V3_DEFAULT_SAMPLE_CONFIG (_sample_num , _div_num , _coarse_freq_tune , _fine_freq_tune );
232
+ #endif
202
233
touch_sensor_sample_config_t sample_cfg [_sample_num ] = {};
203
234
sample_cfg [0 ] = single_sample_cfg ;
204
235
205
- /* Allocate new touch controller handle */
206
236
touch_sensor_config_t sens_cfg = {
237
+ #if SOC_TOUCH_SENSOR_VERSION == 1 // ESP32
238
+ .power_on_wait_us = __touchSleepTime ,
239
+ .meas_interval_us = __touchMeasureTime ,
240
+ .intr_trig_mode = _intr_trig_mode ,
241
+ .intr_trig_group = TOUCH_INTR_TRIG_GROUP_BOTH ,
242
+ .sample_cfg_num = _sample_num ,
243
+ .sample_cfg = sample_cfg ,
244
+ #elif SOC_TOUCH_SENSOR_VERSION == 2 // ESP32S2, ESP32S3
245
+ .power_on_wait_us = __touchSleepTime ,
246
+ .meas_interval_us = __touchMeasureTime ,
247
+ .max_meas_time_us = 0 ,
248
+ .sample_cfg_num = _sample_num ,
249
+ .sample_cfg = sample_cfg ,
250
+ #elif SOC_TOUCH_SENSOR_VERSION == 3 // ESP32P4
207
251
.power_on_wait_us = __touchSleepTime ,
208
252
.meas_interval_us = __touchMeasureTime ,
209
253
.max_meas_time_us = 0 ,
210
254
.output_mode = TOUCH_PAD_OUT_AS_CLOCK ,
211
255
.sample_cfg_num = _sample_num ,
212
256
.sample_cfg = sample_cfg ,
257
+ #endif
213
258
};
214
259
215
- // touch_sensor_config_t sens_cfg = TOUCH_SENSOR_DEFAULT_BASIC_CONFIG(_sample_num, sample_cfg);
216
260
if (touch_sensor_new_controller (& sens_cfg , & touch_sensor_handle ) != ESP_OK ) {
217
261
goto err ;
218
262
}
@@ -225,14 +269,10 @@ static void __touchInit() {
225
269
}
226
270
227
271
/* Register the touch sensor on_active and on_inactive callbacks */
228
- touch_event_callbacks_t callbacks = {
229
- .on_active = __touchOnActiveISR ,
230
- .on_inactive = __touchOnInactiveISR ,
231
- .on_measure_done = NULL ,
232
- .on_scan_done = NULL ,
233
- .on_timeout = NULL ,
234
- .on_proximity_meas_done = NULL ,
235
- };
272
+ touch_event_callbacks_t callbacks = {0 };
273
+ callbacks .on_active = __touchOnActiveISR ;
274
+ callbacks .on_inactive = __touchOnInactiveISR ;
275
+
236
276
if (touch_sensor_register_callbacks (touch_sensor_handle , & callbacks , NULL ) != ESP_OK ) {
237
277
goto err ;
238
278
}
@@ -253,10 +293,8 @@ static void __touchChannelInit(int pad) {
253
293
// Initial setup with default Threshold
254
294
__touchInterruptHandlers [pad ].fn = NULL ;
255
295
256
- touch_channel_config_t chan_cfg = {
257
- .active_thresh = {1000 } // default threshold, will be updated after benchmark
258
- };
259
-
296
+ touch_channel_config_t chan_cfg = TOUCH_CHANNEL_DEFAULT_CONFIG ();
297
+
260
298
if (!touchStop () || !touchDisable ()) {
261
299
log_e ("Touch sensor stop and disable failed!" );
262
300
return ;
@@ -323,8 +361,21 @@ static void __touchConfigInterrupt(uint8_t pin, void (*userFunc)(void), void *Ar
323
361
__touchInterruptHandlers [pad ].arg = NULL ;
324
362
} else {
325
363
// attach ISR User Call
326
- __touchInit ();
327
- __touchChannelInit (pad );
364
+ if (perimanGetPinBus (pin , ESP32_BUS_TYPE_TOUCH ) == NULL ) {
365
+ perimanSetBusDeinit (ESP32_BUS_TYPE_TOUCH , touchDetachBus );
366
+ if (!perimanClearPinBus (pin )) {
367
+ log_e ("Failed to clear pin bus" );
368
+ return ;
369
+ }
370
+ __touchInit ();
371
+ __touchChannelInit (pad );
372
+
373
+ if (!perimanSetPinBus (pin , ESP32_BUS_TYPE_TOUCH , (void * )(pin + 1 ), -1 , pad )) {
374
+ touchDetachBus ((void * )(pin + 1 ));
375
+ log_e ("Failed to set bus to Peripheral manager" );
376
+ return ;
377
+ }
378
+ }
328
379
__touchInterruptHandlers [pad ].fn = userFunc ;
329
380
__touchInterruptHandlers [pad ].callWithArgs = callWithArgs ;
330
381
__touchInterruptHandlers [pad ].arg = Args ;
@@ -338,7 +389,11 @@ static void __touchConfigInterrupt(uint8_t pin, void (*userFunc)(void), void *Ar
338
389
339
390
touch_channel_config_t chan_cfg = {};
340
391
for (int i = 0 ; i < _sample_num ; i ++ ) {
392
+ #if SOC_TOUCH_SENSOR_VERSION == 1 // ESP32
393
+ chan_cfg .abs_active_thresh [i ] = threshold ;
394
+ #else
341
395
chan_cfg .active_thresh [i ] = threshold ;
396
+ #endif
342
397
}
343
398
344
399
if (touch_sensor_reconfig_channel (touch_channel_handle [pad ], & chan_cfg ) != ESP_OK ) {
@@ -375,7 +430,6 @@ bool touchInterruptGetLastStatus(uint8_t pin) {
375
430
if (pad < 0 ) {
376
431
return false;
377
432
}
378
-
379
433
return __touchInterruptHandlers [pad ].lastStatusIsPressed ;
380
434
}
381
435
@@ -405,9 +459,13 @@ void touchSleepWakeUpEnable(uint8_t pin, touch_value_t threshold) {
405
459
406
460
touch_sleep_config_t deep_slp_cfg = {
407
461
.slp_wakeup_lvl = TOUCH_DEEP_SLEEP_WAKEUP ,
462
+ #if SOC_TOUCH_SENSOR_VERSION == 1 // ESP32
463
+ .deep_slp_sens_cfg = NULL , // Use the original touch sensor configuration
464
+ #else // SOC_TOUCH_SENSOR_VERSION 2 and 3// ESP32S2, ESP32S3, ESP32P4
408
465
.deep_slp_chan = touch_channel_handle [pad ],
409
466
.deep_slp_thresh = {threshold },
410
467
.deep_slp_sens_cfg = NULL , // Use the original touch sensor configuration
468
+ #endif
411
469
};
412
470
413
471
// Register the deep sleep wake-up
@@ -434,6 +492,20 @@ void touchSetTiming(float measure, uint32_t sleep) {
434
492
__touchMeasureTime = measure ;
435
493
}
436
494
495
+ #if SOC_TOUCH_SENSOR_VERSION == 1 || SOC_TOUCH_SENSOR_VERSION == 2 // ESP32, ESP32S2, ESP32S3
496
+
497
+ void touchSetConfig (float duration_ms , touch_volt_lim_l_t volt_low , touch_volt_lim_h_t volt_high ) {
498
+ if (initialized ) {
499
+ log_e ("Touch sensor already initialized. Cannot set configuration." );
500
+ return ;
501
+ }
502
+ _duration_ms = duration_ms ;
503
+ _volt_low = volt_low ;
504
+ _volt_high = volt_high ;
505
+ }
506
+
507
+ #elif SOC_TOUCH_SENSOR_VERSION == 3 // ESP32P4
508
+
437
509
void touchSetConfig (uint32_t div_num , uint8_t coarse_freq_tune , uint8_t fine_freq_tune ) {
438
510
if (initialized ) {
439
511
log_e ("Touch sensor already initialized. Cannot set configuration." );
@@ -443,11 +515,22 @@ void touchSetConfig(uint32_t div_num, uint8_t coarse_freq_tune, uint8_t fine_fre
443
515
_coarse_freq_tune = coarse_freq_tune ;
444
516
_fine_freq_tune = fine_freq_tune ;
445
517
}
518
+ #endif
519
+
520
+ #if SOC_TOUCH_SENSOR_VERSION == 1 // ESP32
521
+ void touchInterruptSetThresholdDirection (bool mustbeLower ) {
522
+ if (mustbeLower ) {
523
+ _intr_trig_mode = TOUCH_INTR_TRIG_ON_BELOW_THRESH ;
524
+ } else {
525
+ _intr_trig_mode = TOUCH_INTR_TRIG_ON_ABOVE_THRESH ;
526
+ }
527
+ }
528
+ #endif
446
529
447
530
extern touch_value_t touchRead (uint8_t ) __attribute__((weak , alias ("__touchRead" )));
448
531
extern void touchAttachInterrupt (uint8_t , voidFuncPtr , touch_value_t ) __attribute__((weak , alias ("__touchAttachInterrupt" )));
449
532
extern void touchAttachInterruptArg (uint8_t , voidArgFuncPtr , void * , touch_value_t ) __attribute__((weak , alias ("__touchAttachArgsInterrupt" )));
450
533
extern void touchDetachInterrupt (uint8_t ) __attribute__((weak , alias ("__touchDettachInterrupt" )));
451
534
452
- #endif /* SOC_TOUCH_SENSOR_VERSION == 3 */
535
+ // #endif /* SOC_TOUCH_SENSOR_VERSION == 3 */
453
536
#endif /* SOC_TOUCH_SENSOR_SUPPORTED */
0 commit comments