touch_sensor.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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 "esp32s2beta/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_benchmark(touch_pad_t touch_num, uint32_t *basedata)
  207. {
  208. TOUCH_CHANNEL_CHECK(touch_num);
  209. touch_hal_filter_read_benchmark(touch_num, basedata);
  210. return ESP_OK;
  211. }
  212. /* Should be call after clk enable and filter enable. */
  213. esp_err_t touch_pad_reset_benchmark(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_benchmark(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->noise_thr <= TOUCH_NOISE_THR_MAX, TOUCH_PARAM_CHECK_STR("noise"), ESP_ERR_INVALID_ARG);
  226. TOUCH_CHECK(filter_info->jitter_step <= TOUCH_JITTER_STEP_MAX, TOUCH_PARAM_CHECK_STR("jitter_step"), ESP_ERR_INVALID_ARG);
  227. TOUCH_ENTER_CRITICAL();
  228. touch_hal_filter_set_config(filter_info);
  229. TOUCH_EXIT_CRITICAL();
  230. return ESP_OK;
  231. }
  232. esp_err_t touch_pad_filter_get_config(touch_filter_config_t *filter_info)
  233. {
  234. TOUCH_ENTER_CRITICAL();
  235. touch_hal_filter_get_config(filter_info);
  236. TOUCH_EXIT_CRITICAL();
  237. return ESP_OK;
  238. }
  239. esp_err_t touch_pad_filter_enable(void)
  240. {
  241. TOUCH_ENTER_CRITICAL();
  242. touch_hal_filter_enable();
  243. TOUCH_EXIT_CRITICAL();
  244. return ESP_OK;
  245. }
  246. esp_err_t touch_pad_filter_disable(void)
  247. {
  248. TOUCH_ENTER_CRITICAL();
  249. touch_hal_filter_disable();
  250. TOUCH_EXIT_CRITICAL();
  251. return ESP_OK;
  252. }
  253. esp_err_t touch_pad_denoise_enable(void)
  254. {
  255. TOUCH_ENTER_CRITICAL();
  256. touch_hal_clear_channel_mask(BIT(SOC_TOUCH_DENOISE_CHANNEL));
  257. touch_hal_denoise_enable();
  258. TOUCH_EXIT_CRITICAL();
  259. return ESP_OK;
  260. }
  261. esp_err_t touch_pad_denoise_disable(void)
  262. {
  263. TOUCH_ENTER_CRITICAL();
  264. touch_hal_denoise_disable();
  265. TOUCH_EXIT_CRITICAL();
  266. return ESP_OK;
  267. }
  268. esp_err_t touch_pad_denoise_set_config(touch_pad_denoise_t *denoise)
  269. {
  270. TOUCH_CHECK(denoise->grade < TOUCH_PAD_DENOISE_MAX, TOUCH_PARAM_CHECK_STR("grade"), ESP_ERR_INVALID_ARG);
  271. TOUCH_CHECK(denoise->cap_level < TOUCH_PAD_DENOISE_CAP_MAX, TOUCH_PARAM_CHECK_STR("cap_level"), ESP_ERR_INVALID_ARG);
  272. const touch_hal_meas_mode_t meas = {
  273. .slope = TOUCH_PAD_SLOPE_DEFAULT,
  274. .tie_opt = TOUCH_PAD_TIE_OPT_DEFAULT,
  275. };
  276. TOUCH_ENTER_CRITICAL();
  277. touch_hal_set_meas_mode(SOC_TOUCH_DENOISE_CHANNEL, &meas);
  278. touch_hal_denoise_set_config(denoise);
  279. TOUCH_EXIT_CRITICAL();
  280. return ESP_OK;
  281. }
  282. esp_err_t touch_pad_denoise_get_config(touch_pad_denoise_t *denoise)
  283. {
  284. TOUCH_ENTER_CRITICAL();
  285. touch_hal_denoise_get_config(denoise);
  286. TOUCH_EXIT_CRITICAL();
  287. return ESP_OK;
  288. }
  289. esp_err_t touch_pad_denoise_read_data(uint32_t *data)
  290. {
  291. touch_hal_denoise_read_data(data);
  292. return ESP_OK;
  293. }
  294. esp_err_t touch_pad_waterproof_set_config(touch_pad_waterproof_t *waterproof)
  295. {
  296. TOUCH_CHECK(waterproof->guard_ring_pad < SOC_TOUCH_SENSOR_NUM, TOUCH_PARAM_CHECK_STR("pad"), ESP_ERR_INVALID_ARG);
  297. TOUCH_CHECK(waterproof->shield_driver < TOUCH_PAD_SHIELD_DRV_MAX, TOUCH_PARAM_CHECK_STR("shield_driver"), ESP_ERR_INVALID_ARG);
  298. TOUCH_ENTER_CRITICAL();
  299. touch_hal_waterproof_set_config(waterproof);
  300. TOUCH_EXIT_CRITICAL();
  301. return ESP_OK;
  302. }
  303. esp_err_t touch_pad_waterproof_get_config(touch_pad_waterproof_t *waterproof)
  304. {
  305. TOUCH_ENTER_CRITICAL();
  306. touch_hal_waterproof_get_config(waterproof);
  307. TOUCH_EXIT_CRITICAL();
  308. return ESP_OK;
  309. }
  310. esp_err_t touch_pad_waterproof_enable(void)
  311. {
  312. touch_pad_io_init(SOC_TOUCH_SHIELD_CHANNEL);
  313. TOUCH_ENTER_CRITICAL();
  314. touch_hal_waterproof_enable();
  315. TOUCH_EXIT_CRITICAL();
  316. return ESP_OK;
  317. }
  318. esp_err_t touch_pad_waterproof_disable(void)
  319. {
  320. TOUCH_ENTER_CRITICAL();
  321. touch_hal_waterproof_disable();
  322. TOUCH_EXIT_CRITICAL();
  323. return ESP_OK;
  324. }
  325. esp_err_t touch_pad_proximity_set_config(touch_pad_proximity_t *proximity)
  326. {
  327. for (int i=0; i<SOC_TOUCH_PROXIMITY_CHANNEL_NUM; i++) {
  328. TOUCH_CHECK(proximity->select_pad[i] < SOC_TOUCH_SENSOR_NUM, TOUCH_PARAM_CHECK_STR("pad"), ESP_ERR_INVALID_ARG);
  329. }
  330. TOUCH_CHECK(proximity->meas_num <= TOUCH_PROXIMITY_MEAS_NUM_MAX, TOUCH_PARAM_CHECK_STR("meas_num"), ESP_ERR_INVALID_ARG);
  331. TOUCH_ENTER_CRITICAL();
  332. touch_hal_proximity_set_config(proximity);
  333. TOUCH_EXIT_CRITICAL();
  334. return ESP_OK;
  335. }
  336. esp_err_t touch_pad_proximity_get_config(touch_pad_proximity_t *proximity)
  337. {
  338. TOUCH_ENTER_CRITICAL();
  339. touch_hal_proximity_get_config(proximity);
  340. TOUCH_EXIT_CRITICAL();
  341. return ESP_OK;
  342. }
  343. esp_err_t touch_pad_proximity_read_meas_cnt(touch_pad_t touch_num, uint32_t *cnt)
  344. {
  345. TOUCH_CHECK(touch_hal_proximity_pad_check(touch_num), "touch_num is not proximity", ESP_ERR_INVALID_ARG);
  346. touch_hal_proximity_read_meas_cnt(touch_num, cnt);
  347. return ESP_OK;
  348. }
  349. esp_err_t touch_pad_proximity_data_get(touch_pad_t touch_num, uint32_t *measure_out)
  350. {
  351. TOUCH_CHECK(touch_hal_proximity_pad_check(touch_num), "touch_num is not proximity", ESP_ERR_INVALID_ARG);
  352. touch_hal_filter_read_benchmark(touch_num, measure_out);
  353. return ESP_OK;
  354. }
  355. /************** sleep pad setting ***********************/
  356. esp_err_t touch_pad_sleep_channel_config(touch_pad_sleep_channel_t *slp_config)
  357. {
  358. TOUCH_CHECK(slp_config->touch_num < SOC_TOUCH_SENSOR_NUM, TOUCH_PARAM_CHECK_STR("pad"), ESP_ERR_INVALID_ARG);
  359. TOUCH_ENTER_CRITICAL();
  360. touch_hal_sleep_channel_config(slp_config);
  361. TOUCH_EXIT_CRITICAL();
  362. return ESP_OK;
  363. }
  364. esp_err_t touch_pad_sleep_channel_read_benchmark(uint32_t *benchmark)
  365. {
  366. touch_hal_sleep_read_benchmark(benchmark);
  367. return ESP_OK;
  368. }
  369. esp_err_t touch_pad_sleep_channel_read_debounce(uint32_t *debounce)
  370. {
  371. touch_hal_sleep_read_debounce(debounce);
  372. return ESP_OK;
  373. }
  374. esp_err_t touch_pad_sleep_channel_read_proximity_cnt(uint32_t *approach_cnt)
  375. {
  376. touch_hal_sleep_read_proximity_cnt(approach_cnt);
  377. return ESP_OK;
  378. }