touch_sensor_hal.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. // Copyright 2015-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. // The HAL layer for Touch Sensor (common part)
  15. #include "soc/soc_pins.h"
  16. #include "hal/touch_sensor_hal.h"
  17. #include "hal/touch_sensor_types.h"
  18. static int s_sleep_cycle = -1;
  19. static int s_meas_times = -1;
  20. void touch_hal_init(void)
  21. {
  22. touch_ll_stop_fsm();
  23. touch_ll_intr_disable(TOUCH_PAD_INTR_MASK_ALL);
  24. touch_ll_intr_clear(TOUCH_PAD_INTR_MASK_ALL);
  25. touch_ll_clear_channel_mask(TOUCH_PAD_BIT_MASK_ALL);
  26. touch_ll_clear_trigger_status_mask();
  27. touch_ll_set_meas_times(TOUCH_PAD_MEASURE_CYCLE_DEFAULT);
  28. touch_ll_set_sleep_time(TOUCH_PAD_SLEEP_CYCLE_DEFAULT);
  29. /* Configure the touch-sensor power domain into self-bias since bandgap-bias
  30. * level is different under sleep-mode compared to running-mode. self-bias is
  31. * always on after chip startup. */
  32. touch_ll_sleep_low_power(true);
  33. touch_ll_set_voltage_high(TOUCH_PAD_HIGH_VOLTAGE_THRESHOLD);
  34. touch_ll_set_voltage_low(TOUCH_PAD_LOW_VOLTAGE_THRESHOLD);
  35. touch_ll_set_voltage_attenuation(TOUCH_PAD_ATTEN_VOLTAGE_THRESHOLD);
  36. touch_ll_set_idle_channel_connect(TOUCH_PAD_IDLE_CH_CONNECT_DEFAULT);
  37. /* Clear touch channels to initialize the channel value (benchmark, raw_data).
  38. * Note: Should call it after enable clock gate. */
  39. touch_ll_clkgate(true); // Enable clock gate for touch sensor.
  40. touch_ll_reset_benchmark(TOUCH_PAD_MAX);
  41. touch_ll_sleep_reset_benchmark();
  42. }
  43. void touch_hal_deinit(void)
  44. {
  45. touch_ll_reset_benchmark(TOUCH_PAD_MAX);
  46. touch_ll_sleep_reset_benchmark();
  47. touch_ll_stop_fsm();
  48. touch_ll_clkgate(false);
  49. touch_ll_clear_channel_mask(TOUCH_PAD_BIT_MASK_ALL);
  50. touch_ll_clear_trigger_status_mask();
  51. touch_ll_intr_disable(TOUCH_PAD_INTR_MASK_ALL);
  52. touch_ll_timeout_disable();
  53. touch_ll_waterproof_disable();
  54. touch_ll_denoise_disable();
  55. touch_pad_t prox_pad[SOC_TOUCH_PROXIMITY_CHANNEL_NUM] = {[0 ... (SOC_TOUCH_PROXIMITY_CHANNEL_NUM - 1)] = 0};
  56. touch_ll_proximity_set_channel_num((const touch_pad_t *)prox_pad);
  57. touch_ll_sleep_set_channel_num(0);
  58. touch_ll_sleep_disable_approach();
  59. touch_ll_reset(); // Reset the touch sensor FSM.
  60. }
  61. void touch_hal_filter_set_config(const touch_filter_config_t *filter_info)
  62. {
  63. touch_ll_filter_set_filter_mode(filter_info->mode);
  64. touch_ll_filter_set_debounce(filter_info->debounce_cnt);
  65. touch_ll_filter_set_noise_thres(filter_info->noise_thr);
  66. touch_ll_filter_set_jitter_step(filter_info->jitter_step);
  67. touch_ll_filter_set_smooth_mode(filter_info->smh_lvl);
  68. }
  69. void touch_hal_filter_get_config(touch_filter_config_t *filter_info)
  70. {
  71. touch_ll_filter_get_filter_mode(&filter_info->mode);
  72. touch_ll_filter_get_debounce(&filter_info->debounce_cnt);
  73. touch_ll_filter_get_noise_thres(&filter_info->noise_thr);
  74. touch_ll_filter_get_jitter_step(&filter_info->jitter_step);
  75. touch_ll_filter_get_smooth_mode(&filter_info->smh_lvl);
  76. }
  77. void touch_hal_denoise_set_config(const touch_pad_denoise_t *denoise)
  78. {
  79. touch_ll_denoise_set_cap_level(denoise->cap_level);
  80. touch_ll_denoise_set_grade(denoise->grade);
  81. }
  82. void touch_hal_denoise_get_config(touch_pad_denoise_t *denoise)
  83. {
  84. touch_ll_denoise_get_cap_level(&denoise->cap_level);
  85. touch_ll_denoise_get_grade(&denoise->grade);
  86. }
  87. void touch_hal_denoise_enable(void)
  88. {
  89. touch_ll_clear_channel_mask(1U << SOC_TOUCH_DENOISE_CHANNEL);
  90. touch_ll_denoise_enable();
  91. }
  92. void touch_hal_waterproof_set_config(const touch_pad_waterproof_t *waterproof)
  93. {
  94. touch_ll_waterproof_set_guard_pad(waterproof->guard_ring_pad);
  95. touch_ll_waterproof_set_sheild_driver(waterproof->shield_driver);
  96. }
  97. void touch_hal_waterproof_get_config(touch_pad_waterproof_t *waterproof)
  98. {
  99. touch_ll_waterproof_get_guard_pad(&waterproof->guard_ring_pad);
  100. touch_ll_waterproof_get_sheild_driver(&waterproof->shield_driver);
  101. }
  102. void touch_hal_waterproof_enable(void)
  103. {
  104. touch_ll_clear_channel_mask(1U << SOC_TOUCH_SHIELD_CHANNEL);
  105. touch_ll_waterproof_enable();
  106. }
  107. bool touch_hal_enable_proximity(touch_pad_t touch_num, bool enabled)
  108. {
  109. int i = 0;
  110. touch_pad_t ch_num[SOC_TOUCH_PROXIMITY_CHANNEL_NUM] = {0};
  111. touch_ll_proximity_get_channel_num(ch_num);
  112. if (enabled) {
  113. for (i = 0; i < SOC_TOUCH_PROXIMITY_CHANNEL_NUM; i++) {
  114. if (ch_num[i] == TOUCH_PAD_NUM0 || ch_num[i] >= TOUCH_PAD_MAX || ch_num[i] == touch_num) {
  115. ch_num[i] = touch_num;
  116. break;
  117. }
  118. }
  119. if (i == SOC_TOUCH_PROXIMITY_CHANNEL_NUM) {
  120. return false;
  121. }
  122. } else {
  123. for (i = 0; i < SOC_TOUCH_PROXIMITY_CHANNEL_NUM; i++) {
  124. if (ch_num[i] == touch_num) {
  125. ch_num[i] = TOUCH_PAD_NUM0;
  126. break;
  127. }
  128. }
  129. }
  130. touch_ll_proximity_set_channel_num(ch_num);
  131. return true;
  132. }
  133. void touch_hal_sleep_channel_enable(touch_pad_t pad_num, bool enable)
  134. {
  135. if (enable) {
  136. touch_ll_sleep_set_channel_num(pad_num);
  137. touch_ll_sleep_set_threshold(SOC_TOUCH_PAD_THRESHOLD_MAX);
  138. touch_ll_sleep_reset_benchmark();
  139. } else {
  140. touch_ll_sleep_set_channel_num(TOUCH_PAD_NUM0);
  141. }
  142. }
  143. void touch_hal_sleep_channel_get_config(touch_pad_sleep_channel_t *slp_config)
  144. {
  145. touch_ll_sleep_get_channel_num(&slp_config->touch_num);
  146. slp_config->en_proximity = touch_ll_sleep_get_approach_status();
  147. }
  148. void touch_hal_sleep_channel_set_work_time(uint16_t sleep_cycle, uint16_t meas_times)
  149. {
  150. s_sleep_cycle = (int)sleep_cycle;
  151. s_meas_times = (int)meas_times;
  152. }
  153. void touch_hal_sleep_channel_get_work_time(uint16_t *sleep_cycle, uint16_t *meas_times)
  154. {
  155. if (s_meas_times < 0) {
  156. touch_ll_get_measure_times(meas_times);
  157. } else {
  158. *meas_times = (uint16_t)s_meas_times;
  159. }
  160. if (s_sleep_cycle < 0) {
  161. touch_ll_get_sleep_time(sleep_cycle);
  162. } else {
  163. *sleep_cycle = (uint16_t)s_sleep_cycle;
  164. }
  165. }