touch_sensor.c 18 KB

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