touch_sensor.c 17 KB

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