1
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
32
33
37
42
43
44
45
46
47
48
49
50
51
55
56
57
58
59
60
61
62
63
64
65
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
139
140
147
148
149
150
151
152
153
154
155
156
162
163
164
165
170
171
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
209
210
211
212
213
214
215
216
217
218
219
225
226
232
233
/* ... */
#include <stdlib.h>
#include <ctype.h>
#include "sdkconfig.h"
#include "esp_types.h"
#include "esp_log.h"
#include "sys/lock.h"
#include "soc/soc_pins.h"
#include "freertos/FreeRTOS.h"
#include "freertos/semphr.h"
#include "freertos/timers.h"
#include "esp_intr_alloc.h"
#include "driver/rtc_io.h"
#include "driver/touch_pad.h"
#include "esp_private/rtc_ctrl.h"
#include "driver/gpio.h"
#include "hal/touch_sensor_types.h"
#include "hal/touch_sensor_hal.h"17 includes
static const char *TOUCH_TAG = "TOUCH_SENSOR";
#define TOUCH_CHECK(a, str, ret_val) ({ \
if (!(a)) { \
ESP_LOGE(TOUCH_TAG,"%s(%d): %s", __FUNCTION__, __LINE__, str); \
return (ret_val); \
}{...} \
}{...})...
#ifdef CONFIG_IDF_TARGET_ESP32
#define TOUCH_CHANNEL_CHECK(channel) do { \
TOUCH_CHECK(channel < SOC_TOUCH_SENSOR_NUM && channel >= 0, "Touch channel error", ESP_ERR_INVALID_ARG); \
}{...} while (0);...
/* ... */#else
#define TOUCH_CHANNEL_CHECK(channel) do { \
TOUCH_CHECK(channel < SOC_TOUCH_SENSOR_NUM && channel >= 0, "Touch channel error", ESP_ERR_INVALID_ARG); \
TOUCH_CHECK(channel != SOC_TOUCH_DENOISE_CHANNEL, "TOUCH0 is internal denoise channel", ESP_ERR_INVALID_ARG); \
}{...} while (0);...
/* ... */#endif
#define TOUCH_GET_IO_NUM(channel) (touch_sensor_channel_io_map[channel])
_Static_assert(TOUCH_PAD_MAX == SOC_TOUCH_SENSOR_NUM, "Touch sensor channel number not equal to chip capabilities");
extern portMUX_TYPE rtc_spinlock;
#define TOUCH_ENTER_CRITICAL() portENTER_CRITICAL(&rtc_spinlock)
#define TOUCH_EXIT_CRITICAL() portEXIT_CRITICAL(&rtc_spinlock)
esp_err_t touch_pad_isr_deregister(intr_handler_t fn, void *arg)
{
return rtc_isr_deregister(fn, arg);
}{ ... }
esp_err_t touch_pad_set_voltage(touch_high_volt_t refh, touch_low_volt_t refl, touch_volt_atten_t atten)
{
TOUCH_CHECK(((refh < TOUCH_HVOLT_MAX) && (refh >= (int)TOUCH_HVOLT_KEEP)), "touch refh error",
ESP_ERR_INVALID_ARG);
TOUCH_CHECK(((refl < TOUCH_LVOLT_MAX) && (refh >= (int)TOUCH_LVOLT_KEEP)), "touch refl error",
ESP_ERR_INVALID_ARG);
TOUCH_CHECK(((atten < TOUCH_HVOLT_ATTEN_MAX) && (refh >= (int)TOUCH_HVOLT_ATTEN_KEEP)), "touch atten error",
ESP_ERR_INVALID_ARG);
const touch_hal_volt_t volt = {
.refh = refh,
.refl = refl,
.atten = atten,
}{...};
TOUCH_ENTER_CRITICAL();
touch_hal_set_voltage(&volt);
TOUCH_EXIT_CRITICAL();
return ESP_OK;
}{ ... }
esp_err_t touch_pad_get_voltage(touch_high_volt_t *refh, touch_low_volt_t *refl, touch_volt_atten_t *atten)
{
touch_hal_volt_t volt = {0};
TOUCH_ENTER_CRITICAL();
touch_hal_get_voltage(&volt);
TOUCH_EXIT_CRITICAL();
*refh = volt.refh;
*refl = volt.refl;
*atten = volt.atten;
return ESP_OK;
}{ ... }
esp_err_t touch_pad_set_cnt_mode(touch_pad_t touch_num, touch_cnt_slope_t slope, touch_tie_opt_t opt)
{
TOUCH_CHECK(touch_num < SOC_TOUCH_SENSOR_NUM, "Touch channel error", ESP_ERR_INVALID_ARG);
TOUCH_CHECK(slope < TOUCH_PAD_SLOPE_MAX, "touch slope error", ESP_ERR_INVALID_ARG);
TOUCH_CHECK(opt < TOUCH_PAD_TIE_OPT_MAX, "touch opt error", ESP_ERR_INVALID_ARG);
const touch_hal_meas_mode_t meas = {
.slope = slope,
.tie_opt = opt,
}{...};
TOUCH_ENTER_CRITICAL();
touch_hal_set_meas_mode(touch_num, &meas);
TOUCH_EXIT_CRITICAL();
return ESP_OK;
}{ ... }
esp_err_t touch_pad_get_cnt_mode(touch_pad_t touch_num, touch_cnt_slope_t *slope, touch_tie_opt_t *opt)
{
TOUCH_CHECK(touch_num < SOC_TOUCH_SENSOR_NUM, "Touch channel error", ESP_ERR_INVALID_ARG);
touch_hal_meas_mode_t meas = {0};
TOUCH_ENTER_CRITICAL();
touch_hal_get_meas_mode(touch_num, &meas);
TOUCH_EXIT_CRITICAL();
*slope = meas.slope;
*opt = meas.tie_opt;
return ESP_OK;
}{ ... }
esp_err_t touch_pad_io_init(touch_pad_t touch_num)
{
TOUCH_CHANNEL_CHECK(touch_num);
gpio_num_t gpio_num = TOUCH_GET_IO_NUM(touch_num);
rtc_gpio_init(gpio_num);
rtc_gpio_set_direction(gpio_num, RTC_GPIO_MODE_DISABLED);
rtc_gpio_pulldown_dis(gpio_num);
rtc_gpio_pullup_dis(gpio_num);
return ESP_OK;
}{ ... }
esp_err_t touch_pad_fsm_start(void)
{
TOUCH_ENTER_CRITICAL();
touch_hal_start_fsm();
TOUCH_EXIT_CRITICAL();
return ESP_OK;
}{ ... }
esp_err_t touch_pad_fsm_stop(void)
{
TOUCH_ENTER_CRITICAL();
touch_hal_stop_fsm();
TOUCH_EXIT_CRITICAL();
return ESP_OK;
}{ ... }
esp_err_t touch_pad_set_fsm_mode(touch_fsm_mode_t mode)
{
TOUCH_CHECK((mode < TOUCH_FSM_MODE_MAX), "touch fsm mode error", ESP_ERR_INVALID_ARG);
TOUCH_ENTER_CRITICAL();
touch_hal_set_fsm_mode(mode);
TOUCH_EXIT_CRITICAL();
#ifdef CONFIG_IDF_TARGET_ESP32
if (mode == TOUCH_FSM_MODE_TIMER) {
touch_pad_fsm_start();
}{...} else {
touch_pad_fsm_stop();
}{...}
#endif/* ... */
return ESP_OK;
}{ ... }
esp_err_t touch_pad_get_fsm_mode(touch_fsm_mode_t *mode)
{
touch_hal_get_fsm_mode(mode);
return ESP_OK;
}{ ... }
esp_err_t touch_pad_sw_start(void)
{
TOUCH_ENTER_CRITICAL();
touch_hal_start_sw_meas();
TOUCH_EXIT_CRITICAL();
return ESP_OK;
}{ ... }
#ifdef CONFIG_IDF_TARGET_ESP32
esp_err_t touch_pad_set_thresh(touch_pad_t touch_num, uint16_t threshold)
{
TOUCH_CHANNEL_CHECK(touch_num);
TOUCH_ENTER_CRITICAL();
touch_hal_set_threshold(touch_num, threshold);
TOUCH_EXIT_CRITICAL();
return ESP_OK;
}{ ... }
/* ... */#else
esp_err_t touch_pad_set_thresh(touch_pad_t touch_num, uint32_t threshold)
{
TOUCH_CHANNEL_CHECK(touch_num);
TOUCH_CHECK(touch_num != SOC_TOUCH_DENOISE_CHANNEL,
"TOUCH0 is internal denoise channel", ESP_ERR_INVALID_ARG);
TOUCH_ENTER_CRITICAL();
touch_hal_set_threshold(touch_num, threshold);
TOUCH_EXIT_CRITICAL();
return ESP_OK;
}{...}
/* ... */#endif
#ifdef CONFIG_IDF_TARGET_ESP32
esp_err_t touch_pad_get_thresh(touch_pad_t touch_num, uint16_t *threshold)
{
TOUCH_CHANNEL_CHECK(touch_num);
touch_hal_get_threshold(touch_num, threshold);
return ESP_OK;
}{ ... }
/* ... */#else
esp_err_t touch_pad_get_thresh(touch_pad_t touch_num, uint32_t *threshold)
{
TOUCH_CHANNEL_CHECK(touch_num);
TOUCH_CHECK(touch_num != SOC_TOUCH_DENOISE_CHANNEL,
"TOUCH0 is internal denoise channel", ESP_ERR_INVALID_ARG);
touch_hal_get_threshold(touch_num, threshold);
return ESP_OK;
}{...}
/* ... */#endif
esp_err_t touch_pad_get_wakeup_status(touch_pad_t *pad_num)
{
touch_hal_get_wakeup_status(pad_num);
TOUCH_CHANNEL_CHECK(*pad_num);
return ESP_OK;
}{ ... }
uint32_t IRAM_ATTR touch_pad_get_status(void)
{
uint32_t status = 0;
touch_hal_read_trigger_status_mask(&status);
return status;
}{ ... }
esp_err_t IRAM_ATTR touch_pad_clear_status(void)
{
touch_hal_clear_trigger_status_mask();
return ESP_OK;
}{ ... }