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