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