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 
touch_pad_isr_register(intr_handler_t fn,void * arg,touch_pad_intr_mask_t intr_mask)62 esp_err_t touch_pad_isr_register(intr_handler_t fn, void *arg, touch_pad_intr_mask_t intr_mask)
63 {
64     ESP_RETURN_ON_FALSE(fn, ESP_ERR_INVALID_ARG, TOUCH_TAG,  TOUCH_PARAM_CHECK_STR("intr_mask"));
65     TOUCH_INTR_MASK_CHECK(intr_mask);
66 
67     uint32_t en_msk = 0;
68     if (intr_mask & TOUCH_PAD_INTR_MASK_DONE) {
69         en_msk |= RTC_CNTL_TOUCH_DONE_INT_ST_M;
70     }
71     if (intr_mask & TOUCH_PAD_INTR_MASK_ACTIVE) {
72         en_msk |= RTC_CNTL_TOUCH_ACTIVE_INT_ST_M;
73     }
74     if (intr_mask & TOUCH_PAD_INTR_MASK_INACTIVE) {
75         en_msk |= RTC_CNTL_TOUCH_INACTIVE_INT_ST_M;
76     }
77     if (intr_mask & TOUCH_PAD_INTR_MASK_SCAN_DONE) {
78         en_msk |= RTC_CNTL_TOUCH_SCAN_DONE_INT_ST_M;
79     }
80     if (intr_mask & TOUCH_PAD_INTR_MASK_TIMEOUT) {
81         en_msk |= RTC_CNTL_TOUCH_TIMEOUT_INT_ST_M;
82     }
83 #if SOC_TOUCH_PROXIMITY_MEAS_DONE_SUPPORTED
84     if (intr_mask & TOUCH_PAD_INTR_MASK_PROXI_MEAS_DONE) {
85         en_msk |= RTC_CNTL_TOUCH_APPROACH_LOOP_DONE_INT_ST_M;
86     }
87 #endif
88     esp_err_t ret = rtc_isr_register(fn, arg, en_msk, 0);
89 
90     return ret;
91 }
92 
touch_pad_set_measurement_interval(uint16_t interval_cycle)93 esp_err_t touch_pad_set_measurement_interval(uint16_t interval_cycle)
94 {
95     TOUCH_ENTER_CRITICAL();
96     touch_hal_set_sleep_time(interval_cycle);
97     TOUCH_EXIT_CRITICAL();
98     return ESP_OK;
99 }
100 
touch_pad_get_measurement_interval(uint16_t * interval_cycle)101 esp_err_t touch_pad_get_measurement_interval(uint16_t *interval_cycle)
102 {
103     TOUCH_NULL_POINTER_CHECK(interval_cycle, "interval_cycle");
104     TOUCH_ENTER_CRITICAL();
105     touch_hal_get_sleep_time(interval_cycle);
106     TOUCH_EXIT_CRITICAL();
107     return ESP_OK;
108 }
109 
touch_pad_set_charge_discharge_times(uint16_t charge_discharge_times)110 esp_err_t touch_pad_set_charge_discharge_times(uint16_t charge_discharge_times)
111 {
112     TOUCH_ENTER_CRITICAL();
113     touch_hal_set_meas_times(charge_discharge_times);
114     TOUCH_EXIT_CRITICAL();
115 
116     return ESP_OK;
117 }
118 
touch_pad_get_charge_discharge_times(uint16_t * charge_discharge_times)119 esp_err_t touch_pad_get_charge_discharge_times(uint16_t *charge_discharge_times)
120 {
121     TOUCH_NULL_POINTER_CHECK(charge_discharge_times, "charge_discharge_times");
122     TOUCH_ENTER_CRITICAL();
123     touch_hal_get_measure_times(charge_discharge_times);
124     TOUCH_EXIT_CRITICAL();
125 
126     return ESP_OK;
127 }
128 
touch_pad_set_meas_time(uint16_t sleep_cycle,uint16_t meas_times)129 esp_err_t touch_pad_set_meas_time(uint16_t sleep_cycle, uint16_t meas_times)
130 {
131     touch_pad_set_charge_discharge_times(meas_times);
132     touch_pad_set_measurement_interval(sleep_cycle);
133     return ESP_OK;
134 }
135 
touch_pad_get_meas_time(uint16_t * sleep_cycle,uint16_t * meas_times)136 esp_err_t touch_pad_get_meas_time(uint16_t *sleep_cycle, uint16_t *meas_times)
137 {
138     TOUCH_NULL_POINTER_CHECK(sleep_cycle, "sleep_cycle");
139     TOUCH_NULL_POINTER_CHECK(meas_times, "meas_times");
140     touch_pad_get_measurement_interval(sleep_cycle);
141     touch_pad_get_charge_discharge_times(meas_times);
142     return ESP_OK;
143 }
144 
touch_pad_set_idle_channel_connect(touch_pad_conn_type_t type)145 esp_err_t touch_pad_set_idle_channel_connect(touch_pad_conn_type_t type)
146 {
147     ESP_RETURN_ON_FALSE(type < TOUCH_PAD_CONN_MAX, ESP_ERR_INVALID_ARG, TOUCH_TAG,  TOUCH_PARAM_CHECK_STR("type"));
148     TOUCH_ENTER_CRITICAL();
149     touch_hal_set_idle_channel_connect(type);
150     TOUCH_EXIT_CRITICAL();
151     return ESP_OK;
152 }
153 
touch_pad_get_idle_channel_connect(touch_pad_conn_type_t * type)154 esp_err_t touch_pad_get_idle_channel_connect(touch_pad_conn_type_t *type)
155 {
156     TOUCH_NULL_POINTER_CHECK(type, "type");
157     touch_hal_get_idle_channel_connect(type);
158     return ESP_OK;
159 }
160 
touch_pad_meas_is_done(void)161 bool touch_pad_meas_is_done(void)
162 {
163     return touch_hal_meas_is_done();
164 }
165 
touch_pad_set_channel_mask(uint16_t enable_mask)166 esp_err_t touch_pad_set_channel_mask(uint16_t enable_mask)
167 {
168     TOUCH_CH_MASK_CHECK(enable_mask);
169     TOUCH_ENTER_CRITICAL();
170     touch_hal_set_channel_mask(enable_mask);
171     TOUCH_EXIT_CRITICAL();
172 
173     return ESP_OK;
174 }
175 
touch_pad_get_channel_mask(uint16_t * enable_mask)176 esp_err_t touch_pad_get_channel_mask(uint16_t *enable_mask)
177 {
178     TOUCH_NULL_POINTER_CHECK(enable_mask, "enable_mask");
179     TOUCH_ENTER_CRITICAL();
180     touch_hal_get_channel_mask(enable_mask);
181     TOUCH_EXIT_CRITICAL();
182 
183     return ESP_OK;
184 }
185 
touch_pad_clear_channel_mask(uint16_t enable_mask)186 esp_err_t touch_pad_clear_channel_mask(uint16_t enable_mask)
187 {
188     TOUCH_CH_MASK_CHECK(enable_mask);
189     TOUCH_ENTER_CRITICAL();
190     touch_hal_clear_channel_mask(enable_mask);
191     TOUCH_EXIT_CRITICAL();
192     return ESP_OK;
193 }
194 
touch_pad_get_current_meas_channel(void)195 touch_pad_t IRAM_ATTR touch_pad_get_current_meas_channel(void)
196 {
197     return (touch_pad_t)touch_hal_get_current_meas_channel();
198 }
199 
touch_pad_intr_enable(touch_pad_intr_mask_t int_mask)200 esp_err_t touch_pad_intr_enable(touch_pad_intr_mask_t int_mask)
201 {
202     if (!(int_mask & TOUCH_PAD_INTR_MASK_ALL)) {
203         return ESP_ERR_INVALID_ARG;
204     }
205     TOUCH_ENTER_CRITICAL_SAFE();
206     touch_hal_intr_enable(int_mask);
207     TOUCH_EXIT_CRITICAL_SAFE();
208     return ESP_OK;
209 }
210 
touch_pad_intr_disable(touch_pad_intr_mask_t int_mask)211 esp_err_t touch_pad_intr_disable(touch_pad_intr_mask_t int_mask)
212 {
213     if (!(int_mask & TOUCH_PAD_INTR_MASK_ALL)) {
214         return ESP_ERR_INVALID_ARG;
215     }
216     TOUCH_ENTER_CRITICAL_SAFE();
217     touch_hal_intr_disable(int_mask);
218     TOUCH_EXIT_CRITICAL_SAFE();
219     return ESP_OK;
220 }
221 
touch_pad_intr_clear(touch_pad_intr_mask_t int_mask)222 esp_err_t touch_pad_intr_clear(touch_pad_intr_mask_t int_mask)
223 {
224     TOUCH_INTR_MASK_CHECK(int_mask);
225     TOUCH_ENTER_CRITICAL();
226     touch_hal_intr_clear(int_mask);
227     TOUCH_EXIT_CRITICAL();
228     return ESP_OK;
229 }
230 
touch_pad_read_intr_status_mask(void)231 uint32_t touch_pad_read_intr_status_mask(void)
232 {
233     return touch_hal_read_intr_status_mask();
234 }
235 
touch_pad_timeout_set(bool enable,uint32_t threshold)236 esp_err_t touch_pad_timeout_set(bool enable, uint32_t threshold)
237 {
238     TOUCH_ENTER_CRITICAL();
239     if (enable) {
240         touch_hal_timeout_enable();
241     } else {
242         touch_hal_timeout_disable();
243     }
244     touch_hal_timeout_set_threshold(threshold);
245     TOUCH_EXIT_CRITICAL();
246     return ESP_OK;
247 }
248 
touch_pad_timeout_get_threshold(uint32_t * threshold)249 esp_err_t touch_pad_timeout_get_threshold(uint32_t *threshold)
250 {
251     TOUCH_NULL_POINTER_CHECK(threshold, "threshold");
252     TOUCH_ENTER_CRITICAL();
253     touch_hal_timeout_get_threshold(threshold);
254     TOUCH_EXIT_CRITICAL();
255     return ESP_OK;
256 }
257 
touch_pad_timeout_resume(void)258 esp_err_t touch_pad_timeout_resume(void)
259 {
260     TOUCH_ENTER_CRITICAL();
261     touch_hal_timer_force_done();
262     TOUCH_EXIT_CRITICAL();
263     return ESP_OK;
264 }
265 
touch_pad_config(touch_pad_t touch_num)266 esp_err_t touch_pad_config(touch_pad_t touch_num)
267 {
268     TOUCH_CHANNEL_CHECK(touch_num);
269 
270     touch_pad_io_init(touch_num);
271     TOUCH_ENTER_CRITICAL();
272     touch_hal_config(touch_num);
273     touch_hal_set_channel_mask(BIT(touch_num));
274     TOUCH_EXIT_CRITICAL();
275 
276     return ESP_OK;
277 }
278 
touch_pad_init(void)279 esp_err_t touch_pad_init(void)
280 {
281     if (rtc_touch_mux == NULL) {
282         rtc_touch_mux = xSemaphoreCreateMutex();
283     }
284     if (rtc_touch_mux == NULL) {
285         return ESP_ERR_NO_MEM;
286     }
287     TOUCH_ENTER_CRITICAL();
288     touch_hal_init();
289     TOUCH_EXIT_CRITICAL();
290     return ESP_OK;
291 }
292 
touch_pad_deinit(void)293 esp_err_t touch_pad_deinit(void)
294 {
295     ESP_RETURN_ON_FALSE(rtc_touch_mux, ESP_FAIL, TOUCH_TAG,  "Touch pad not initialized");
296     xSemaphoreTake(rtc_touch_mux, portMAX_DELAY);
297     TOUCH_ENTER_CRITICAL();
298     touch_hal_deinit();
299     TOUCH_EXIT_CRITICAL();
300     xSemaphoreGive(rtc_touch_mux);
301     vSemaphoreDelete(rtc_touch_mux);
302     rtc_touch_mux = NULL;
303     return ESP_OK;
304 }
305 
touch_pad_reset(void)306 esp_err_t touch_pad_reset(void)
307 {
308     TOUCH_ENTER_CRITICAL();
309     touch_hal_reset();
310     TOUCH_EXIT_CRITICAL();
311     return ESP_OK;
312 }
313 
touch_pad_read_raw_data(touch_pad_t touch_num,uint32_t * raw_data)314 esp_err_t IRAM_ATTR touch_pad_read_raw_data(touch_pad_t touch_num, uint32_t *raw_data)
315 {
316     TOUCH_CHANNEL_CHECK(touch_num);
317     TOUCH_NULL_POINTER_CHECK(raw_data, "raw_data");
318     TOUCH_ENTER_CRITICAL_SAFE();
319     *raw_data = touch_hal_read_raw_data(touch_num);
320     TOUCH_EXIT_CRITICAL_SAFE();
321     return ESP_OK;
322 }
323 
touch_pad_filter_read_smooth(touch_pad_t touch_num,uint32_t * smooth_data)324 esp_err_t IRAM_ATTR touch_pad_filter_read_smooth(touch_pad_t touch_num, uint32_t *smooth_data)
325 {
326     TOUCH_CHANNEL_CHECK(touch_num);
327     TOUCH_NULL_POINTER_CHECK(smooth_data, "smooth_data");
328     TOUCH_ENTER_CRITICAL_SAFE();
329     touch_hal_filter_read_smooth(touch_num, smooth_data);
330     TOUCH_EXIT_CRITICAL_SAFE();
331     return ESP_OK;
332 }
333 
touch_pad_read_benchmark(touch_pad_t touch_num,uint32_t * benchmark)334 esp_err_t IRAM_ATTR touch_pad_read_benchmark(touch_pad_t touch_num, uint32_t *benchmark)
335 {
336     TOUCH_CHANNEL_CHECK(touch_num);
337     TOUCH_NULL_POINTER_CHECK(benchmark, "benchmark");
338     TOUCH_ENTER_CRITICAL_SAFE();
339     touch_hal_read_benchmark(touch_num, benchmark);
340     TOUCH_EXIT_CRITICAL_SAFE();
341     return ESP_OK;
342 }
343 
344 /* Should be call after clk enable and filter enable. */
touch_pad_reset_benchmark(touch_pad_t touch_num)345 esp_err_t touch_pad_reset_benchmark(touch_pad_t touch_num)
346 {
347     ESP_RETURN_ON_FALSE(touch_num <= TOUCH_PAD_MAX && touch_num >= 0, ESP_ERR_INVALID_ARG, TOUCH_TAG,  "Touch channel error");
348     TOUCH_ENTER_CRITICAL();
349     touch_hal_reset_benchmark(touch_num);
350     TOUCH_EXIT_CRITICAL();
351     return ESP_OK;
352 }
353 
touch_pad_filter_set_config(const touch_filter_config_t * filter_info)354 esp_err_t touch_pad_filter_set_config(const touch_filter_config_t *filter_info)
355 {
356     TOUCH_NULL_POINTER_CHECK(filter_info, "filter_info");
357     ESP_RETURN_ON_FALSE(filter_info->mode < TOUCH_PAD_FILTER_MAX, ESP_ERR_INVALID_ARG, TOUCH_TAG,  TOUCH_PARAM_CHECK_STR("mode"));
358     ESP_RETURN_ON_FALSE(filter_info->debounce_cnt <= TOUCH_DEBOUNCE_CNT_MAX, ESP_ERR_INVALID_ARG, TOUCH_TAG,  TOUCH_PARAM_CHECK_STR("debounce"));
359     ESP_RETURN_ON_FALSE(filter_info->noise_thr <= TOUCH_NOISE_THR_MAX, ESP_ERR_INVALID_ARG, TOUCH_TAG,  TOUCH_PARAM_CHECK_STR("noise"));
360     ESP_RETURN_ON_FALSE(filter_info->jitter_step <= TOUCH_JITTER_STEP_MAX, ESP_ERR_INVALID_ARG, TOUCH_TAG,  TOUCH_PARAM_CHECK_STR("jitter_step"));
361     ESP_RETURN_ON_FALSE(filter_info->smh_lvl < TOUCH_PAD_SMOOTH_MAX, ESP_ERR_INVALID_ARG, TOUCH_TAG,  TOUCH_PARAM_CHECK_STR("smooth level"));
362 
363     TOUCH_ENTER_CRITICAL();
364     touch_hal_filter_set_config(filter_info);
365     TOUCH_EXIT_CRITICAL();
366 
367     return ESP_OK;
368 }
369 
touch_pad_filter_get_config(touch_filter_config_t * filter_info)370 esp_err_t touch_pad_filter_get_config(touch_filter_config_t *filter_info)
371 {
372     TOUCH_NULL_POINTER_CHECK(filter_info, "filter_info");
373     TOUCH_ENTER_CRITICAL();
374     touch_hal_filter_get_config(filter_info);
375     TOUCH_EXIT_CRITICAL();
376     return ESP_OK;
377 }
378 
touch_pad_filter_enable(void)379 esp_err_t touch_pad_filter_enable(void)
380 {
381     TOUCH_ENTER_CRITICAL();
382     touch_hal_filter_enable();
383     TOUCH_EXIT_CRITICAL();
384     return ESP_OK;
385 }
386 
touch_pad_filter_disable(void)387 esp_err_t touch_pad_filter_disable(void)
388 {
389     TOUCH_ENTER_CRITICAL();
390     touch_hal_filter_disable();
391     TOUCH_EXIT_CRITICAL();
392     return ESP_OK;
393 }
394 
touch_pad_denoise_enable(void)395 esp_err_t touch_pad_denoise_enable(void)
396 {
397     TOUCH_ENTER_CRITICAL();
398     touch_hal_clear_channel_mask(BIT(SOC_TOUCH_DENOISE_CHANNEL));
399     touch_hal_denoise_enable();
400     TOUCH_EXIT_CRITICAL();
401     return ESP_OK;
402 }
403 
touch_pad_denoise_disable(void)404 esp_err_t touch_pad_denoise_disable(void)
405 {
406     TOUCH_ENTER_CRITICAL();
407     touch_hal_denoise_disable();
408     TOUCH_EXIT_CRITICAL();
409     return ESP_OK;
410 }
411 
touch_pad_denoise_set_config(const touch_pad_denoise_t * denoise)412 esp_err_t touch_pad_denoise_set_config(const touch_pad_denoise_t *denoise)
413 {
414     TOUCH_NULL_POINTER_CHECK(denoise, "denoise");
415     ESP_RETURN_ON_FALSE(denoise->grade < TOUCH_PAD_DENOISE_MAX, ESP_ERR_INVALID_ARG, TOUCH_TAG,  TOUCH_PARAM_CHECK_STR("grade"));
416     ESP_RETURN_ON_FALSE(denoise->cap_level < TOUCH_PAD_DENOISE_CAP_MAX, ESP_ERR_INVALID_ARG, TOUCH_TAG,  TOUCH_PARAM_CHECK_STR("cap_level"));
417 
418     const touch_hal_meas_mode_t meas = {
419         .slope = TOUCH_PAD_SLOPE_DEFAULT,
420         .tie_opt = TOUCH_PAD_TIE_OPT_DEFAULT,
421     };
422     TOUCH_ENTER_CRITICAL();
423     touch_hal_set_meas_mode(SOC_TOUCH_DENOISE_CHANNEL, &meas);
424     touch_hal_denoise_set_config(denoise);
425     TOUCH_EXIT_CRITICAL();
426 
427     return ESP_OK;
428 }
429 
touch_pad_denoise_get_config(touch_pad_denoise_t * denoise)430 esp_err_t touch_pad_denoise_get_config(touch_pad_denoise_t *denoise)
431 {
432     TOUCH_NULL_POINTER_CHECK(denoise, "denoise");
433     TOUCH_ENTER_CRITICAL();
434     touch_hal_denoise_get_config(denoise);
435     TOUCH_EXIT_CRITICAL();
436     return ESP_OK;
437 }
438 
touch_pad_denoise_read_data(uint32_t * data)439 esp_err_t touch_pad_denoise_read_data(uint32_t *data)
440 {
441     touch_hal_denoise_read_data(data);
442     return ESP_OK;
443 }
444 
touch_pad_waterproof_set_config(const touch_pad_waterproof_t * waterproof)445 esp_err_t touch_pad_waterproof_set_config(const touch_pad_waterproof_t *waterproof)
446 {
447     TOUCH_NULL_POINTER_CHECK(waterproof, "waterproof");
448     ESP_RETURN_ON_FALSE(waterproof->guard_ring_pad < SOC_TOUCH_SENSOR_NUM, ESP_ERR_INVALID_ARG, TOUCH_TAG,  TOUCH_PARAM_CHECK_STR("pad"));
449     ESP_RETURN_ON_FALSE(waterproof->shield_driver < TOUCH_PAD_SHIELD_DRV_MAX, ESP_ERR_INVALID_ARG, TOUCH_TAG,  TOUCH_PARAM_CHECK_STR("shield_driver"));
450 
451     TOUCH_ENTER_CRITICAL();
452     touch_hal_waterproof_set_config(waterproof);
453     TOUCH_EXIT_CRITICAL();
454     return ESP_OK;
455 }
456 
touch_pad_waterproof_get_config(touch_pad_waterproof_t * waterproof)457 esp_err_t touch_pad_waterproof_get_config(touch_pad_waterproof_t *waterproof)
458 {
459     TOUCH_NULL_POINTER_CHECK(waterproof, "waterproof");
460     TOUCH_ENTER_CRITICAL();
461     touch_hal_waterproof_get_config(waterproof);
462     TOUCH_EXIT_CRITICAL();
463     return ESP_OK;
464 }
465 
touch_pad_waterproof_enable(void)466 esp_err_t touch_pad_waterproof_enable(void)
467 {
468     touch_pad_io_init(SOC_TOUCH_SHIELD_CHANNEL);
469     TOUCH_ENTER_CRITICAL();
470     touch_hal_waterproof_enable();
471     TOUCH_EXIT_CRITICAL();
472     return ESP_OK;
473 }
474 
touch_pad_waterproof_disable(void)475 esp_err_t touch_pad_waterproof_disable(void)
476 {
477     TOUCH_ENTER_CRITICAL();
478     touch_hal_waterproof_disable();
479     TOUCH_EXIT_CRITICAL();
480     return ESP_OK;
481 }
482 
touch_pad_proximity_enable(touch_pad_t touch_num,bool enabled)483 esp_err_t touch_pad_proximity_enable(touch_pad_t touch_num, bool enabled)
484 {
485     esp_err_t ret = ESP_OK;
486     ESP_RETURN_ON_FALSE(touch_num < TOUCH_PAD_MAX, ESP_ERR_INVALID_ARG, TOUCH_TAG,  "Touch channel error");
487 
488     TOUCH_ENTER_CRITICAL();
489     if (!touch_hal_enable_proximity(touch_num, enabled)) {
490         ret = ESP_ERR_NOT_SUPPORTED;
491     }
492     TOUCH_EXIT_CRITICAL();
493     return ret;
494 }
495 
touch_pad_proximity_set_count(touch_pad_t touch_num,uint32_t count)496 esp_err_t touch_pad_proximity_set_count(touch_pad_t touch_num, uint32_t count)
497 {
498     ESP_RETURN_ON_FALSE(count <= TOUCH_PROXIMITY_MEAS_NUM_MAX, ESP_ERR_INVALID_ARG, TOUCH_TAG,  TOUCH_PARAM_CHECK_STR("measure count"));
499 
500     TOUCH_ENTER_CRITICAL();
501     touch_hal_proximity_set_meas_times(count);
502     TOUCH_EXIT_CRITICAL();
503     return ESP_OK;
504 }
505 
touch_pad_proximity_get_count(touch_pad_t touch_num,uint32_t * count)506 esp_err_t touch_pad_proximity_get_count(touch_pad_t touch_num, uint32_t *count)
507 {
508     ESP_RETURN_ON_FALSE(count, ESP_ERR_INVALID_ARG, TOUCH_TAG,  TOUCH_PARAM_CHECK_STR("measure count"));
509 
510     TOUCH_ENTER_CRITICAL_SAFE();
511     touch_hal_proximity_get_meas_times(count);
512     TOUCH_EXIT_CRITICAL_SAFE();
513     return ESP_OK;
514 }
515 
516 /**
517  * @brief Get measure count of proximity channel.
518  *        The proximity sensor measurement is the accumulation of touch channel measurements.
519  * @param touch_num touch pad index
520  * @param cnt Pointer to receive proximity channel measurement count
521  * @return
522  *     - ESP_OK Success
523  *     - ESP_ERR_INVALID_ARG parameter is NULL
524  */
touch_pad_proximity_read_meas_cnt(touch_pad_t touch_num,uint32_t * cnt)525 esp_err_t touch_pad_proximity_read_meas_cnt(touch_pad_t touch_num, uint32_t *cnt)
526 {
527     TOUCH_NULL_POINTER_CHECK(cnt, "cnt");
528     ESP_RETURN_ON_FALSE(touch_hal_proximity_pad_check(touch_num), ESP_ERR_INVALID_ARG, TOUCH_TAG,  "touch num is not proximity");
529     TOUCH_ENTER_CRITICAL_SAFE();
530     touch_hal_proximity_read_meas_cnt(touch_num, cnt);
531     TOUCH_EXIT_CRITICAL_SAFE();
532     return ESP_OK;
533 }
534 
touch_pad_proximity_get_data(touch_pad_t touch_num,uint32_t * measure_out)535 esp_err_t touch_pad_proximity_get_data(touch_pad_t touch_num, uint32_t *measure_out)
536 {
537     TOUCH_NULL_POINTER_CHECK(measure_out, "measure_out");
538     ESP_RETURN_ON_FALSE(touch_hal_proximity_pad_check(touch_num), ESP_ERR_INVALID_ARG, TOUCH_TAG,  "touch num is not proximity");
539     TOUCH_ENTER_CRITICAL_SAFE();
540     touch_hal_read_benchmark(touch_num, measure_out);
541     TOUCH_EXIT_CRITICAL_SAFE();
542     return ESP_OK;
543 }
544 
545 /************** sleep pad setting ***********************/
546 
touch_pad_sleep_channel_get_info(touch_pad_sleep_channel_t * slp_config)547 esp_err_t touch_pad_sleep_channel_get_info(touch_pad_sleep_channel_t *slp_config)
548 {
549     TOUCH_NULL_POINTER_CHECK(slp_config, "slp_config");
550     TOUCH_ENTER_CRITICAL_SAFE();
551     touch_hal_sleep_channel_get_config(slp_config);
552     TOUCH_EXIT_CRITICAL_SAFE();
553     return ESP_OK;
554 }
555 
touch_pad_sleep_channel_enable(touch_pad_t pad_num,bool enable)556 esp_err_t touch_pad_sleep_channel_enable(touch_pad_t pad_num, bool enable)
557 {
558     TOUCH_CHANNEL_CHECK(pad_num);
559 
560     TOUCH_ENTER_CRITICAL();
561     touch_hal_sleep_channel_enable(pad_num, enable);
562     TOUCH_EXIT_CRITICAL();
563     return ESP_OK;
564 }
565 
touch_pad_sleep_channel_enable_proximity(touch_pad_t pad_num,bool enable)566 esp_err_t touch_pad_sleep_channel_enable_proximity(touch_pad_t pad_num, bool enable)
567 {
568     TOUCH_CHANNEL_CHECK(pad_num);
569 
570     TOUCH_ENTER_CRITICAL();
571     if (enable) {
572         touch_hal_sleep_enable_approach();
573     } else {
574         touch_hal_sleep_disable_approach();
575     }
576     TOUCH_EXIT_CRITICAL();
577     return ESP_OK;
578 }
579 
touch_pad_sleep_get_channel_num(touch_pad_t * pad_num)580 esp_err_t touch_pad_sleep_get_channel_num(touch_pad_t *pad_num)
581 {
582     TOUCH_NULL_POINTER_CHECK(pad_num, "pad_num");
583     TOUCH_ENTER_CRITICAL();
584     touch_hal_sleep_get_channel_num(pad_num);
585     TOUCH_EXIT_CRITICAL();
586     return ESP_OK;
587 }
588 
touch_pad_sleep_set_threshold(touch_pad_t pad_num,uint32_t touch_thres)589 esp_err_t touch_pad_sleep_set_threshold(touch_pad_t pad_num, uint32_t touch_thres)
590 {
591     TOUCH_ENTER_CRITICAL();
592     touch_hal_sleep_set_threshold(touch_thres);
593     TOUCH_EXIT_CRITICAL();
594     return ESP_OK;
595 }
596 
touch_pad_sleep_get_threshold(touch_pad_t pad_num,uint32_t * touch_thres)597 esp_err_t touch_pad_sleep_get_threshold(touch_pad_t pad_num, uint32_t *touch_thres)
598 {
599     TOUCH_ENTER_CRITICAL();
600     touch_hal_sleep_get_threshold(touch_thres);
601     TOUCH_EXIT_CRITICAL();
602     return ESP_OK;
603 }
604 
touch_pad_sleep_channel_read_benchmark(touch_pad_t pad_num,uint32_t * benchmark)605 esp_err_t touch_pad_sleep_channel_read_benchmark(touch_pad_t pad_num, uint32_t *benchmark)
606 {
607     TOUCH_NULL_POINTER_CHECK(benchmark, "benchmark");
608     TOUCH_ENTER_CRITICAL_SAFE();
609     touch_hal_sleep_read_benchmark(benchmark);
610     TOUCH_EXIT_CRITICAL_SAFE();
611     return ESP_OK;
612 }
613 
touch_pad_sleep_channel_read_smooth(touch_pad_t pad_num,uint32_t * smooth_data)614 esp_err_t touch_pad_sleep_channel_read_smooth(touch_pad_t pad_num, uint32_t *smooth_data)
615 {
616     TOUCH_NULL_POINTER_CHECK(smooth_data, "smooth_data");
617     TOUCH_ENTER_CRITICAL_SAFE();
618     touch_hal_sleep_read_smooth(smooth_data);
619     TOUCH_EXIT_CRITICAL_SAFE();
620     return ESP_OK;
621 }
622 
touch_pad_sleep_channel_read_data(touch_pad_t pad_num,uint32_t * raw_data)623 esp_err_t touch_pad_sleep_channel_read_data(touch_pad_t pad_num, uint32_t *raw_data)
624 {
625     TOUCH_NULL_POINTER_CHECK(raw_data, "raw_data");
626     TOUCH_ENTER_CRITICAL_SAFE();
627     touch_hal_sleep_read_data(raw_data);
628     TOUCH_EXIT_CRITICAL_SAFE();
629     return ESP_OK;
630 }
631 
touch_pad_sleep_channel_reset_benchmark(void)632 esp_err_t touch_pad_sleep_channel_reset_benchmark(void)
633 {
634     TOUCH_ENTER_CRITICAL();
635     touch_hal_sleep_reset_benchmark();
636     TOUCH_EXIT_CRITICAL();
637     return ESP_OK;
638 }
639 
touch_pad_sleep_channel_read_debounce(touch_pad_t pad_num,uint32_t * debounce)640 esp_err_t touch_pad_sleep_channel_read_debounce(touch_pad_t pad_num, uint32_t *debounce)
641 {
642     TOUCH_NULL_POINTER_CHECK(debounce, "debounce");
643     touch_hal_sleep_read_debounce(debounce);
644     return ESP_OK;
645 }
646 
touch_pad_sleep_channel_read_proximity_cnt(touch_pad_t pad_num,uint32_t * approach_cnt)647 esp_err_t touch_pad_sleep_channel_read_proximity_cnt(touch_pad_t pad_num, uint32_t *approach_cnt)
648 {
649     TOUCH_NULL_POINTER_CHECK(approach_cnt, "approach_cnt");
650     touch_hal_sleep_read_proximity_cnt(approach_cnt);
651     return ESP_OK;
652 }
653 
touch_pad_sleep_channel_set_work_time(uint16_t sleep_cycle,uint16_t meas_times)654 esp_err_t touch_pad_sleep_channel_set_work_time(uint16_t sleep_cycle, uint16_t meas_times)
655 {
656     touch_hal_sleep_channel_set_work_time(sleep_cycle, meas_times);
657     return ESP_OK;
658 }
659