touch_sensor.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686
  1. /*
  2. * SPDX-FileCopyrightText: 2016-2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <esp_types.h>
  7. #include <stdlib.h>
  8. #include <ctype.h>
  9. #include "esp_log.h"
  10. #include "sys/lock.h"
  11. #include "soc/soc_pins.h"
  12. #include "freertos/FreeRTOS.h"
  13. #include "freertos/xtensa_api.h"
  14. #include "freertos/semphr.h"
  15. #include "freertos/timers.h"
  16. #include "esp_intr_alloc.h"
  17. #include "driver/rtc_io.h"
  18. #include "driver/touch_pad.h"
  19. #include "esp_private/rtc_ctrl.h"
  20. #include "driver/gpio.h"
  21. #include "sdkconfig.h"
  22. #include "esp_check.h"
  23. #include "hal/touch_sensor_types.h"
  24. #include "hal/touch_sensor_hal.h"
  25. #ifndef NDEBUG
  26. // Enable built-in checks in queue.h in debug builds
  27. #define INVARIANTS
  28. #endif
  29. #include "sys/queue.h"
  30. #define TOUCH_PAD_FILTER_FACTOR_DEFAULT (4) // IIR filter coefficient.
  31. #define TOUCH_PAD_SHIFT_DEFAULT (4) // Increase computing accuracy.
  32. #define TOUCH_PAD_SHIFT_ROUND_DEFAULT (8) // ROUND = 2^(n-1); rounding off for fractional.
  33. #define TOUCH_PAD_MEASURE_WAIT_DEFAULT (0xFF) // The timer frequency is 8Mhz, the max value is 0xff
  34. static __attribute__((unused)) const char *TOUCH_TAG = "TOUCH_SENSOR";
  35. #define TOUCH_CHANNEL_CHECK(channel) do { \
  36. ESP_RETURN_ON_FALSE(channel < SOC_TOUCH_SENSOR_NUM && channel >= 0, ESP_ERR_INVALID_ARG, TOUCH_TAG, "Touch channel error"); \
  37. ESP_RETURN_ON_FALSE(channel != SOC_TOUCH_DENOISE_CHANNEL, ESP_ERR_INVALID_ARG, TOUCH_TAG, "TOUCH0 is internal denoise channel"); \
  38. } while (0);
  39. #define TOUCH_CH_MASK_CHECK(mask) ESP_RETURN_ON_FALSE((mask <= TOUCH_PAD_BIT_MASK_ALL), ESP_ERR_INVALID_ARG, TOUCH_TAG, "touch channel bitmask error");
  40. #define TOUCH_INTR_MASK_CHECK(mask) ESP_RETURN_ON_FALSE(mask & TOUCH_PAD_INTR_MASK_ALL, ESP_ERR_INVALID_ARG, TOUCH_TAG, "intr mask error");
  41. #define TOUCH_NULL_POINTER_CHECK(p, name) ESP_RETURN_ON_FALSE((p), ESP_ERR_INVALID_ARG, TOUCH_TAG, "input param '"name"' is NULL")
  42. #define TOUCH_PARAM_CHECK_STR(s) ""s" parameter error"
  43. extern portMUX_TYPE rtc_spinlock; //TODO: Will be placed in the appropriate position after the rtc module is finished.
  44. #define TOUCH_ENTER_CRITICAL_SAFE() portENTER_CRITICAL_SAFE(&rtc_spinlock) // Can be called in isr and task.
  45. #define TOUCH_EXIT_CRITICAL_SAFE() portEXIT_CRITICAL_SAFE(&rtc_spinlock)
  46. #define TOUCH_ENTER_CRITICAL() portENTER_CRITICAL(&rtc_spinlock)
  47. #define TOUCH_EXIT_CRITICAL() portEXIT_CRITICAL(&rtc_spinlock)
  48. static SemaphoreHandle_t rtc_touch_mux = NULL;
  49. /*---------------------------------------------------------------
  50. Touch Pad
  51. ---------------------------------------------------------------*/
  52. /** Workaround for scan done interrupt issue. */
  53. static void touch_pad_workaround_isr_internal(void *arg)
  54. {
  55. uint16_t ch_mask = 0;
  56. uint32_t intr_mask = touch_hal_read_intr_status_mask();
  57. int pad_num = touch_hal_get_current_meas_channel();
  58. /* Make sure that the scan done interrupt is generated after the last channel measurement is completed. */
  59. if (intr_mask & TOUCH_PAD_INTR_MASK_SCAN_DONE) {
  60. touch_hal_get_channel_mask(&ch_mask);
  61. for (int i = TOUCH_PAD_MAX - 1; i >= 0; i--) {
  62. if (BIT(i) & ch_mask) {
  63. if (pad_num != i) {
  64. touch_hal_intr_clear(TOUCH_PAD_INTR_MASK_SCAN_DONE);
  65. }
  66. break;
  67. }
  68. }
  69. }
  70. }
  71. esp_err_t touch_pad_isr_register(intr_handler_t fn, void *arg, touch_pad_intr_mask_t intr_mask)
  72. {
  73. static bool reg_flag = false;
  74. ESP_RETURN_ON_FALSE(fn, ESP_ERR_INVALID_ARG, TOUCH_TAG, TOUCH_PARAM_CHECK_STR("intr_mask"));
  75. TOUCH_INTR_MASK_CHECK(intr_mask);
  76. uint32_t en_msk = 0;
  77. if (intr_mask & TOUCH_PAD_INTR_MASK_DONE) {
  78. en_msk |= RTC_CNTL_TOUCH_DONE_INT_ST_M;
  79. }
  80. if (intr_mask & TOUCH_PAD_INTR_MASK_ACTIVE) {
  81. en_msk |= RTC_CNTL_TOUCH_ACTIVE_INT_ST_M;
  82. }
  83. if (intr_mask & TOUCH_PAD_INTR_MASK_INACTIVE) {
  84. en_msk |= RTC_CNTL_TOUCH_INACTIVE_INT_ST_M;
  85. }
  86. if (intr_mask & TOUCH_PAD_INTR_MASK_SCAN_DONE) {
  87. en_msk |= RTC_CNTL_TOUCH_SCAN_DONE_INT_ST_M;
  88. }
  89. if (intr_mask & TOUCH_PAD_INTR_MASK_TIMEOUT) {
  90. en_msk |= RTC_CNTL_TOUCH_TIMEOUT_INT_ST_M;
  91. }
  92. #if SOC_TOUCH_PROXIMITY_MEAS_DONE_SUPPORTED
  93. if (intr_mask & TOUCH_PAD_INTR_MASK_PROXI_MEAS_DONE) {
  94. en_msk |= RTC_CNTL_TOUCH_APPROACH_LOOP_DONE_INT_ST_M;
  95. }
  96. #endif
  97. esp_err_t ret = rtc_isr_register(fn, arg, en_msk, 0);
  98. /* Must ensure: After being registered, it is executed first. */
  99. if ( (ret == ESP_OK) && (reg_flag == false) && (intr_mask & (TOUCH_PAD_INTR_MASK_SCAN_DONE | TOUCH_PAD_INTR_MASK_TIMEOUT)) ) {
  100. rtc_isr_register(touch_pad_workaround_isr_internal, NULL, RTC_CNTL_TOUCH_SCAN_DONE_INT_ST_M | RTC_CNTL_TOUCH_TIMEOUT_INT_ST_M, 0);
  101. reg_flag = true;
  102. }
  103. return ret;
  104. }
  105. esp_err_t touch_pad_set_measurement_interval(uint16_t interval_cycle)
  106. {
  107. TOUCH_ENTER_CRITICAL();
  108. touch_hal_set_sleep_time(interval_cycle);
  109. TOUCH_EXIT_CRITICAL();
  110. return ESP_OK;
  111. }
  112. esp_err_t touch_pad_get_measurement_interval(uint16_t *interval_cycle)
  113. {
  114. TOUCH_NULL_POINTER_CHECK(interval_cycle, "interval_cycle");
  115. TOUCH_ENTER_CRITICAL();
  116. touch_hal_get_sleep_time(interval_cycle);
  117. TOUCH_EXIT_CRITICAL();
  118. return ESP_OK;
  119. }
  120. esp_err_t touch_pad_set_charge_discharge_times(uint16_t charge_discharge_times)
  121. {
  122. TOUCH_ENTER_CRITICAL();
  123. touch_hal_set_meas_times(charge_discharge_times);
  124. TOUCH_EXIT_CRITICAL();
  125. return ESP_OK;
  126. }
  127. esp_err_t touch_pad_get_charge_discharge_times(uint16_t *charge_discharge_times)
  128. {
  129. TOUCH_NULL_POINTER_CHECK(charge_discharge_times, "charge_discharge_times");
  130. TOUCH_ENTER_CRITICAL();
  131. touch_hal_get_measure_times(charge_discharge_times);
  132. TOUCH_EXIT_CRITICAL();
  133. return ESP_OK;
  134. }
  135. esp_err_t touch_pad_set_meas_time(uint16_t sleep_cycle, uint16_t meas_times)
  136. {
  137. touch_pad_set_charge_discharge_times(meas_times);
  138. touch_pad_set_measurement_interval(sleep_cycle);
  139. return ESP_OK;
  140. }
  141. esp_err_t touch_pad_get_meas_time(uint16_t *sleep_cycle, uint16_t *meas_times)
  142. {
  143. TOUCH_NULL_POINTER_CHECK(sleep_cycle, "sleep_cycle");
  144. TOUCH_NULL_POINTER_CHECK(meas_times, "meas_times");
  145. touch_pad_get_measurement_interval(sleep_cycle);
  146. touch_pad_get_charge_discharge_times(meas_times);
  147. return ESP_OK;
  148. }
  149. esp_err_t touch_pad_set_idle_channel_connect(touch_pad_conn_type_t type)
  150. {
  151. ESP_RETURN_ON_FALSE(type < TOUCH_PAD_CONN_MAX, ESP_ERR_INVALID_ARG, TOUCH_TAG, TOUCH_PARAM_CHECK_STR("type"));
  152. TOUCH_ENTER_CRITICAL();
  153. touch_hal_set_idle_channel_connect(type);
  154. TOUCH_EXIT_CRITICAL();
  155. return ESP_OK;
  156. }
  157. esp_err_t touch_pad_get_idle_channel_connect(touch_pad_conn_type_t *type)
  158. {
  159. TOUCH_NULL_POINTER_CHECK(type, "type");
  160. touch_hal_get_idle_channel_connect(type);
  161. return ESP_OK;
  162. }
  163. bool touch_pad_meas_is_done(void)
  164. {
  165. return touch_hal_meas_is_done();
  166. }
  167. esp_err_t touch_pad_set_channel_mask(uint16_t enable_mask)
  168. {
  169. TOUCH_CH_MASK_CHECK(enable_mask);
  170. TOUCH_ENTER_CRITICAL();
  171. touch_hal_set_channel_mask(enable_mask);
  172. TOUCH_EXIT_CRITICAL();
  173. return ESP_OK;
  174. }
  175. esp_err_t touch_pad_get_channel_mask(uint16_t *enable_mask)
  176. {
  177. TOUCH_NULL_POINTER_CHECK(enable_mask, "enable_mask");
  178. TOUCH_ENTER_CRITICAL();
  179. touch_hal_get_channel_mask(enable_mask);
  180. TOUCH_EXIT_CRITICAL();
  181. return ESP_OK;
  182. }
  183. esp_err_t touch_pad_clear_channel_mask(uint16_t enable_mask)
  184. {
  185. TOUCH_CH_MASK_CHECK(enable_mask);
  186. TOUCH_ENTER_CRITICAL();
  187. touch_hal_clear_channel_mask(enable_mask);
  188. TOUCH_EXIT_CRITICAL();
  189. return ESP_OK;
  190. }
  191. touch_pad_t IRAM_ATTR touch_pad_get_current_meas_channel(void)
  192. {
  193. return (touch_pad_t)touch_hal_get_current_meas_channel();
  194. }
  195. esp_err_t touch_pad_intr_enable(touch_pad_intr_mask_t int_mask)
  196. {
  197. if (!(int_mask & TOUCH_PAD_INTR_MASK_ALL)) {
  198. return ESP_ERR_INVALID_ARG;
  199. }
  200. TOUCH_ENTER_CRITICAL_SAFE();
  201. touch_hal_intr_enable(int_mask);
  202. TOUCH_EXIT_CRITICAL_SAFE();
  203. return ESP_OK;
  204. }
  205. esp_err_t touch_pad_intr_disable(touch_pad_intr_mask_t int_mask)
  206. {
  207. if (!(int_mask & TOUCH_PAD_INTR_MASK_ALL)) {
  208. return ESP_ERR_INVALID_ARG;
  209. }
  210. TOUCH_ENTER_CRITICAL_SAFE();
  211. touch_hal_intr_disable(int_mask);
  212. TOUCH_EXIT_CRITICAL_SAFE();
  213. return ESP_OK;
  214. }
  215. esp_err_t touch_pad_intr_clear(touch_pad_intr_mask_t int_mask)
  216. {
  217. TOUCH_INTR_MASK_CHECK(int_mask);
  218. TOUCH_ENTER_CRITICAL();
  219. touch_hal_intr_clear(int_mask);
  220. TOUCH_EXIT_CRITICAL();
  221. return ESP_OK;
  222. }
  223. uint32_t touch_pad_read_intr_status_mask(void)
  224. {
  225. return touch_hal_read_intr_status_mask();
  226. }
  227. esp_err_t touch_pad_timeout_set(bool enable, uint32_t threshold)
  228. {
  229. TOUCH_ENTER_CRITICAL();
  230. if (enable) {
  231. touch_hal_timeout_enable();
  232. } else {
  233. touch_hal_timeout_disable();
  234. }
  235. touch_hal_timeout_set_threshold(threshold);
  236. TOUCH_EXIT_CRITICAL();
  237. return ESP_OK;
  238. }
  239. esp_err_t touch_pad_timeout_get_threshold(uint32_t *threshold)
  240. {
  241. TOUCH_NULL_POINTER_CHECK(threshold, "threshold");
  242. TOUCH_ENTER_CRITICAL();
  243. touch_hal_timeout_get_threshold(threshold);
  244. TOUCH_EXIT_CRITICAL();
  245. return ESP_OK;
  246. }
  247. esp_err_t touch_pad_timeout_resume(void)
  248. {
  249. TOUCH_ENTER_CRITICAL();
  250. touch_hal_timer_force_done();
  251. TOUCH_EXIT_CRITICAL();
  252. return ESP_OK;
  253. }
  254. esp_err_t touch_pad_config(touch_pad_t touch_num)
  255. {
  256. TOUCH_CHANNEL_CHECK(touch_num);
  257. touch_pad_io_init(touch_num);
  258. TOUCH_ENTER_CRITICAL();
  259. touch_hal_config(touch_num);
  260. touch_hal_set_channel_mask(BIT(touch_num));
  261. TOUCH_EXIT_CRITICAL();
  262. return ESP_OK;
  263. }
  264. esp_err_t touch_pad_init(void)
  265. {
  266. if (rtc_touch_mux == NULL) {
  267. rtc_touch_mux = xSemaphoreCreateMutex();
  268. }
  269. if (rtc_touch_mux == NULL) {
  270. return ESP_ERR_NO_MEM;
  271. }
  272. TOUCH_ENTER_CRITICAL();
  273. touch_hal_init();
  274. TOUCH_EXIT_CRITICAL();
  275. return ESP_OK;
  276. }
  277. esp_err_t touch_pad_deinit(void)
  278. {
  279. ESP_RETURN_ON_FALSE(rtc_touch_mux, ESP_FAIL, TOUCH_TAG, "Touch pad not initialized");
  280. xSemaphoreTake(rtc_touch_mux, portMAX_DELAY);
  281. TOUCH_ENTER_CRITICAL();
  282. touch_hal_deinit();
  283. TOUCH_EXIT_CRITICAL();
  284. xSemaphoreGive(rtc_touch_mux);
  285. vSemaphoreDelete(rtc_touch_mux);
  286. rtc_touch_mux = NULL;
  287. return ESP_OK;
  288. }
  289. esp_err_t touch_pad_reset(void)
  290. {
  291. TOUCH_ENTER_CRITICAL();
  292. touch_hal_reset();
  293. TOUCH_EXIT_CRITICAL();
  294. return ESP_OK;
  295. }
  296. esp_err_t IRAM_ATTR touch_pad_read_raw_data(touch_pad_t touch_num, uint32_t *raw_data)
  297. {
  298. ESP_RETURN_ON_FALSE(touch_num < TOUCH_PAD_MAX && touch_num >= 0, ESP_ERR_INVALID_ARG, TOUCH_TAG, "Touch channel error");
  299. TOUCH_NULL_POINTER_CHECK(raw_data, "raw_data");
  300. TOUCH_ENTER_CRITICAL_SAFE();
  301. *raw_data = touch_hal_read_raw_data(touch_num);
  302. TOUCH_EXIT_CRITICAL_SAFE();
  303. return ESP_OK;
  304. }
  305. esp_err_t IRAM_ATTR touch_pad_filter_read_smooth(touch_pad_t touch_num, uint32_t *smooth_data)
  306. {
  307. TOUCH_NULL_POINTER_CHECK(smooth_data, "smooth_data");
  308. TOUCH_CHANNEL_CHECK(touch_num);
  309. TOUCH_ENTER_CRITICAL_SAFE();
  310. touch_hal_filter_read_smooth(touch_num, smooth_data);
  311. TOUCH_EXIT_CRITICAL_SAFE();
  312. return ESP_OK;
  313. }
  314. esp_err_t IRAM_ATTR touch_pad_read_benchmark(touch_pad_t touch_num, uint32_t *benchmark)
  315. {
  316. TOUCH_NULL_POINTER_CHECK(benchmark, "benchmark");
  317. TOUCH_CHANNEL_CHECK(touch_num);
  318. TOUCH_ENTER_CRITICAL_SAFE();
  319. touch_hal_read_benchmark(touch_num, benchmark);
  320. TOUCH_EXIT_CRITICAL_SAFE();
  321. return ESP_OK;
  322. }
  323. /* Should be call after clk enable and filter enable. */
  324. esp_err_t touch_pad_reset_benchmark(touch_pad_t touch_num)
  325. {
  326. ESP_RETURN_ON_FALSE(touch_num <= TOUCH_PAD_MAX && touch_num >= 0, ESP_ERR_INVALID_ARG, TOUCH_TAG, "Touch channel error");
  327. TOUCH_ENTER_CRITICAL();
  328. touch_hal_reset_benchmark(touch_num);
  329. TOUCH_EXIT_CRITICAL();
  330. return ESP_OK;
  331. }
  332. esp_err_t touch_pad_filter_set_config(const touch_filter_config_t *filter_info)
  333. {
  334. TOUCH_NULL_POINTER_CHECK(filter_info, "filter_info");
  335. ESP_RETURN_ON_FALSE(filter_info->mode < TOUCH_PAD_FILTER_MAX, ESP_ERR_INVALID_ARG, TOUCH_TAG, TOUCH_PARAM_CHECK_STR("mode"));
  336. ESP_RETURN_ON_FALSE(filter_info->debounce_cnt <= TOUCH_DEBOUNCE_CNT_MAX, ESP_ERR_INVALID_ARG, TOUCH_TAG, TOUCH_PARAM_CHECK_STR("debounce"));
  337. ESP_RETURN_ON_FALSE(filter_info->noise_thr <= TOUCH_NOISE_THR_MAX, ESP_ERR_INVALID_ARG, TOUCH_TAG, TOUCH_PARAM_CHECK_STR("noise"));
  338. ESP_RETURN_ON_FALSE(filter_info->jitter_step <= TOUCH_JITTER_STEP_MAX, ESP_ERR_INVALID_ARG, TOUCH_TAG, TOUCH_PARAM_CHECK_STR("jitter_step"));
  339. ESP_RETURN_ON_FALSE(filter_info->smh_lvl < TOUCH_PAD_SMOOTH_MAX, ESP_ERR_INVALID_ARG, TOUCH_TAG, TOUCH_PARAM_CHECK_STR("smooth level"));
  340. TOUCH_ENTER_CRITICAL();
  341. touch_hal_filter_set_config(filter_info);
  342. TOUCH_EXIT_CRITICAL();
  343. return ESP_OK;
  344. }
  345. esp_err_t touch_pad_filter_get_config(touch_filter_config_t *filter_info)
  346. {
  347. TOUCH_NULL_POINTER_CHECK(filter_info, "filter_info");
  348. TOUCH_ENTER_CRITICAL();
  349. touch_hal_filter_get_config(filter_info);
  350. TOUCH_EXIT_CRITICAL();
  351. return ESP_OK;
  352. }
  353. esp_err_t touch_pad_filter_enable(void)
  354. {
  355. TOUCH_ENTER_CRITICAL();
  356. touch_hal_filter_enable();
  357. TOUCH_EXIT_CRITICAL();
  358. return ESP_OK;
  359. }
  360. esp_err_t touch_pad_filter_disable(void)
  361. {
  362. TOUCH_ENTER_CRITICAL();
  363. touch_hal_filter_disable();
  364. TOUCH_EXIT_CRITICAL();
  365. return ESP_OK;
  366. }
  367. esp_err_t touch_pad_denoise_enable(void)
  368. {
  369. TOUCH_ENTER_CRITICAL();
  370. touch_hal_clear_channel_mask(BIT(SOC_TOUCH_DENOISE_CHANNEL));
  371. touch_hal_denoise_enable();
  372. TOUCH_EXIT_CRITICAL();
  373. return ESP_OK;
  374. }
  375. esp_err_t touch_pad_denoise_disable(void)
  376. {
  377. TOUCH_ENTER_CRITICAL();
  378. touch_hal_denoise_disable();
  379. TOUCH_EXIT_CRITICAL();
  380. return ESP_OK;
  381. }
  382. esp_err_t touch_pad_denoise_set_config(const touch_pad_denoise_t *denoise)
  383. {
  384. TOUCH_NULL_POINTER_CHECK(denoise, "denoise");
  385. ESP_RETURN_ON_FALSE(denoise->grade < TOUCH_PAD_DENOISE_MAX, ESP_ERR_INVALID_ARG, TOUCH_TAG, TOUCH_PARAM_CHECK_STR("grade"));
  386. ESP_RETURN_ON_FALSE(denoise->cap_level < TOUCH_PAD_DENOISE_CAP_MAX, ESP_ERR_INVALID_ARG, TOUCH_TAG, TOUCH_PARAM_CHECK_STR("cap_level"));
  387. const touch_hal_meas_mode_t meas = {
  388. .slope = TOUCH_PAD_SLOPE_DEFAULT,
  389. .tie_opt = TOUCH_PAD_TIE_OPT_DEFAULT,
  390. };
  391. TOUCH_ENTER_CRITICAL();
  392. touch_hal_set_meas_mode(SOC_TOUCH_DENOISE_CHANNEL, &meas);
  393. touch_hal_denoise_set_config(denoise);
  394. TOUCH_EXIT_CRITICAL();
  395. return ESP_OK;
  396. }
  397. esp_err_t touch_pad_denoise_get_config(touch_pad_denoise_t *denoise)
  398. {
  399. TOUCH_NULL_POINTER_CHECK(denoise, "denoise");
  400. TOUCH_ENTER_CRITICAL();
  401. touch_hal_denoise_get_config(denoise);
  402. TOUCH_EXIT_CRITICAL();
  403. return ESP_OK;
  404. }
  405. esp_err_t touch_pad_denoise_read_data(uint32_t *data)
  406. {
  407. TOUCH_NULL_POINTER_CHECK(data, "data");
  408. touch_hal_denoise_read_data(data);
  409. return ESP_OK;
  410. }
  411. esp_err_t touch_pad_waterproof_set_config(const touch_pad_waterproof_t *waterproof)
  412. {
  413. TOUCH_NULL_POINTER_CHECK(waterproof, "waterproof");
  414. ESP_RETURN_ON_FALSE(waterproof->guard_ring_pad < SOC_TOUCH_SENSOR_NUM, ESP_ERR_INVALID_ARG, TOUCH_TAG, TOUCH_PARAM_CHECK_STR("pad"));
  415. ESP_RETURN_ON_FALSE(waterproof->shield_driver < TOUCH_PAD_SHIELD_DRV_MAX, ESP_ERR_INVALID_ARG, TOUCH_TAG, TOUCH_PARAM_CHECK_STR("shield_driver"));
  416. TOUCH_ENTER_CRITICAL();
  417. touch_hal_waterproof_set_config(waterproof);
  418. TOUCH_EXIT_CRITICAL();
  419. return ESP_OK;
  420. }
  421. esp_err_t touch_pad_waterproof_get_config(touch_pad_waterproof_t *waterproof)
  422. {
  423. TOUCH_NULL_POINTER_CHECK(waterproof, "waterproof");
  424. TOUCH_ENTER_CRITICAL();
  425. touch_hal_waterproof_get_config(waterproof);
  426. TOUCH_EXIT_CRITICAL();
  427. return ESP_OK;
  428. }
  429. esp_err_t touch_pad_waterproof_enable(void)
  430. {
  431. touch_pad_io_init(SOC_TOUCH_SHIELD_CHANNEL);
  432. TOUCH_ENTER_CRITICAL();
  433. touch_hal_waterproof_enable();
  434. TOUCH_EXIT_CRITICAL();
  435. return ESP_OK;
  436. }
  437. esp_err_t touch_pad_waterproof_disable(void)
  438. {
  439. TOUCH_ENTER_CRITICAL();
  440. touch_hal_waterproof_disable();
  441. TOUCH_EXIT_CRITICAL();
  442. return ESP_OK;
  443. }
  444. esp_err_t touch_pad_proximity_enable(touch_pad_t touch_num, bool enabled)
  445. {
  446. esp_err_t ret = ESP_OK;
  447. ESP_RETURN_ON_FALSE(touch_num < TOUCH_PAD_MAX, ESP_ERR_INVALID_ARG, TOUCH_TAG, "Touch channel error");
  448. TOUCH_ENTER_CRITICAL();
  449. if (!touch_hal_enable_proximity(touch_num, enabled)) {
  450. ret = ESP_ERR_NOT_SUPPORTED;
  451. }
  452. TOUCH_EXIT_CRITICAL();
  453. return ret;
  454. }
  455. esp_err_t touch_pad_proximity_set_count(touch_pad_t touch_num, uint32_t count)
  456. {
  457. ESP_RETURN_ON_FALSE(count <= TOUCH_PROXIMITY_MEAS_NUM_MAX, ESP_ERR_INVALID_ARG, TOUCH_TAG, TOUCH_PARAM_CHECK_STR("measure count"));
  458. TOUCH_ENTER_CRITICAL();
  459. touch_hal_proximity_set_meas_times(count);
  460. TOUCH_EXIT_CRITICAL();
  461. return ESP_OK;
  462. }
  463. esp_err_t touch_pad_proximity_get_count(touch_pad_t touch_num, uint32_t *count)
  464. {
  465. ESP_RETURN_ON_FALSE(count, ESP_ERR_INVALID_ARG, TOUCH_TAG, TOUCH_PARAM_CHECK_STR("measure count"));
  466. TOUCH_ENTER_CRITICAL_SAFE();
  467. touch_hal_proximity_get_meas_times(count);
  468. TOUCH_EXIT_CRITICAL_SAFE();
  469. return ESP_OK;
  470. }
  471. /**
  472. * @brief Get measure count of proximity channel.
  473. * The proximity sensor measurement is the accumulation of touch channel measurements.
  474. * @param touch_num touch pad index
  475. * @param cnt Pointer to receive proximity channel measurement count
  476. * @return
  477. * - ESP_OK Success
  478. * - ESP_ERR_INVALID_ARG parameter is NULL
  479. */
  480. esp_err_t touch_pad_proximity_read_meas_cnt(touch_pad_t touch_num, uint32_t *cnt)
  481. {
  482. TOUCH_NULL_POINTER_CHECK(cnt, "cnt");
  483. ESP_RETURN_ON_FALSE(touch_hal_proximity_pad_check(touch_num), ESP_ERR_INVALID_ARG, TOUCH_TAG, "touch num is not proximity");
  484. TOUCH_ENTER_CRITICAL_SAFE();
  485. touch_hal_proximity_read_meas_cnt(touch_num, cnt);
  486. TOUCH_EXIT_CRITICAL_SAFE();
  487. return ESP_OK;
  488. }
  489. esp_err_t touch_pad_proximity_get_data(touch_pad_t touch_num, uint32_t *measure_out)
  490. {
  491. ESP_RETURN_ON_FALSE(touch_hal_proximity_pad_check(touch_num), ESP_ERR_INVALID_ARG, TOUCH_TAG, "touch num is not proximity");
  492. TOUCH_NULL_POINTER_CHECK(measure_out, "measure_out");
  493. TOUCH_ENTER_CRITICAL_SAFE();
  494. touch_hal_read_benchmark(touch_num, measure_out);
  495. TOUCH_EXIT_CRITICAL_SAFE();
  496. return ESP_OK;
  497. }
  498. /************** sleep pad setting ***********************/
  499. esp_err_t touch_pad_sleep_channel_get_info(touch_pad_sleep_channel_t *slp_config)
  500. {
  501. TOUCH_NULL_POINTER_CHECK(slp_config, "slp_config");
  502. TOUCH_ENTER_CRITICAL_SAFE();
  503. touch_hal_sleep_channel_get_config(slp_config);
  504. TOUCH_EXIT_CRITICAL_SAFE();
  505. return ESP_OK;
  506. }
  507. esp_err_t touch_pad_sleep_channel_enable(touch_pad_t pad_num, bool enable)
  508. {
  509. TOUCH_CHANNEL_CHECK(pad_num);
  510. TOUCH_ENTER_CRITICAL();
  511. touch_hal_sleep_channel_enable(pad_num, enable);
  512. TOUCH_EXIT_CRITICAL();
  513. return ESP_OK;
  514. }
  515. esp_err_t touch_pad_sleep_channel_enable_proximity(touch_pad_t pad_num, bool enable)
  516. {
  517. TOUCH_CHANNEL_CHECK(pad_num);
  518. TOUCH_ENTER_CRITICAL();
  519. if (enable) {
  520. touch_hal_sleep_enable_approach();
  521. } else {
  522. touch_hal_sleep_disable_approach();
  523. }
  524. TOUCH_EXIT_CRITICAL();
  525. return ESP_OK;
  526. }
  527. esp_err_t touch_pad_sleep_get_channel_num(touch_pad_t *pad_num)
  528. {
  529. TOUCH_NULL_POINTER_CHECK(pad_num, "pad_num");
  530. TOUCH_ENTER_CRITICAL();
  531. touch_hal_sleep_get_channel_num(pad_num);
  532. TOUCH_EXIT_CRITICAL();
  533. return ESP_OK;
  534. }
  535. esp_err_t touch_pad_sleep_set_threshold(touch_pad_t pad_num, uint32_t touch_thres)
  536. {
  537. TOUCH_ENTER_CRITICAL();
  538. touch_hal_sleep_set_threshold(touch_thres);
  539. TOUCH_EXIT_CRITICAL();
  540. return ESP_OK;
  541. }
  542. esp_err_t touch_pad_sleep_get_threshold(touch_pad_t pad_num, uint32_t *touch_thres)
  543. {
  544. TOUCH_NULL_POINTER_CHECK(touch_thres, "touch_thres");
  545. TOUCH_ENTER_CRITICAL();
  546. touch_hal_sleep_get_threshold(touch_thres);
  547. TOUCH_EXIT_CRITICAL();
  548. return ESP_OK;
  549. }
  550. esp_err_t touch_pad_sleep_channel_read_benchmark(touch_pad_t pad_num, uint32_t *benchmark)
  551. {
  552. TOUCH_NULL_POINTER_CHECK(benchmark, "benchmark");
  553. TOUCH_ENTER_CRITICAL_SAFE();
  554. touch_hal_sleep_read_benchmark(benchmark);
  555. TOUCH_EXIT_CRITICAL_SAFE();
  556. return ESP_OK;
  557. }
  558. esp_err_t touch_pad_sleep_channel_read_smooth(touch_pad_t pad_num, uint32_t *smooth_data)
  559. {
  560. TOUCH_NULL_POINTER_CHECK(smooth_data, "smooth_data");
  561. TOUCH_ENTER_CRITICAL_SAFE();
  562. touch_hal_sleep_read_smooth(smooth_data);
  563. TOUCH_EXIT_CRITICAL_SAFE();
  564. return ESP_OK;
  565. }
  566. esp_err_t touch_pad_sleep_channel_read_data(touch_pad_t pad_num, uint32_t *raw_data)
  567. {
  568. TOUCH_NULL_POINTER_CHECK(raw_data, "raw_data");
  569. TOUCH_ENTER_CRITICAL_SAFE();
  570. touch_hal_sleep_read_data(raw_data);
  571. TOUCH_EXIT_CRITICAL_SAFE();
  572. return ESP_OK;
  573. }
  574. esp_err_t touch_pad_sleep_channel_reset_benchmark(void)
  575. {
  576. TOUCH_ENTER_CRITICAL();
  577. touch_hal_sleep_reset_benchmark();
  578. TOUCH_EXIT_CRITICAL();
  579. return ESP_OK;
  580. }
  581. esp_err_t touch_pad_sleep_channel_read_debounce(touch_pad_t pad_num, uint32_t *debounce)
  582. {
  583. TOUCH_NULL_POINTER_CHECK(debounce, "debounce");
  584. touch_hal_sleep_read_debounce(debounce);
  585. return ESP_OK;
  586. }
  587. esp_err_t touch_pad_sleep_channel_read_proximity_cnt(touch_pad_t pad_num, uint32_t *approach_cnt)
  588. {
  589. TOUCH_NULL_POINTER_CHECK(approach_cnt, "approach_cnt");
  590. touch_hal_sleep_read_proximity_cnt(approach_cnt);
  591. return ESP_OK;
  592. }
  593. esp_err_t touch_pad_sleep_channel_set_work_time(uint16_t sleep_cycle, uint16_t meas_times)
  594. {
  595. touch_hal_sleep_channel_set_work_time(sleep_cycle, meas_times);
  596. return ESP_OK;
  597. }