Skip to content

Commit 5c2ebf4

Browse files
committed
feat(touch): Support all touch versions in NG driver
1 parent c7520cc commit 5c2ebf4

File tree

10 files changed

+189
-547
lines changed

10 files changed

+189
-547
lines changed

cores/esp32/esp32-hal-touch-ng.c

Lines changed: 108 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,8 @@
1414
#include "soc/soc_caps.h"
1515

1616
#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
1818

19-
#include "driver/touch_sens.h"
2019
#include "esp32-hal-touch-ng.h"
2120
#include "esp32-hal-periman.h"
2221

@@ -37,11 +36,24 @@ typedef struct {
3736
static TouchInterruptHandle_t __touchInterruptHandlers[SOC_TOUCH_SENSOR_NUM] = {
3837
0,
3938
};
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
4252
static uint32_t _div_num = 1;
4353
static uint8_t _coarse_freq_tune = 1;
4454
static uint8_t _fine_freq_tune = 1;
55+
#endif
56+
4557
static uint8_t used_pads = 0;
4658

4759
static uint32_t __touchSleepTime = 256;
@@ -156,15 +168,28 @@ bool touchBenchmarkThreshold(uint8_t pad) {
156168

157169
// Reconfigure passed pad with new threshold
158170
uint32_t benchmark[_sample_num] = {};
171+
#if SOC_TOUCH_SUPPORT_BENCHMARK // ESP32S2, ESP32S3,ESP32P4
159172
if (touch_channel_read_data(touch_channel_handle[pad], TOUCH_CHAN_DATA_TYPE_BENCHMARK, benchmark) != ESP_OK) {
160173
log_e("Touch channel read data failed!");
161174
return false;
162175
}
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+
163183
/* 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();
165185
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
166190
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
168193
}
169194
/* Update the channel configuration */
170195
if (touch_sensor_reconfig_channel(touch_channel_handle[pad], &chan_cfg) != ESP_OK) {
@@ -198,21 +223,40 @@ static void __touchInit() {
198223
return;
199224
}
200225
// 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
202233
touch_sensor_sample_config_t sample_cfg[_sample_num] = {};
203234
sample_cfg[0] = single_sample_cfg;
204235

205-
/* Allocate new touch controller handle */
206236
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
207251
.power_on_wait_us = __touchSleepTime,
208252
.meas_interval_us = __touchMeasureTime,
209253
.max_meas_time_us = 0,
210254
.output_mode = TOUCH_PAD_OUT_AS_CLOCK,
211255
.sample_cfg_num = _sample_num,
212256
.sample_cfg = sample_cfg,
257+
#endif
213258
};
214259

215-
// touch_sensor_config_t sens_cfg = TOUCH_SENSOR_DEFAULT_BASIC_CONFIG(_sample_num, sample_cfg);
216260
if (touch_sensor_new_controller(&sens_cfg, &touch_sensor_handle) != ESP_OK) {
217261
goto err;
218262
}
@@ -225,14 +269,10 @@ static void __touchInit() {
225269
}
226270

227271
/* 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+
236276
if (touch_sensor_register_callbacks(touch_sensor_handle, &callbacks, NULL) != ESP_OK) {
237277
goto err;
238278
}
@@ -253,10 +293,8 @@ static void __touchChannelInit(int pad) {
253293
// Initial setup with default Threshold
254294
__touchInterruptHandlers[pad].fn = NULL;
255295

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+
260298
if (!touchStop() || !touchDisable()) {
261299
log_e("Touch sensor stop and disable failed!");
262300
return;
@@ -323,8 +361,21 @@ static void __touchConfigInterrupt(uint8_t pin, void (*userFunc)(void), void *Ar
323361
__touchInterruptHandlers[pad].arg = NULL;
324362
} else {
325363
// 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+
}
328379
__touchInterruptHandlers[pad].fn = userFunc;
329380
__touchInterruptHandlers[pad].callWithArgs = callWithArgs;
330381
__touchInterruptHandlers[pad].arg = Args;
@@ -338,7 +389,11 @@ static void __touchConfigInterrupt(uint8_t pin, void (*userFunc)(void), void *Ar
338389

339390
touch_channel_config_t chan_cfg = {};
340391
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
341395
chan_cfg.active_thresh[i] = threshold;
396+
#endif
342397
}
343398

344399
if (touch_sensor_reconfig_channel(touch_channel_handle[pad], &chan_cfg) != ESP_OK) {
@@ -375,7 +430,6 @@ bool touchInterruptGetLastStatus(uint8_t pin) {
375430
if (pad < 0) {
376431
return false;
377432
}
378-
379433
return __touchInterruptHandlers[pad].lastStatusIsPressed;
380434
}
381435

@@ -405,9 +459,13 @@ void touchSleepWakeUpEnable(uint8_t pin, touch_value_t threshold) {
405459

406460
touch_sleep_config_t deep_slp_cfg = {
407461
.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
408465
.deep_slp_chan = touch_channel_handle[pad],
409466
.deep_slp_thresh = {threshold},
410467
.deep_slp_sens_cfg = NULL, // Use the original touch sensor configuration
468+
#endif
411469
};
412470

413471
// Register the deep sleep wake-up
@@ -434,6 +492,20 @@ void touchSetTiming(float measure, uint32_t sleep) {
434492
__touchMeasureTime = measure;
435493
}
436494

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+
437509
void touchSetConfig(uint32_t div_num, uint8_t coarse_freq_tune, uint8_t fine_freq_tune) {
438510
if (initialized) {
439511
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
443515
_coarse_freq_tune = coarse_freq_tune;
444516
_fine_freq_tune = fine_freq_tune;
445517
}
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
446529

447530
extern touch_value_t touchRead(uint8_t) __attribute__((weak, alias("__touchRead")));
448531
extern void touchAttachInterrupt(uint8_t, voidFuncPtr, touch_value_t) __attribute__((weak, alias("__touchAttachInterrupt")));
449532
extern void touchAttachInterruptArg(uint8_t, voidArgFuncPtr, void *, touch_value_t) __attribute__((weak, alias("__touchAttachArgsInterrupt")));
450533
extern void touchDetachInterrupt(uint8_t) __attribute__((weak, alias("__touchDettachInterrupt")));
451534

452-
#endif /* SOC_TOUCH_SENSOR_VERSION == 3 */
535+
//#endif /* SOC_TOUCH_SENSOR_VERSION == 3 */
453536
#endif /* SOC_TOUCH_SENSOR_SUPPORTED */

cores/esp32/esp32-hal-touch-ng.h

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,32 @@
2222

2323
#include "soc/soc_caps.h"
2424
#if SOC_TOUCH_SENSOR_SUPPORTED
25-
#if SOC_TOUCH_SENSOR_VERSION == 3 // ESP32P4
2625

2726
#ifdef __cplusplus
2827
extern "C" {
2928
#endif
3029

3130
#include "esp32-hal.h"
31+
#include "driver/touch_sens.h"
32+
33+
#if SOC_TOUCH_SENSOR_VERSION == 1 // ESP32
34+
#define TOUCH_CHANNEL_DEFAULT_CONFIG() { \
35+
.abs_active_thresh = {1000}, \
36+
.charge_speed = TOUCH_CHARGE_SPEED_7, \
37+
.init_charge_volt = TOUCH_INIT_CHARGE_VOLT_DEFAULT, \
38+
.group = TOUCH_CHAN_TRIG_GROUP_BOTH, \
39+
}
40+
#elif SOC_TOUCH_SENSOR_VERSION == 2 // ESP32-S2 & ESP32-S3
41+
#define TOUCH_CHANNEL_DEFAULT_CONFIG() { \
42+
.active_thresh = {2000}, \
43+
.charge_speed = TOUCH_CHARGE_SPEED_7, \
44+
.init_charge_volt = TOUCH_INIT_CHARGE_VOLT_DEFAULT, \
45+
}
46+
#elif SOC_TOUCH_SENSOR_VERSION == 3 // ESP32-P4
47+
#define TOUCH_CHANNEL_DEFAULT_CONFIG() { \
48+
.active_thresh = {1000}, \
49+
}
50+
#endif
3251

3352
typedef uint32_t touch_value_t;
3453

@@ -40,11 +59,31 @@ typedef uint32_t touch_value_t;
4059
**/
4160
void touchSetTiming(float measure, uint32_t sleep);
4261

62+
#if SOC_TOUCH_SENSOR_VERSION == 1 || SOC_TOUCH_SENSOR_VERSION == 2 // ESP32, ESP32S2, ESP32S3
63+
/*
64+
* @param[in] duration_ms The measurement duration of the touch channel
65+
* @param[in] volt_low The low voltage limit of the touch channel
66+
* @param[in] volt_high The high voltage limit of the touch channel
67+
*/
68+
void touchSetConfig(float duration_ms, touch_volt_lim_l_t volt_low, touch_volt_lim_h_t volt_high);
69+
70+
#elif SOC_TOUCH_SENSOR_VERSION == 3 // ESP32P4
4371
/*
4472
* Tune the touch pad frequency.
4573
* Note: Must be called before setting up touch pads
4674
*/
4775
void touchSetConfig(uint32_t _div_num, uint8_t coarse_freq_tune, uint8_t fine_freq_tune);
76+
#endif
77+
78+
#if SOC_TOUCH_SENSOR_VERSION == 1
79+
/*
80+
* Specific functions to ESP32
81+
* Tells the driver if it shall activate the ISR if the sensor is Lower or Higher than the Threshold
82+
* Default if Lower.
83+
* Note: Must be called before setting up touch pads
84+
**/
85+
void touchInterruptSetThresholdDirection(bool mustbeLower);
86+
#endif
4887

4988
/*
5089
* Read touch pad value.
@@ -86,6 +125,5 @@ void touchSleepWakeUpEnable(uint8_t pin, touch_value_t threshold);
86125
}
87126
#endif
88127

89-
#endif /* SOC_TOUCH_SENSOR_VERSION == 3 */
90128
#endif /* SOC_TOUCH_SENSOR_SUPPORTED */
91129
#endif /* MAIN_ESP32_HAL_TOUCH_H_ */

0 commit comments

Comments
 (0)