mirror of
https://github.com/espressif/esp-idf.git
synced 2025-09-30 19:19:21 +00:00
Driver(touch): fix touch sensor driver for esp32s2.
1.update touch sensor driver for esp32s2; 2.update unit test for touch sensor; 3.update register files about touch sensor;
This commit is contained in:
@@ -11,7 +11,6 @@
|
||||
#include "freertos/task.h"
|
||||
#include "freertos/queue.h"
|
||||
#include "esp_log.h"
|
||||
|
||||
#include "driver/touch_pad.h"
|
||||
#include "soc/rtc_periph.h"
|
||||
#include "soc/sens_periph.h"
|
||||
@@ -29,20 +28,21 @@ typedef struct touch_msg {
|
||||
#define TOUCH_BUTTON_NUM 4
|
||||
#define TOUCH_BUTTON_WATERPROOF_ENABLE 1
|
||||
#define TOUCH_BUTTON_DENOISE_ENABLE 1
|
||||
#define TOUCH_CHANGE_CONFIG 0
|
||||
|
||||
static const touch_pad_t button[TOUCH_BUTTON_NUM] = {
|
||||
TOUCH_PAD_NUM7, // 'SELECT' button.
|
||||
TOUCH_PAD_NUM9, // 'MENU' button.
|
||||
TOUCH_PAD_NUM11, // 'BACK' button.
|
||||
TOUCH_PAD_NUM13, // Guard ring for waterproof design.
|
||||
// if this pad be touched, other pads no response.
|
||||
// If this pad be touched, other pads no response.
|
||||
};
|
||||
|
||||
/*
|
||||
* Touch threshold. The threshold determines the sensitivity of the touch.
|
||||
* This threshold is derived by testing changes in readings from different touch channels.
|
||||
* If (raw_data - baseline) > baseline * threshold, the pad be actived.
|
||||
* If (raw_data - baseline) < baseline * threshold, the pad be inactived.
|
||||
* If (raw_data - baseline) > baseline * threshold, the pad be activated.
|
||||
* If (raw_data - baseline) < baseline * threshold, the pad be inactivated.
|
||||
*/
|
||||
static const float button_threshold[TOUCH_BUTTON_NUM] = {
|
||||
0.2, // 20%.
|
||||
@@ -64,9 +64,6 @@ static void touchsensor_interrupt_cb(void *arg)
|
||||
evt.pad_status = touch_pad_get_status();
|
||||
evt.pad_num = touch_pad_get_current_meas_channel();
|
||||
|
||||
if (evt.intr_mask & TOUCH_PAD_INTR_MASK_DONE) {
|
||||
touch_pad_filter_read_baseline(evt.pad_num, &evt.pad_val);
|
||||
}
|
||||
xQueueSendFromISR(que_touch, &evt, &task_awoken);
|
||||
if (task_awoken == pdTRUE) {
|
||||
portYIELD_FROM_ISR();
|
||||
@@ -81,7 +78,7 @@ static void tp_example_set_thresholds(void)
|
||||
touch_pad_filter_read_baseline(button[i], &touch_value);
|
||||
//set interrupt threshold.
|
||||
touch_pad_set_thresh(button[i], touch_value * button_threshold[i]);
|
||||
ESP_LOGI(TAG, "test init: touch pad [%d] base %d, thresh %d", \
|
||||
ESP_LOGI(TAG, "touch pad [%d] base %d, thresh %d", \
|
||||
button[i], touch_value, (uint32_t)(touch_value * button_threshold[i]));
|
||||
}
|
||||
}
|
||||
@@ -97,10 +94,10 @@ static void touchsensor_filter_set(touch_filter_mode_t mode)
|
||||
.noise_neg_thr = 0, // 50%
|
||||
.neg_noise_limit = 10, // 10 time count.
|
||||
.jitter_step = 4, // use for jitter mode.
|
||||
.smh_lvl = TOUCH_PAD_SMOOTH_IIR_2,
|
||||
};
|
||||
touch_pad_filter_set_config(&filter_info);
|
||||
touch_pad_filter_enable();
|
||||
touch_pad_filter_reset_baseline(TOUCH_PAD_MAX);
|
||||
ESP_LOGI(TAG, "touch pad filter init");
|
||||
}
|
||||
|
||||
@@ -109,7 +106,7 @@ static void tp_example_read_task(void *pvParameter)
|
||||
touch_event_t evt = {0};
|
||||
static uint8_t guard_mode_flag = 0;
|
||||
/* Wait touch sensor init done */
|
||||
vTaskDelay(100 / portTICK_RATE_MS);
|
||||
vTaskDelay(50 / portTICK_RATE_MS);
|
||||
tp_example_set_thresholds();
|
||||
|
||||
while (1) {
|
||||
@@ -121,10 +118,10 @@ static void tp_example_read_task(void *pvParameter)
|
||||
/* if guard pad be touched, other pads no response. */
|
||||
if (evt.pad_num == button[3]) {
|
||||
guard_mode_flag = 1;
|
||||
ESP_LOGW(TAG, "TouchSensor [%d] be actived, enter guard mode", evt.pad_num);
|
||||
ESP_LOGW(TAG, "TouchSensor [%d] be activated, enter guard mode", evt.pad_num);
|
||||
} else {
|
||||
if (guard_mode_flag == 0) {
|
||||
ESP_LOGI(TAG, "TouchSensor [%d] be actived, status mask 0x%x", evt.pad_num, evt.pad_status);
|
||||
ESP_LOGI(TAG, "TouchSensor [%d] be inactivated, status mask 0x%x", evt.pad_num, evt.pad_status);
|
||||
} else {
|
||||
ESP_LOGW(TAG, "In guard mode. No response");
|
||||
}
|
||||
@@ -134,15 +131,20 @@ static void tp_example_read_task(void *pvParameter)
|
||||
/* if guard pad be touched, other pads no response. */
|
||||
if (evt.pad_num == button[3]) {
|
||||
guard_mode_flag = 0;
|
||||
ESP_LOGW(TAG, "TouchSensor [%d] be actived, exit guard mode", evt.pad_num);
|
||||
ESP_LOGW(TAG, "TouchSensor [%d] be activated, exit guard mode", evt.pad_num);
|
||||
} else {
|
||||
if (guard_mode_flag == 0) {
|
||||
ESP_LOGI(TAG, "TouchSensor [%d] be inactived, status mask 0x%x", evt.pad_num, evt.pad_status);
|
||||
ESP_LOGI(TAG, "TouchSensor [%d] be inactivated, status mask 0x%x", evt.pad_num, evt.pad_status);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (evt.intr_mask & TOUCH_PAD_INTR_MASK_DONE) {
|
||||
ESP_LOGI(TAG, "TouchSensor [%d] measure done, raw data %d", evt.pad_num, evt.pad_val);
|
||||
if (evt.intr_mask & TOUCH_PAD_INTR_MASK_SCAN_DONE) {
|
||||
ESP_LOGI(TAG, "The touch sensor group measurement is done [%d].", evt.pad_num);
|
||||
}
|
||||
if (evt.intr_mask & TOUCH_PAD_INTR_MASK_TIMEOUT) {
|
||||
/* Add your exception handling in here. */
|
||||
ESP_LOGI(TAG, "Touch sensor channel %d measure timeout. Skip this exception channel!!", evt.pad_num);
|
||||
touch_pad_timeout_resume(); // Point on the next channel to measure.
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -159,21 +161,24 @@ void app_main(void)
|
||||
for (int i = 0; i < TOUCH_BUTTON_NUM; i++) {
|
||||
touch_pad_config(button[i]);
|
||||
}
|
||||
#if 0
|
||||
|
||||
#if TOUCH_CHANGE_CONFIG
|
||||
/* If you want change the touch sensor default setting, please write here(after initialize). There are examples: */
|
||||
touch_pad_set_meas_time(TOUCH_PAD_SLEEP_CYCLE_DEFAULT, TOUCH_PAD_SLEEP_CYCLE_DEFAULT);
|
||||
touch_pad_set_voltage(TOUCH_PAD_HIGH_VOLTAGE_THRESHOLD, TOUCH_PAD_LOW_VOLTAGE_THRESHOLD, TOUCH_PAD_ATTEN_VOLTAGE_THRESHOLD);
|
||||
touch_pad_set_inactive_connect(TOUCH_PAD_INACTIVE_CONNECT_DEFAULT);
|
||||
touch_pad_set_idle_channel_connect(TOUCH_PAD_IDLE_CH_CONNECT_DEFAULT);
|
||||
for (int i = 0; i < TOUCH_BUTTON_NUM; i++) {
|
||||
touch_pad_set_cnt_mode(i, TOUCH_PAD_SLOPE_DEFAULT, TOUCH_PAD_TIE_OPT_DEFAULT);
|
||||
}
|
||||
#endif
|
||||
|
||||
#if TOUCH_BUTTON_DENOISE_ENABLE
|
||||
/* Denoise setting at TouchSensor 0. */
|
||||
touch_pad_denoise_t denoise = {
|
||||
/* The bits to be cancelled are determined according to the noise level. */
|
||||
.grade = TOUCH_PAD_DENOISE_BIT4,
|
||||
.cap_level = TOUCH_PAD_DENOISE_CAP_L7,
|
||||
/* By adjusting the parameters, the reading of T0 should be approximated to the reading of the measured channel. */
|
||||
.cap_level = TOUCH_PAD_DENOISE_CAP_L4,
|
||||
};
|
||||
touch_pad_denoise_set_config(&denoise);
|
||||
touch_pad_denoise_enable();
|
||||
@@ -184,8 +189,10 @@ void app_main(void)
|
||||
/* Waterproof function */
|
||||
touch_pad_waterproof_t waterproof = {
|
||||
.guard_ring_pad = button[3], // If no ring pad, set 0;
|
||||
/* It depends on the number of the parasitic capacitance of the shield pad. */
|
||||
.shield_driver = TOUCH_PAD_SHIELD_DRV_L0, //40pf
|
||||
/* It depends on the number of the parasitic capacitance of the shield pad.
|
||||
Based on the touch readings of T14 and T0, estimate the size of the parasitic capacitance on T14
|
||||
and set the parameters of the appropriate hardware. */
|
||||
.shield_driver = TOUCH_PAD_SHIELD_DRV_L2,
|
||||
};
|
||||
touch_pad_waterproof_set_config(&waterproof);
|
||||
touch_pad_waterproof_enable();
|
||||
@@ -193,11 +200,12 @@ void app_main(void)
|
||||
#endif
|
||||
|
||||
/* Filter setting */
|
||||
touchsensor_filter_set(TOUCH_PAD_FILTER_IIR_8);
|
||||
touchsensor_filter_set(TOUCH_PAD_FILTER_IIR_16);
|
||||
touch_pad_timeout_set(true, SOC_TOUCH_PAD_THRESHOLD_MAX);
|
||||
/* Register touch interrupt ISR, enable intr type. */
|
||||
touch_pad_isr_register(touchsensor_interrupt_cb, NULL, TOUCH_PAD_INTR_MASK_ALL);
|
||||
touch_pad_intr_enable(TOUCH_PAD_INTR_MASK_ACTIVE | TOUCH_PAD_INTR_MASK_INACTIVE);
|
||||
// touch_pad_intr_enable(TOUCH_PAD_INTR_MASK_DONE); // Use for debug
|
||||
/* If you have other touch algorithm, you can get the measured value after the `TOUCH_PAD_INTR_MASK_SCAN_DONE` interrupt is generated. */
|
||||
touch_pad_intr_enable(TOUCH_PAD_INTR_MASK_ACTIVE | TOUCH_PAD_INTR_MASK_INACTIVE | TOUCH_PAD_INTR_MASK_TIMEOUT);
|
||||
|
||||
/* Enable touch sensor clock. Work mode is "timer trigger". */
|
||||
touch_pad_set_fsm_mode(TOUCH_FSM_MODE_TIMER);
|
||||
|
@@ -13,13 +13,14 @@
|
||||
#include "esp_log.h"
|
||||
|
||||
#define TOUCH_BUTTON_NUM 14
|
||||
#define TOUCH_CHANGE_CONFIG 0
|
||||
|
||||
static const char * TAG = "touch read";
|
||||
static const char *TAG = "touch read";
|
||||
static const touch_pad_t button[TOUCH_BUTTON_NUM] = {
|
||||
TOUCH_PAD_NUM1,
|
||||
TOUCH_PAD_NUM2,
|
||||
TOUCH_PAD_NUM3,
|
||||
TOUCH_PAD_NUM4,
|
||||
TOUCH_PAD_NUM4,
|
||||
TOUCH_PAD_NUM5,
|
||||
TOUCH_PAD_NUM6,
|
||||
TOUCH_PAD_NUM7,
|
||||
@@ -39,7 +40,7 @@ static const touch_pad_t button[TOUCH_BUTTON_NUM] = {
|
||||
static void tp_example_read_task(void *pvParameter)
|
||||
{
|
||||
uint32_t touch_value;
|
||||
|
||||
|
||||
/* Wait touch sensor init done */
|
||||
vTaskDelay(100 / portTICK_RATE_MS);
|
||||
printf("Touch Sensor read, the output format is: \nTouchpad num:[raw data]\n\n");
|
||||
@@ -61,20 +62,20 @@ void app_main(void)
|
||||
for (int i = 0; i < TOUCH_BUTTON_NUM; i++) {
|
||||
touch_pad_config(button[i]);
|
||||
}
|
||||
#if 0
|
||||
#if TOUCH_CHANGE_CONFIG
|
||||
/* If you want change the touch sensor default setting, please write here(after initialize). There are examples: */
|
||||
touch_pad_set_meas_time(TOUCH_PAD_SLEEP_CYCLE_DEFAULT, TOUCH_PAD_SLEEP_CYCLE_DEFAULT);
|
||||
touch_pad_set_voltage(TOUCH_PAD_HIGH_VOLTAGE_THRESHOLD, TOUCH_PAD_LOW_VOLTAGE_THRESHOLD, TOUCH_PAD_ATTEN_VOLTAGE_THRESHOLD);
|
||||
touch_pad_set_inactive_connect(TOUCH_PAD_INACTIVE_CONNECT_DEFAULT);
|
||||
touch_pad_set_idle_channel_connect(TOUCH_PAD_IDLE_CH_CONNECT_DEFAULT);
|
||||
for (int i = 0; i < TOUCH_BUTTON_NUM; i++) {
|
||||
touch_pad_set_cnt_mode(i, TOUCH_PAD_SLOPE_DEFAULT, TOUCH_PAD_TIE_OPT_DEFAULT);
|
||||
}
|
||||
#endif
|
||||
/* Denoise setting at TouchSensor 0. */
|
||||
touch_pad_denoise_t denoise = {
|
||||
/* The bits to be cancelled are determined according to the noise level. */
|
||||
.grade = TOUCH_PAD_DENOISE_BIT4,
|
||||
.cap_level = TOUCH_PAD_DENOISE_CAP_L7,
|
||||
/* The bits to be cancelled are determined according to the noise level. */
|
||||
.grade = TOUCH_PAD_DENOISE_BIT4,
|
||||
.cap_level = TOUCH_PAD_DENOISE_CAP_L4,
|
||||
};
|
||||
touch_pad_denoise_set_config(&denoise);
|
||||
touch_pad_denoise_enable();
|
||||
@@ -83,7 +84,7 @@ void app_main(void)
|
||||
/* Enable touch sensor clock. Work mode is "timer trigger". */
|
||||
touch_pad_set_fsm_mode(TOUCH_FSM_MODE_TIMER);
|
||||
touch_pad_fsm_start();
|
||||
|
||||
|
||||
/* Start task to read values by pads. */
|
||||
xTaskCreate(&tp_example_read_task, "touch_pad_read_task", 2048, NULL, 5, NULL);
|
||||
}
|
||||
|
Reference in New Issue
Block a user