touch_sensor.c 19 KB

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