touch_sensor_common.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /*
  2. * SPDX-FileCopyrightText: 2016-2021 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <stdlib.h>
  7. #include <ctype.h>
  8. #include "sdkconfig.h"
  9. #include "esp_types.h"
  10. #include "esp_log.h"
  11. #include "sys/lock.h"
  12. #include "soc/soc_pins.h"
  13. #include "freertos/FreeRTOS.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 "hal/touch_sensor_types.h"
  22. #include "hal/touch_sensor_hal.h"
  23. static const char *TOUCH_TAG = "TOUCH_SENSOR";
  24. #define TOUCH_CHECK(a, str, ret_val) ({ \
  25. if (!(a)) { \
  26. ESP_LOGE(TOUCH_TAG,"%s(%d): %s", __FUNCTION__, __LINE__, str); \
  27. return (ret_val); \
  28. } \
  29. })
  30. #ifdef CONFIG_IDF_TARGET_ESP32
  31. #define TOUCH_CHANNEL_CHECK(channel) do { \
  32. TOUCH_CHECK(channel < SOC_TOUCH_SENSOR_NUM && channel >= 0, "Touch channel error", ESP_ERR_INVALID_ARG); \
  33. } while (0);
  34. #else // !CONFIG_IDF_TARGET_ESP32
  35. #define TOUCH_CHANNEL_CHECK(channel) do { \
  36. TOUCH_CHECK(channel < SOC_TOUCH_SENSOR_NUM && channel >= 0, "Touch channel error", ESP_ERR_INVALID_ARG); \
  37. TOUCH_CHECK(channel != SOC_TOUCH_DENOISE_CHANNEL, "TOUCH0 is internal denoise channel", ESP_ERR_INVALID_ARG); \
  38. } while (0);
  39. #endif // CONFIG_IDF_TARGET_ESP32
  40. #define TOUCH_GET_IO_NUM(channel) (touch_sensor_channel_io_map[channel])
  41. _Static_assert(TOUCH_PAD_MAX == SOC_TOUCH_SENSOR_NUM, "Touch sensor channel number not equal to chip capabilities");
  42. extern portMUX_TYPE rtc_spinlock; //TODO: Will be placed in the appropriate position after the rtc module is finished.
  43. #define TOUCH_ENTER_CRITICAL() portENTER_CRITICAL(&rtc_spinlock)
  44. #define TOUCH_EXIT_CRITICAL() portEXIT_CRITICAL(&rtc_spinlock)
  45. esp_err_t touch_pad_isr_deregister(intr_handler_t fn, void *arg)
  46. {
  47. return rtc_isr_deregister(fn, arg);
  48. }
  49. esp_err_t touch_pad_set_voltage(touch_high_volt_t refh, touch_low_volt_t refl, touch_volt_atten_t atten)
  50. {
  51. TOUCH_CHECK(((refh < TOUCH_HVOLT_MAX) && (refh >= (int )TOUCH_HVOLT_KEEP)), "touch refh error",
  52. ESP_ERR_INVALID_ARG);
  53. TOUCH_CHECK(((refl < TOUCH_LVOLT_MAX) && (refh >= (int )TOUCH_LVOLT_KEEP)), "touch refl error",
  54. ESP_ERR_INVALID_ARG);
  55. TOUCH_CHECK(((atten < TOUCH_HVOLT_ATTEN_MAX) && (refh >= (int )TOUCH_HVOLT_ATTEN_KEEP)), "touch atten error",
  56. ESP_ERR_INVALID_ARG);
  57. const touch_hal_volt_t volt = {
  58. .refh = refh,
  59. .refl = refl,
  60. .atten = atten,
  61. };
  62. TOUCH_ENTER_CRITICAL();
  63. touch_hal_set_voltage(&volt);
  64. TOUCH_EXIT_CRITICAL();
  65. return ESP_OK;
  66. }
  67. esp_err_t touch_pad_get_voltage(touch_high_volt_t *refh, touch_low_volt_t *refl, touch_volt_atten_t *atten)
  68. {
  69. touch_hal_volt_t volt = {0};
  70. TOUCH_ENTER_CRITICAL();
  71. touch_hal_get_voltage(&volt);
  72. TOUCH_EXIT_CRITICAL();
  73. *refh = volt.refh;
  74. *refl = volt.refl;
  75. *atten = volt.atten;
  76. return ESP_OK;
  77. }
  78. esp_err_t touch_pad_set_cnt_mode(touch_pad_t touch_num, touch_cnt_slope_t slope, touch_tie_opt_t opt)
  79. {
  80. TOUCH_CHECK(touch_num < SOC_TOUCH_SENSOR_NUM, "Touch channel error", ESP_ERR_INVALID_ARG);
  81. TOUCH_CHECK(slope < TOUCH_PAD_SLOPE_MAX, "touch slope error", ESP_ERR_INVALID_ARG);
  82. TOUCH_CHECK(opt < TOUCH_PAD_TIE_OPT_MAX, "touch opt error", ESP_ERR_INVALID_ARG);
  83. const touch_hal_meas_mode_t meas = {
  84. .slope = slope,
  85. .tie_opt = opt,
  86. };
  87. TOUCH_ENTER_CRITICAL();
  88. touch_hal_set_meas_mode(touch_num, &meas);
  89. TOUCH_EXIT_CRITICAL();
  90. return ESP_OK;
  91. }
  92. esp_err_t touch_pad_get_cnt_mode(touch_pad_t touch_num, touch_cnt_slope_t *slope, touch_tie_opt_t *opt)
  93. {
  94. TOUCH_CHECK(touch_num < SOC_TOUCH_SENSOR_NUM, "Touch channel error", ESP_ERR_INVALID_ARG);
  95. touch_hal_meas_mode_t meas = {0};
  96. TOUCH_ENTER_CRITICAL();
  97. touch_hal_get_meas_mode(touch_num, &meas);
  98. TOUCH_EXIT_CRITICAL();
  99. *slope = meas.slope;
  100. *opt = meas.tie_opt;
  101. return ESP_OK;
  102. }
  103. esp_err_t touch_pad_io_init(touch_pad_t touch_num)
  104. {
  105. TOUCH_CHANNEL_CHECK(touch_num);
  106. gpio_num_t gpio_num = TOUCH_GET_IO_NUM(touch_num);
  107. rtc_gpio_init(gpio_num);
  108. rtc_gpio_set_direction(gpio_num, RTC_GPIO_MODE_DISABLED);
  109. rtc_gpio_pulldown_dis(gpio_num);
  110. rtc_gpio_pullup_dis(gpio_num);
  111. return ESP_OK;
  112. }
  113. esp_err_t touch_pad_fsm_start(void)
  114. {
  115. TOUCH_ENTER_CRITICAL();
  116. touch_hal_start_fsm();
  117. TOUCH_EXIT_CRITICAL();
  118. return ESP_OK;
  119. }
  120. esp_err_t touch_pad_fsm_stop(void)
  121. {
  122. TOUCH_ENTER_CRITICAL();
  123. touch_hal_stop_fsm();
  124. TOUCH_EXIT_CRITICAL();
  125. return ESP_OK;
  126. }
  127. esp_err_t touch_pad_set_fsm_mode(touch_fsm_mode_t mode)
  128. {
  129. TOUCH_CHECK((mode < TOUCH_FSM_MODE_MAX), "touch fsm mode error", ESP_ERR_INVALID_ARG);
  130. TOUCH_ENTER_CRITICAL();
  131. touch_hal_set_fsm_mode(mode);
  132. TOUCH_EXIT_CRITICAL();
  133. #ifdef CONFIG_IDF_TARGET_ESP32
  134. if (mode == TOUCH_FSM_MODE_TIMER) {
  135. touch_pad_fsm_start();
  136. } else {
  137. touch_pad_fsm_stop();
  138. }
  139. #endif
  140. return ESP_OK;
  141. }
  142. esp_err_t touch_pad_get_fsm_mode(touch_fsm_mode_t *mode)
  143. {
  144. touch_hal_get_fsm_mode(mode);
  145. return ESP_OK;
  146. }
  147. esp_err_t touch_pad_sw_start(void)
  148. {
  149. TOUCH_ENTER_CRITICAL();
  150. touch_hal_start_sw_meas();
  151. TOUCH_EXIT_CRITICAL();
  152. return ESP_OK;
  153. }
  154. #ifdef CONFIG_IDF_TARGET_ESP32
  155. esp_err_t touch_pad_set_thresh(touch_pad_t touch_num, uint16_t threshold)
  156. {
  157. TOUCH_CHANNEL_CHECK(touch_num);
  158. TOUCH_ENTER_CRITICAL();
  159. touch_hal_set_threshold(touch_num, threshold);
  160. TOUCH_EXIT_CRITICAL();
  161. return ESP_OK;
  162. }
  163. #else // !CONFIG_IDF_TARGET_ESP32
  164. esp_err_t touch_pad_set_thresh(touch_pad_t touch_num, uint32_t threshold)
  165. {
  166. TOUCH_CHANNEL_CHECK(touch_num);
  167. TOUCH_CHECK(touch_num != SOC_TOUCH_DENOISE_CHANNEL,
  168. "TOUCH0 is internal denoise channel", ESP_ERR_INVALID_ARG);
  169. TOUCH_ENTER_CRITICAL();
  170. touch_hal_set_threshold(touch_num, threshold);
  171. TOUCH_EXIT_CRITICAL();
  172. return ESP_OK;
  173. }
  174. #endif // CONFIG_IDF_TARGET_ESP32
  175. #ifdef CONFIG_IDF_TARGET_ESP32
  176. esp_err_t touch_pad_get_thresh(touch_pad_t touch_num, uint16_t *threshold)
  177. {
  178. TOUCH_CHANNEL_CHECK(touch_num);
  179. touch_hal_get_threshold(touch_num, threshold);
  180. return ESP_OK;
  181. }
  182. #else // !CONFIG_IDF_TARGET_ESP32
  183. esp_err_t touch_pad_get_thresh(touch_pad_t touch_num, uint32_t *threshold)
  184. {
  185. TOUCH_CHANNEL_CHECK(touch_num);
  186. TOUCH_CHECK(touch_num != SOC_TOUCH_DENOISE_CHANNEL,
  187. "TOUCH0 is internal denoise channel", ESP_ERR_INVALID_ARG);
  188. touch_hal_get_threshold(touch_num, threshold);
  189. return ESP_OK;
  190. }
  191. #endif // CONFIG_IDF_TARGET_ESP32
  192. esp_err_t touch_pad_get_wakeup_status(touch_pad_t *pad_num)
  193. {
  194. touch_hal_get_wakeup_status(pad_num);
  195. TOUCH_CHANNEL_CHECK(*pad_num);
  196. return ESP_OK;
  197. }
  198. uint32_t IRAM_ATTR touch_pad_get_status(void)
  199. {
  200. uint32_t status = 0;
  201. touch_hal_read_trigger_status_mask(&status);
  202. return status;
  203. }
  204. esp_err_t IRAM_ATTR touch_pad_clear_status(void)
  205. {
  206. touch_hal_clear_trigger_status_mask();
  207. return ESP_OK;
  208. }