touch_sensor.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  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 "freertos/FreeRTOS.h"
  20. #include "freertos/xtensa_api.h"
  21. #include "freertos/semphr.h"
  22. #include "freertos/timers.h"
  23. #include "esp_intr_alloc.h"
  24. #include "driver/rtc_io.h"
  25. #include "driver/touch_pad.h"
  26. #include "driver/rtc_cntl.h"
  27. #include "driver/gpio.h"
  28. #include "sdkconfig.h"
  29. #include "esp32s2/rom/ets_sys.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):%s", __FILE__, __LINE__, __FUNCTION__, 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 <= SOC_TOUCH_SENSOR_BIT_MASK_MAX), "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() portENTER_CRITICAL(&rtc_spinlock)
  57. #define TOUCH_EXIT_CRITICAL() portEXIT_CRITICAL(&rtc_spinlock)
  58. static SemaphoreHandle_t rtc_touch_mux = NULL;
  59. /*---------------------------------------------------------------
  60. Touch Pad
  61. ---------------------------------------------------------------*/
  62. esp_err_t touch_pad_isr_register(intr_handler_t fn, void *arg, touch_pad_intr_mask_t intr_mask)
  63. {
  64. TOUCH_CHECK(fn != NULL, TOUCH_PARAM_CHECK_STR("intr_mask"), ESP_ERR_INVALID_ARG);
  65. TOUCH_INTR_MASK_CHECK(intr_mask);
  66. uint32_t en_msk = 0;
  67. if (intr_mask & TOUCH_PAD_INTR_MASK_DONE) {
  68. en_msk |= RTC_CNTL_TOUCH_DONE_INT_ST_M;
  69. }
  70. if (intr_mask & TOUCH_PAD_INTR_MASK_ACTIVE) {
  71. en_msk |= RTC_CNTL_TOUCH_ACTIVE_INT_ST_M;
  72. }
  73. if (intr_mask & TOUCH_PAD_INTR_MASK_INACTIVE) {
  74. en_msk |= RTC_CNTL_TOUCH_INACTIVE_INT_ST_M;
  75. }
  76. return rtc_isr_register(fn, arg, en_msk);
  77. }
  78. esp_err_t touch_pad_set_meas_time(uint16_t sleep_cycle, uint16_t meas_times)
  79. {
  80. TOUCH_ENTER_CRITICAL();
  81. touch_hal_set_meas_times(meas_times);
  82. touch_hal_set_sleep_time(sleep_cycle);
  83. TOUCH_EXIT_CRITICAL();
  84. return ESP_OK;
  85. }
  86. esp_err_t touch_pad_get_meas_time(uint16_t *sleep_cycle, uint16_t *meas_times)
  87. {
  88. TOUCH_ENTER_CRITICAL();
  89. touch_hal_get_measure_times(meas_times);
  90. touch_hal_get_sleep_time(sleep_cycle);
  91. TOUCH_EXIT_CRITICAL();
  92. return ESP_OK;
  93. }
  94. esp_err_t touch_pad_set_inactive_connect(touch_pad_conn_type_t type)
  95. {
  96. TOUCH_CHECK(type < TOUCH_PAD_CONN_MAX, TOUCH_PARAM_CHECK_STR("type"), ESP_ERR_INVALID_ARG);
  97. TOUCH_ENTER_CRITICAL();
  98. touch_hal_set_inactive_connect(type);
  99. TOUCH_EXIT_CRITICAL();
  100. return ESP_OK;
  101. }
  102. esp_err_t touch_pad_get_inactive_connect(touch_pad_conn_type_t *type)
  103. {
  104. touch_hal_get_inactive_connect(type);
  105. return ESP_OK;
  106. }
  107. bool touch_pad_meas_is_done(void)
  108. {
  109. return touch_hal_meas_is_done();
  110. }
  111. esp_err_t touch_pad_set_channel_mask(uint16_t enable_mask)
  112. {
  113. TOUCH_CH_MASK_CHECK(enable_mask);
  114. TOUCH_ENTER_CRITICAL();
  115. touch_hal_set_channel_mask(enable_mask);
  116. TOUCH_EXIT_CRITICAL();
  117. return ESP_OK;
  118. }
  119. esp_err_t touch_pad_get_channel_mask(uint16_t *enable_mask)
  120. {
  121. TOUCH_ENTER_CRITICAL();
  122. touch_hal_get_channel_mask(enable_mask);
  123. TOUCH_EXIT_CRITICAL();
  124. return ESP_OK;
  125. }
  126. esp_err_t touch_pad_clear_channel_mask(uint16_t enable_mask)
  127. {
  128. TOUCH_CH_MASK_CHECK(enable_mask);
  129. TOUCH_ENTER_CRITICAL();
  130. touch_hal_clear_channel_mask(enable_mask);
  131. TOUCH_EXIT_CRITICAL();
  132. return ESP_OK;
  133. }
  134. touch_pad_t IRAM_ATTR touch_pad_get_current_meas_channel(void)
  135. {
  136. return (touch_pad_t)touch_hal_get_current_meas_channel();
  137. }
  138. esp_err_t touch_pad_intr_enable(touch_pad_intr_mask_t int_mask)
  139. {
  140. TOUCH_INTR_MASK_CHECK(int_mask);
  141. TOUCH_ENTER_CRITICAL();
  142. touch_hal_intr_enable(int_mask);
  143. TOUCH_EXIT_CRITICAL();
  144. return ESP_OK;
  145. }
  146. esp_err_t touch_pad_intr_disable(touch_pad_intr_mask_t int_mask)
  147. {
  148. TOUCH_INTR_MASK_CHECK(int_mask);
  149. TOUCH_ENTER_CRITICAL();
  150. touch_hal_intr_disable(int_mask);
  151. TOUCH_EXIT_CRITICAL();
  152. return ESP_OK;
  153. }
  154. uint32_t touch_pad_read_intr_status_mask(void)
  155. {
  156. return touch_hal_read_intr_status_mask();
  157. }
  158. esp_err_t touch_pad_config(touch_pad_t touch_num)
  159. {
  160. TOUCH_CHANNEL_CHECK(touch_num);
  161. touch_pad_io_init(touch_num);
  162. TOUCH_ENTER_CRITICAL();
  163. touch_hal_config(touch_num);
  164. touch_hal_set_channel_mask(BIT(touch_num));
  165. TOUCH_EXIT_CRITICAL();
  166. return ESP_OK;
  167. }
  168. esp_err_t touch_pad_init(void)
  169. {
  170. if (rtc_touch_mux == NULL) {
  171. rtc_touch_mux = xSemaphoreCreateMutex();
  172. }
  173. if (rtc_touch_mux == NULL) {
  174. return ESP_ERR_NO_MEM;
  175. }
  176. TOUCH_ENTER_CRITICAL();
  177. touch_hal_init();
  178. TOUCH_EXIT_CRITICAL();
  179. return ESP_OK;
  180. }
  181. esp_err_t touch_pad_deinit(void)
  182. {
  183. TOUCH_CHECK(rtc_touch_mux != NULL, "Touch pad not initialized", ESP_FAIL);
  184. xSemaphoreTake(rtc_touch_mux, portMAX_DELAY);
  185. TOUCH_ENTER_CRITICAL();
  186. touch_hal_deinit();
  187. TOUCH_EXIT_CRITICAL();
  188. xSemaphoreGive(rtc_touch_mux);
  189. vSemaphoreDelete(rtc_touch_mux);
  190. rtc_touch_mux = NULL;
  191. return ESP_OK;
  192. }
  193. esp_err_t touch_pad_reset(void)
  194. {
  195. TOUCH_ENTER_CRITICAL();
  196. touch_hal_reset();
  197. TOUCH_EXIT_CRITICAL();
  198. return ESP_OK;
  199. }
  200. esp_err_t IRAM_ATTR touch_pad_read_raw_data(touch_pad_t touch_num, uint32_t *raw_data)
  201. {
  202. TOUCH_CHANNEL_CHECK(touch_num);
  203. *raw_data = touch_hal_read_raw_data(touch_num);
  204. return ESP_OK;
  205. }
  206. esp_err_t IRAM_ATTR touch_pad_filter_read_baseline(touch_pad_t touch_num, uint32_t *basedata)
  207. {
  208. TOUCH_CHANNEL_CHECK(touch_num);
  209. touch_hal_filter_read_baseline(touch_num, basedata);
  210. return ESP_OK;
  211. }
  212. /* Should be call after clk enable and filter enable. */
  213. esp_err_t touch_pad_filter_reset_baseline(touch_pad_t touch_num)
  214. {
  215. TOUCH_CHECK(touch_num <= TOUCH_PAD_MAX && touch_num >= 0, "Touch channel error", ESP_ERR_INVALID_ARG);
  216. TOUCH_ENTER_CRITICAL();
  217. touch_hal_filter_reset_baseline(touch_num);
  218. TOUCH_EXIT_CRITICAL();
  219. return ESP_OK;
  220. }
  221. esp_err_t touch_pad_filter_set_config(touch_filter_config_t *filter_info)
  222. {
  223. TOUCH_CHECK(filter_info->mode < TOUCH_PAD_FILTER_MAX, TOUCH_PARAM_CHECK_STR("mode"), ESP_ERR_INVALID_ARG);
  224. TOUCH_CHECK(filter_info->debounce_cnt <= TOUCH_DEBOUNCE_CNT_MAX, TOUCH_PARAM_CHECK_STR("debounce"), ESP_ERR_INVALID_ARG);
  225. TOUCH_CHECK(filter_info->hysteresis_thr <= TOUCH_HYSTERESIS_THR_MAX, TOUCH_PARAM_CHECK_STR("hysteresis"), ESP_ERR_INVALID_ARG);
  226. TOUCH_CHECK(filter_info->noise_thr <= TOUCH_NOISE_THR_MAX, TOUCH_PARAM_CHECK_STR("noise"), ESP_ERR_INVALID_ARG);
  227. TOUCH_CHECK(filter_info->noise_neg_thr <= TOUCH_NOISE_NEG_THR_MAX, TOUCH_PARAM_CHECK_STR("noise"), ESP_ERR_INVALID_ARG);
  228. TOUCH_CHECK(filter_info->neg_noise_limit <= TOUCH_NEG_NOISE_CNT_LIMIT, TOUCH_PARAM_CHECK_STR("noise_limit"), ESP_ERR_INVALID_ARG);
  229. TOUCH_CHECK(filter_info->jitter_step <= TOUCH_JITTER_STEP_MAX, TOUCH_PARAM_CHECK_STR("jitter_step"), ESP_ERR_INVALID_ARG);
  230. TOUCH_ENTER_CRITICAL();
  231. touch_hal_filter_set_config(filter_info);
  232. TOUCH_EXIT_CRITICAL();
  233. return ESP_OK;
  234. }
  235. esp_err_t touch_pad_filter_get_config(touch_filter_config_t *filter_info)
  236. {
  237. TOUCH_ENTER_CRITICAL();
  238. touch_hal_filter_get_config(filter_info);
  239. TOUCH_EXIT_CRITICAL();
  240. return ESP_OK;
  241. }
  242. esp_err_t touch_pad_filter_enable(void)
  243. {
  244. TOUCH_ENTER_CRITICAL();
  245. touch_hal_filter_enable();
  246. TOUCH_EXIT_CRITICAL();
  247. return ESP_OK;
  248. }
  249. esp_err_t touch_pad_filter_disable(void)
  250. {
  251. TOUCH_ENTER_CRITICAL();
  252. touch_hal_filter_disable();
  253. TOUCH_EXIT_CRITICAL();
  254. return ESP_OK;
  255. }
  256. esp_err_t touch_pad_denoise_enable(void)
  257. {
  258. TOUCH_ENTER_CRITICAL();
  259. touch_hal_clear_channel_mask(BIT(SOC_TOUCH_DENOISE_CHANNEL));
  260. touch_hal_denoise_enable();
  261. TOUCH_EXIT_CRITICAL();
  262. return ESP_OK;
  263. }
  264. esp_err_t touch_pad_denoise_disable(void)
  265. {
  266. TOUCH_ENTER_CRITICAL();
  267. touch_hal_denoise_disable();
  268. TOUCH_EXIT_CRITICAL();
  269. return ESP_OK;
  270. }
  271. esp_err_t touch_pad_denoise_set_config(touch_pad_denoise_t *denoise)
  272. {
  273. TOUCH_CHECK(denoise->grade < TOUCH_PAD_DENOISE_MAX, TOUCH_PARAM_CHECK_STR("grade"), ESP_ERR_INVALID_ARG);
  274. TOUCH_CHECK(denoise->cap_level < TOUCH_PAD_DENOISE_CAP_MAX, TOUCH_PARAM_CHECK_STR("cap_level"), ESP_ERR_INVALID_ARG);
  275. const touch_hal_meas_mode_t meas = {
  276. .slope = TOUCH_PAD_SLOPE_DEFAULT,
  277. .tie_opt = TOUCH_PAD_TIE_OPT_DEFAULT,
  278. };
  279. TOUCH_ENTER_CRITICAL();
  280. touch_hal_set_meas_mode(SOC_TOUCH_DENOISE_CHANNEL, &meas);
  281. touch_hal_denoise_set_config(denoise);
  282. TOUCH_EXIT_CRITICAL();
  283. return ESP_OK;
  284. }
  285. esp_err_t touch_pad_denoise_get_config(touch_pad_denoise_t *denoise)
  286. {
  287. TOUCH_ENTER_CRITICAL();
  288. touch_hal_denoise_get_config(denoise);
  289. TOUCH_EXIT_CRITICAL();
  290. return ESP_OK;
  291. }
  292. esp_err_t touch_pad_denoise_read_data(uint32_t *data)
  293. {
  294. touch_hal_denoise_read_data(data);
  295. return ESP_OK;
  296. }
  297. esp_err_t touch_pad_waterproof_set_config(touch_pad_waterproof_t *waterproof)
  298. {
  299. TOUCH_CHECK(waterproof->guard_ring_pad < SOC_TOUCH_SENSOR_NUM, TOUCH_PARAM_CHECK_STR("pad"), ESP_ERR_INVALID_ARG);
  300. TOUCH_CHECK(waterproof->shield_driver < TOUCH_PAD_SHIELD_DRV_MAX, TOUCH_PARAM_CHECK_STR("shield_driver"), ESP_ERR_INVALID_ARG);
  301. TOUCH_ENTER_CRITICAL();
  302. touch_hal_waterproof_set_config(waterproof);
  303. TOUCH_EXIT_CRITICAL();
  304. return ESP_OK;
  305. }
  306. esp_err_t touch_pad_waterproof_get_config(touch_pad_waterproof_t *waterproof)
  307. {
  308. TOUCH_ENTER_CRITICAL();
  309. touch_hal_waterproof_get_config(waterproof);
  310. TOUCH_EXIT_CRITICAL();
  311. return ESP_OK;
  312. }
  313. esp_err_t touch_pad_waterproof_enable(void)
  314. {
  315. touch_pad_io_init(SOC_TOUCH_SHIELD_CHANNEL);
  316. TOUCH_ENTER_CRITICAL();
  317. touch_hal_waterproof_enable();
  318. TOUCH_EXIT_CRITICAL();
  319. return ESP_OK;
  320. }
  321. esp_err_t touch_pad_waterproof_disable(void)
  322. {
  323. TOUCH_ENTER_CRITICAL();
  324. touch_hal_waterproof_disable();
  325. TOUCH_EXIT_CRITICAL();
  326. return ESP_OK;
  327. }
  328. esp_err_t touch_pad_proximity_set_config(touch_pad_proximity_t *proximity)
  329. {
  330. for (int i=0; i<SOC_TOUCH_PROXIMITY_CHANNEL_NUM; i++) {
  331. TOUCH_CHECK(proximity->select_pad[i] < SOC_TOUCH_SENSOR_NUM, TOUCH_PARAM_CHECK_STR("pad"), ESP_ERR_INVALID_ARG);
  332. }
  333. TOUCH_CHECK(proximity->meas_num <= TOUCH_PROXIMITY_MEAS_NUM_MAX, TOUCH_PARAM_CHECK_STR("meas_num"), ESP_ERR_INVALID_ARG);
  334. TOUCH_ENTER_CRITICAL();
  335. touch_hal_proximity_set_config(proximity);
  336. TOUCH_EXIT_CRITICAL();
  337. return ESP_OK;
  338. }
  339. esp_err_t touch_pad_proximity_get_config(touch_pad_proximity_t *proximity)
  340. {
  341. TOUCH_ENTER_CRITICAL();
  342. touch_hal_proximity_get_config(proximity);
  343. TOUCH_EXIT_CRITICAL();
  344. return ESP_OK;
  345. }
  346. esp_err_t touch_pad_proximity_read_meas_cnt(touch_pad_t touch_num, uint32_t *cnt)
  347. {
  348. TOUCH_CHECK(touch_hal_proximity_pad_check(touch_num), "touch_num is not proximity", ESP_ERR_INVALID_ARG);
  349. touch_hal_proximity_read_meas_cnt(touch_num, cnt);
  350. return ESP_OK;
  351. }
  352. esp_err_t touch_pad_proximity_data_get(touch_pad_t touch_num, uint32_t *measure_out)
  353. {
  354. TOUCH_CHECK(touch_hal_proximity_pad_check(touch_num), "touch_num is not proximity", ESP_ERR_INVALID_ARG);
  355. touch_hal_filter_read_baseline(touch_num, measure_out);
  356. return ESP_OK;
  357. }
  358. /************** sleep pad setting ***********************/
  359. esp_err_t touch_pad_sleep_channel_config(touch_pad_sleep_channel_t *slp_config)
  360. {
  361. TOUCH_CHECK(slp_config->touch_num < SOC_TOUCH_SENSOR_NUM, TOUCH_PARAM_CHECK_STR("pad"), ESP_ERR_INVALID_ARG);
  362. TOUCH_ENTER_CRITICAL();
  363. touch_hal_sleep_channel_config(slp_config);
  364. TOUCH_EXIT_CRITICAL();
  365. return ESP_OK;
  366. }
  367. esp_err_t touch_pad_sleep_channel_read_baseline(uint32_t *baseline)
  368. {
  369. touch_hal_sleep_read_baseline(baseline);
  370. return ESP_OK;
  371. }
  372. esp_err_t touch_pad_sleep_channel_read_debounce(uint32_t *debounce)
  373. {
  374. touch_hal_sleep_read_debounce(debounce);
  375. return ESP_OK;
  376. }
  377. esp_err_t touch_pad_sleep_channel_read_proximity_cnt(uint32_t *approach_cnt)
  378. {
  379. touch_hal_sleep_read_proximity_cnt(approach_cnt);
  380. return ESP_OK;
  381. }