1 /*
2  * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #pragma once
8 
9 #include <stdbool.h>
10 #include "esp_attr.h"
11 #include "soc/soc.h"
12 #include "soc/soc_caps.h"
13 #include "sdkconfig.h"
14 
15 /** Touch pad channel */
16 typedef enum {
17     TOUCH_PAD_NUM0 = 0, /*!< Touch pad channel 0 is GPIO4(ESP32) */
18     TOUCH_PAD_NUM1,     /*!< Touch pad channel 1 is GPIO0(ESP32) / GPIO1(ESP32-S2) */
19     TOUCH_PAD_NUM2,     /*!< Touch pad channel 2 is GPIO2(ESP32) / GPIO2(ESP32-S2) */
20     TOUCH_PAD_NUM3,     /*!< Touch pad channel 3 is GPIO15(ESP32) / GPIO3(ESP32-S2) */
21     TOUCH_PAD_NUM4,     /*!< Touch pad channel 4 is GPIO13(ESP32) / GPIO4(ESP32-S2) */
22     TOUCH_PAD_NUM5,     /*!< Touch pad channel 5 is GPIO12(ESP32) / GPIO5(ESP32-S2) */
23     TOUCH_PAD_NUM6,     /*!< Touch pad channel 6 is GPIO14(ESP32) / GPIO6(ESP32-S2) */
24     TOUCH_PAD_NUM7,     /*!< Touch pad channel 7 is GPIO27(ESP32) / GPIO7(ESP32-S2) */
25     TOUCH_PAD_NUM8,     /*!< Touch pad channel 8 is GPIO33(ESP32) / GPIO8(ESP32-S2) */
26     TOUCH_PAD_NUM9,     /*!< Touch pad channel 9 is GPIO32(ESP32) / GPIO9(ESP32-S2) */
27 #if SOC_TOUCH_SENSOR_NUM > 10
28     TOUCH_PAD_NUM10,    /*!< Touch channel 10 is GPIO10(ESP32-S2) */
29     TOUCH_PAD_NUM11,    /*!< Touch channel 11 is GPIO11(ESP32-S2) */
30     TOUCH_PAD_NUM12,    /*!< Touch channel 12 is GPIO12(ESP32-S2) */
31     TOUCH_PAD_NUM13,    /*!< Touch channel 13 is GPIO13(ESP32-S2) */
32     TOUCH_PAD_NUM14,    /*!< Touch channel 14 is GPIO14(ESP32-S2) */
33 #endif
34     TOUCH_PAD_MAX,
35 } touch_pad_t;
36 
37 /** Touch sensor high reference voltage */
38 typedef enum {
39     TOUCH_HVOLT_KEEP = -1, /*!<Touch sensor high reference voltage, no change  */
40     TOUCH_HVOLT_2V4 = 0,   /*!<Touch sensor high reference voltage, 2.4V  */
41     TOUCH_HVOLT_2V5,       /*!<Touch sensor high reference voltage, 2.5V  */
42     TOUCH_HVOLT_2V6,       /*!<Touch sensor high reference voltage, 2.6V  */
43     TOUCH_HVOLT_2V7,       /*!<Touch sensor high reference voltage, 2.7V  */
44     TOUCH_HVOLT_MAX,
45 } touch_high_volt_t;
46 
47 /** Touch sensor low reference voltage */
48 typedef enum {
49     TOUCH_LVOLT_KEEP = -1, /*!<Touch sensor low reference voltage, no change  */
50     TOUCH_LVOLT_0V5 = 0,   /*!<Touch sensor low reference voltage, 0.5V  */
51     TOUCH_LVOLT_0V6,       /*!<Touch sensor low reference voltage, 0.6V  */
52     TOUCH_LVOLT_0V7,       /*!<Touch sensor low reference voltage, 0.7V  */
53     TOUCH_LVOLT_0V8,       /*!<Touch sensor low reference voltage, 0.8V  */
54     TOUCH_LVOLT_MAX,
55 } touch_low_volt_t;
56 
57 /** Touch sensor high reference voltage attenuation */
58 typedef enum {
59     TOUCH_HVOLT_ATTEN_KEEP = -1,  /*!<Touch sensor high reference voltage attenuation, no change  */
60     TOUCH_HVOLT_ATTEN_1V5 = 0,    /*!<Touch sensor high reference voltage attenuation, 1.5V attenuation  */
61     TOUCH_HVOLT_ATTEN_1V,         /*!<Touch sensor high reference voltage attenuation, 1.0V attenuation  */
62     TOUCH_HVOLT_ATTEN_0V5,        /*!<Touch sensor high reference voltage attenuation, 0.5V attenuation  */
63     TOUCH_HVOLT_ATTEN_0V,         /*!<Touch sensor high reference voltage attenuation,   0V attenuation  */
64     TOUCH_HVOLT_ATTEN_MAX,
65 } touch_volt_atten_t;
66 
67 /** Touch sensor charge/discharge speed */
68 typedef enum {
69     TOUCH_PAD_SLOPE_0 = 0,       /*!<Touch sensor charge / discharge speed, always zero  */
70     TOUCH_PAD_SLOPE_1 = 1,       /*!<Touch sensor charge / discharge speed, slowest  */
71     TOUCH_PAD_SLOPE_2 = 2,       /*!<Touch sensor charge / discharge speed */
72     TOUCH_PAD_SLOPE_3 = 3,       /*!<Touch sensor charge / discharge speed  */
73     TOUCH_PAD_SLOPE_4 = 4,       /*!<Touch sensor charge / discharge speed  */
74     TOUCH_PAD_SLOPE_5 = 5,       /*!<Touch sensor charge / discharge speed  */
75     TOUCH_PAD_SLOPE_6 = 6,       /*!<Touch sensor charge / discharge speed  */
76     TOUCH_PAD_SLOPE_7 = 7,       /*!<Touch sensor charge / discharge speed, fast  */
77     TOUCH_PAD_SLOPE_MAX,
78 } touch_cnt_slope_t;
79 
80 /** Touch sensor initial charge level */
81 typedef enum {
82     TOUCH_PAD_TIE_OPT_LOW = 0,    /*!<Initial level of charging voltage, low level */
83     TOUCH_PAD_TIE_OPT_HIGH = 1,   /*!<Initial level of charging voltage, high level */
84     TOUCH_PAD_TIE_OPT_MAX,
85 } touch_tie_opt_t;
86 
87 /** Touch sensor FSM mode */
88 typedef enum {
89     TOUCH_FSM_MODE_TIMER = 0,   /*!<To start touch FSM by timer */
90     TOUCH_FSM_MODE_SW,          /*!<To start touch FSM by software trigger */
91     TOUCH_FSM_MODE_MAX,
92 } touch_fsm_mode_t;
93 
94 /**** ESP32 Only *****/
95 
96 typedef enum {
97     TOUCH_TRIGGER_BELOW = 0,   /*!<Touch interrupt will happen if counter value is less than threshold.*/
98     TOUCH_TRIGGER_ABOVE = 1,   /*!<Touch interrupt will happen if counter value is larger than threshold.*/
99     TOUCH_TRIGGER_MAX,
100 } touch_trigger_mode_t;
101 
102 typedef enum {
103     TOUCH_TRIGGER_SOURCE_BOTH = 0,  /*!< wakeup interrupt is generated if both SET1 and SET2 are "touched"*/
104     TOUCH_TRIGGER_SOURCE_SET1 = 1,  /*!< wakeup interrupt is generated if SET1 is "touched"*/
105     TOUCH_TRIGGER_SOURCE_MAX,
106 } touch_trigger_src_t;
107 
108 /********************************/
109 #define TOUCH_PAD_BIT_MASK_ALL              ((1<<SOC_TOUCH_SENSOR_NUM)-1)
110 #define TOUCH_PAD_SLOPE_DEFAULT             (TOUCH_PAD_SLOPE_7)
111 #define TOUCH_PAD_TIE_OPT_DEFAULT           (TOUCH_PAD_TIE_OPT_LOW)
112 #define TOUCH_PAD_BIT_MASK_MAX              (TOUCH_PAD_BIT_MASK_ALL)
113 #define TOUCH_PAD_HIGH_VOLTAGE_THRESHOLD    (TOUCH_HVOLT_2V7)
114 #define TOUCH_PAD_LOW_VOLTAGE_THRESHOLD     (TOUCH_LVOLT_0V5)
115 #define TOUCH_PAD_ATTEN_VOLTAGE_THRESHOLD   (TOUCH_HVOLT_ATTEN_0V5)
116 #define TOUCH_PAD_IDLE_CH_CONNECT_DEFAULT   (TOUCH_PAD_CONN_GND)
117 #define TOUCH_PAD_THRESHOLD_MAX             (SOC_TOUCH_PAD_THRESHOLD_MAX) /*!<If set touch threshold max value, The touch sensor can't be in touched status */
118 
119 #ifdef CONFIG_IDF_TARGET_ESP32
120 
121 #define TOUCH_PAD_SLEEP_CYCLE_DEFAULT   (0x1000)  /*!<The timer frequency is RTC_SLOW_CLK (can be 150k or 32k depending on the options), max value is 0xffff */
122 #define TOUCH_PAD_MEASURE_CYCLE_DEFAULT (0x7fff)  /*!<The timer frequency is 8Mhz, the max value is 0x7fff */
123 #define TOUCH_FSM_MODE_DEFAULT          (TOUCH_FSM_MODE_SW)  /*!<The touch FSM my be started by the software or timer */
124 #define TOUCH_TRIGGER_MODE_DEFAULT      (TOUCH_TRIGGER_BELOW)   /*!<Interrupts can be triggered if sensor value gets below or above threshold */
125 #define TOUCH_TRIGGER_SOURCE_DEFAULT    (TOUCH_TRIGGER_SOURCE_SET1)  /*!<The wakeup trigger source can be SET1 or both SET1 and SET2 */
126 
127 #endif // CONFIG_IDF_TARGET ESP32
128 
129 #if !CONFIG_IDF_TARGET_ESP32
130 /**
131  * Excessive total time will slow down the touch response.
132  * Too small measurement time will not be sampled enough, resulting in inaccurate measurements.
133  *
134  * @note The greater the duty cycle of the measurement time, the more system power is consumed.
135  */
136 #define TOUCH_PAD_SLEEP_CYCLE_DEFAULT   (0xf)   /*!<The number of sleep cycle in each measure process of touch channels.
137                                                     The timer frequency is RTC_SLOW_CLK (can be 150k or 32k depending on the options).
138                                                     Range: 0 ~ 0xffff */
139 #define TOUCH_PAD_MEASURE_CYCLE_DEFAULT (500)   /*!<The times of charge and discharge in each measure process of touch channels.
140                                                     The timer frequency is 8Mhz.
141                                                     Recommended typical value: Modify this value to make the measurement time around 1ms.
142                                                     Range: 0 ~ 0xffff */
143 
144 typedef enum {
145     TOUCH_PAD_INTR_MASK_DONE = BIT(0),      /*!<Measurement done for one of the enabled channels. */
146     TOUCH_PAD_INTR_MASK_ACTIVE = BIT(1),    /*!<Active for one of the enabled channels. */
147     TOUCH_PAD_INTR_MASK_INACTIVE = BIT(2),  /*!<Inactive for one of the enabled channels. */
148     TOUCH_PAD_INTR_MASK_SCAN_DONE = BIT(3), /*!<Measurement done for all the enabled channels. */
149     TOUCH_PAD_INTR_MASK_TIMEOUT = BIT(4),   /*!<Timeout for one of the enabled channels. */
150 #if SOC_TOUCH_PROXIMITY_MEAS_DONE_SUPPORTED
151     TOUCH_PAD_INTR_MASK_PROXI_MEAS_DONE = BIT(5),   /*!<For proximity sensor, when the number of measurements reaches the set count of measurements, an interrupt will be generated. */
152     TOUCH_PAD_INTR_MASK_MAX
153 #define TOUCH_PAD_INTR_MASK_ALL (TOUCH_PAD_INTR_MASK_TIMEOUT    \
154                                 | TOUCH_PAD_INTR_MASK_SCAN_DONE \
155                                 | TOUCH_PAD_INTR_MASK_INACTIVE  \
156                                 | TOUCH_PAD_INTR_MASK_ACTIVE    \
157                                 | TOUCH_PAD_INTR_MASK_DONE      \
158                                 | TOUCH_PAD_INTR_MASK_PROXI_MEAS_DONE) /*!<All touch interrupt type enable. */
159 #else
160     TOUCH_PAD_INTR_MASK_MAX
161 #define TOUCH_PAD_INTR_MASK_ALL (TOUCH_PAD_INTR_MASK_TIMEOUT    \
162                                 | TOUCH_PAD_INTR_MASK_SCAN_DONE \
163                                 | TOUCH_PAD_INTR_MASK_INACTIVE  \
164                                 | TOUCH_PAD_INTR_MASK_ACTIVE    \
165                                 | TOUCH_PAD_INTR_MASK_DONE) /*!<All touch interrupt type enable. */
166 #endif
167 } touch_pad_intr_mask_t;
168 FLAG_ATTR(touch_pad_intr_mask_t)
169 
170 typedef enum {
171     TOUCH_PAD_DENOISE_BIT12 = 0,    /*!<Denoise range is 12bit */
172     TOUCH_PAD_DENOISE_BIT10 = 1,    /*!<Denoise range is 10bit */
173     TOUCH_PAD_DENOISE_BIT8 = 2,     /*!<Denoise range is 8bit */
174     TOUCH_PAD_DENOISE_BIT4 = 3,     /*!<Denoise range is 4bit */
175     TOUCH_PAD_DENOISE_MAX
176 } touch_pad_denoise_grade_t;
177 
178 typedef enum {
179     TOUCH_PAD_DENOISE_CAP_L0 = 0,   /*!<Denoise channel internal reference capacitance is 5pf */
180     TOUCH_PAD_DENOISE_CAP_L1 = 1,   /*!<Denoise channel internal reference capacitance is 6.4pf */
181     TOUCH_PAD_DENOISE_CAP_L2 = 2,   /*!<Denoise channel internal reference capacitance is 7.8pf */
182     TOUCH_PAD_DENOISE_CAP_L3 = 3,   /*!<Denoise channel internal reference capacitance is 9.2pf */
183     TOUCH_PAD_DENOISE_CAP_L4 = 4,   /*!<Denoise channel internal reference capacitance is 10.6pf */
184     TOUCH_PAD_DENOISE_CAP_L5 = 5,   /*!<Denoise channel internal reference capacitance is 12.0pf */
185     TOUCH_PAD_DENOISE_CAP_L6 = 6,   /*!<Denoise channel internal reference capacitance is 13.4pf */
186     TOUCH_PAD_DENOISE_CAP_L7 = 7,   /*!<Denoise channel internal reference capacitance is 14.8pf */
187     TOUCH_PAD_DENOISE_CAP_MAX = 8
188 } touch_pad_denoise_cap_t;
189 
190 /** Touch sensor denoise configuration */
191 typedef struct touch_pad_denoise {
192     touch_pad_denoise_grade_t grade;    /*!<Select denoise range of denoise channel.
193                                             Determined by measuring the noise amplitude of the denoise channel. */
194     touch_pad_denoise_cap_t cap_level;  /*!<Select internal reference capacitance of denoise channel.
195                                             Ensure that the denoise readings are closest to the readings of the channel being measured.
196                                             Use `touch_pad_denoise_read_data` to get the reading of denoise channel.
197                                             The equivalent capacitance of the shielded channel can be calculated
198                                             from the reading of denoise channel. */
199 } touch_pad_denoise_t;
200 
201 /** Touch sensor shield channel drive capability level */
202 typedef enum {
203     TOUCH_PAD_SHIELD_DRV_L0 = 0,/*!<The max equivalent capacitance in shield channel is 40pf */
204     TOUCH_PAD_SHIELD_DRV_L1,    /*!<The max equivalent capacitance in shield channel is 80pf */
205     TOUCH_PAD_SHIELD_DRV_L2,    /*!<The max equivalent capacitance in shield channel is 120pf */
206     TOUCH_PAD_SHIELD_DRV_L3,    /*!<The max equivalent capacitance in shield channel is 160pf */
207     TOUCH_PAD_SHIELD_DRV_L4,    /*!<The max equivalent capacitance in shield channel is 200pf */
208     TOUCH_PAD_SHIELD_DRV_L5,    /*!<The max equivalent capacitance in shield channel is 240pf */
209     TOUCH_PAD_SHIELD_DRV_L6,    /*!<The max equivalent capacitance in shield channel is 280pf */
210     TOUCH_PAD_SHIELD_DRV_L7,    /*!<The max equivalent capacitance in shield channel is 320pf */
211     TOUCH_PAD_SHIELD_DRV_MAX
212 } touch_pad_shield_driver_t;
213 
214 /** Touch sensor waterproof configuration */
215 typedef struct touch_pad_waterproof {
216     touch_pad_t guard_ring_pad;             /*!<Waterproof. Select touch channel use for guard pad.
217                                                 Guard pad is used to detect the large area of water covering the touch panel. */
218     touch_pad_shield_driver_t shield_driver;/*!<Waterproof. Shield channel drive capability configuration.
219                                                 Shield pad is used to shield the influence of water droplets covering the touch panel.
220                                                 When the waterproof function is enabled, Touch14 is set as shield channel by default.
221                                                 The larger the parasitic capacitance on the shielding channel, the higher the drive capability needs to be set.
222                                                 The equivalent capacitance of the shield channel can be estimated through the reading value of the denoise channel(Touch0).*/
223 } touch_pad_waterproof_t;
224 
225 /** Touch sensor proximity detection configuration */
226 #define TOUCH_PROXIMITY_MEAS_NUM_MAX (0xFF)
227 
228 /** Touch channel idle state configuration */
229 typedef enum {
230     TOUCH_PAD_CONN_HIGHZ = 0,   /*!<Idle status of touch channel is high resistance state */
231     TOUCH_PAD_CONN_GND = 1,     /*!<Idle status of touch channel is ground connection */
232     TOUCH_PAD_CONN_MAX
233 } touch_pad_conn_type_t;
234 
235 /**
236  * @brief Touch channel IIR filter coefficient configuration.
237  * @note On ESP32S2. There is an error in the IIR calculation. The magnitude of the error is twice the filter coefficient.
238  *       So please select a smaller filter coefficient on the basis of meeting the filtering requirements.
239  *       Recommended filter coefficient selection `IIR_16`.
240  */
241 typedef enum {
242     TOUCH_PAD_FILTER_IIR_4 = 0, /*!<The filter mode is first-order IIR filter. The coefficient is 4. */
243     TOUCH_PAD_FILTER_IIR_8,     /*!<The filter mode is first-order IIR filter. The coefficient is 8. */
244     TOUCH_PAD_FILTER_IIR_16,    /*!<The filter mode is first-order IIR filter. The coefficient is 16 (Typical value). */
245     TOUCH_PAD_FILTER_IIR_32,    /*!<The filter mode is first-order IIR filter. The coefficient is 32. */
246     TOUCH_PAD_FILTER_IIR_64,    /*!<The filter mode is first-order IIR filter. The coefficient is 64. */
247     TOUCH_PAD_FILTER_IIR_128,   /*!<The filter mode is first-order IIR filter. The coefficient is 128. */
248     TOUCH_PAD_FILTER_IIR_256,   /*!<The filter mode is first-order IIR filter. The coefficient is 256. */
249     TOUCH_PAD_FILTER_JITTER,    /*!<The filter mode is jitter filter */
250     TOUCH_PAD_FILTER_MAX
251 } touch_filter_mode_t;
252 
253 /**
254  * @brief Level of filter applied on the original data against large noise interference.
255  * @note On ESP32S2. There is an error in the IIR calculation. The magnitude of the error is twice the filter coefficient.
256  *       So please select a smaller filter coefficient on the basis of meeting the filtering requirements.
257  *       Recommended filter coefficient selection `IIR_2`.
258  */
259 typedef enum {
260     TOUCH_PAD_SMOOTH_OFF   = 0, /*!<No filtering of raw data. */
261     TOUCH_PAD_SMOOTH_IIR_2 = 1, /*!<Filter the raw data. The coefficient is 2 (Typical value). */
262     TOUCH_PAD_SMOOTH_IIR_4 = 2, /*!<Filter the raw data. The coefficient is 4. */
263     TOUCH_PAD_SMOOTH_IIR_8 = 3, /*!<Filter the raw data. The coefficient is 8. */
264     TOUCH_PAD_SMOOTH_MAX,
265 } touch_smooth_mode_t;
266 
267 /** Touch sensor filter configuration */
268 typedef struct touch_filter_config {
269     touch_filter_mode_t mode;   /*!<Set filter mode. The input of the filter is the raw value of touch reading,
270                                     and the output of the filter is involved in the judgment of the touch state. */
271     uint32_t debounce_cnt;      /*!<Set debounce count, such as `n`. If the measured values continue to exceed
272                                     the threshold for `n+1` times, the touch sensor state changes.
273                                     Range: 0 ~ 7 */
274     uint32_t noise_thr;         /*!<Noise threshold coefficient. Higher = More noise resistance.
275                                     The actual noise should be less than (noise coefficient * touch threshold).
276                                     Range: 0 ~ 3. The coefficient is 0: 4/8;  1: 3/8;   2: 2/8;   3: 1; */
277     uint32_t jitter_step;       /*!<Set jitter filter step size. Range: 0 ~ 15 */
278     touch_smooth_mode_t smh_lvl;/*!<Level of filter applied on the original data against large noise interference. */
279 #define TOUCH_DEBOUNCE_CNT_MAX      (7)
280 #define TOUCH_NOISE_THR_MAX         (3)
281 #define TOUCH_JITTER_STEP_MAX       (15)
282 } touch_filter_config_t;
283 
284 /** Touch sensor channel sleep configuration */
285 typedef struct {
286     touch_pad_t touch_num;          /*!<Set touch channel number for sleep pad.
287                                         Only one touch sensor channel is supported in deep sleep mode.
288                                         If clear the sleep channel, point this pad to `TOUCH_PAD_NUM0`  */
289     bool en_proximity;              /*!<enable proximity function for sleep pad */
290 } touch_pad_sleep_channel_t;
291 
292 #endif // !CONFIG_IDF_TARGET_ESP32
293