touch_sensor.c 17 KB

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