touch_sensor.c 17 KB

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