touch_sensor_common.c 7.7 KB

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