touch_sensor.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  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. 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 __attribute__((unused)) 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, 0);
  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, 0);
  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_measurement_interval(uint16_t interval_cycle)
  113. {
  114. TOUCH_ENTER_CRITICAL();
  115. touch_hal_set_sleep_time(interval_cycle);
  116. TOUCH_EXIT_CRITICAL();
  117. return ESP_OK;
  118. }
  119. esp_err_t touch_pad_get_measurement_interval(uint16_t *interval_cycle)
  120. {
  121. TOUCH_NULL_POINTER_CHECK(interval_cycle, "interval_cycle");
  122. TOUCH_ENTER_CRITICAL();
  123. touch_hal_get_sleep_time(interval_cycle);
  124. TOUCH_EXIT_CRITICAL();
  125. return ESP_OK;
  126. }
  127. esp_err_t touch_pad_set_measurement_clock_cycles(uint16_t clock_cycle)
  128. {
  129. TOUCH_ENTER_CRITICAL();
  130. touch_hal_set_meas_time(clock_cycle);
  131. TOUCH_EXIT_CRITICAL();
  132. return ESP_OK;
  133. }
  134. esp_err_t touch_pad_get_measurement_clock_cycles(uint16_t *clock_cycle)
  135. {
  136. TOUCH_NULL_POINTER_CHECK(clock_cycle, "clock_cycle");
  137. TOUCH_ENTER_CRITICAL();
  138. touch_hal_get_meas_time(clock_cycle);
  139. TOUCH_EXIT_CRITICAL();
  140. return ESP_OK;
  141. }
  142. esp_err_t touch_pad_set_meas_time(uint16_t sleep_cycle, uint16_t meas_cycle)
  143. {
  144. touch_pad_set_measurement_clock_cycles(meas_cycle);
  145. touch_pad_set_measurement_interval(sleep_cycle);
  146. return ESP_OK;
  147. }
  148. esp_err_t touch_pad_get_meas_time(uint16_t *sleep_cycle, uint16_t *meas_cycle)
  149. {
  150. TOUCH_NULL_POINTER_CHECK(sleep_cycle, "sleep_cycle");
  151. TOUCH_NULL_POINTER_CHECK(meas_cycle, "meas_cycle");
  152. touch_pad_get_measurement_interval(sleep_cycle);
  153. touch_pad_get_measurement_clock_cycles(meas_cycle);
  154. return ESP_OK;
  155. }
  156. esp_err_t touch_pad_set_trigger_mode(touch_trigger_mode_t mode)
  157. {
  158. ESP_RETURN_ON_FALSE((mode < TOUCH_TRIGGER_MAX), ESP_ERR_INVALID_ARG, TOUCH_TAG, TOUCH_PARAM_CHECK_STR("mode"));
  159. TOUCH_ENTER_CRITICAL();
  160. touch_hal_set_trigger_mode(mode);
  161. TOUCH_EXIT_CRITICAL();
  162. return ESP_OK;
  163. }
  164. esp_err_t touch_pad_get_trigger_mode(touch_trigger_mode_t *mode)
  165. {
  166. TOUCH_NULL_POINTER_CHECK(mode, "mode");
  167. touch_hal_get_trigger_mode(mode);
  168. return ESP_OK;
  169. }
  170. esp_err_t touch_pad_set_trigger_source(touch_trigger_src_t src)
  171. {
  172. ESP_RETURN_ON_FALSE((src < TOUCH_TRIGGER_SOURCE_MAX), ESP_ERR_INVALID_ARG, TOUCH_TAG, TOUCH_PARAM_CHECK_STR("src"));
  173. TOUCH_ENTER_CRITICAL();
  174. touch_hal_set_trigger_source(src);
  175. TOUCH_EXIT_CRITICAL();
  176. return ESP_OK;
  177. }
  178. esp_err_t touch_pad_get_trigger_source(touch_trigger_src_t *src)
  179. {
  180. TOUCH_NULL_POINTER_CHECK(src, "src");
  181. touch_hal_get_trigger_source(src);
  182. return ESP_OK;
  183. }
  184. esp_err_t touch_pad_set_group_mask(uint16_t set1_mask, uint16_t set2_mask, uint16_t en_mask)
  185. {
  186. ESP_RETURN_ON_FALSE((set1_mask <= TOUCH_PAD_BIT_MASK_ALL), ESP_ERR_INVALID_ARG, TOUCH_TAG, "touch set1 bitmask error");
  187. ESP_RETURN_ON_FALSE((set2_mask <= TOUCH_PAD_BIT_MASK_ALL), ESP_ERR_INVALID_ARG, TOUCH_TAG, "touch set2 bitmask error");
  188. ESP_RETURN_ON_FALSE((en_mask <= TOUCH_PAD_BIT_MASK_ALL), ESP_ERR_INVALID_ARG, TOUCH_TAG, "touch work_en bitmask error");
  189. TOUCH_ENTER_CRITICAL();
  190. touch_hal_set_group_mask(set1_mask, set2_mask);
  191. touch_hal_set_channel_mask(en_mask);
  192. TOUCH_EXIT_CRITICAL();
  193. return ESP_OK;
  194. }
  195. esp_err_t touch_pad_get_group_mask(uint16_t *set1_mask, uint16_t *set2_mask, uint16_t *en_mask)
  196. {
  197. TOUCH_NULL_POINTER_CHECK(set1_mask, "set1_mask");
  198. TOUCH_NULL_POINTER_CHECK(set2_mask, "set2_mask");
  199. TOUCH_NULL_POINTER_CHECK(en_mask, "en_mask");
  200. TOUCH_ENTER_CRITICAL();
  201. touch_hal_get_channel_mask(en_mask);
  202. touch_hal_get_group_mask(set1_mask, set2_mask);
  203. TOUCH_EXIT_CRITICAL();
  204. return ESP_OK;
  205. }
  206. esp_err_t touch_pad_clear_group_mask(uint16_t set1_mask, uint16_t set2_mask, uint16_t en_mask)
  207. {
  208. ESP_RETURN_ON_FALSE((set1_mask <= TOUCH_PAD_BIT_MASK_ALL), ESP_ERR_INVALID_ARG, TOUCH_TAG, "touch set1 bitmask error");
  209. ESP_RETURN_ON_FALSE((set2_mask <= TOUCH_PAD_BIT_MASK_ALL), ESP_ERR_INVALID_ARG, TOUCH_TAG, "touch set2 bitmask error");
  210. ESP_RETURN_ON_FALSE((en_mask <= TOUCH_PAD_BIT_MASK_ALL), ESP_ERR_INVALID_ARG, TOUCH_TAG, "touch work_en bitmask error");
  211. TOUCH_ENTER_CRITICAL();
  212. touch_hal_clear_channel_mask(en_mask);
  213. touch_hal_clear_group_mask(set1_mask, set2_mask);
  214. TOUCH_EXIT_CRITICAL();
  215. return ESP_OK;
  216. }
  217. esp_err_t touch_pad_intr_enable(void)
  218. {
  219. TOUCH_ENTER_CRITICAL();
  220. touch_hal_intr_enable();
  221. TOUCH_EXIT_CRITICAL();
  222. return ESP_OK;
  223. }
  224. esp_err_t touch_pad_intr_disable(void)
  225. {
  226. TOUCH_ENTER_CRITICAL();
  227. touch_hal_intr_disable();
  228. TOUCH_EXIT_CRITICAL();
  229. return ESP_OK;
  230. }
  231. esp_err_t touch_pad_intr_clear(void)
  232. {
  233. TOUCH_ENTER_CRITICAL();
  234. touch_hal_intr_clear();
  235. TOUCH_EXIT_CRITICAL();
  236. return ESP_OK;
  237. }
  238. bool touch_pad_meas_is_done(void)
  239. {
  240. return touch_hal_meas_is_done();
  241. }
  242. esp_err_t touch_pad_config(touch_pad_t touch_num, uint16_t threshold)
  243. {
  244. ESP_RETURN_ON_FALSE(rtc_touch_mux, ESP_FAIL, TOUCH_TAG, "Touch pad not initialized");
  245. TOUCH_CHANNEL_CHECK(touch_num);
  246. touch_fsm_mode_t mode;
  247. touch_pad_io_init(touch_num);
  248. TOUCH_ENTER_CRITICAL();
  249. touch_hal_config(touch_num);
  250. touch_hal_set_threshold(touch_num, threshold);
  251. TOUCH_EXIT_CRITICAL();
  252. touch_pad_get_fsm_mode(&mode);
  253. if (TOUCH_FSM_MODE_SW == mode) {
  254. touch_pad_clear_group_mask((1 << touch_num), (1 << touch_num), (1 << touch_num));
  255. s_touch_pad_init_bit |= (1 << touch_num);
  256. } else if (TOUCH_FSM_MODE_TIMER == mode) {
  257. uint16_t sleep_time = 0;
  258. uint16_t meas_cycle = 0;
  259. uint32_t wait_time_ms = 0;
  260. uint32_t wait_tick = 0;
  261. uint32_t rtc_clk_freq = rtc_clk_slow_freq_get_hz();
  262. touch_pad_set_group_mask((1 << touch_num), (1 << touch_num), (1 << touch_num));
  263. touch_pad_get_measurement_interval(&sleep_time);
  264. touch_pad_get_measurement_clock_cycles(&meas_cycle);
  265. //If the FSM mode is 'TOUCH_FSM_MODE_TIMER', The data will be ready after one measurement cycle
  266. //after this function is executed, otherwise, the "touch_value" by "touch_pad_read" is 0.
  267. wait_time_ms = sleep_time / (rtc_clk_freq / 1000) + meas_cycle / (SOC_CLK_RC_FAST_FREQ_APPROX / 1000);
  268. wait_tick = wait_time_ms / portTICK_PERIOD_MS;
  269. vTaskDelay(wait_tick ? wait_tick : 1);
  270. s_touch_pad_init_bit |= (1 << touch_num);
  271. } else {
  272. return ESP_FAIL;
  273. }
  274. return ESP_OK;
  275. }
  276. esp_err_t touch_pad_init(void)
  277. {
  278. #ifdef CONFIG_RTC_EXT_CRYST_ADDIT_CURRENT_V2
  279. ESP_LOGE(TOUCH_TAG, "Touch Pad can't work because it provides current to external XTAL");
  280. return ESP_ERR_NOT_SUPPORTED;
  281. #endif // CONFIG_RTC_EXT_CRYST_ADDIT_CURRENT_V2
  282. if (rtc_touch_mux == NULL) {
  283. rtc_touch_mux = xSemaphoreCreateMutex();
  284. }
  285. if (rtc_touch_mux == NULL) {
  286. return ESP_FAIL;
  287. }
  288. TOUCH_ENTER_CRITICAL();
  289. touch_hal_init();
  290. TOUCH_EXIT_CRITICAL();
  291. return ESP_OK;
  292. }
  293. esp_err_t touch_pad_deinit(void)
  294. {
  295. ESP_RETURN_ON_FALSE(rtc_touch_mux, ESP_FAIL, TOUCH_TAG, "Touch pad not initialized");
  296. if (s_touch_pad_filter) {
  297. touch_pad_filter_stop();
  298. touch_pad_filter_delete();
  299. }
  300. xSemaphoreTake(rtc_touch_mux, portMAX_DELAY);
  301. s_touch_pad_init_bit = 0x0000;
  302. TOUCH_ENTER_CRITICAL();
  303. touch_hal_deinit();
  304. TOUCH_EXIT_CRITICAL();
  305. xSemaphoreGive(rtc_touch_mux);
  306. vSemaphoreDelete(rtc_touch_mux);
  307. rtc_touch_mux = NULL;
  308. return ESP_OK;
  309. }
  310. static esp_err_t _touch_pad_read(touch_pad_t touch_num, uint16_t *touch_value, touch_fsm_mode_t mode)
  311. {
  312. esp_err_t res = ESP_OK;
  313. if (TOUCH_FSM_MODE_SW == mode) {
  314. touch_pad_set_group_mask((1 << touch_num), (1 << touch_num), (1 << touch_num));
  315. touch_pad_sw_start();
  316. while (!touch_hal_meas_is_done()) {};
  317. *touch_value = touch_hal_read_raw_data(touch_num);
  318. touch_pad_clear_group_mask((1 << touch_num), (1 << touch_num), (1 << touch_num));
  319. } else if (TOUCH_FSM_MODE_TIMER == mode) {
  320. while (!touch_hal_meas_is_done()) {};
  321. *touch_value = touch_hal_read_raw_data(touch_num);
  322. } else {
  323. res = ESP_FAIL;
  324. }
  325. if (*touch_value == 0) {
  326. res = ESP_ERR_INVALID_STATE;
  327. }
  328. return res;
  329. }
  330. esp_err_t touch_pad_read(touch_pad_t touch_num, uint16_t *touch_value)
  331. {
  332. TOUCH_CHANNEL_CHECK(touch_num);
  333. TOUCH_NULL_POINTER_CHECK(touch_value, "touch_value");
  334. ESP_RETURN_ON_FALSE(rtc_touch_mux, ESP_FAIL, TOUCH_TAG, "Touch pad not initialized");
  335. esp_err_t res = ESP_OK;
  336. touch_fsm_mode_t mode;
  337. touch_pad_get_fsm_mode(&mode);
  338. xSemaphoreTake(rtc_touch_mux, portMAX_DELAY);
  339. res = _touch_pad_read(touch_num, touch_value, mode);
  340. xSemaphoreGive(rtc_touch_mux);
  341. return res;
  342. }
  343. IRAM_ATTR esp_err_t touch_pad_read_raw_data(touch_pad_t touch_num, uint16_t *touch_value)
  344. {
  345. ESP_RETURN_ON_FALSE(rtc_touch_mux, ESP_FAIL, TOUCH_TAG, "Touch pad not initialized");
  346. TOUCH_CHANNEL_CHECK(touch_num);
  347. TOUCH_NULL_POINTER_CHECK(touch_value, "touch_value");
  348. ESP_RETURN_ON_FALSE(s_touch_pad_filter, ESP_FAIL, TOUCH_TAG, "Touch pad filter not initialized");
  349. *touch_value = s_touch_pad_filter->raw_val[touch_num];
  350. if (*touch_value == 0) {
  351. return ESP_ERR_INVALID_STATE;
  352. }
  353. return ESP_OK;
  354. }
  355. IRAM_ATTR esp_err_t touch_pad_read_filtered(touch_pad_t touch_num, uint16_t *touch_value)
  356. {
  357. ESP_RETURN_ON_FALSE(rtc_touch_mux, ESP_FAIL, TOUCH_TAG, "Touch pad not initialized");
  358. TOUCH_CHANNEL_CHECK(touch_num);
  359. TOUCH_NULL_POINTER_CHECK(touch_value, "touch_value");
  360. ESP_RETURN_ON_FALSE(s_touch_pad_filter, ESP_FAIL, TOUCH_TAG, "Touch pad filter not initialized");
  361. *touch_value = (s_touch_pad_filter->filtered_val[touch_num]);
  362. if (*touch_value == 0) {
  363. return ESP_ERR_INVALID_STATE;
  364. }
  365. return ESP_OK;
  366. }
  367. esp_err_t touch_pad_set_filter_period(uint32_t new_period_ms)
  368. {
  369. ESP_RETURN_ON_FALSE(s_touch_pad_filter, ESP_ERR_INVALID_STATE, TOUCH_TAG, "Touch pad filter not initialized");
  370. ESP_RETURN_ON_FALSE(new_period_ms > 0, ESP_ERR_INVALID_ARG, TOUCH_TAG, "Touch pad filter period error");
  371. ESP_RETURN_ON_FALSE(rtc_touch_mux, ESP_ERR_INVALID_STATE, TOUCH_TAG, "Touch pad not initialized");
  372. esp_err_t ret = ESP_OK;
  373. xSemaphoreTake(rtc_touch_mux, portMAX_DELAY);
  374. if (s_touch_pad_filter) {
  375. xTimerChangePeriod(s_touch_pad_filter->timer, new_period_ms / portTICK_PERIOD_MS, portMAX_DELAY);
  376. s_touch_pad_filter->period = new_period_ms;
  377. } else {
  378. ESP_LOGE(TOUCH_TAG, "Touch pad filter deleted");
  379. ret = ESP_ERR_INVALID_STATE;
  380. }
  381. xSemaphoreGive(rtc_touch_mux);
  382. return ret;
  383. }
  384. esp_err_t touch_pad_get_filter_period(uint32_t *p_period_ms)
  385. {
  386. ESP_RETURN_ON_FALSE(s_touch_pad_filter, ESP_ERR_INVALID_STATE, TOUCH_TAG, "Touch pad filter not initialized");
  387. TOUCH_NULL_POINTER_CHECK(p_period_ms, "p_period_ms");
  388. ESP_RETURN_ON_FALSE(rtc_touch_mux, ESP_ERR_INVALID_STATE, TOUCH_TAG, "Touch pad not initialized");
  389. esp_err_t ret = ESP_OK;
  390. xSemaphoreTake(rtc_touch_mux, portMAX_DELAY);
  391. if (s_touch_pad_filter) {
  392. *p_period_ms = s_touch_pad_filter->period;
  393. } else {
  394. ESP_LOGE(TOUCH_TAG, "Touch pad filter deleted");
  395. ret = ESP_ERR_INVALID_STATE;
  396. }
  397. xSemaphoreGive(rtc_touch_mux);
  398. return ret;
  399. }
  400. esp_err_t touch_pad_filter_start(uint32_t filter_period_ms)
  401. {
  402. ESP_RETURN_ON_FALSE(filter_period_ms >= portTICK_PERIOD_MS, ESP_ERR_INVALID_ARG, TOUCH_TAG, "Touch pad filter period error");
  403. ESP_RETURN_ON_FALSE(rtc_touch_mux, ESP_ERR_INVALID_STATE, TOUCH_TAG, "Touch pad not initialized");
  404. xSemaphoreTake(rtc_touch_mux, portMAX_DELAY);
  405. if (s_touch_pad_filter == NULL) {
  406. s_touch_pad_filter = (touch_pad_filter_t *) calloc(1, sizeof(touch_pad_filter_t));
  407. if (s_touch_pad_filter == NULL) {
  408. goto err_no_mem;
  409. }
  410. }
  411. if (s_touch_pad_filter->timer == NULL) {
  412. s_touch_pad_filter->timer = xTimerCreate("filter_tmr", filter_period_ms / portTICK_PERIOD_MS, pdFALSE,
  413. NULL, (TimerCallbackFunction_t) touch_pad_filter_cb);
  414. if (s_touch_pad_filter->timer == NULL) {
  415. free(s_touch_pad_filter);
  416. s_touch_pad_filter = NULL;
  417. goto err_no_mem;
  418. }
  419. s_touch_pad_filter->period = filter_period_ms;
  420. }
  421. xSemaphoreGive(rtc_touch_mux);
  422. touch_pad_filter_cb(NULL);
  423. return ESP_OK;
  424. err_no_mem:
  425. xSemaphoreGive(rtc_touch_mux);
  426. return ESP_ERR_NO_MEM;
  427. }
  428. esp_err_t touch_pad_filter_stop(void)
  429. {
  430. ESP_RETURN_ON_FALSE(s_touch_pad_filter, ESP_ERR_INVALID_STATE, TOUCH_TAG, "Touch pad filter not initialized");
  431. ESP_RETURN_ON_FALSE(rtc_touch_mux, ESP_ERR_INVALID_STATE, TOUCH_TAG, "Touch pad not initialized");
  432. esp_err_t ret = ESP_OK;
  433. xSemaphoreTake(rtc_touch_mux, portMAX_DELAY);
  434. if (s_touch_pad_filter) {
  435. xTimerStop(s_touch_pad_filter->timer, portMAX_DELAY);
  436. } else {
  437. ESP_LOGE(TOUCH_TAG, "Touch pad filter deleted");
  438. ret = ESP_ERR_INVALID_STATE;
  439. }
  440. xSemaphoreGive(rtc_touch_mux);
  441. return ret;
  442. }
  443. esp_err_t touch_pad_filter_delete(void)
  444. {
  445. ESP_RETURN_ON_FALSE(s_touch_pad_filter, ESP_ERR_INVALID_STATE, TOUCH_TAG, "Touch pad filter not initialized");
  446. ESP_RETURN_ON_FALSE(rtc_touch_mux, ESP_ERR_INVALID_STATE, TOUCH_TAG, "Touch pad not initialized");
  447. xSemaphoreTake(rtc_touch_mux, portMAX_DELAY);
  448. if (s_touch_pad_filter) {
  449. if (s_touch_pad_filter->timer) {
  450. xTimerStop(s_touch_pad_filter->timer, portMAX_DELAY);
  451. xTimerDelete(s_touch_pad_filter->timer, portMAX_DELAY);
  452. s_touch_pad_filter->timer = NULL;
  453. }
  454. free(s_touch_pad_filter);
  455. s_touch_pad_filter = NULL;
  456. }
  457. xSemaphoreGive(rtc_touch_mux);
  458. return ESP_OK;
  459. }