Skip to content

Commit c1fdd45

Browse files
committed
Merge branch 'bugfix/fix_touch_pad_can_not_wake_up' into 'master'
bugfix(touch pad): modify deep-sleep example and add note for sleep api See merge request idf/esp-idf!2553
2 parents e3a3130 + 9a0d57b commit c1fdd45

File tree

6 files changed

+71
-31
lines changed

6 files changed

+71
-31
lines changed

components/driver/include/driver/touch_pad.h

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,12 @@ esp_err_t touch_pad_deinit();
130130

131131
/**
132132
* @brief Configure touch pad interrupt threshold.
133+
*
134+
* @note If FSM mode is set to TOUCH_FSM_MODE_TIMER, this function will be blocked for one measurement cycle and wait for data to be valid.
135+
*
133136
* @param touch_num touch pad index
134137
* @param threshold interrupt threshold,
138+
*
135139
* @return
136140
* - ESP_OK Success
137141
* - ESP_ERR_INVALID_ARG if argument wrong
@@ -144,46 +148,53 @@ esp_err_t touch_pad_config(touch_pad_t touch_num, uint16_t threshold);
144148
* Each touch sensor has a counter to count the number of charge/discharge cycles.
145149
* When the pad is not 'touched', we can get a number of the counter.
146150
* When the pad is 'touched', the value in counter will get smaller because of the larger equivalent capacitance.
147-
* @note This API requests hardware measurement once. If IIR filter mode is enabled,,
151+
*
152+
* @note This API requests hardware measurement once. If IIR filter mode is enabled,
148153
* please use 'touch_pad_read_raw_data' interface instead.
154+
*
149155
* @param touch_num touch pad index
150156
* @param touch_value pointer to accept touch sensor value
157+
*
151158
* @return
152159
* - ESP_OK Success
153-
* - ESP_ERR_INVALID_ARG Touch pad error
160+
* - ESP_ERR_INVALID_ARG Touch pad parameter error
161+
* - ESP_ERR_INVALID_STATE This touch pad hardware connection is error, the value of "touch_value" is 0.
154162
* - ESP_FAIL Touch pad not initialized
155163
*/
156164
esp_err_t touch_pad_read(touch_pad_t touch_num, uint16_t * touch_value);
157165

158166
/**
159167
* @brief get filtered touch sensor counter value by IIR filter.
168+
*
160169
* @note touch_pad_filter_start has to be called before calling touch_pad_read_filtered.
161170
* This function can be called from ISR
162171
*
163172
* @param touch_num touch pad index
164173
* @param touch_value pointer to accept touch sensor value
174+
*
165175
* @return
166176
* - ESP_OK Success
167-
* - ESP_ERR_INVALID_ARG Touch pad error
168-
* - ESP_ERR_INVALID_STATE Touch pad not initialized
177+
* - ESP_ERR_INVALID_ARG Touch pad parameter error
178+
* - ESP_ERR_INVALID_STATE This touch pad hardware connection is error, the value of "touch_value" is 0.
169179
* - ESP_FAIL Touch pad not initialized
170180
*/
171181
esp_err_t touch_pad_read_filtered(touch_pad_t touch_num, uint16_t *touch_value);
172182

173183
/**
174184
* @brief get raw data (touch sensor counter value) from IIR filter process.
175185
* Need not request hardware measurements.
186+
*
176187
* @note touch_pad_filter_start has to be called before calling touch_pad_read_raw_data.
177188
* This function can be called from ISR
178189
*
179190
* @param touch_num touch pad index
180191
* @param touch_value pointer to accept touch sensor value
181192
*
182193
* @return
183-
* - ESP_OK Success
184-
* - ESP_ERR_INVALID_ARG Touch pad error
185-
* - ESP_ERR_INVALID_STATE Touch pad not initialized
186-
* - ESP_FAIL Touch pad not initialized
194+
* - ESP_OK Success
195+
* - ESP_ERR_INVALID_ARG Touch pad parameter error
196+
* - ESP_ERR_INVALID_STATE This touch pad hardware connection is error, the value of "touch_value" is 0.
197+
* - ESP_FAIL Touch pad not initialized
187198
*/
188199
esp_err_t touch_pad_read_raw_data(touch_pad_t touch_num, uint16_t *touch_value);
189200

@@ -508,8 +519,6 @@ esp_err_t touch_pad_get_filter_period(uint32_t* p_period_ms);
508519
* when detecting slight change of capacitance.
509520
* Need to call touch_pad_filter_start before all touch filter APIs
510521
*
511-
* If filter is not initialized, this API will initialize the filter with given period.
512-
* If filter is already initialized, this API will update the filter period.
513522
* @note This filter uses FreeRTOS timer, which is dispatched from a task with
514523
* priority 1 by default on CPU 0. So if some application task with higher priority
515524
* takes a lot of CPU0 time, then the quality of data obtained from this filter will be affected.

components/driver/rtc_module.c

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "soc/rtc_cntl_struct.h"
2323
#include "soc/syscon_reg.h"
2424
#include "soc/syscon_struct.h"
25+
#include "soc/rtc.h"
2526
#include "rtc_io.h"
2627
#include "touch_pad.h"
2728
#include "adc.h"
@@ -820,16 +821,28 @@ esp_err_t touch_pad_config(touch_pad_t touch_num, uint16_t threshold)
820821
{
821822
RTC_MODULE_CHECK(rtc_touch_mux != NULL, "Touch pad not initialized", ESP_FAIL);
822823
RTC_MODULE_CHECK(touch_num < TOUCH_PAD_MAX, "Touch_Pad Num Err", ESP_ERR_INVALID_ARG);
823-
s_touch_pad_init_bit |= (1 << touch_num);
824+
touch_fsm_mode_t mode;
824825
touch_pad_set_thresh(touch_num, threshold);
825826
touch_pad_io_init(touch_num);
826827
touch_pad_set_cnt_mode(touch_num, TOUCH_PAD_SLOPE_7, TOUCH_PAD_TIE_OPT_LOW);
827-
touch_fsm_mode_t mode;
828828
touch_pad_get_fsm_mode(&mode);
829829
if (TOUCH_FSM_MODE_SW == mode) {
830830
touch_pad_clear_group_mask((1 << touch_num), (1 << touch_num), (1 << touch_num));
831+
s_touch_pad_init_bit |= (1 << touch_num);
831832
} else if (TOUCH_FSM_MODE_TIMER == mode){
833+
uint16_t sleep_time = 0;
834+
uint16_t meas_cycle = 0;
835+
uint32_t wait_time_ms = 0;
836+
uint32_t wait_tick = 0;
837+
uint32_t rtc_clk = rtc_clk_slow_freq_get_hz();
832838
touch_pad_set_group_mask((1 << touch_num), (1 << touch_num), (1 << touch_num));
839+
touch_pad_get_meas_time(&sleep_time, &meas_cycle);
840+
//If the FSM mode is 'TOUCH_FSM_MODE_TIMER', The data will be ready after one measurement cycle
841+
//after this function is executed, otherwise, the "touch_value" by "touch_pad_read" is 0.
842+
wait_time_ms = sleep_time/(rtc_clk/1000) + meas_cycle/(RTC_FAST_CLK_FREQ_APPROX/1000);
843+
wait_tick = wait_time_ms/portTICK_RATE_MS;
844+
vTaskDelay(wait_tick ? wait_tick : 1);
845+
s_touch_pad_init_bit |= (1 << touch_num);
833846
} else {
834847
return ESP_FAIL;
835848
}
@@ -846,11 +859,11 @@ esp_err_t touch_pad_init()
846859
}
847860
touch_pad_intr_disable();
848861
touch_pad_clear_group_mask(TOUCH_PAD_BIT_MASK_MAX, TOUCH_PAD_BIT_MASK_MAX, TOUCH_PAD_BIT_MASK_MAX);
849-
touch_pad_set_fsm_mode(TOUCH_FSM_MODE_DEFAULT);
850862
touch_pad_set_trigger_mode(TOUCH_TRIGGER_MODE_DEFAULT);
851863
touch_pad_set_trigger_source(TOUCH_TRIGGER_SOURCE_DEFAULT);
852864
touch_pad_clear_status();
853865
touch_pad_set_meas_time(TOUCH_PAD_SLEEP_CYCLE_DEFAULT, TOUCH_PAD_MEASURE_CYCLE_DEFAULT);
866+
touch_pad_set_fsm_mode(TOUCH_FSM_MODE_DEFAULT);
854867
return ESP_OK;
855868
}
856869

@@ -890,6 +903,9 @@ static esp_err_t _touch_pad_read(touch_pad_t touch_num, uint16_t *touch_value, t
890903
} else {
891904
res = ESP_FAIL;
892905
}
906+
if (*touch_value == 0) {
907+
res = ESP_ERR_INVALID_STATE;
908+
}
893909
return res;
894910
}
895911

@@ -913,8 +929,11 @@ IRAM_ATTR esp_err_t touch_pad_read_raw_data(touch_pad_t touch_num, uint16_t *tou
913929
RTC_MODULE_CHECK(rtc_touch_mux != NULL, "Touch pad not initialized", ESP_FAIL);
914930
RTC_MODULE_CHECK(touch_num < TOUCH_PAD_MAX, "Touch_Pad Num Err", ESP_ERR_INVALID_ARG);
915931
RTC_MODULE_CHECK(touch_value != NULL, "touch_value", ESP_ERR_INVALID_ARG);
916-
RTC_MODULE_CHECK(s_touch_pad_filter != NULL, "Touch pad filter not initialized", ESP_ERR_INVALID_STATE);
932+
RTC_MODULE_CHECK(s_touch_pad_filter != NULL, "Touch pad filter not initialized", ESP_FAIL);
917933
*touch_value = s_touch_pad_filter->raw_val[touch_num];
934+
if (*touch_value == 0) {
935+
return ESP_ERR_INVALID_STATE;
936+
}
918937
return ESP_OK;
919938
}
920939

@@ -923,8 +942,11 @@ IRAM_ATTR esp_err_t touch_pad_read_filtered(touch_pad_t touch_num, uint16_t *tou
923942
RTC_MODULE_CHECK(rtc_touch_mux != NULL, "Touch pad not initialized", ESP_FAIL);
924943
RTC_MODULE_CHECK(touch_num < TOUCH_PAD_MAX, "Touch_Pad Num Err", ESP_ERR_INVALID_ARG);
925944
RTC_MODULE_CHECK(touch_value != NULL, "touch_value", ESP_ERR_INVALID_ARG);
926-
RTC_MODULE_CHECK(s_touch_pad_filter != NULL, "Touch pad filter not initialized", ESP_ERR_INVALID_STATE);
945+
RTC_MODULE_CHECK(s_touch_pad_filter != NULL, "Touch pad filter not initialized", ESP_FAIL);
927946
*touch_value = (s_touch_pad_filter->filtered_val[touch_num]);
947+
if (*touch_value == 0) {
948+
return ESP_ERR_INVALID_STATE;
949+
}
928950
return ESP_OK;
929951
}
930952

@@ -984,13 +1006,10 @@ esp_err_t touch_pad_filter_start(uint32_t filter_period_ms)
9841006
if (s_touch_pad_filter->timer == NULL) {
9851007
ret = ESP_ERR_NO_MEM;
9861008
}
987-
xTimerStart(s_touch_pad_filter->timer, portMAX_DELAY);
988-
} else {
989-
xTimerChangePeriod(s_touch_pad_filter->timer, filter_period_ms / portTICK_PERIOD_MS, portMAX_DELAY);
9901009
s_touch_pad_filter->period = filter_period_ms;
991-
xTimerStart(s_touch_pad_filter->timer, portMAX_DELAY);
9921010
}
9931011
xSemaphoreGive(rtc_touch_mux);
1012+
touch_pad_filter_cb(NULL);
9941013
return ret;
9951014
}
9961015

components/esp32/include/esp_sleep.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,9 @@ esp_err_t esp_sleep_enable_timer_wakeup(uint64_t time_in_us);
113113
* to be powered on (ESP_PD_OPTION_ON) or when ext0 wakeup
114114
* source is used.
115115
*
116+
* @note The FSM mode of the touch button should be configured
117+
* as the timer trigger mode.
118+
*
116119
* @return
117120
* - ESP_OK on success
118121
* - ESP_ERR_INVALID_STATE if wakeup triggers conflict

examples/peripherals/touch_pad_interrupt/main/tp_interrupt_main.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,11 @@ static uint32_t s_pad_init_val[TOUCH_PAD_MAX];
3535
static void tp_example_set_thresholds(void)
3636
{
3737
uint16_t touch_value;
38-
//delay some time in order to make the filter work and get a initial value
39-
vTaskDelay(500/portTICK_PERIOD_MS);
40-
4138
for (int i = 0; i<TOUCH_PAD_MAX; i++) {
4239
//read filtered value
4340
touch_pad_read_filtered(i, &touch_value);
4441
s_pad_init_val[i] = touch_value;
45-
ESP_LOGI(TAG, "test init touch val: %d", touch_value);
42+
ESP_LOGI(TAG, "test init: touch pad [%d] val is %d", i, touch_value);
4643
//set interrupt threshold.
4744
ESP_ERROR_CHECK(touch_pad_set_thresh(i, touch_value * 2 / 3));
4845

@@ -159,10 +156,10 @@ void app_main()
159156
// For most usage scenarios, we recommend using the following combination:
160157
// the high reference valtage will be 2.7V - 1V = 1.7V, The low reference voltage will be 0.5V.
161158
touch_pad_set_voltage(TOUCH_HVOLT_2V7, TOUCH_LVOLT_0V5, TOUCH_HVOLT_ATTEN_1V);
162-
// Initialize and start a software filter to detect slight change of capacitance.
163-
touch_pad_filter_start(TOUCHPAD_FILTER_TOUCH_PERIOD);
164159
// Init touch pad IO
165160
tp_example_touch_pad_init();
161+
// Initialize and start a software filter to detect slight change of capacitance.
162+
touch_pad_filter_start(TOUCHPAD_FILTER_TOUCH_PERIOD);
166163
// Set thresh hold
167164
tp_example_set_thresholds();
168165
// Register touch interrupt ISR

examples/peripherals/touch_pad_read/main/tp_read_main.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@ static void tp_example_read_task(void *pvParameter)
3232
for (int i = 0; i < TOUCH_PAD_MAX; i++) {
3333
#if TOUCH_FILTER_MODE_EN
3434
// If open the filter mode, please use this API to get the touch pad count.
35-
ESP_ERROR_CHECK(touch_pad_read_raw_data(i, &touch_value));
36-
ESP_ERROR_CHECK(touch_pad_read_filtered(i, &touch_filter_value));
35+
touch_pad_read_raw_data(i, &touch_value);
36+
touch_pad_read_filtered(i, &touch_filter_value);
3737
printf("T%d:[%4d,%4d] ", i, touch_value, touch_filter_value);
3838
#else
39-
ESP_ERROR_CHECK(touch_pad_read(i, &touch_value));
39+
touch_pad_read(i, &touch_value);
4040
printf("T%d:[%4d] ", i, touch_value);
4141
#endif
4242
}
@@ -62,10 +62,10 @@ void app_main()
6262
// The low reference voltage will be 0.5
6363
// The larger the range, the larger the pulse count value.
6464
touch_pad_set_voltage(TOUCH_HVOLT_2V7, TOUCH_LVOLT_0V5, TOUCH_HVOLT_ATTEN_1V);
65+
tp_example_touch_pad_init();
6566
#if TOUCH_FILTER_MODE_EN
6667
touch_pad_filter_start(TOUCHPAD_FILTER_TOUCH_PERIOD);
6768
#endif
68-
tp_example_touch_pad_init();
6969
// Start task to read values sensed by pads
7070
xTaskCreate(&tp_example_read_task, "touch_pad_read_task", 2048, NULL, 5, NULL);
7171
}

examples/system/deep_sleep/main/deep_sleep_example_main.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ static inline void ulp_data_write(size_t offset, uint16_t value)
7373
#endif // CONFIG_ENABLE_ULP_TEMPERATURE_WAKEUP
7474

7575
#ifdef CONFIG_ENABLE_TOUCH_WAKEUP
76+
#define TOUCH_THRESH_NO_USE 0
7677
static void calibrate_touch_pad(touch_pad_t pad);
7778
#endif
7879

@@ -145,11 +146,24 @@ void app_main()
145146
esp_sleep_enable_ext1_wakeup(ext_wakeup_pin_1_mask | ext_wakeup_pin_2_mask, ESP_EXT1_WAKEUP_ANY_HIGH);
146147

147148
#ifdef CONFIG_ENABLE_TOUCH_WAKEUP
149+
// Initialize touch pad peripheral.
150+
// The default fsm mode is software trigger mode.
148151
touch_pad_init();
152+
// If use touch pad wake up, should set touch sensor FSM mode at 'TOUCH_FSM_MODE_TIMER'.
153+
touch_pad_set_fsm_mode(TOUCH_FSM_MODE_TIMER);
154+
// Set reference voltage for charging/discharging
155+
// In this case, the high reference valtage will be 2.4V - 1V = 1.4V
156+
// The low reference voltage will be 0.5
157+
// The larger the range, the larger the pulse count value.
158+
touch_pad_set_voltage(TOUCH_HVOLT_2V4, TOUCH_LVOLT_0V5, TOUCH_HVOLT_ATTEN_1V);
159+
//init RTC IO and mode for touch pad.
160+
touch_pad_config(TOUCH_PAD_NUM8, TOUCH_THRESH_NO_USE);
161+
touch_pad_config(TOUCH_PAD_NUM9, TOUCH_THRESH_NO_USE);
149162
calibrate_touch_pad(TOUCH_PAD_NUM8);
150163
calibrate_touch_pad(TOUCH_PAD_NUM9);
151164
printf("Enabling touch pad wakeup\n");
152165
esp_sleep_enable_touchpad_wakeup();
166+
153167
#endif // CONFIG_ENABLE_TOUCH_WAKEUP
154168

155169
#ifdef CONFIG_ENABLE_ULP_TEMPERATURE_WAKEUP
@@ -175,8 +189,6 @@ void app_main()
175189
#ifdef CONFIG_ENABLE_TOUCH_WAKEUP
176190
static void calibrate_touch_pad(touch_pad_t pad)
177191
{
178-
touch_pad_config(pad, 1000);
179-
180192
int avg = 0;
181193
const size_t calibration_count = 128;
182194
for (int i = 0; i < calibration_count; ++i) {

0 commit comments

Comments
 (0)