touch_sensor.c 18 KB

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