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