touch_sensor_common.c 7.7 KB

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