touch_sensor_hal.h 7.4 KB

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