1 /*
2 * SPDX-FileCopyrightText: 2016-2022 Espressif Systems (Shanghai) CO LTD
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include <esp_types.h>
8 #include <stdlib.h>
9 #include <ctype.h>
10 #include "esp_log.h"
11 #include "sys/lock.h"
12 #include "soc/soc_pins.h"
13 #include "freertos/FreeRTOS.h"
14 #include "freertos/xtensa_api.h"
15 #include "freertos/semphr.h"
16 #include "freertos/timers.h"
17 #include "esp_intr_alloc.h"
18 #include "driver/rtc_io.h"
19 #include "driver/touch_pad.h"
20 #include "esp_private/rtc_ctrl.h"
21 #include "driver/gpio.h"
22 #include "sdkconfig.h"
23 #include "esp_check.h"
24
25 #include "hal/touch_sensor_types.h"
26 #include "hal/touch_sensor_hal.h"
27
28 #ifndef NDEBUG
29 // Enable built-in checks in queue.h in debug builds
30 #define INVARIANTS
31 #endif
32 #include "sys/queue.h"
33
34 #define TOUCH_PAD_FILTER_FACTOR_DEFAULT (4) // IIR filter coefficient.
35 #define TOUCH_PAD_SHIFT_DEFAULT (4) // Increase computing accuracy.
36 #define TOUCH_PAD_SHIFT_ROUND_DEFAULT (8) // ROUND = 2^(n-1); rounding off for fractional.
37 #define TOUCH_PAD_MEASURE_WAIT_DEFAULT (0xFF) // The timer frequency is 8Mhz, the max value is 0xff
38
39 static __attribute__((unused)) const char *TOUCH_TAG = "TOUCH_SENSOR";
40
41 #define TOUCH_CHANNEL_CHECK(channel) do { \
42 ESP_RETURN_ON_FALSE(channel < SOC_TOUCH_SENSOR_NUM && channel >= 0, ESP_ERR_INVALID_ARG, TOUCH_TAG, "Touch channel error"); \
43 ESP_RETURN_ON_FALSE(channel != SOC_TOUCH_DENOISE_CHANNEL, ESP_ERR_INVALID_ARG, TOUCH_TAG, "TOUCH0 is internal denoise channel"); \
44 } while (0);
45 #define TOUCH_CH_MASK_CHECK(mask) ESP_RETURN_ON_FALSE((mask <= TOUCH_PAD_BIT_MASK_ALL), ESP_ERR_INVALID_ARG, TOUCH_TAG, "touch channel bitmask error");
46 #define TOUCH_INTR_MASK_CHECK(mask) ESP_RETURN_ON_FALSE(mask & TOUCH_PAD_INTR_MASK_ALL, ESP_ERR_INVALID_ARG, TOUCH_TAG, "intr mask error");
47 #define TOUCH_NULL_POINTER_CHECK(p, name) ESP_RETURN_ON_FALSE((p), ESP_ERR_INVALID_ARG, TOUCH_TAG, "input param '"name"' is NULL")
48 #define TOUCH_PARAM_CHECK_STR(s) ""s" parameter error"
49
50 extern portMUX_TYPE rtc_spinlock; //TODO: Will be placed in the appropriate position after the rtc module is finished.
51 #define TOUCH_ENTER_CRITICAL_SAFE() portENTER_CRITICAL_SAFE(&rtc_spinlock) // Can be called in isr and task.
52 #define TOUCH_EXIT_CRITICAL_SAFE() portEXIT_CRITICAL_SAFE(&rtc_spinlock)
53 #define TOUCH_ENTER_CRITICAL() portENTER_CRITICAL(&rtc_spinlock)
54 #define TOUCH_EXIT_CRITICAL() portEXIT_CRITICAL(&rtc_spinlock)
55
56 static SemaphoreHandle_t rtc_touch_mux = NULL;
57
58 /*---------------------------------------------------------------
59 Touch Pad
60 ---------------------------------------------------------------*/
61
62 /** Workaround for scan done interrupt issue. */
touch_pad_workaround_isr_internal(void * arg)63 static void touch_pad_workaround_isr_internal(void *arg)
64 {
65 uint16_t ch_mask = 0;
66 uint32_t intr_mask = touch_hal_read_intr_status_mask();
67 int pad_num = touch_hal_get_current_meas_channel();
68 /* Make sure that the scan done interrupt is generated after the last channel measurement is completed. */
69 if (intr_mask & TOUCH_PAD_INTR_MASK_SCAN_DONE) {
70 touch_hal_get_channel_mask(&ch_mask);
71 for (int i = TOUCH_PAD_MAX - 1; i >= 0; i--) {
72 if (BIT(i) & ch_mask) {
73 if (pad_num != i) {
74 touch_hal_intr_clear(TOUCH_PAD_INTR_MASK_SCAN_DONE);
75 }
76 break;
77 }
78 }
79 }
80 }
81
touch_pad_isr_register(intr_handler_t fn,void * arg,touch_pad_intr_mask_t intr_mask)82 esp_err_t touch_pad_isr_register(intr_handler_t fn, void *arg, touch_pad_intr_mask_t intr_mask)
83 {
84 static bool reg_flag = false;
85 ESP_RETURN_ON_FALSE(fn, ESP_ERR_INVALID_ARG, TOUCH_TAG, TOUCH_PARAM_CHECK_STR("intr_mask"));
86 TOUCH_INTR_MASK_CHECK(intr_mask);
87
88 uint32_t en_msk = 0;
89 if (intr_mask & TOUCH_PAD_INTR_MASK_DONE) {
90 en_msk |= RTC_CNTL_TOUCH_DONE_INT_ST_M;
91 }
92 if (intr_mask & TOUCH_PAD_INTR_MASK_ACTIVE) {
93 en_msk |= RTC_CNTL_TOUCH_ACTIVE_INT_ST_M;
94 }
95 if (intr_mask & TOUCH_PAD_INTR_MASK_INACTIVE) {
96 en_msk |= RTC_CNTL_TOUCH_INACTIVE_INT_ST_M;
97 }
98 if (intr_mask & TOUCH_PAD_INTR_MASK_SCAN_DONE) {
99 en_msk |= RTC_CNTL_TOUCH_SCAN_DONE_INT_ST_M;
100 }
101 if (intr_mask & TOUCH_PAD_INTR_MASK_TIMEOUT) {
102 en_msk |= RTC_CNTL_TOUCH_TIMEOUT_INT_ST_M;
103 }
104 #if SOC_TOUCH_PROXIMITY_MEAS_DONE_SUPPORTED
105 if (intr_mask & TOUCH_PAD_INTR_MASK_PROXI_MEAS_DONE) {
106 en_msk |= RTC_CNTL_TOUCH_APPROACH_LOOP_DONE_INT_ST_M;
107 }
108 #endif
109 esp_err_t ret = rtc_isr_register(fn, arg, en_msk, 0);
110 /* Must ensure: After being registered, it is executed first. */
111 if ( (ret == ESP_OK) && (reg_flag == false) && (intr_mask & (TOUCH_PAD_INTR_MASK_SCAN_DONE | TOUCH_PAD_INTR_MASK_TIMEOUT)) ) {
112 rtc_isr_register(touch_pad_workaround_isr_internal, NULL, RTC_CNTL_TOUCH_SCAN_DONE_INT_ST_M | RTC_CNTL_TOUCH_TIMEOUT_INT_ST_M, 0);
113 reg_flag = true;
114 }
115
116 return ret;
117 }
118
touch_pad_set_measurement_interval(uint16_t interval_cycle)119 esp_err_t touch_pad_set_measurement_interval(uint16_t interval_cycle)
120 {
121 TOUCH_ENTER_CRITICAL();
122 touch_hal_set_sleep_time(interval_cycle);
123 TOUCH_EXIT_CRITICAL();
124 return ESP_OK;
125 }
126
touch_pad_get_measurement_interval(uint16_t * interval_cycle)127 esp_err_t touch_pad_get_measurement_interval(uint16_t *interval_cycle)
128 {
129 TOUCH_NULL_POINTER_CHECK(interval_cycle, "interval_cycle");
130 TOUCH_ENTER_CRITICAL();
131 touch_hal_get_sleep_time(interval_cycle);
132 TOUCH_EXIT_CRITICAL();
133 return ESP_OK;
134 }
135
touch_pad_set_charge_discharge_times(uint16_t charge_discharge_times)136 esp_err_t touch_pad_set_charge_discharge_times(uint16_t charge_discharge_times)
137 {
138 TOUCH_ENTER_CRITICAL();
139 touch_hal_set_meas_times(charge_discharge_times);
140 TOUCH_EXIT_CRITICAL();
141
142 return ESP_OK;
143 }
144
touch_pad_get_charge_discharge_times(uint16_t * charge_discharge_times)145 esp_err_t touch_pad_get_charge_discharge_times(uint16_t *charge_discharge_times)
146 {
147 TOUCH_NULL_POINTER_CHECK(charge_discharge_times, "charge_discharge_times");
148 TOUCH_ENTER_CRITICAL();
149 touch_hal_get_measure_times(charge_discharge_times);
150 TOUCH_EXIT_CRITICAL();
151
152 return ESP_OK;
153 }
154
touch_pad_set_meas_time(uint16_t sleep_cycle,uint16_t meas_times)155 esp_err_t touch_pad_set_meas_time(uint16_t sleep_cycle, uint16_t meas_times)
156 {
157 touch_pad_set_charge_discharge_times(meas_times);
158 touch_pad_set_measurement_interval(sleep_cycle);
159 return ESP_OK;
160 }
161
touch_pad_get_meas_time(uint16_t * sleep_cycle,uint16_t * meas_times)162 esp_err_t touch_pad_get_meas_time(uint16_t *sleep_cycle, uint16_t *meas_times)
163 {
164 TOUCH_NULL_POINTER_CHECK(sleep_cycle, "sleep_cycle");
165 TOUCH_NULL_POINTER_CHECK(meas_times, "meas_times");
166 touch_pad_get_measurement_interval(sleep_cycle);
167 touch_pad_get_charge_discharge_times(meas_times);
168 return ESP_OK;
169 }
170
touch_pad_set_idle_channel_connect(touch_pad_conn_type_t type)171 esp_err_t touch_pad_set_idle_channel_connect(touch_pad_conn_type_t type)
172 {
173 ESP_RETURN_ON_FALSE(type < TOUCH_PAD_CONN_MAX, ESP_ERR_INVALID_ARG, TOUCH_TAG, TOUCH_PARAM_CHECK_STR("type"));
174 TOUCH_ENTER_CRITICAL();
175 touch_hal_set_idle_channel_connect(type);
176 TOUCH_EXIT_CRITICAL();
177 return ESP_OK;
178 }
179
touch_pad_get_idle_channel_connect(touch_pad_conn_type_t * type)180 esp_err_t touch_pad_get_idle_channel_connect(touch_pad_conn_type_t *type)
181 {
182 TOUCH_NULL_POINTER_CHECK(type, "type");
183 touch_hal_get_idle_channel_connect(type);
184 return ESP_OK;
185 }
186
touch_pad_meas_is_done(void)187 bool touch_pad_meas_is_done(void)
188 {
189 return touch_hal_meas_is_done();
190 }
191
touch_pad_set_channel_mask(uint16_t enable_mask)192 esp_err_t touch_pad_set_channel_mask(uint16_t enable_mask)
193 {
194 TOUCH_CH_MASK_CHECK(enable_mask);
195 TOUCH_ENTER_CRITICAL();
196 touch_hal_set_channel_mask(enable_mask);
197 TOUCH_EXIT_CRITICAL();
198
199 return ESP_OK;
200 }
201
touch_pad_get_channel_mask(uint16_t * enable_mask)202 esp_err_t touch_pad_get_channel_mask(uint16_t *enable_mask)
203 {
204 TOUCH_NULL_POINTER_CHECK(enable_mask, "enable_mask");
205 TOUCH_ENTER_CRITICAL();
206 touch_hal_get_channel_mask(enable_mask);
207 TOUCH_EXIT_CRITICAL();
208
209 return ESP_OK;
210 }
211
touch_pad_clear_channel_mask(uint16_t enable_mask)212 esp_err_t touch_pad_clear_channel_mask(uint16_t enable_mask)
213 {
214 TOUCH_CH_MASK_CHECK(enable_mask);
215 TOUCH_ENTER_CRITICAL();
216 touch_hal_clear_channel_mask(enable_mask);
217 TOUCH_EXIT_CRITICAL();
218 return ESP_OK;
219 }
220
touch_pad_get_current_meas_channel(void)221 touch_pad_t IRAM_ATTR touch_pad_get_current_meas_channel(void)
222 {
223 return (touch_pad_t)touch_hal_get_current_meas_channel();
224 }
225
touch_pad_intr_enable(touch_pad_intr_mask_t int_mask)226 esp_err_t touch_pad_intr_enable(touch_pad_intr_mask_t int_mask)
227 {
228 if (!(int_mask & TOUCH_PAD_INTR_MASK_ALL)) {
229 return ESP_ERR_INVALID_ARG;
230 }
231 TOUCH_ENTER_CRITICAL_SAFE();
232 touch_hal_intr_enable(int_mask);
233 TOUCH_EXIT_CRITICAL_SAFE();
234 return ESP_OK;
235 }
236
touch_pad_intr_disable(touch_pad_intr_mask_t int_mask)237 esp_err_t touch_pad_intr_disable(touch_pad_intr_mask_t int_mask)
238 {
239 if (!(int_mask & TOUCH_PAD_INTR_MASK_ALL)) {
240 return ESP_ERR_INVALID_ARG;
241 }
242 TOUCH_ENTER_CRITICAL_SAFE();
243 touch_hal_intr_disable(int_mask);
244 TOUCH_EXIT_CRITICAL_SAFE();
245 return ESP_OK;
246 }
247
touch_pad_intr_clear(touch_pad_intr_mask_t int_mask)248 esp_err_t touch_pad_intr_clear(touch_pad_intr_mask_t int_mask)
249 {
250 TOUCH_INTR_MASK_CHECK(int_mask);
251 TOUCH_ENTER_CRITICAL();
252 touch_hal_intr_clear(int_mask);
253 TOUCH_EXIT_CRITICAL();
254 return ESP_OK;
255 }
256
touch_pad_read_intr_status_mask(void)257 uint32_t touch_pad_read_intr_status_mask(void)
258 {
259 return touch_hal_read_intr_status_mask();
260 }
261
touch_pad_timeout_set(bool enable,uint32_t threshold)262 esp_err_t touch_pad_timeout_set(bool enable, uint32_t threshold)
263 {
264 TOUCH_ENTER_CRITICAL();
265 if (enable) {
266 touch_hal_timeout_enable();
267 } else {
268 touch_hal_timeout_disable();
269 }
270 touch_hal_timeout_set_threshold(threshold);
271 TOUCH_EXIT_CRITICAL();
272 return ESP_OK;
273 }
274
touch_pad_timeout_get_threshold(uint32_t * threshold)275 esp_err_t touch_pad_timeout_get_threshold(uint32_t *threshold)
276 {
277 TOUCH_NULL_POINTER_CHECK(threshold, "threshold");
278 TOUCH_ENTER_CRITICAL();
279 touch_hal_timeout_get_threshold(threshold);
280 TOUCH_EXIT_CRITICAL();
281 return ESP_OK;
282 }
283
touch_pad_timeout_resume(void)284 esp_err_t touch_pad_timeout_resume(void)
285 {
286 TOUCH_ENTER_CRITICAL();
287 touch_hal_timer_force_done();
288 TOUCH_EXIT_CRITICAL();
289 return ESP_OK;
290 }
291
touch_pad_config(touch_pad_t touch_num)292 esp_err_t touch_pad_config(touch_pad_t touch_num)
293 {
294 TOUCH_CHANNEL_CHECK(touch_num);
295
296 touch_pad_io_init(touch_num);
297 TOUCH_ENTER_CRITICAL();
298 touch_hal_config(touch_num);
299 touch_hal_set_channel_mask(BIT(touch_num));
300 TOUCH_EXIT_CRITICAL();
301
302 return ESP_OK;
303 }
304
touch_pad_init(void)305 esp_err_t touch_pad_init(void)
306 {
307 if (rtc_touch_mux == NULL) {
308 rtc_touch_mux = xSemaphoreCreateMutex();
309 }
310 if (rtc_touch_mux == NULL) {
311 return ESP_ERR_NO_MEM;
312 }
313 TOUCH_ENTER_CRITICAL();
314 touch_hal_init();
315 TOUCH_EXIT_CRITICAL();
316 return ESP_OK;
317 }
318
touch_pad_deinit(void)319 esp_err_t touch_pad_deinit(void)
320 {
321 ESP_RETURN_ON_FALSE(rtc_touch_mux, ESP_FAIL, TOUCH_TAG, "Touch pad not initialized");
322 xSemaphoreTake(rtc_touch_mux, portMAX_DELAY);
323 TOUCH_ENTER_CRITICAL();
324 touch_hal_deinit();
325 TOUCH_EXIT_CRITICAL();
326 xSemaphoreGive(rtc_touch_mux);
327 vSemaphoreDelete(rtc_touch_mux);
328 rtc_touch_mux = NULL;
329 return ESP_OK;
330 }
331
touch_pad_reset(void)332 esp_err_t touch_pad_reset(void)
333 {
334 TOUCH_ENTER_CRITICAL();
335 touch_hal_reset();
336 TOUCH_EXIT_CRITICAL();
337 return ESP_OK;
338 }
339
touch_pad_read_raw_data(touch_pad_t touch_num,uint32_t * raw_data)340 esp_err_t IRAM_ATTR touch_pad_read_raw_data(touch_pad_t touch_num, uint32_t *raw_data)
341 {
342 ESP_RETURN_ON_FALSE(touch_num < TOUCH_PAD_MAX && touch_num >= 0, ESP_ERR_INVALID_ARG, TOUCH_TAG, "Touch channel error");
343 TOUCH_NULL_POINTER_CHECK(raw_data, "raw_data");
344 TOUCH_ENTER_CRITICAL_SAFE();
345 *raw_data = touch_hal_read_raw_data(touch_num);
346 TOUCH_EXIT_CRITICAL_SAFE();
347 return ESP_OK;
348 }
349
touch_pad_filter_read_smooth(touch_pad_t touch_num,uint32_t * smooth_data)350 esp_err_t IRAM_ATTR touch_pad_filter_read_smooth(touch_pad_t touch_num, uint32_t *smooth_data)
351 {
352 TOUCH_NULL_POINTER_CHECK(smooth_data, "smooth_data");
353 TOUCH_CHANNEL_CHECK(touch_num);
354 TOUCH_ENTER_CRITICAL_SAFE();
355 touch_hal_filter_read_smooth(touch_num, smooth_data);
356 TOUCH_EXIT_CRITICAL_SAFE();
357 return ESP_OK;
358 }
359
touch_pad_read_benchmark(touch_pad_t touch_num,uint32_t * benchmark)360 esp_err_t IRAM_ATTR touch_pad_read_benchmark(touch_pad_t touch_num, uint32_t *benchmark)
361 {
362 TOUCH_NULL_POINTER_CHECK(benchmark, "benchmark");
363 TOUCH_CHANNEL_CHECK(touch_num);
364 TOUCH_ENTER_CRITICAL_SAFE();
365 touch_hal_read_benchmark(touch_num, benchmark);
366 TOUCH_EXIT_CRITICAL_SAFE();
367 return ESP_OK;
368 }
369
370 /* Should be call after clk enable and filter enable. */
touch_pad_reset_benchmark(touch_pad_t touch_num)371 esp_err_t touch_pad_reset_benchmark(touch_pad_t touch_num)
372 {
373 ESP_RETURN_ON_FALSE(touch_num <= TOUCH_PAD_MAX && touch_num >= 0, ESP_ERR_INVALID_ARG, TOUCH_TAG, "Touch channel error");
374 TOUCH_ENTER_CRITICAL();
375 touch_hal_reset_benchmark(touch_num);
376 TOUCH_EXIT_CRITICAL();
377 return ESP_OK;
378 }
379
touch_pad_filter_set_config(const touch_filter_config_t * filter_info)380 esp_err_t touch_pad_filter_set_config(const touch_filter_config_t *filter_info)
381 {
382 TOUCH_NULL_POINTER_CHECK(filter_info, "filter_info");
383 ESP_RETURN_ON_FALSE(filter_info->mode < TOUCH_PAD_FILTER_MAX, ESP_ERR_INVALID_ARG, TOUCH_TAG, TOUCH_PARAM_CHECK_STR("mode"));
384 ESP_RETURN_ON_FALSE(filter_info->debounce_cnt <= TOUCH_DEBOUNCE_CNT_MAX, ESP_ERR_INVALID_ARG, TOUCH_TAG, TOUCH_PARAM_CHECK_STR("debounce"));
385 ESP_RETURN_ON_FALSE(filter_info->noise_thr <= TOUCH_NOISE_THR_MAX, ESP_ERR_INVALID_ARG, TOUCH_TAG, TOUCH_PARAM_CHECK_STR("noise"));
386 ESP_RETURN_ON_FALSE(filter_info->jitter_step <= TOUCH_JITTER_STEP_MAX, ESP_ERR_INVALID_ARG, TOUCH_TAG, TOUCH_PARAM_CHECK_STR("jitter_step"));
387 ESP_RETURN_ON_FALSE(filter_info->smh_lvl < TOUCH_PAD_SMOOTH_MAX, ESP_ERR_INVALID_ARG, TOUCH_TAG, TOUCH_PARAM_CHECK_STR("smooth level"));
388
389 TOUCH_ENTER_CRITICAL();
390 touch_hal_filter_set_config(filter_info);
391 TOUCH_EXIT_CRITICAL();
392
393 return ESP_OK;
394 }
395
touch_pad_filter_get_config(touch_filter_config_t * filter_info)396 esp_err_t touch_pad_filter_get_config(touch_filter_config_t *filter_info)
397 {
398 TOUCH_NULL_POINTER_CHECK(filter_info, "filter_info");
399 TOUCH_ENTER_CRITICAL();
400 touch_hal_filter_get_config(filter_info);
401 TOUCH_EXIT_CRITICAL();
402 return ESP_OK;
403 }
404
touch_pad_filter_enable(void)405 esp_err_t touch_pad_filter_enable(void)
406 {
407 TOUCH_ENTER_CRITICAL();
408 touch_hal_filter_enable();
409 TOUCH_EXIT_CRITICAL();
410 return ESP_OK;
411 }
412
touch_pad_filter_disable(void)413 esp_err_t touch_pad_filter_disable(void)
414 {
415 TOUCH_ENTER_CRITICAL();
416 touch_hal_filter_disable();
417 TOUCH_EXIT_CRITICAL();
418 return ESP_OK;
419 }
420
touch_pad_denoise_enable(void)421 esp_err_t touch_pad_denoise_enable(void)
422 {
423 TOUCH_ENTER_CRITICAL();
424 touch_hal_clear_channel_mask(BIT(SOC_TOUCH_DENOISE_CHANNEL));
425 touch_hal_denoise_enable();
426 TOUCH_EXIT_CRITICAL();
427 return ESP_OK;
428 }
429
touch_pad_denoise_disable(void)430 esp_err_t touch_pad_denoise_disable(void)
431 {
432 TOUCH_ENTER_CRITICAL();
433 touch_hal_denoise_disable();
434 TOUCH_EXIT_CRITICAL();
435 return ESP_OK;
436 }
437
touch_pad_denoise_set_config(const touch_pad_denoise_t * denoise)438 esp_err_t touch_pad_denoise_set_config(const touch_pad_denoise_t *denoise)
439 {
440 TOUCH_NULL_POINTER_CHECK(denoise, "denoise");
441 ESP_RETURN_ON_FALSE(denoise->grade < TOUCH_PAD_DENOISE_MAX, ESP_ERR_INVALID_ARG, TOUCH_TAG, TOUCH_PARAM_CHECK_STR("grade"));
442 ESP_RETURN_ON_FALSE(denoise->cap_level < TOUCH_PAD_DENOISE_CAP_MAX, ESP_ERR_INVALID_ARG, TOUCH_TAG, TOUCH_PARAM_CHECK_STR("cap_level"));
443
444 const touch_hal_meas_mode_t meas = {
445 .slope = TOUCH_PAD_SLOPE_DEFAULT,
446 .tie_opt = TOUCH_PAD_TIE_OPT_DEFAULT,
447 };
448 TOUCH_ENTER_CRITICAL();
449 touch_hal_set_meas_mode(SOC_TOUCH_DENOISE_CHANNEL, &meas);
450 touch_hal_denoise_set_config(denoise);
451 TOUCH_EXIT_CRITICAL();
452
453 return ESP_OK;
454 }
455
touch_pad_denoise_get_config(touch_pad_denoise_t * denoise)456 esp_err_t touch_pad_denoise_get_config(touch_pad_denoise_t *denoise)
457 {
458 TOUCH_NULL_POINTER_CHECK(denoise, "denoise");
459 TOUCH_ENTER_CRITICAL();
460 touch_hal_denoise_get_config(denoise);
461 TOUCH_EXIT_CRITICAL();
462 return ESP_OK;
463 }
464
touch_pad_denoise_read_data(uint32_t * data)465 esp_err_t touch_pad_denoise_read_data(uint32_t *data)
466 {
467 TOUCH_NULL_POINTER_CHECK(data, "data");
468 touch_hal_denoise_read_data(data);
469 return ESP_OK;
470 }
471
touch_pad_waterproof_set_config(const touch_pad_waterproof_t * waterproof)472 esp_err_t touch_pad_waterproof_set_config(const touch_pad_waterproof_t *waterproof)
473 {
474 TOUCH_NULL_POINTER_CHECK(waterproof, "waterproof");
475 ESP_RETURN_ON_FALSE(waterproof->guard_ring_pad < SOC_TOUCH_SENSOR_NUM, ESP_ERR_INVALID_ARG, TOUCH_TAG, TOUCH_PARAM_CHECK_STR("pad"));
476 ESP_RETURN_ON_FALSE(waterproof->shield_driver < TOUCH_PAD_SHIELD_DRV_MAX, ESP_ERR_INVALID_ARG, TOUCH_TAG, TOUCH_PARAM_CHECK_STR("shield_driver"));
477
478 TOUCH_ENTER_CRITICAL();
479 touch_hal_waterproof_set_config(waterproof);
480 TOUCH_EXIT_CRITICAL();
481 return ESP_OK;
482 }
483
touch_pad_waterproof_get_config(touch_pad_waterproof_t * waterproof)484 esp_err_t touch_pad_waterproof_get_config(touch_pad_waterproof_t *waterproof)
485 {
486 TOUCH_NULL_POINTER_CHECK(waterproof, "waterproof");
487 TOUCH_ENTER_CRITICAL();
488 touch_hal_waterproof_get_config(waterproof);
489 TOUCH_EXIT_CRITICAL();
490 return ESP_OK;
491 }
492
touch_pad_waterproof_enable(void)493 esp_err_t touch_pad_waterproof_enable(void)
494 {
495 touch_pad_io_init(SOC_TOUCH_SHIELD_CHANNEL);
496 TOUCH_ENTER_CRITICAL();
497 touch_hal_waterproof_enable();
498 TOUCH_EXIT_CRITICAL();
499 return ESP_OK;
500 }
501
touch_pad_waterproof_disable(void)502 esp_err_t touch_pad_waterproof_disable(void)
503 {
504 TOUCH_ENTER_CRITICAL();
505 touch_hal_waterproof_disable();
506 TOUCH_EXIT_CRITICAL();
507 return ESP_OK;
508 }
509
touch_pad_proximity_enable(touch_pad_t touch_num,bool enabled)510 esp_err_t touch_pad_proximity_enable(touch_pad_t touch_num, bool enabled)
511 {
512 esp_err_t ret = ESP_OK;
513 ESP_RETURN_ON_FALSE(touch_num < TOUCH_PAD_MAX, ESP_ERR_INVALID_ARG, TOUCH_TAG, "Touch channel error");
514
515 TOUCH_ENTER_CRITICAL();
516 if (!touch_hal_enable_proximity(touch_num, enabled)) {
517 ret = ESP_ERR_NOT_SUPPORTED;
518 }
519 TOUCH_EXIT_CRITICAL();
520 return ret;
521 }
522
touch_pad_proximity_set_count(touch_pad_t touch_num,uint32_t count)523 esp_err_t touch_pad_proximity_set_count(touch_pad_t touch_num, uint32_t count)
524 {
525 ESP_RETURN_ON_FALSE(count <= TOUCH_PROXIMITY_MEAS_NUM_MAX, ESP_ERR_INVALID_ARG, TOUCH_TAG, TOUCH_PARAM_CHECK_STR("measure count"));
526
527 TOUCH_ENTER_CRITICAL();
528 touch_hal_proximity_set_meas_times(count);
529 TOUCH_EXIT_CRITICAL();
530 return ESP_OK;
531 }
532
touch_pad_proximity_get_count(touch_pad_t touch_num,uint32_t * count)533 esp_err_t touch_pad_proximity_get_count(touch_pad_t touch_num, uint32_t *count)
534 {
535 ESP_RETURN_ON_FALSE(count, ESP_ERR_INVALID_ARG, TOUCH_TAG, TOUCH_PARAM_CHECK_STR("measure count"));
536
537 TOUCH_ENTER_CRITICAL_SAFE();
538 touch_hal_proximity_get_meas_times(count);
539 TOUCH_EXIT_CRITICAL_SAFE();
540 return ESP_OK;
541 }
542
543 /**
544 * @brief Get measure count of proximity channel.
545 * The proximity sensor measurement is the accumulation of touch channel measurements.
546 * @param touch_num touch pad index
547 * @param cnt Pointer to receive proximity channel measurement count
548 * @return
549 * - ESP_OK Success
550 * - ESP_ERR_INVALID_ARG parameter is NULL
551 */
touch_pad_proximity_read_meas_cnt(touch_pad_t touch_num,uint32_t * cnt)552 esp_err_t touch_pad_proximity_read_meas_cnt(touch_pad_t touch_num, uint32_t *cnt)
553 {
554 TOUCH_NULL_POINTER_CHECK(cnt, "cnt");
555 ESP_RETURN_ON_FALSE(touch_hal_proximity_pad_check(touch_num), ESP_ERR_INVALID_ARG, TOUCH_TAG, "touch num is not proximity");
556 TOUCH_ENTER_CRITICAL_SAFE();
557 touch_hal_proximity_read_meas_cnt(touch_num, cnt);
558 TOUCH_EXIT_CRITICAL_SAFE();
559 return ESP_OK;
560 }
561
touch_pad_proximity_get_data(touch_pad_t touch_num,uint32_t * measure_out)562 esp_err_t touch_pad_proximity_get_data(touch_pad_t touch_num, uint32_t *measure_out)
563 {
564 ESP_RETURN_ON_FALSE(touch_hal_proximity_pad_check(touch_num), ESP_ERR_INVALID_ARG, TOUCH_TAG, "touch num is not proximity");
565 TOUCH_NULL_POINTER_CHECK(measure_out, "measure_out");
566 TOUCH_ENTER_CRITICAL_SAFE();
567 touch_hal_read_benchmark(touch_num, measure_out);
568 TOUCH_EXIT_CRITICAL_SAFE();
569 return ESP_OK;
570 }
571
572 /************** sleep pad setting ***********************/
573
touch_pad_sleep_channel_get_info(touch_pad_sleep_channel_t * slp_config)574 esp_err_t touch_pad_sleep_channel_get_info(touch_pad_sleep_channel_t *slp_config)
575 {
576 TOUCH_NULL_POINTER_CHECK(slp_config, "slp_config");
577 TOUCH_ENTER_CRITICAL_SAFE();
578 touch_hal_sleep_channel_get_config(slp_config);
579 TOUCH_EXIT_CRITICAL_SAFE();
580 return ESP_OK;
581 }
582
touch_pad_sleep_channel_enable(touch_pad_t pad_num,bool enable)583 esp_err_t touch_pad_sleep_channel_enable(touch_pad_t pad_num, bool enable)
584 {
585 TOUCH_CHANNEL_CHECK(pad_num);
586
587 TOUCH_ENTER_CRITICAL();
588 touch_hal_sleep_channel_enable(pad_num, enable);
589 TOUCH_EXIT_CRITICAL();
590 return ESP_OK;
591 }
592
touch_pad_sleep_channel_enable_proximity(touch_pad_t pad_num,bool enable)593 esp_err_t touch_pad_sleep_channel_enable_proximity(touch_pad_t pad_num, bool enable)
594 {
595 TOUCH_CHANNEL_CHECK(pad_num);
596
597 TOUCH_ENTER_CRITICAL();
598 if (enable) {
599 touch_hal_sleep_enable_approach();
600 } else {
601 touch_hal_sleep_disable_approach();
602 }
603 TOUCH_EXIT_CRITICAL();
604 return ESP_OK;
605 }
606
touch_pad_sleep_get_channel_num(touch_pad_t * pad_num)607 esp_err_t touch_pad_sleep_get_channel_num(touch_pad_t *pad_num)
608 {
609 TOUCH_NULL_POINTER_CHECK(pad_num, "pad_num");
610 TOUCH_ENTER_CRITICAL();
611 touch_hal_sleep_get_channel_num(pad_num);
612 TOUCH_EXIT_CRITICAL();
613 return ESP_OK;
614 }
615
touch_pad_sleep_set_threshold(touch_pad_t pad_num,uint32_t touch_thres)616 esp_err_t touch_pad_sleep_set_threshold(touch_pad_t pad_num, uint32_t touch_thres)
617 {
618 TOUCH_ENTER_CRITICAL();
619 touch_hal_sleep_set_threshold(touch_thres);
620 TOUCH_EXIT_CRITICAL();
621 return ESP_OK;
622 }
623
touch_pad_sleep_get_threshold(touch_pad_t pad_num,uint32_t * touch_thres)624 esp_err_t touch_pad_sleep_get_threshold(touch_pad_t pad_num, uint32_t *touch_thres)
625 {
626 TOUCH_NULL_POINTER_CHECK(touch_thres, "touch_thres");
627 TOUCH_ENTER_CRITICAL();
628 touch_hal_sleep_get_threshold(touch_thres);
629 TOUCH_EXIT_CRITICAL();
630 return ESP_OK;
631 }
632
touch_pad_sleep_channel_read_benchmark(touch_pad_t pad_num,uint32_t * benchmark)633 esp_err_t touch_pad_sleep_channel_read_benchmark(touch_pad_t pad_num, uint32_t *benchmark)
634 {
635 TOUCH_NULL_POINTER_CHECK(benchmark, "benchmark");
636 TOUCH_ENTER_CRITICAL_SAFE();
637 touch_hal_sleep_read_benchmark(benchmark);
638 TOUCH_EXIT_CRITICAL_SAFE();
639 return ESP_OK;
640 }
641
touch_pad_sleep_channel_read_smooth(touch_pad_t pad_num,uint32_t * smooth_data)642 esp_err_t touch_pad_sleep_channel_read_smooth(touch_pad_t pad_num, uint32_t *smooth_data)
643 {
644 TOUCH_NULL_POINTER_CHECK(smooth_data, "smooth_data");
645 TOUCH_ENTER_CRITICAL_SAFE();
646 touch_hal_sleep_read_smooth(smooth_data);
647 TOUCH_EXIT_CRITICAL_SAFE();
648 return ESP_OK;
649 }
650
touch_pad_sleep_channel_read_data(touch_pad_t pad_num,uint32_t * raw_data)651 esp_err_t touch_pad_sleep_channel_read_data(touch_pad_t pad_num, uint32_t *raw_data)
652 {
653 TOUCH_NULL_POINTER_CHECK(raw_data, "raw_data");
654 TOUCH_ENTER_CRITICAL_SAFE();
655 touch_hal_sleep_read_data(raw_data);
656 TOUCH_EXIT_CRITICAL_SAFE();
657 return ESP_OK;
658 }
659
touch_pad_sleep_channel_reset_benchmark(void)660 esp_err_t touch_pad_sleep_channel_reset_benchmark(void)
661 {
662 TOUCH_ENTER_CRITICAL();
663 touch_hal_sleep_reset_benchmark();
664 TOUCH_EXIT_CRITICAL();
665 return ESP_OK;
666 }
667
touch_pad_sleep_channel_read_debounce(touch_pad_t pad_num,uint32_t * debounce)668 esp_err_t touch_pad_sleep_channel_read_debounce(touch_pad_t pad_num, uint32_t *debounce)
669 {
670 TOUCH_NULL_POINTER_CHECK(debounce, "debounce");
671 touch_hal_sleep_read_debounce(debounce);
672 return ESP_OK;
673 }
674
touch_pad_sleep_channel_read_proximity_cnt(touch_pad_t pad_num,uint32_t * approach_cnt)675 esp_err_t touch_pad_sleep_channel_read_proximity_cnt(touch_pad_t pad_num, uint32_t *approach_cnt)
676 {
677 TOUCH_NULL_POINTER_CHECK(approach_cnt, "approach_cnt");
678 touch_hal_sleep_read_proximity_cnt(approach_cnt);
679 return ESP_OK;
680 }
681
touch_pad_sleep_channel_set_work_time(uint16_t sleep_cycle,uint16_t meas_times)682 esp_err_t touch_pad_sleep_channel_set_work_time(uint16_t sleep_cycle, uint16_t meas_times)
683 {
684 touch_hal_sleep_channel_set_work_time(sleep_cycle, meas_times);
685 return ESP_OK;
686 }
687