touch_sensor.c 17 KB

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