touch_sensor.c 19 KB

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