1 // Copyright 2016-2019 Espressif Systems (Shanghai) PTE LTD
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include <stdlib.h>
16 #include <ctype.h>
17 #include "sdkconfig.h"
18 #include "esp_types.h"
19 #include "esp_log.h"
20 #include "sys/lock.h"
21 #include "soc/rtc.h"
22 #include "soc/periph_defs.h"
23 #include "esp_osal/esp_osal.h"
24 #include "esp_osal/xtensa_api.h"
25 #include "esp_osal/semphr.h"
26 #include "esp_osal/timers.h"
27 #include "esp_intr_alloc.h"
28 #include "driver/rtc_io.h"
29 #include "driver/touch_pad.h"
30 #include "driver/rtc_cntl.h"
31 #include "driver/gpio.h"
32
33 #ifndef NDEBUG
34 // Enable built-in checks in queue.h in debug builds
35 #define INVARIANTS
36 #endif
37 #include "sys/queue.h"
38 #include "hal/touch_sensor_types.h"
39 #include "hal/touch_sensor_hal.h"
40
41 typedef struct {
42 TimerHandle_t timer;
43 uint16_t filtered_val[TOUCH_PAD_MAX];
44 uint16_t raw_val[TOUCH_PAD_MAX];
45 uint32_t filter_period;
46 uint32_t period;
47 bool enable;
48 } touch_pad_filter_t;
49 static touch_pad_filter_t *s_touch_pad_filter = NULL;
50 // check if touch pad be initialized.
51 static uint16_t s_touch_pad_init_bit = 0x0000;
52 static filter_cb_t s_filter_cb = NULL;
53 static SemaphoreHandle_t rtc_touch_mux = NULL;
54
55 #define TOUCH_PAD_FILTER_FACTOR_DEFAULT (4) // IIR filter coefficient.
56 #define TOUCH_PAD_SHIFT_DEFAULT (4) // Increase computing accuracy.
57 #define TOUCH_PAD_SHIFT_ROUND_DEFAULT (8) // ROUND = 2^(n-1); rounding off for fractional.
58
59 static const char *TOUCH_TAG = "TOUCH_SENSOR";
60 #define TOUCH_CHECK(a, str, ret_val) ({ \
61 if (!(a)) { \
62 ESP_LOGE(TOUCH_TAG,"%s(%d): %s", __FUNCTION__, __LINE__, str); \
63 return (ret_val); \
64 } \
65 })
66 #define TOUCH_CHANNEL_CHECK(channel) TOUCH_CHECK(channel < SOC_TOUCH_SENSOR_NUM, "Touch channel error", ESP_ERR_INVALID_ARG)
67 #define TOUCH_PARAM_CHECK_STR(s) ""s" parameter error"
68
69 extern portMUX_TYPE rtc_spinlock; //TODO: Will be placed in the appropriate position after the rtc module is finished.
70 #define TOUCH_ENTER_CRITICAL() portENTER_CRITICAL(&rtc_spinlock)
71 #define TOUCH_EXIT_CRITICAL() portEXIT_CRITICAL(&rtc_spinlock)
72
73 /*---------------------------------------------------------------
74 Touch Pad
75 ---------------------------------------------------------------*/
76 //Some register bits of touch sensor 8 and 9 are mismatched, we need to swap the bits.
77 #define BITSWAP(data, n, m) (((data >> n) & 0x1) == ((data >> m) & 0x1) ? (data) : ((data) ^ ((0x1 <<n) | (0x1 << m))))
78 #define TOUCH_BITS_SWAP(v) BITSWAP(v, TOUCH_PAD_NUM8, TOUCH_PAD_NUM9)
79 static esp_err_t _touch_pad_read(touch_pad_t touch_num, uint16_t *touch_value, touch_fsm_mode_t mode);
80
touch_pad_isr_handler_register(void (* fn)(void *),void * arg,int no_use,intr_handle_t * handle_no_use)81 esp_err_t touch_pad_isr_handler_register(void (*fn)(void *), void *arg, int no_use, intr_handle_t *handle_no_use)
82 {
83 TOUCH_CHECK(fn, "Touch_Pad ISR null", ESP_ERR_INVALID_ARG);
84 return rtc_isr_register(fn, arg, RTC_CNTL_TOUCH_INT_ST_M);
85 }
86
touch_pad_isr_register(intr_handler_t fn,void * arg)87 esp_err_t touch_pad_isr_register(intr_handler_t fn, void *arg)
88 {
89 TOUCH_CHECK(fn, "Touch_Pad ISR null", ESP_ERR_INVALID_ARG);
90 return rtc_isr_register(fn, arg, RTC_CNTL_TOUCH_INT_ST_M);
91 }
92
_touch_filter_iir(uint32_t in_now,uint32_t out_last,uint32_t k)93 static uint32_t _touch_filter_iir(uint32_t in_now, uint32_t out_last, uint32_t k)
94 {
95 if (k == 0) {
96 return in_now;
97 } else {
98 uint32_t out_now = (in_now + (k - 1) * out_last) / k;
99 return out_now;
100 }
101 }
102
touch_pad_set_filter_read_cb(filter_cb_t read_cb)103 esp_err_t touch_pad_set_filter_read_cb(filter_cb_t read_cb)
104 {
105 s_filter_cb = read_cb;
106 return ESP_OK;
107 }
108
touch_pad_filter_cb(void * arg)109 static void touch_pad_filter_cb(void *arg)
110 {
111 static uint32_t s_filtered_temp[TOUCH_PAD_MAX] = {0};
112
113 if (s_touch_pad_filter == NULL || rtc_touch_mux == NULL) {
114 return;
115 }
116 uint16_t val = 0;
117 touch_fsm_mode_t mode;
118 xSemaphoreTake(rtc_touch_mux, portMAX_DELAY);
119 touch_pad_get_fsm_mode(&mode);
120 for (int i = 0; i < TOUCH_PAD_MAX; i++) {
121 if ((s_touch_pad_init_bit >> i) & 0x1) {
122 _touch_pad_read(i, &val, mode);
123 s_touch_pad_filter->raw_val[i] = val;
124 s_filtered_temp[i] = s_filtered_temp[i] == 0 ? ((uint32_t)val << TOUCH_PAD_SHIFT_DEFAULT) : s_filtered_temp[i];
125 s_filtered_temp[i] = _touch_filter_iir((val << TOUCH_PAD_SHIFT_DEFAULT),
126 s_filtered_temp[i], TOUCH_PAD_FILTER_FACTOR_DEFAULT);
127 s_touch_pad_filter->filtered_val[i] = (s_filtered_temp[i] + TOUCH_PAD_SHIFT_ROUND_DEFAULT) >> TOUCH_PAD_SHIFT_DEFAULT;
128 }
129 }
130 xTimerReset(s_touch_pad_filter->timer, portMAX_DELAY);
131 xSemaphoreGive(rtc_touch_mux);
132 if (s_filter_cb != NULL) {
133 //return the raw data and filtered data.
134 s_filter_cb(s_touch_pad_filter->raw_val, s_touch_pad_filter->filtered_val);
135 }
136 }
137
touch_pad_set_meas_time(uint16_t sleep_cycle,uint16_t meas_cycle)138 esp_err_t touch_pad_set_meas_time(uint16_t sleep_cycle, uint16_t meas_cycle)
139 {
140 TOUCH_ENTER_CRITICAL();
141 touch_hal_set_meas_time(meas_cycle);
142 touch_hal_set_sleep_time(sleep_cycle);
143 TOUCH_EXIT_CRITICAL();
144
145 return ESP_OK;
146 }
147
touch_pad_get_meas_time(uint16_t * sleep_cycle,uint16_t * meas_cycle)148 esp_err_t touch_pad_get_meas_time(uint16_t *sleep_cycle, uint16_t *meas_cycle)
149 {
150 TOUCH_ENTER_CRITICAL();
151 touch_hal_get_meas_time(meas_cycle);
152 touch_hal_get_sleep_time(sleep_cycle);
153 TOUCH_EXIT_CRITICAL();
154
155 return ESP_OK;
156 }
157
touch_pad_set_trigger_mode(touch_trigger_mode_t mode)158 esp_err_t touch_pad_set_trigger_mode(touch_trigger_mode_t mode)
159 {
160 TOUCH_CHECK((mode < TOUCH_TRIGGER_MAX), TOUCH_PARAM_CHECK_STR("mode"), ESP_ERR_INVALID_ARG);
161 TOUCH_ENTER_CRITICAL();
162 touch_hal_set_trigger_mode(mode);
163 TOUCH_EXIT_CRITICAL();
164 return ESP_OK;
165 }
166
touch_pad_get_trigger_mode(touch_trigger_mode_t * mode)167 esp_err_t touch_pad_get_trigger_mode(touch_trigger_mode_t *mode)
168 {
169 touch_hal_get_trigger_mode(mode);
170 return ESP_OK;
171 }
172
touch_pad_set_trigger_source(touch_trigger_src_t src)173 esp_err_t touch_pad_set_trigger_source(touch_trigger_src_t src)
174 {
175 TOUCH_CHECK((src < TOUCH_TRIGGER_SOURCE_MAX), TOUCH_PARAM_CHECK_STR("src"), ESP_ERR_INVALID_ARG);
176 TOUCH_ENTER_CRITICAL();
177 touch_hal_set_trigger_source(src);
178 TOUCH_EXIT_CRITICAL();
179 return ESP_OK;
180 }
181
touch_pad_get_trigger_source(touch_trigger_src_t * src)182 esp_err_t touch_pad_get_trigger_source(touch_trigger_src_t *src)
183 {
184 touch_hal_get_trigger_source(src);
185 return ESP_OK;
186 }
187
touch_pad_set_group_mask(uint16_t set1_mask,uint16_t set2_mask,uint16_t en_mask)188 esp_err_t touch_pad_set_group_mask(uint16_t set1_mask, uint16_t set2_mask, uint16_t en_mask)
189 {
190 TOUCH_CHECK((set1_mask <= TOUCH_PAD_BIT_MASK_ALL), "touch set1 bitmask error", ESP_ERR_INVALID_ARG);
191 TOUCH_CHECK((set2_mask <= TOUCH_PAD_BIT_MASK_ALL), "touch set2 bitmask error", ESP_ERR_INVALID_ARG);
192 TOUCH_CHECK((en_mask <= TOUCH_PAD_BIT_MASK_ALL), "touch work_en bitmask error", ESP_ERR_INVALID_ARG);
193
194 TOUCH_ENTER_CRITICAL();
195 touch_hal_set_group_mask(set1_mask, set2_mask);
196 touch_hal_set_channel_mask(en_mask);
197 TOUCH_EXIT_CRITICAL();
198
199 return ESP_OK;
200 }
201
touch_pad_get_group_mask(uint16_t * set1_mask,uint16_t * set2_mask,uint16_t * en_mask)202 esp_err_t touch_pad_get_group_mask(uint16_t *set1_mask, uint16_t *set2_mask, uint16_t *en_mask)
203 {
204 TOUCH_ENTER_CRITICAL();
205 touch_hal_get_channel_mask(en_mask);
206 touch_hal_get_group_mask(set1_mask, set2_mask);
207 TOUCH_EXIT_CRITICAL();
208
209 return ESP_OK;
210 }
211
touch_pad_clear_group_mask(uint16_t set1_mask,uint16_t set2_mask,uint16_t en_mask)212 esp_err_t touch_pad_clear_group_mask(uint16_t set1_mask, uint16_t set2_mask, uint16_t en_mask)
213 {
214 TOUCH_CHECK((set1_mask <= TOUCH_PAD_BIT_MASK_ALL), "touch set1 bitmask error", ESP_ERR_INVALID_ARG);
215 TOUCH_CHECK((set2_mask <= TOUCH_PAD_BIT_MASK_ALL), "touch set2 bitmask error", ESP_ERR_INVALID_ARG);
216 TOUCH_CHECK((en_mask <= TOUCH_PAD_BIT_MASK_ALL), "touch work_en bitmask error", ESP_ERR_INVALID_ARG);
217
218 TOUCH_ENTER_CRITICAL();
219 touch_hal_clear_channel_mask(en_mask);
220 touch_hal_clear_group_mask(set1_mask, set2_mask);
221 TOUCH_EXIT_CRITICAL();
222 return ESP_OK;
223 }
224
touch_pad_intr_enable(void)225 esp_err_t touch_pad_intr_enable(void)
226 {
227 TOUCH_ENTER_CRITICAL();
228 touch_hal_intr_enable();
229 TOUCH_EXIT_CRITICAL();
230 return ESP_OK;
231 }
232
touch_pad_intr_disable(void)233 esp_err_t touch_pad_intr_disable(void)
234 {
235 TOUCH_ENTER_CRITICAL();
236 touch_hal_intr_disable();
237 TOUCH_EXIT_CRITICAL();
238 return ESP_OK;
239 }
240
touch_pad_intr_clear(void)241 esp_err_t touch_pad_intr_clear(void)
242 {
243 TOUCH_ENTER_CRITICAL();
244 touch_hal_intr_clear();
245 TOUCH_EXIT_CRITICAL();
246 return ESP_OK;
247 }
248
touch_pad_meas_is_done(void)249 bool touch_pad_meas_is_done(void)
250 {
251 return touch_hal_meas_is_done();
252 }
253
touch_pad_config(touch_pad_t touch_num,uint16_t threshold)254 esp_err_t touch_pad_config(touch_pad_t touch_num, uint16_t threshold)
255 {
256 TOUCH_CHECK(rtc_touch_mux != NULL, "Touch pad not initialized", ESP_FAIL);
257 TOUCH_CHANNEL_CHECK(touch_num);
258 touch_fsm_mode_t mode;
259 touch_pad_io_init(touch_num);
260 TOUCH_ENTER_CRITICAL();
261 touch_hal_config(touch_num);
262 touch_hal_set_threshold(touch_num, threshold);
263 TOUCH_EXIT_CRITICAL();
264 touch_pad_get_fsm_mode(&mode);
265 if (TOUCH_FSM_MODE_SW == mode) {
266 touch_pad_clear_group_mask((1 << touch_num), (1 << touch_num), (1 << touch_num));
267 s_touch_pad_init_bit |= (1 << touch_num);
268 } else if (TOUCH_FSM_MODE_TIMER == mode) {
269 uint16_t sleep_time = 0;
270 uint16_t meas_cycle = 0;
271 uint32_t wait_time_ms = 0;
272 uint32_t wait_tick = 0;
273 uint32_t rtc_clk = rtc_clk_slow_freq_get_hz();
274 touch_pad_set_group_mask((1 << touch_num), (1 << touch_num), (1 << touch_num));
275 touch_pad_get_meas_time(&sleep_time, &meas_cycle);
276 //If the FSM mode is 'TOUCH_FSM_MODE_TIMER', The data will be ready after one measurement cycle
277 //after this function is executed, otherwise, the "touch_value" by "touch_pad_read" is 0.
278 wait_time_ms = sleep_time / (rtc_clk / 1000) + meas_cycle / (RTC_FAST_CLK_FREQ_APPROX / 1000);
279 wait_tick = wait_time_ms / portTICK_RATE_MS;
280 vTaskDelay(wait_tick ? wait_tick : 1);
281 s_touch_pad_init_bit |= (1 << touch_num);
282 } else {
283 return ESP_FAIL;
284 }
285 return ESP_OK;
286 }
287
touch_pad_init(void)288 esp_err_t touch_pad_init(void)
289 {
290 #ifdef CONFIG_ESP32_RTC_EXT_CRYST_ADDIT_CURRENT_V2
291 ESP_LOGE(TOUCH_TAG, "Touch Pad can't work because it provides current to external XTAL");
292 return ESP_ERR_NOT_SUPPORTED;
293 #endif // CONFIG_ESP32_RTC_EXT_CRYST_ADDIT_CURRENT_V2
294 if (rtc_touch_mux == NULL) {
295 rtc_touch_mux = xSemaphoreCreateMutex();
296 }
297 if (rtc_touch_mux == NULL) {
298 return ESP_FAIL;
299 }
300 TOUCH_ENTER_CRITICAL();
301 touch_hal_init();
302 TOUCH_EXIT_CRITICAL();
303 return ESP_OK;
304 }
305
touch_pad_deinit(void)306 esp_err_t touch_pad_deinit(void)
307 {
308 TOUCH_CHECK(rtc_touch_mux != NULL, "Touch pad not initialized", ESP_FAIL);
309 if (s_touch_pad_filter != NULL) {
310 touch_pad_filter_stop();
311 touch_pad_filter_delete();
312 }
313 xSemaphoreTake(rtc_touch_mux, portMAX_DELAY);
314 s_touch_pad_init_bit = 0x0000;
315 TOUCH_ENTER_CRITICAL();
316 touch_hal_deinit();
317 TOUCH_EXIT_CRITICAL();
318 xSemaphoreGive(rtc_touch_mux);
319 vSemaphoreDelete(rtc_touch_mux);
320 rtc_touch_mux = NULL;
321 return ESP_OK;
322 }
323
_touch_pad_read(touch_pad_t touch_num,uint16_t * touch_value,touch_fsm_mode_t mode)324 static esp_err_t _touch_pad_read(touch_pad_t touch_num, uint16_t *touch_value, touch_fsm_mode_t mode)
325 {
326 esp_err_t res = ESP_OK;
327 if (TOUCH_FSM_MODE_SW == mode) {
328 touch_pad_set_group_mask((1 << touch_num), (1 << touch_num), (1 << touch_num));
329 touch_pad_sw_start();
330 while (!touch_hal_meas_is_done()) {};
331 *touch_value = touch_hal_read_raw_data(touch_num);
332 touch_pad_clear_group_mask((1 << touch_num), (1 << touch_num), (1 << touch_num));
333 } else if (TOUCH_FSM_MODE_TIMER == mode) {
334 while (!touch_hal_meas_is_done()) {};
335 *touch_value = touch_hal_read_raw_data(touch_num);
336 } else {
337 res = ESP_FAIL;
338 }
339 if (*touch_value == 0) {
340 res = ESP_ERR_INVALID_STATE;
341 }
342 return res;
343 }
344
touch_pad_read(touch_pad_t touch_num,uint16_t * touch_value)345 esp_err_t touch_pad_read(touch_pad_t touch_num, uint16_t *touch_value)
346 {
347 TOUCH_CHANNEL_CHECK(touch_num);
348 TOUCH_CHECK(touch_value != NULL, "touch_value", ESP_ERR_INVALID_ARG);
349 TOUCH_CHECK(rtc_touch_mux != NULL, "Touch pad not initialized", ESP_FAIL);
350
351 esp_err_t res = ESP_OK;
352 touch_fsm_mode_t mode;
353 touch_pad_get_fsm_mode(&mode);
354 xSemaphoreTake(rtc_touch_mux, portMAX_DELAY);
355 res = _touch_pad_read(touch_num, touch_value, mode);
356 xSemaphoreGive(rtc_touch_mux);
357 return res;
358 }
359
touch_pad_read_raw_data(touch_pad_t touch_num,uint16_t * touch_value)360 IRAM_ATTR esp_err_t touch_pad_read_raw_data(touch_pad_t touch_num, uint16_t *touch_value)
361 {
362 TOUCH_CHECK(rtc_touch_mux != NULL, "Touch pad not initialized", ESP_FAIL);
363 TOUCH_CHANNEL_CHECK(touch_num);
364 TOUCH_CHECK(touch_value != NULL, "touch_value", ESP_ERR_INVALID_ARG);
365 TOUCH_CHECK(s_touch_pad_filter != NULL, "Touch pad filter not initialized", ESP_FAIL);
366 *touch_value = s_touch_pad_filter->raw_val[touch_num];
367 if (*touch_value == 0) {
368 return ESP_ERR_INVALID_STATE;
369 }
370 return ESP_OK;
371 }
372
touch_pad_read_filtered(touch_pad_t touch_num,uint16_t * touch_value)373 IRAM_ATTR esp_err_t touch_pad_read_filtered(touch_pad_t touch_num, uint16_t *touch_value)
374 {
375 TOUCH_CHECK(rtc_touch_mux != NULL, "Touch pad not initialized", ESP_FAIL);
376 TOUCH_CHANNEL_CHECK(touch_num);
377 TOUCH_CHECK(touch_value != NULL, "touch_value", ESP_ERR_INVALID_ARG);
378 TOUCH_CHECK(s_touch_pad_filter != NULL, "Touch pad filter not initialized", ESP_FAIL);
379 *touch_value = (s_touch_pad_filter->filtered_val[touch_num]);
380 if (*touch_value == 0) {
381 return ESP_ERR_INVALID_STATE;
382 }
383 return ESP_OK;
384 }
385
touch_pad_set_filter_period(uint32_t new_period_ms)386 esp_err_t touch_pad_set_filter_period(uint32_t new_period_ms)
387 {
388 TOUCH_CHECK(s_touch_pad_filter != NULL, "Touch pad filter not initialized", ESP_ERR_INVALID_STATE);
389 TOUCH_CHECK(new_period_ms > 0, "Touch pad filter period error", ESP_ERR_INVALID_ARG);
390 TOUCH_CHECK(rtc_touch_mux != NULL, "Touch pad not initialized", ESP_ERR_INVALID_STATE);
391
392 esp_err_t ret = ESP_OK;
393 xSemaphoreTake(rtc_touch_mux, portMAX_DELAY);
394 if (s_touch_pad_filter != NULL) {
395 xTimerChangePeriod(s_touch_pad_filter->timer, new_period_ms / portTICK_PERIOD_MS, portMAX_DELAY);
396 s_touch_pad_filter->period = new_period_ms;
397 } else {
398 ESP_LOGE(TOUCH_TAG, "Touch pad filter deleted");
399 ret = ESP_ERR_INVALID_STATE;
400 }
401 xSemaphoreGive(rtc_touch_mux);
402 return ret;
403 }
404
touch_pad_get_filter_period(uint32_t * p_period_ms)405 esp_err_t touch_pad_get_filter_period(uint32_t *p_period_ms)
406 {
407 TOUCH_CHECK(s_touch_pad_filter != NULL, "Touch pad filter not initialized", ESP_ERR_INVALID_STATE);
408 TOUCH_CHECK(p_period_ms != NULL, "Touch pad period pointer error", ESP_ERR_INVALID_ARG);
409 TOUCH_CHECK(rtc_touch_mux != NULL, "Touch pad not initialized", ESP_ERR_INVALID_STATE);
410
411 esp_err_t ret = ESP_OK;
412 xSemaphoreTake(rtc_touch_mux, portMAX_DELAY);
413 if (s_touch_pad_filter != NULL) {
414 *p_period_ms = s_touch_pad_filter->period;
415 } else {
416 ESP_LOGE(TOUCH_TAG, "Touch pad filter deleted");
417 ret = ESP_ERR_INVALID_STATE;
418 }
419 xSemaphoreGive(rtc_touch_mux);
420 return ret;
421 }
422
touch_pad_filter_start(uint32_t filter_period_ms)423 esp_err_t touch_pad_filter_start(uint32_t filter_period_ms)
424 {
425 TOUCH_CHECK(filter_period_ms >= portTICK_PERIOD_MS, "Touch pad filter period error", ESP_ERR_INVALID_ARG);
426 TOUCH_CHECK(rtc_touch_mux != NULL, "Touch pad not initialized", ESP_ERR_INVALID_STATE);
427
428 xSemaphoreTake(rtc_touch_mux, portMAX_DELAY);
429 if (s_touch_pad_filter == NULL) {
430 s_touch_pad_filter = (touch_pad_filter_t *) calloc(1, sizeof(touch_pad_filter_t));
431 if (s_touch_pad_filter == NULL) {
432 goto err_no_mem;
433 }
434 }
435 if (s_touch_pad_filter->timer == NULL) {
436 s_touch_pad_filter->timer = xTimerCreate("filter_tmr", filter_period_ms / portTICK_PERIOD_MS, pdFALSE,
437 NULL, (TimerCallbackFunction_t) touch_pad_filter_cb);
438 if (s_touch_pad_filter->timer == NULL) {
439 free(s_touch_pad_filter);
440 s_touch_pad_filter = NULL;
441 goto err_no_mem;
442 }
443 s_touch_pad_filter->period = filter_period_ms;
444 }
445 xSemaphoreGive(rtc_touch_mux);
446 touch_pad_filter_cb(NULL);
447 return ESP_OK;
448
449 err_no_mem:
450 xSemaphoreGive(rtc_touch_mux);
451 return ESP_ERR_NO_MEM;
452 }
453
touch_pad_filter_stop(void)454 esp_err_t touch_pad_filter_stop(void)
455 {
456 TOUCH_CHECK(s_touch_pad_filter != NULL, "Touch pad filter not initialized", ESP_ERR_INVALID_STATE);
457 TOUCH_CHECK(rtc_touch_mux != NULL, "Touch pad not initialized", ESP_ERR_INVALID_STATE);
458 esp_err_t ret = ESP_OK;
459 xSemaphoreTake(rtc_touch_mux, portMAX_DELAY);
460 if (s_touch_pad_filter != NULL) {
461 xTimerStop(s_touch_pad_filter->timer, portMAX_DELAY);
462 } else {
463 ESP_LOGE(TOUCH_TAG, "Touch pad filter deleted");
464 ret = ESP_ERR_INVALID_STATE;
465 }
466 xSemaphoreGive(rtc_touch_mux);
467 return ret;
468 }
469
touch_pad_filter_delete(void)470 esp_err_t touch_pad_filter_delete(void)
471 {
472 TOUCH_CHECK(s_touch_pad_filter != NULL, "Touch pad filter not initialized", ESP_ERR_INVALID_STATE);
473 TOUCH_CHECK(rtc_touch_mux != NULL, "Touch pad not initialized", ESP_ERR_INVALID_STATE);
474 xSemaphoreTake(rtc_touch_mux, portMAX_DELAY);
475 if (s_touch_pad_filter != NULL) {
476 if (s_touch_pad_filter->timer != NULL) {
477 xTimerStop(s_touch_pad_filter->timer, portMAX_DELAY);
478 xTimerDelete(s_touch_pad_filter->timer, portMAX_DELAY);
479 s_touch_pad_filter->timer = NULL;
480 }
481 free(s_touch_pad_filter);
482 s_touch_pad_filter = NULL;
483 }
484 xSemaphoreGive(rtc_touch_mux);
485 return ESP_OK;
486 }
487