touch_sensor_hal.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. // Copyright 2019 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. /*******************************************************************************
  15. * NOTICE
  16. * The hal is not public api, don't use in application code.
  17. * See readme.md in soc/include/hal/readme.md
  18. ******************************************************************************/
  19. // The HAL layer for touch sensor (common part)
  20. #pragma once
  21. #include "hal/touch_sensor_ll.h"
  22. #include "hal/touch_sensor_types.h"
  23. #ifdef CONFIG_IDF_TARGET_ESP32
  24. #include "hal/touch_sensor_hal_esp32.h"
  25. #elif CONFIG_IDF_TARGET_ESP32S2BETA
  26. #include "hal/touch_sensor_hal_esp32s2beta.h"
  27. #endif
  28. typedef struct {
  29. touch_high_volt_t refh;
  30. touch_low_volt_t refl;
  31. touch_volt_atten_t atten;
  32. }touch_hal_volt_t;
  33. typedef struct {
  34. touch_cnt_slope_t slope; /*!<Set touch sensor charge/discharge speed(currents) for each pad.*/
  35. touch_tie_opt_t tie_opt; /*!<Set initial voltage state of touch channel for each measurement.*/
  36. } touch_hal_meas_mode_t;
  37. /**
  38. * Set touch sensor sleep time (interval of measurement).
  39. *
  40. * @param sleep_time The touch sensor will sleep after each measurement.
  41. * sleep_cycle decide the interval between each measurement.
  42. * t_sleep = sleep_cycle / (RTC_SLOW_CLK frequency).
  43. * The approximate frequency value of RTC_SLOW_CLK can be obtained using `rtc_clk_slow_freq_get_hz` function.
  44. */
  45. #define touch_hal_set_sleep_time(sleep_time) touch_ll_set_sleep_time(sleep_time)
  46. /**
  47. * Get touch sensor sleep time.
  48. *
  49. * @param sleep_time Pointer to accept sleep cycle count.
  50. */
  51. #define touch_hal_get_sleep_time(sleep_time) touch_ll_get_sleep_time(sleep_time)
  52. /**
  53. * Set touch sensor high / low voltage threshold of chanrge.
  54. * The touch sensor measures the channel capacitance value by charging and discharging the channel.
  55. * So charge threshold should be less than the supply voltage.
  56. * The actual charge threshold is high voltage threshold minus attenuation value.
  57. *
  58. * @param refh The high voltage threshold of chanrge.
  59. */
  60. void touch_hal_set_voltage(const touch_hal_volt_t *volt);
  61. /**
  62. * Get touch sensor high / low voltage threshold of chanrge.
  63. * The touch sensor measures the channel capacitance value by charging and discharging the channel.
  64. * So charge threshold should be less than the supply voltage.
  65. * The actual charge threshold is high voltage threshold minus attenuation value.
  66. *
  67. * @param refh The voltage threshold of chanrge / discharge.
  68. */
  69. void touch_hal_get_voltage(touch_hal_volt_t *volt);
  70. /**
  71. * Set touch sensor charge/discharge speed(currents) and initial voltage state for each pad measurement.
  72. *
  73. * @param touch_num Touch pad index.
  74. * @param meas Touch pad measurement config.
  75. */
  76. void touch_hal_set_meas_mode(touch_pad_t touch_num, const touch_hal_meas_mode_t *meas);
  77. /**
  78. * Get touch sensor charge/discharge speed(currents) and initial voltage state for each pad measurement.
  79. *
  80. * @param touch_num Touch pad index.
  81. * @param meas Touch pad measurement config.
  82. */
  83. void touch_hal_get_meas_mode(touch_pad_t touch_num, touch_hal_meas_mode_t *meas);
  84. /**
  85. * Set touch sensor FSM mode.
  86. * The measurement action can be triggered by the hardware timer, as well as by the software instruction.
  87. *
  88. * @param mode FSM mode.
  89. */
  90. #define touch_hal_set_fsm_mode(mode) touch_ll_set_fsm_mode(mode)
  91. /**
  92. * Get touch sensor FSM mode.
  93. * The measurement action can be triggered by the hardware timer, as well as by the software instruction.
  94. *
  95. * @param mode FSM mode.
  96. */
  97. #define touch_hal_get_fsm_mode(mode) touch_ll_get_fsm_mode(mode)
  98. /**
  99. * Start touch sensor FSM timer.
  100. * The measurement action can be triggered by the hardware timer, as well as by the software instruction.
  101. *
  102. * @param mode FSM mode.
  103. */
  104. #define touch_hal_start_fsm() touch_ll_start_fsm()
  105. /**
  106. * Stop touch sensor FSM timer.
  107. * The measurement action can be triggered by the hardware timer, as well as by the software instruction.
  108. *
  109. * @param mode FSM mode.
  110. */
  111. #define touch_hal_stop_fsm() touch_ll_stop_fsm()
  112. /**
  113. * Trigger a touch sensor measurement, only support in SW mode of FSM.
  114. */
  115. #define touch_hal_start_sw_meas() touch_ll_start_sw_meas()
  116. /**
  117. * Set touch sensor interrupt threshold.
  118. *
  119. * @note Refer to `touch_pad_set_trigger_mode` to see how to set trigger mode.
  120. * @param touch_num touch pad index.
  121. * @param threshold threshold of touchpad count.
  122. */
  123. #define touch_hal_set_threshold(touch_num, threshold) touch_ll_set_threshold(touch_num, threshold)
  124. /**
  125. * Get touch sensor interrupt threshold.
  126. *
  127. * @param touch_num touch pad index.
  128. * @param threshold pointer to accept threshold.
  129. */
  130. #define touch_hal_get_threshold(touch_num, threshold) touch_ll_get_threshold(touch_num, threshold)
  131. /**
  132. * Enable touch sensor channel. Register touch channel into touch sensor measurement group.
  133. * The working mode of the touch sensor is simultaneous measurement.
  134. * This function will set the measure bits according to the given bitmask.
  135. *
  136. * @note If set this mask, the FSM timer should be stop firsty.
  137. * @note The touch sensor that in scan map, should be deinit GPIO function firstly.
  138. * @param enable_mask bitmask of touch sensor scan group.
  139. * e.g. TOUCH_PAD_NUM1 -> BIT(1)
  140. * @return
  141. * - ESP_OK on success
  142. */
  143. #define touch_hal_set_channel_mask(enable_mask) touch_ll_set_channel_mask(enable_mask)
  144. /**
  145. * Get touch sensor channel mask.
  146. *
  147. * @param enable_mask bitmask of touch sensor scan group.
  148. * e.g. TOUCH_PAD_NUM1 -> BIT(1)
  149. */
  150. #define touch_hal_get_channel_mask(enable_mask) touch_ll_get_channel_mask(enable_mask)
  151. /**
  152. * Disable touch sensor channel by bitmask.
  153. *
  154. * @param enable_mask bitmask of touch sensor scan group.
  155. * e.g. TOUCH_PAD_NUM1 -> BIT(1)
  156. */
  157. #define touch_hal_clear_channel_mask(disable_mask) touch_ll_clear_channel_mask(disable_mask)
  158. /**
  159. * Get the touch sensor status, usually used in ISR to decide which pads are 'touched'.
  160. *
  161. * @param status_mask The touch sensor status. e.g. Touch1 trigger status is `status_mask & (BIT1)`.
  162. */
  163. #define touch_hal_read_trigger_status_mask(status_mask) touch_ll_read_trigger_status_mask(status_mask)
  164. /**
  165. * Clear all touch sensor status.
  166. */
  167. #define touch_hal_clear_trigger_status_mask() touch_ll_clear_trigger_status_mask()
  168. /**
  169. * Get touch sensor raw data (touch sensor counter value) from register. No block.
  170. *
  171. * @param touch_num touch pad index.
  172. * @return touch_value pointer to accept touch sensor value.
  173. */
  174. #define touch_hal_read_raw_data(touch_num) touch_ll_read_raw_data(touch_num)
  175. /**
  176. * Get touch sensor measure status. No block.
  177. *
  178. * @return
  179. * - If touch sensors measure done.
  180. */
  181. #define touch_hal_meas_is_done() touch_ll_meas_is_done()
  182. /**
  183. * Initialize touch module.
  184. *
  185. * @note If default parameter don't match the usage scenario, it can be changed after this function.
  186. */
  187. void touch_hal_init(void);
  188. /**
  189. * Un-install touch pad driver.
  190. *
  191. * @note After this function is called, other touch functions are prohibited from being called.
  192. */
  193. void touch_hal_deinit(void);
  194. /**
  195. * Configure touch sensor for each channel.
  196. */
  197. void touch_hal_config(touch_pad_t touch_num);