touch_sensor_common.c 7.6 KB

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