touch_sensor.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  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 <= SOC_TOUCH_SENSOR_BIT_MASK_MAX), "touch set1 bitmask error", ESP_ERR_INVALID_ARG);
  169. TOUCH_CHECK((set2_mask <= SOC_TOUCH_SENSOR_BIT_MASK_MAX), "touch set2 bitmask error", ESP_ERR_INVALID_ARG);
  170. TOUCH_CHECK((en_mask <= SOC_TOUCH_SENSOR_BIT_MASK_MAX), "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 <= SOC_TOUCH_SENSOR_BIT_MASK_MAX), "touch set1 bitmask error", ESP_ERR_INVALID_ARG);
  188. TOUCH_CHECK((set2_mask <= SOC_TOUCH_SENSOR_BIT_MASK_MAX), "touch set2 bitmask error", ESP_ERR_INVALID_ARG);
  189. TOUCH_CHECK((en_mask <= SOC_TOUCH_SENSOR_BIT_MASK_MAX), "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. if (rtc_touch_mux == NULL) {
  257. rtc_touch_mux = xSemaphoreCreateMutex();
  258. }
  259. if (rtc_touch_mux == NULL) {
  260. return ESP_FAIL;
  261. }
  262. TOUCH_ENTER_CRITICAL();
  263. touch_hal_init();
  264. TOUCH_EXIT_CRITICAL();
  265. return ESP_OK;
  266. }
  267. esp_err_t touch_pad_deinit(void)
  268. {
  269. TOUCH_CHECK(rtc_touch_mux != NULL, "Touch pad not initialized", ESP_FAIL);
  270. if (s_touch_pad_filter != NULL) {
  271. touch_pad_filter_stop();
  272. touch_pad_filter_delete();
  273. }
  274. xSemaphoreTake(rtc_touch_mux, portMAX_DELAY);
  275. s_touch_pad_init_bit = 0x0000;
  276. TOUCH_ENTER_CRITICAL();
  277. touch_hal_deinit();
  278. TOUCH_EXIT_CRITICAL();
  279. xSemaphoreGive(rtc_touch_mux);
  280. vSemaphoreDelete(rtc_touch_mux);
  281. rtc_touch_mux = NULL;
  282. return ESP_OK;
  283. }
  284. static esp_err_t _touch_pad_read(touch_pad_t touch_num, uint16_t *touch_value, touch_fsm_mode_t mode)
  285. {
  286. esp_err_t res = ESP_OK;
  287. if (TOUCH_FSM_MODE_SW == mode) {
  288. touch_pad_set_group_mask((1 << touch_num), (1 << touch_num), (1 << touch_num));
  289. touch_pad_sw_start();
  290. while (!touch_hal_meas_is_done()) {};
  291. *touch_value = touch_hal_read_raw_data(touch_num);
  292. touch_pad_clear_group_mask((1 << touch_num), (1 << touch_num), (1 << touch_num));
  293. } else if (TOUCH_FSM_MODE_TIMER == mode) {
  294. while (!touch_hal_meas_is_done()) {};
  295. *touch_value = touch_hal_read_raw_data(touch_num);
  296. } else {
  297. res = ESP_FAIL;
  298. }
  299. if (*touch_value == 0) {
  300. res = ESP_ERR_INVALID_STATE;
  301. }
  302. return res;
  303. }
  304. esp_err_t touch_pad_read(touch_pad_t touch_num, uint16_t *touch_value)
  305. {
  306. TOUCH_CHANNEL_CHECK(touch_num);
  307. TOUCH_CHECK(touch_value != NULL, "touch_value", ESP_ERR_INVALID_ARG);
  308. TOUCH_CHECK(rtc_touch_mux != NULL, "Touch pad not initialized", ESP_FAIL);
  309. esp_err_t res = ESP_OK;
  310. touch_fsm_mode_t mode;
  311. touch_pad_get_fsm_mode(&mode);
  312. xSemaphoreTake(rtc_touch_mux, portMAX_DELAY);
  313. res = _touch_pad_read(touch_num, touch_value, mode);
  314. xSemaphoreGive(rtc_touch_mux);
  315. return res;
  316. }
  317. IRAM_ATTR esp_err_t touch_pad_read_raw_data(touch_pad_t touch_num, uint16_t *touch_value)
  318. {
  319. TOUCH_CHECK(rtc_touch_mux != NULL, "Touch pad not initialized", ESP_FAIL);
  320. TOUCH_CHANNEL_CHECK(touch_num);
  321. TOUCH_CHECK(touch_value != NULL, "touch_value", ESP_ERR_INVALID_ARG);
  322. TOUCH_CHECK(s_touch_pad_filter != NULL, "Touch pad filter not initialized", ESP_FAIL);
  323. *touch_value = s_touch_pad_filter->raw_val[touch_num];
  324. if (*touch_value == 0) {
  325. return ESP_ERR_INVALID_STATE;
  326. }
  327. return ESP_OK;
  328. }
  329. IRAM_ATTR esp_err_t touch_pad_read_filtered(touch_pad_t touch_num, uint16_t *touch_value)
  330. {
  331. TOUCH_CHECK(rtc_touch_mux != NULL, "Touch pad not initialized", ESP_FAIL);
  332. TOUCH_CHANNEL_CHECK(touch_num);
  333. TOUCH_CHECK(touch_value != NULL, "touch_value", ESP_ERR_INVALID_ARG);
  334. TOUCH_CHECK(s_touch_pad_filter != NULL, "Touch pad filter not initialized", ESP_FAIL);
  335. *touch_value = (s_touch_pad_filter->filtered_val[touch_num]);
  336. if (*touch_value == 0) {
  337. return ESP_ERR_INVALID_STATE;
  338. }
  339. return ESP_OK;
  340. }
  341. esp_err_t touch_pad_set_filter_period(uint32_t new_period_ms)
  342. {
  343. TOUCH_CHECK(s_touch_pad_filter != NULL, "Touch pad filter not initialized", ESP_ERR_INVALID_STATE);
  344. TOUCH_CHECK(new_period_ms > 0, "Touch pad filter period error", ESP_ERR_INVALID_ARG);
  345. TOUCH_CHECK(rtc_touch_mux != NULL, "Touch pad not initialized", ESP_ERR_INVALID_STATE);
  346. esp_err_t ret = ESP_OK;
  347. xSemaphoreTake(rtc_touch_mux, portMAX_DELAY);
  348. if (s_touch_pad_filter != NULL) {
  349. xTimerChangePeriod(s_touch_pad_filter->timer, new_period_ms / portTICK_PERIOD_MS, portMAX_DELAY);
  350. s_touch_pad_filter->period = new_period_ms;
  351. } else {
  352. ESP_LOGE(TOUCH_TAG, "Touch pad filter deleted");
  353. ret = ESP_ERR_INVALID_STATE;
  354. }
  355. xSemaphoreGive(rtc_touch_mux);
  356. return ret;
  357. }
  358. esp_err_t touch_pad_get_filter_period(uint32_t *p_period_ms)
  359. {
  360. TOUCH_CHECK(s_touch_pad_filter != NULL, "Touch pad filter not initialized", ESP_ERR_INVALID_STATE);
  361. TOUCH_CHECK(p_period_ms != NULL, "Touch pad period pointer error", ESP_ERR_INVALID_ARG);
  362. TOUCH_CHECK(rtc_touch_mux != NULL, "Touch pad not initialized", ESP_ERR_INVALID_STATE);
  363. esp_err_t ret = ESP_OK;
  364. xSemaphoreTake(rtc_touch_mux, portMAX_DELAY);
  365. if (s_touch_pad_filter != NULL) {
  366. *p_period_ms = s_touch_pad_filter->period;
  367. } else {
  368. ESP_LOGE(TOUCH_TAG, "Touch pad filter deleted");
  369. ret = ESP_ERR_INVALID_STATE;
  370. }
  371. xSemaphoreGive(rtc_touch_mux);
  372. return ret;
  373. }
  374. esp_err_t touch_pad_filter_start(uint32_t filter_period_ms)
  375. {
  376. TOUCH_CHECK(filter_period_ms >= portTICK_PERIOD_MS, "Touch pad filter period error", ESP_ERR_INVALID_ARG);
  377. TOUCH_CHECK(rtc_touch_mux != NULL, "Touch pad not initialized", ESP_ERR_INVALID_STATE);
  378. xSemaphoreTake(rtc_touch_mux, portMAX_DELAY);
  379. if (s_touch_pad_filter == NULL) {
  380. s_touch_pad_filter = (touch_pad_filter_t *) calloc(1, sizeof(touch_pad_filter_t));
  381. if (s_touch_pad_filter == NULL) {
  382. goto err_no_mem;
  383. }
  384. }
  385. if (s_touch_pad_filter->timer == NULL) {
  386. s_touch_pad_filter->timer = xTimerCreate("filter_tmr", filter_period_ms / portTICK_PERIOD_MS, pdFALSE,
  387. NULL, (TimerCallbackFunction_t) touch_pad_filter_cb);
  388. if (s_touch_pad_filter->timer == NULL) {
  389. free(s_touch_pad_filter);
  390. s_touch_pad_filter = NULL;
  391. goto err_no_mem;
  392. }
  393. s_touch_pad_filter->period = filter_period_ms;
  394. }
  395. xSemaphoreGive(rtc_touch_mux);
  396. touch_pad_filter_cb(NULL);
  397. return ESP_OK;
  398. err_no_mem:
  399. xSemaphoreGive(rtc_touch_mux);
  400. return ESP_ERR_NO_MEM;
  401. }
  402. esp_err_t touch_pad_filter_stop(void)
  403. {
  404. TOUCH_CHECK(s_touch_pad_filter != NULL, "Touch pad filter not initialized", ESP_ERR_INVALID_STATE);
  405. TOUCH_CHECK(rtc_touch_mux != NULL, "Touch pad not initialized", ESP_ERR_INVALID_STATE);
  406. esp_err_t ret = ESP_OK;
  407. xSemaphoreTake(rtc_touch_mux, portMAX_DELAY);
  408. if (s_touch_pad_filter != NULL) {
  409. xTimerStop(s_touch_pad_filter->timer, portMAX_DELAY);
  410. } else {
  411. ESP_LOGE(TOUCH_TAG, "Touch pad filter deleted");
  412. ret = ESP_ERR_INVALID_STATE;
  413. }
  414. xSemaphoreGive(rtc_touch_mux);
  415. return ret;
  416. }
  417. esp_err_t touch_pad_filter_delete(void)
  418. {
  419. TOUCH_CHECK(s_touch_pad_filter != NULL, "Touch pad filter not initialized", ESP_ERR_INVALID_STATE);
  420. TOUCH_CHECK(rtc_touch_mux != NULL, "Touch pad not initialized", ESP_ERR_INVALID_STATE);
  421. xSemaphoreTake(rtc_touch_mux, portMAX_DELAY);
  422. if (s_touch_pad_filter != NULL) {
  423. if (s_touch_pad_filter->timer != NULL) {
  424. xTimerStop(s_touch_pad_filter->timer, portMAX_DELAY);
  425. xTimerDelete(s_touch_pad_filter->timer, portMAX_DELAY);
  426. s_touch_pad_filter->timer = NULL;
  427. }
  428. free(s_touch_pad_filter);
  429. s_touch_pad_filter = NULL;
  430. }
  431. xSemaphoreGive(rtc_touch_mux);
  432. return ESP_OK;
  433. }