touch_sensor.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  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 "esp_timer.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. esp_timer_handle_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. touch_pad_get_fsm_mode(&mode);
  94. for (int i = 0; i < TOUCH_PAD_MAX; i++) {
  95. if ((s_touch_pad_init_bit >> i) & 0x1) {
  96. _touch_pad_read(i, &val, mode);
  97. s_touch_pad_filter->raw_val[i] = val;
  98. s_touch_pad_filter->filter_last_val[i] = s_touch_pad_filter->filter_last_val[i] == 0 ?
  99. ((uint32_t)val << TOUCH_PAD_SHIFT_DEFAULT) : s_touch_pad_filter->filter_last_val[i];
  100. s_touch_pad_filter->filter_last_val[i] = _touch_filter_iir((val << TOUCH_PAD_SHIFT_DEFAULT),
  101. s_touch_pad_filter->filter_last_val[i], TOUCH_PAD_FILTER_FACTOR_DEFAULT);
  102. s_touch_pad_filter->filtered_val[i] =
  103. (s_touch_pad_filter->filter_last_val[i] + TOUCH_PAD_SHIFT_ROUND_DEFAULT) >> TOUCH_PAD_SHIFT_DEFAULT;
  104. }
  105. }
  106. if (s_filter_cb) {
  107. //return the raw data and filtered data.
  108. s_filter_cb(s_touch_pad_filter->raw_val, s_touch_pad_filter->filtered_val);
  109. }
  110. }
  111. esp_err_t touch_pad_set_measurement_interval(uint16_t interval_cycle)
  112. {
  113. TOUCH_ENTER_CRITICAL();
  114. touch_hal_set_sleep_time(interval_cycle);
  115. TOUCH_EXIT_CRITICAL();
  116. return ESP_OK;
  117. }
  118. esp_err_t touch_pad_get_measurement_interval(uint16_t *interval_cycle)
  119. {
  120. TOUCH_NULL_POINTER_CHECK(interval_cycle, "interval_cycle");
  121. TOUCH_ENTER_CRITICAL();
  122. touch_hal_get_sleep_time(interval_cycle);
  123. TOUCH_EXIT_CRITICAL();
  124. return ESP_OK;
  125. }
  126. esp_err_t touch_pad_set_measurement_clock_cycles(uint16_t clock_cycle)
  127. {
  128. TOUCH_ENTER_CRITICAL();
  129. touch_hal_set_meas_time(clock_cycle);
  130. TOUCH_EXIT_CRITICAL();
  131. return ESP_OK;
  132. }
  133. esp_err_t touch_pad_get_measurement_clock_cycles(uint16_t *clock_cycle)
  134. {
  135. TOUCH_NULL_POINTER_CHECK(clock_cycle, "clock_cycle");
  136. TOUCH_ENTER_CRITICAL();
  137. touch_hal_get_meas_time(clock_cycle);
  138. TOUCH_EXIT_CRITICAL();
  139. return ESP_OK;
  140. }
  141. esp_err_t touch_pad_set_meas_time(uint16_t sleep_cycle, uint16_t meas_cycle)
  142. {
  143. touch_pad_set_measurement_clock_cycles(meas_cycle);
  144. touch_pad_set_measurement_interval(sleep_cycle);
  145. return ESP_OK;
  146. }
  147. esp_err_t touch_pad_get_meas_time(uint16_t *sleep_cycle, uint16_t *meas_cycle)
  148. {
  149. TOUCH_NULL_POINTER_CHECK(sleep_cycle, "sleep_cycle");
  150. TOUCH_NULL_POINTER_CHECK(meas_cycle, "meas_cycle");
  151. touch_pad_get_measurement_interval(sleep_cycle);
  152. touch_pad_get_measurement_clock_cycles(meas_cycle);
  153. return ESP_OK;
  154. }
  155. esp_err_t touch_pad_set_trigger_mode(touch_trigger_mode_t mode)
  156. {
  157. ESP_RETURN_ON_FALSE((mode < TOUCH_TRIGGER_MAX), ESP_ERR_INVALID_ARG, TOUCH_TAG, TOUCH_PARAM_CHECK_STR("mode"));
  158. TOUCH_ENTER_CRITICAL();
  159. touch_hal_set_trigger_mode(mode);
  160. TOUCH_EXIT_CRITICAL();
  161. return ESP_OK;
  162. }
  163. esp_err_t touch_pad_get_trigger_mode(touch_trigger_mode_t *mode)
  164. {
  165. TOUCH_NULL_POINTER_CHECK(mode, "mode");
  166. touch_hal_get_trigger_mode(mode);
  167. return ESP_OK;
  168. }
  169. esp_err_t touch_pad_set_trigger_source(touch_trigger_src_t src)
  170. {
  171. ESP_RETURN_ON_FALSE((src < TOUCH_TRIGGER_SOURCE_MAX), ESP_ERR_INVALID_ARG, TOUCH_TAG, TOUCH_PARAM_CHECK_STR("src"));
  172. TOUCH_ENTER_CRITICAL();
  173. touch_hal_set_trigger_source(src);
  174. TOUCH_EXIT_CRITICAL();
  175. return ESP_OK;
  176. }
  177. esp_err_t touch_pad_get_trigger_source(touch_trigger_src_t *src)
  178. {
  179. TOUCH_NULL_POINTER_CHECK(src, "src");
  180. touch_hal_get_trigger_source(src);
  181. return ESP_OK;
  182. }
  183. esp_err_t touch_pad_set_group_mask(uint16_t set1_mask, uint16_t set2_mask, uint16_t en_mask)
  184. {
  185. ESP_RETURN_ON_FALSE((set1_mask <= TOUCH_PAD_BIT_MASK_ALL), ESP_ERR_INVALID_ARG, TOUCH_TAG, "touch set1 bitmask error");
  186. ESP_RETURN_ON_FALSE((set2_mask <= TOUCH_PAD_BIT_MASK_ALL), ESP_ERR_INVALID_ARG, TOUCH_TAG, "touch set2 bitmask error");
  187. ESP_RETURN_ON_FALSE((en_mask <= TOUCH_PAD_BIT_MASK_ALL), ESP_ERR_INVALID_ARG, TOUCH_TAG, "touch work_en bitmask error");
  188. TOUCH_ENTER_CRITICAL();
  189. touch_hal_set_group_mask(set1_mask, set2_mask);
  190. touch_hal_set_channel_mask(en_mask);
  191. TOUCH_EXIT_CRITICAL();
  192. return ESP_OK;
  193. }
  194. esp_err_t touch_pad_get_group_mask(uint16_t *set1_mask, uint16_t *set2_mask, uint16_t *en_mask)
  195. {
  196. TOUCH_NULL_POINTER_CHECK(set1_mask, "set1_mask");
  197. TOUCH_NULL_POINTER_CHECK(set2_mask, "set2_mask");
  198. TOUCH_NULL_POINTER_CHECK(en_mask, "en_mask");
  199. TOUCH_ENTER_CRITICAL();
  200. touch_hal_get_channel_mask(en_mask);
  201. touch_hal_get_group_mask(set1_mask, set2_mask);
  202. TOUCH_EXIT_CRITICAL();
  203. return ESP_OK;
  204. }
  205. esp_err_t touch_pad_clear_group_mask(uint16_t set1_mask, uint16_t set2_mask, uint16_t en_mask)
  206. {
  207. ESP_RETURN_ON_FALSE((set1_mask <= TOUCH_PAD_BIT_MASK_ALL), ESP_ERR_INVALID_ARG, TOUCH_TAG, "touch set1 bitmask error");
  208. ESP_RETURN_ON_FALSE((set2_mask <= TOUCH_PAD_BIT_MASK_ALL), ESP_ERR_INVALID_ARG, TOUCH_TAG, "touch set2 bitmask error");
  209. ESP_RETURN_ON_FALSE((en_mask <= TOUCH_PAD_BIT_MASK_ALL), ESP_ERR_INVALID_ARG, TOUCH_TAG, "touch work_en bitmask error");
  210. TOUCH_ENTER_CRITICAL();
  211. touch_hal_clear_channel_mask(en_mask);
  212. touch_hal_clear_group_mask(set1_mask, set2_mask);
  213. TOUCH_EXIT_CRITICAL();
  214. return ESP_OK;
  215. }
  216. esp_err_t touch_pad_intr_enable(void)
  217. {
  218. TOUCH_ENTER_CRITICAL();
  219. touch_hal_intr_enable();
  220. TOUCH_EXIT_CRITICAL();
  221. return ESP_OK;
  222. }
  223. esp_err_t touch_pad_intr_disable(void)
  224. {
  225. TOUCH_ENTER_CRITICAL();
  226. touch_hal_intr_disable();
  227. TOUCH_EXIT_CRITICAL();
  228. return ESP_OK;
  229. }
  230. esp_err_t touch_pad_intr_clear(void)
  231. {
  232. TOUCH_ENTER_CRITICAL();
  233. touch_hal_intr_clear();
  234. TOUCH_EXIT_CRITICAL();
  235. return ESP_OK;
  236. }
  237. bool touch_pad_meas_is_done(void)
  238. {
  239. return touch_hal_meas_is_done();
  240. }
  241. esp_err_t touch_pad_config(touch_pad_t touch_num, uint16_t threshold)
  242. {
  243. ESP_RETURN_ON_FALSE(rtc_touch_mux, ESP_FAIL, TOUCH_TAG, "Touch pad not initialized");
  244. TOUCH_CHANNEL_CHECK(touch_num);
  245. touch_fsm_mode_t mode;
  246. touch_pad_io_init(touch_num);
  247. TOUCH_ENTER_CRITICAL();
  248. touch_hal_config(touch_num);
  249. touch_hal_set_threshold(touch_num, threshold);
  250. TOUCH_EXIT_CRITICAL();
  251. touch_pad_get_fsm_mode(&mode);
  252. if (TOUCH_FSM_MODE_SW == mode) {
  253. touch_pad_clear_group_mask((1 << touch_num), (1 << touch_num), (1 << touch_num));
  254. s_touch_pad_init_bit |= (1 << touch_num);
  255. } else if (TOUCH_FSM_MODE_TIMER == mode) {
  256. uint16_t sleep_time = 0;
  257. uint16_t meas_cycle = 0;
  258. uint32_t wait_time_ms = 0;
  259. uint32_t wait_tick = 0;
  260. uint32_t rtc_clk_freq = rtc_clk_slow_freq_get_hz();
  261. assert(rtc_clk_freq != 0);
  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. esp_err_t ret = ESP_OK;
  297. xSemaphoreTake(rtc_touch_mux, portMAX_DELAY);
  298. if (s_touch_pad_filter) {
  299. if (s_touch_pad_filter->timer) {
  300. ESP_GOTO_ON_ERROR(esp_timer_stop(s_touch_pad_filter->timer), err, TOUCH_TAG, "failed to stop the timer");
  301. ESP_GOTO_ON_ERROR(esp_timer_delete(s_touch_pad_filter->timer), err, TOUCH_TAG, "failed to delete the timer");
  302. s_touch_pad_filter->timer = NULL;
  303. }
  304. free(s_touch_pad_filter);
  305. s_touch_pad_filter = NULL;
  306. }
  307. s_touch_pad_init_bit = 0x0000;
  308. TOUCH_ENTER_CRITICAL();
  309. touch_hal_deinit();
  310. TOUCH_EXIT_CRITICAL();
  311. xSemaphoreGive(rtc_touch_mux);
  312. vSemaphoreDelete(rtc_touch_mux);
  313. rtc_touch_mux = NULL;
  314. return ESP_OK;
  315. err:
  316. xSemaphoreGive(rtc_touch_mux);
  317. return ret;
  318. }
  319. static esp_err_t _touch_pad_read(touch_pad_t touch_num, uint16_t *touch_value, touch_fsm_mode_t mode)
  320. {
  321. esp_err_t res = ESP_OK;
  322. if (TOUCH_FSM_MODE_SW == mode) {
  323. touch_pad_set_group_mask((1 << touch_num), (1 << touch_num), (1 << touch_num));
  324. touch_pad_sw_start();
  325. while (!touch_hal_meas_is_done()) {};
  326. *touch_value = touch_hal_read_raw_data(touch_num);
  327. touch_pad_clear_group_mask((1 << touch_num), (1 << touch_num), (1 << touch_num));
  328. } else if (TOUCH_FSM_MODE_TIMER == mode) {
  329. while (!touch_hal_meas_is_done()) {};
  330. *touch_value = touch_hal_read_raw_data(touch_num);
  331. } else {
  332. res = ESP_FAIL;
  333. }
  334. if (*touch_value == 0) {
  335. res = ESP_ERR_INVALID_STATE;
  336. }
  337. return res;
  338. }
  339. esp_err_t touch_pad_read(touch_pad_t touch_num, uint16_t *touch_value)
  340. {
  341. TOUCH_CHANNEL_CHECK(touch_num);
  342. TOUCH_NULL_POINTER_CHECK(touch_value, "touch_value");
  343. ESP_RETURN_ON_FALSE(rtc_touch_mux, ESP_FAIL, TOUCH_TAG, "Touch pad not initialized");
  344. esp_err_t res = ESP_OK;
  345. touch_fsm_mode_t mode;
  346. touch_pad_get_fsm_mode(&mode);
  347. xSemaphoreTake(rtc_touch_mux, portMAX_DELAY);
  348. res = _touch_pad_read(touch_num, touch_value, mode);
  349. xSemaphoreGive(rtc_touch_mux);
  350. return res;
  351. }
  352. IRAM_ATTR esp_err_t touch_pad_read_raw_data(touch_pad_t touch_num, uint16_t *touch_value)
  353. {
  354. ESP_RETURN_ON_FALSE(rtc_touch_mux, ESP_FAIL, TOUCH_TAG, "Touch pad not initialized");
  355. TOUCH_CHANNEL_CHECK(touch_num);
  356. TOUCH_NULL_POINTER_CHECK(touch_value, "touch_value");
  357. ESP_RETURN_ON_FALSE(s_touch_pad_filter, ESP_FAIL, TOUCH_TAG, "Touch pad filter not initialized");
  358. *touch_value = s_touch_pad_filter->raw_val[touch_num];
  359. if (*touch_value == 0) {
  360. return ESP_ERR_INVALID_STATE;
  361. }
  362. return ESP_OK;
  363. }
  364. IRAM_ATTR esp_err_t touch_pad_read_filtered(touch_pad_t touch_num, uint16_t *touch_value)
  365. {
  366. ESP_RETURN_ON_FALSE(rtc_touch_mux, ESP_FAIL, TOUCH_TAG, "Touch pad not initialized");
  367. TOUCH_CHANNEL_CHECK(touch_num);
  368. TOUCH_NULL_POINTER_CHECK(touch_value, "touch_value");
  369. ESP_RETURN_ON_FALSE(s_touch_pad_filter, ESP_FAIL, TOUCH_TAG, "Touch pad filter not initialized");
  370. *touch_value = (s_touch_pad_filter->filtered_val[touch_num]);
  371. if (*touch_value == 0) {
  372. return ESP_ERR_INVALID_STATE;
  373. }
  374. return ESP_OK;
  375. }
  376. esp_err_t touch_pad_set_filter_period(uint32_t new_period_ms)
  377. {
  378. ESP_RETURN_ON_FALSE(s_touch_pad_filter, ESP_ERR_INVALID_STATE, TOUCH_TAG, "Touch pad filter not initialized");
  379. ESP_RETURN_ON_FALSE(new_period_ms > 0, ESP_ERR_INVALID_ARG, TOUCH_TAG, "Touch pad filter period error");
  380. ESP_RETURN_ON_FALSE(rtc_touch_mux, ESP_ERR_INVALID_STATE, TOUCH_TAG, "Touch pad not initialized");
  381. esp_err_t ret = ESP_OK;
  382. xSemaphoreTake(rtc_touch_mux, portMAX_DELAY);
  383. ESP_GOTO_ON_ERROR(esp_timer_stop(s_touch_pad_filter->timer), err, TOUCH_TAG, "failed to stop the timer");
  384. ESP_GOTO_ON_ERROR(esp_timer_start_periodic(s_touch_pad_filter->timer, new_period_ms * 1000), err, TOUCH_TAG, "failed to start the timer");
  385. s_touch_pad_filter->period = new_period_ms;
  386. err:
  387. xSemaphoreGive(rtc_touch_mux);
  388. return ret;
  389. }
  390. esp_err_t touch_pad_get_filter_period(uint32_t *p_period_ms)
  391. {
  392. ESP_RETURN_ON_FALSE(s_touch_pad_filter, ESP_ERR_INVALID_STATE, TOUCH_TAG, "Touch pad filter not initialized");
  393. TOUCH_NULL_POINTER_CHECK(p_period_ms, "p_period_ms");
  394. ESP_RETURN_ON_FALSE(rtc_touch_mux, ESP_ERR_INVALID_STATE, TOUCH_TAG, "Touch pad not initialized");
  395. esp_err_t ret = ESP_OK;
  396. xSemaphoreTake(rtc_touch_mux, portMAX_DELAY);
  397. if (s_touch_pad_filter) {
  398. *p_period_ms = s_touch_pad_filter->period;
  399. } else {
  400. ESP_LOGE(TOUCH_TAG, "Touch pad filter deleted");
  401. ret = ESP_ERR_INVALID_STATE;
  402. }
  403. xSemaphoreGive(rtc_touch_mux);
  404. return ret;
  405. }
  406. esp_err_t touch_pad_filter_start(uint32_t filter_period_ms)
  407. {
  408. ESP_RETURN_ON_FALSE(filter_period_ms >= portTICK_PERIOD_MS, ESP_ERR_INVALID_ARG, TOUCH_TAG, "Touch pad filter period error");
  409. ESP_RETURN_ON_FALSE(rtc_touch_mux, ESP_ERR_INVALID_STATE, TOUCH_TAG, "Touch pad not initialized");
  410. esp_err_t ret = ESP_OK;
  411. xSemaphoreTake(rtc_touch_mux, portMAX_DELAY);
  412. if (s_touch_pad_filter == NULL) {
  413. s_touch_pad_filter = (touch_pad_filter_t *) calloc(1, sizeof(touch_pad_filter_t));
  414. ESP_GOTO_ON_FALSE(s_touch_pad_filter, ESP_ERR_NO_MEM, err_no_mem, TOUCH_TAG, "no memory for filter");
  415. }
  416. if (s_touch_pad_filter->timer == NULL) {
  417. esp_timer_create_args_t timer_cfg = {
  418. .callback = touch_pad_filter_cb,
  419. .arg = NULL,
  420. .dispatch_method = ESP_TIMER_TASK,
  421. .name = "touch filter timer",
  422. .skip_unhandled_events = true,
  423. };
  424. ESP_GOTO_ON_ERROR(esp_timer_create(&timer_cfg, &(s_touch_pad_filter->timer)),
  425. err_timer_create, TOUCH_TAG, "failed to create the filter timer");
  426. s_touch_pad_filter->period = filter_period_ms;
  427. touch_pad_filter_cb(NULL); // Trigger once immediately to get the initial raw value
  428. ESP_GOTO_ON_ERROR(esp_timer_start_periodic(s_touch_pad_filter->timer, filter_period_ms * 1000),
  429. err_timer_start, TOUCH_TAG, "failed to start the filter timer");
  430. }
  431. xSemaphoreGive(rtc_touch_mux);
  432. return ret;
  433. err_timer_start:
  434. esp_timer_delete(s_touch_pad_filter->timer);
  435. err_timer_create:
  436. free(s_touch_pad_filter);
  437. s_touch_pad_filter = NULL;
  438. err_no_mem:
  439. xSemaphoreGive(rtc_touch_mux);
  440. return ret;
  441. }
  442. esp_err_t touch_pad_filter_stop(void)
  443. {
  444. ESP_RETURN_ON_FALSE(s_touch_pad_filter, ESP_ERR_INVALID_STATE, TOUCH_TAG, "Touch pad filter not initialized");
  445. ESP_RETURN_ON_FALSE(rtc_touch_mux, ESP_ERR_INVALID_STATE, TOUCH_TAG, "Touch pad not initialized");
  446. xSemaphoreTake(rtc_touch_mux, portMAX_DELAY);
  447. esp_err_t ret = esp_timer_stop(s_touch_pad_filter->timer);
  448. if (ret != ESP_OK) {
  449. ESP_LOGE(TOUCH_TAG, "failed to stop the timer");
  450. }
  451. xSemaphoreGive(rtc_touch_mux);
  452. return ret;
  453. }
  454. esp_err_t touch_pad_filter_delete(void)
  455. {
  456. ESP_RETURN_ON_FALSE(s_touch_pad_filter, ESP_ERR_INVALID_STATE, TOUCH_TAG, "Touch pad filter not initialized");
  457. ESP_RETURN_ON_FALSE(rtc_touch_mux, ESP_ERR_INVALID_STATE, TOUCH_TAG, "Touch pad not initialized");
  458. esp_err_t ret = ESP_OK;
  459. xSemaphoreTake(rtc_touch_mux, portMAX_DELAY);
  460. if (s_touch_pad_filter->timer) {
  461. ESP_GOTO_ON_ERROR(esp_timer_stop(s_touch_pad_filter->timer), err, TOUCH_TAG, "failed to stop the timer");
  462. ESP_GOTO_ON_ERROR(esp_timer_delete(s_touch_pad_filter->timer), err, TOUCH_TAG, "failed to delete the timer");
  463. s_touch_pad_filter->timer = NULL;
  464. }
  465. free(s_touch_pad_filter);
  466. s_touch_pad_filter = NULL;
  467. err:
  468. xSemaphoreGive(rtc_touch_mux);
  469. return ret;
  470. }