From 3f7019529a59d1e18fc73fe287b4fa4c2915c8ce Mon Sep 17 00:00:00 2001 From: Rodrigo Garcia Date: Tue, 25 Jan 2022 20:53:02 -0300 Subject: [PATCH 01/11] Touch Sensor IDF Refactoring --- cores/esp32/esp32-hal-touch.c | 325 ++++++++++-------- cores/esp32/esp32-hal-touch.h | 42 ++- .../examples/Touch/TouchButton/.skip.esp32S2 | 0 .../examples/Touch/TouchButton/.skip.esp32S3 | 0 .../examples/Touch/TouchButton/.skip.esp32c3 | 0 .../Touch/TouchButton/TouchButton.ino | 46 +++ .../examples/Touch/TouchButtonV2/.skip.esp32 | 0 .../Touch/TouchButtonV2/.skip.esp32c3 | 0 .../Touch/TouchButtonV2/TouchButtonV2.ino | 53 +++ 9 files changed, 309 insertions(+), 157 deletions(-) create mode 100644 libraries/ESP32/examples/Touch/TouchButton/.skip.esp32S2 create mode 100644 libraries/ESP32/examples/Touch/TouchButton/.skip.esp32S3 create mode 100644 libraries/ESP32/examples/Touch/TouchButton/.skip.esp32c3 create mode 100644 libraries/ESP32/examples/Touch/TouchButton/TouchButton.ino create mode 100644 libraries/ESP32/examples/Touch/TouchButtonV2/.skip.esp32 create mode 100644 libraries/ESP32/examples/Touch/TouchButtonV2/.skip.esp32c3 create mode 100644 libraries/ESP32/examples/Touch/TouchButtonV2/TouchButtonV2.ino diff --git a/cores/esp32/esp32-hal-touch.c b/cores/esp32/esp32-hal-touch.c index 1492eff78a8..b7b3b1c385c 100644 --- a/cores/esp32/esp32-hal-touch.c +++ b/cores/esp32/esp32-hal-touch.c @@ -12,219 +12,242 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include "esp32-hal-touch.h" -#ifndef CONFIG_IDF_TARGET_ESP32C3 -#include "freertos/FreeRTOS.h" -#include "freertos/task.h" -#include "esp_attr.h" -#include "soc/rtc_io_reg.h" -#include "soc/sens_reg.h" -#include "soc/sens_struct.h" -#include "soc/rtc_cntl_reg.h" +#include "soc/soc_caps.h" + +#if SOC_TOUCH_SENSOR_NUM > 0 #include "driver/touch_sensor.h" +#include "esp32-hal-touch.h" -#include "esp_system.h" -#ifdef ESP_IDF_VERSION_MAJOR // IDF 4+ -#if CONFIG_IDF_TARGET_ESP32 // ESP32/PICO-D4 -#include "esp32/rom/ets_sys.h" -#include "esp_intr_alloc.h" -#elif CONFIG_IDF_TARGET_ESP32S2 -#include "esp32s2/rom/ets_sys.h" -#include "esp_intr_alloc.h" -#include "soc/periph_defs.h" -#else -#error Target CONFIG_IDF_TARGET is not supported -#endif -#else // ESP32 Before IDF 4.0 -#include "rom/ets_sys.h" -#include "esp_intr.h" -#endif +/* + Internal Private Touch Data Structure and Functions +*/ static uint16_t __touchSleepCycles = 0x1000; static uint16_t __touchMeasureCycles = 0x1000; typedef void (*voidFuncPtr)(void); -static voidFuncPtr __touchInterruptHandlers[10] = {0,}; -static intr_handle_t touch_intr_handle = NULL; +typedef void (*voidArgFuncPtr)(void *); + +typedef struct { + voidFuncPtr fn; + bool callWithArgs; + void* arg; +#if SOC_TOUCH_VERSION_2 // Only for ESP32S2 and ESP32S3 + bool lastStatusIsPressed; +#endif +} TouchInterruptHandle_t; -void ARDUINO_ISR_ATTR __touchISR(void * arg) +static TouchInterruptHandle_t __touchInterruptHandlers[SOC_TOUCH_SENSOR_NUM] = {0,}; + +static void ARDUINO_ISR_ATTR __touchISR(void * arg) { -#if CONFIG_IDF_TARGET_ESP32 - uint32_t pad_intr = READ_PERI_REG(SENS_SAR_TOUCH_CTRL2_REG) & 0x3ff; - uint32_t rtc_intr = READ_PERI_REG(RTC_CNTL_INT_ST_REG); - uint8_t i = 0; +#if SOC_TOUCH_VERSION_1 // ESP32 + uint32_t pad_intr = touch_pad_get_status(); //clear interrupt - WRITE_PERI_REG(RTC_CNTL_INT_CLR_REG, rtc_intr); - SET_PERI_REG_MASK(SENS_SAR_TOUCH_CTRL2_REG, SENS_TOUCH_MEAS_EN_CLR); - - if (rtc_intr & RTC_CNTL_TOUCH_INT_ST) { - for (i = 0; i < 10; ++i) { - if ((pad_intr >> i) & 0x01) { - if(__touchInterruptHandlers[i]){ - __touchInterruptHandlers[i](); + touch_pad_clear_status(); + // call Pad ISR User callback + for (int i = 0; i < SOC_TOUCH_SENSOR_NUM; i++) { + if ((pad_intr >> i) & 0x01) { + if(__touchInterruptHandlers[i].fn){ + // keeping backward compatibility with "void cb(void)" and with new "void cb(vooid *)" + if (__touchInterruptHandlers[i].callWithArgs) { + ((voidArgFuncPtr)__touchInterruptHandlers[i].fn)(__touchInterruptHandlers[i].arg); + } else { + __touchInterruptHandlers[i].fn(); } } } } #endif + +#if SOC_TOUCH_VERSION_2 // ESP32S2, ESP32S3 + touch_pad_intr_mask_t evt = touch_pad_read_intr_status_mask(); + uint8_t pad_num = touch_pad_get_current_meas_channel(); + if (evt & TOUCH_PAD_INTR_MASK_ACTIVE) { + // touch has been pressed / touched + __touchInterruptHandlers[pad_num].lastStatusIsPressed = true; + } + if (evt & TOUCH_PAD_INTR_MASK_INACTIVE) { + // touch has been released / untouched + __touchInterruptHandlers[pad_num].lastStatusIsPressed = false; + } + if(__touchInterruptHandlers[pad_num].fn){ + // keeping backward compatibility with "void cb(void)" and with new "void cb(vooid *)" + if (__touchInterruptHandlers[pad_num].callWithArgs) { + ((voidArgFuncPtr)__touchInterruptHandlers[pad_num].fn)(__touchInterruptHandlers[pad_num].arg); + } else { + __touchInterruptHandlers[pad_num].fn(); + } + } +#endif } -void __touchSetCycles(uint16_t measure, uint16_t sleep) + + +static void __touchSetCycles(uint16_t measure, uint16_t sleep) { __touchSleepCycles = sleep; __touchMeasureCycles = measure; -#if CONFIG_IDF_TARGET_ESP32 - //Touch pad SleepCycle Time - SET_PERI_REG_BITS(SENS_SAR_TOUCH_CTRL2_REG, SENS_TOUCH_SLEEP_CYCLES, __touchSleepCycles, SENS_TOUCH_SLEEP_CYCLES_S); - //Touch Pad Measure Time - SET_PERI_REG_BITS(SENS_SAR_TOUCH_CTRL1_REG, SENS_TOUCH_MEAS_DELAY, __touchMeasureCycles, SENS_TOUCH_MEAS_DELAY_S); -#else +#if SOC_TOUCH_VERSION_1 || SOC_TOUCH_VERSION_2 // ESP32 || ESP32S2, ESP32S3 touch_pad_set_meas_time(sleep, measure); #endif } -void __touchInit() + + +static void __touchInit() { static bool initialized = false; if(initialized){ return; } initialized = true; -#if CONFIG_IDF_TARGET_ESP32 - SET_PERI_REG_BITS(RTC_IO_TOUCH_CFG_REG, RTC_IO_TOUCH_XPD_BIAS, 1, RTC_IO_TOUCH_XPD_BIAS_S); - SET_PERI_REG_MASK(SENS_SAR_TOUCH_CTRL2_REG, SENS_TOUCH_MEAS_EN_CLR); - //clear touch enable - WRITE_PERI_REG(SENS_SAR_TOUCH_ENABLE_REG, 0x0); - SET_PERI_REG_MASK(RTC_CNTL_STATE0_REG, RTC_CNTL_TOUCH_SLP_TIMER_EN); - __touchSetCycles(__touchMeasureCycles, __touchSleepCycles); - esp_intr_alloc(ETS_RTC_CORE_INTR_SOURCE, (int)ARDUINO_ISR_FLAG, __touchISR, NULL, &touch_intr_handle); -#else + +#if SOC_TOUCH_VERSION_1 // ESP32 + touch_pad_init(); + // the next two lines will drive the touch reading values + touch_pad_set_voltage(TOUCH_HVOLT_2V7, TOUCH_LVOLT_0V5, TOUCH_HVOLT_ATTEN_0V); + touch_pad_set_meas_time(__touchMeasureCycles, __touchSleepCycles); + // Touch Sensor Timer initiated + touch_pad_set_fsm_mode(TOUCH_FSM_MODE_TIMER); + touch_pad_filter_start(10); + // Initial no Threshold and setup + for (int i = 0; i < SOC_TOUCH_SENSOR_NUM; i++) { + __touchInterruptHandlers[i].fn = NULL; + touch_pad_config(i, SOC_TOUCH_PAD_THRESHOLD_MAX); + } + // keep ISR activated - it can run all together (ISR + touchRead()) + touch_pad_isr_register(__touchISR, NULL); + touch_pad_intr_enable(); +#endif + +#if SOC_TOUCH_VERSION_2 // ESP32S2, ESP32S3 touch_pad_init(); - touch_pad_set_voltage(TOUCH_HVOLT_2V7, TOUCH_LVOLT_0V5, TOUCH_HVOLT_ATTEN_0V5); - touch_pad_set_idle_channel_connect(TOUCH_PAD_CONN_GND); - __touchSetCycles(__touchMeasureCycles, __touchSleepCycles); + // the next two lines will drive the touch reading values + touch_pad_set_meas_time(TOUCH_PAD_SLEEP_CYCLE_DEFAULT, TOUCH_PAD_MEASURE_CYCLE_DEFAULT); + touch_pad_set_voltage(TOUCH_PAD_HIGH_VOLTAGE_THRESHOLD, TOUCH_PAD_LOW_VOLTAGE_THRESHOLD, TOUCH_PAD_ATTEN_VOLTAGE_THRESHOLD); + touch_pad_set_idle_channel_connect(TOUCH_PAD_IDLE_CH_CONNECT_DEFAULT); touch_pad_denoise_t denoise = { .grade = TOUCH_PAD_DENOISE_BIT4, .cap_level = TOUCH_PAD_DENOISE_CAP_L4, }; touch_pad_denoise_set_config(&denoise); touch_pad_denoise_enable(); + // Touch Sensor Timer initiated touch_pad_set_fsm_mode(TOUCH_FSM_MODE_TIMER); touch_pad_fsm_start(); + + // Initial no Threshold and setup - TOUCH0 is internal denoise channel + for (int i = 0; i < SOC_TOUCH_SENSOR_NUM; i++) { + __touchInterruptHandlers[i].fn = NULL; + touch_pad_config(i); + } + // keep ISR activated - it can run all together (ISR + touchRead()) + touch_pad_isr_register(__touchISR, NULL, TOUCH_PAD_INTR_MASK_ACTIVE | TOUCH_PAD_INTR_MASK_INACTIVE); + touch_pad_intr_enable(TOUCH_PAD_INTR_MASK_ACTIVE | TOUCH_PAD_INTR_MASK_INACTIVE); #endif } -uint16_t __touchRead(uint8_t pin) +static uint32_t __touchRead(uint8_t pin) { int8_t pad = digitalPinToTouchChannel(pin); if(pad < 0){ return 0; } + __touchInit(); - pinMode(pin, ANALOG); +#if SOC_TOUCH_VERSION_1 // ESP32 + uint16_t touch_value; +#endif - __touchInit(); +#if SOC_TOUCH_VERSION_2 // ESP32S2 ESP32S3 + uint32_t touch_value; +#endif -#if CONFIG_IDF_TARGET_ESP32 - uint32_t v0 = READ_PERI_REG(SENS_SAR_TOUCH_ENABLE_REG); - //Disable Intr & enable touch pad - WRITE_PERI_REG(SENS_SAR_TOUCH_ENABLE_REG, - (v0 & ~((1 << (pad + SENS_TOUCH_PAD_OUTEN2_S)) | (1 << (pad + SENS_TOUCH_PAD_OUTEN1_S)))) - | (1 << (pad + SENS_TOUCH_PAD_WORKEN_S))); - - SET_PERI_REG_MASK(SENS_SAR_TOUCH_ENABLE_REG, (1 << (pad + SENS_TOUCH_PAD_WORKEN_S))); - - uint32_t rtc_tio_reg = RTC_IO_TOUCH_PAD0_REG + pad * 4; - WRITE_PERI_REG(rtc_tio_reg, (READ_PERI_REG(rtc_tio_reg) - & ~(RTC_IO_TOUCH_PAD0_DAC_M)) - | (7 << RTC_IO_TOUCH_PAD0_DAC_S)//Touch Set Slope - | RTC_IO_TOUCH_PAD0_TIE_OPT_M //Enable Tie,Init Level - | RTC_IO_TOUCH_PAD0_START_M //Enable Touch Pad IO - | RTC_IO_TOUCH_PAD0_XPD_M); //Enable Touch Pad Power on - - //force oneTime test start - SET_PERI_REG_MASK(SENS_SAR_TOUCH_CTRL2_REG, SENS_TOUCH_START_EN_M|SENS_TOUCH_START_FORCE_M); - - SET_PERI_REG_BITS(SENS_SAR_TOUCH_CTRL1_REG, SENS_TOUCH_XPD_WAIT, 10, SENS_TOUCH_XPD_WAIT_S); - - while (GET_PERI_REG_MASK(SENS_SAR_TOUCH_CTRL2_REG, SENS_TOUCH_MEAS_DONE) == 0) {}; - - uint16_t touch_value = READ_PERI_REG(SENS_SAR_TOUCH_OUT1_REG + (pad / 2) * 4) >> ((pad & 1) ? SENS_TOUCH_MEAS_OUT1_S : SENS_TOUCH_MEAS_OUT0_S); - - //clear touch force ,select the Touch mode is Timer - CLEAR_PERI_REG_MASK(SENS_SAR_TOUCH_CTRL2_REG, SENS_TOUCH_START_EN_M|SENS_TOUCH_START_FORCE_M); - - //restore previous value - WRITE_PERI_REG(SENS_SAR_TOUCH_ENABLE_REG, v0); - return touch_value; -#else - static uint32_t chan_mask = 0; - uint32_t value = 0; - if((chan_mask & (1 << pad)) == 0){ - if(touch_pad_set_thresh((touch_pad_t)pad, TOUCH_PAD_THRESHOLD_MAX) != ESP_OK){ - log_e("touch_pad_set_thresh failed"); - } else if(touch_pad_config((touch_pad_t)pad) != ESP_OK){ - log_e("touch_pad_config failed"); - } else { - chan_mask |= (1 << pad); - } - } - if((chan_mask & (1 << pad)) != 0) { - if(touch_pad_read_raw_data((touch_pad_t)pad, &value) != ESP_OK){ - log_e("touch_pad_read_raw_data failed"); - } - } - return value; +#if SOC_TOUCH_VERSION_1 || SOC_TOUCH_VERSION_2 // ESP32 || ESP32S2, ESP32S3 + touch_pad_read_raw_data(pad, &touch_value); #endif + + return (uint32_t) touch_value; } -void __touchAttachInterrupt(uint8_t pin, void (*userFunc)(void), uint16_t threshold) +static void __touchConfigInterrupt(uint8_t pin, void (*userFunc)(void), void *Args, uint32_t threshold, bool callWithArgs) { int8_t pad = digitalPinToTouchChannel(pin); if(pad < 0){ return; } - pinMode(pin, ANALOG); - - __touchInit(); + if (userFunc == NULL) { + // dettach ISR User Call + __touchInterruptHandlers[pad].fn = NULL; + threshold = SOC_TOUCH_PAD_THRESHOLD_MAX; // deactivate the ISR with SOC_TOUCH_PAD_THRESHOLD_MAX + } else { + // attach ISR User Call + __touchInit(); + __touchInterruptHandlers[pad].fn = userFunc; + __touchInterruptHandlers[pad].callWithArgs = callWithArgs; + __touchInterruptHandlers[pad].arg = Args; + } - __touchInterruptHandlers[pad] = userFunc; - -#if CONFIG_IDF_TARGET_ESP32 - //clear touch force ,select the Touch mode is Timer - CLEAR_PERI_REG_MASK(SENS_SAR_TOUCH_CTRL2_REG, SENS_TOUCH_START_EN_M|SENS_TOUCH_START_FORCE_M); - - //interrupt when touch value < threshold - CLEAR_PERI_REG_MASK(SENS_SAR_TOUCH_CTRL1_REG, SENS_TOUCH_OUT_SEL); - //Intr will give ,when SET0 < threshold - SET_PERI_REG_MASK(SENS_SAR_TOUCH_CTRL1_REG, SENS_TOUCH_OUT_1EN); - //Enable Rtc Touch Module Intr,the Interrupt need Rtc out Enable - SET_PERI_REG_MASK(RTC_CNTL_INT_ENA_REG, RTC_CNTL_TOUCH_INT_ENA); - - //set threshold - uint8_t shift = (pad & 1) ? SENS_TOUCH_OUT_TH1_S : SENS_TOUCH_OUT_TH0_S; - SET_PERI_REG_BITS((SENS_SAR_TOUCH_THRES1_REG + (pad / 2) * 4), SENS_TOUCH_OUT_TH0, threshold, shift); - - uint32_t rtc_tio_reg = RTC_IO_TOUCH_PAD0_REG + pad * 4; - WRITE_PERI_REG(rtc_tio_reg, (READ_PERI_REG(rtc_tio_reg) - & ~(RTC_IO_TOUCH_PAD0_DAC_M)) - | (7 << RTC_IO_TOUCH_PAD0_DAC_S)//Touch Set Slope - | RTC_IO_TOUCH_PAD0_TIE_OPT_M //Enable Tie,Init Level - | RTC_IO_TOUCH_PAD0_START_M //Enable Touch Pad IO - | RTC_IO_TOUCH_PAD0_XPD_M); //Enable Touch Pad Power on - - //Enable Digital rtc control :work mode and out mode - SET_PERI_REG_MASK(SENS_SAR_TOUCH_ENABLE_REG, - (1 << (pad + SENS_TOUCH_PAD_WORKEN_S)) | \ - (1 << (pad + SENS_TOUCH_PAD_OUTEN2_S)) | \ - (1 << (pad + SENS_TOUCH_PAD_OUTEN1_S))); -#else +#if SOC_TOUCH_VERSION_1 // ESP32 + touch_pad_config(pad, (uint16_t) threshold); +#endif +#if SOC_TOUCH_VERSION_2 // ESP32S2, ESP32S3 + touch_pad_set_thresh(pad, threshold); #endif } -extern uint16_t touchRead(uint8_t pin) __attribute__ ((weak, alias("__touchRead"))); -extern void touchAttachInterrupt(uint8_t pin, void (*userFunc)(void), uint16_t threshold) __attribute__ ((weak, alias("__touchAttachInterrupt"))); -extern void touchSetCycles(uint16_t measure, uint16_t sleep) __attribute__ ((weak, alias("__touchSetCycles"))); +// it keeps backwards compatibility +static void __touchAttachInterrupt(uint8_t pin, void (*userFunc)(void), uint32_t threshold) +{ + __touchConfigInterrupt(pin, userFunc, NULL, threshold, false); +} + +// new additional version of the API with User Args +static void __touchAttachArgsInterrupt(uint8_t pin, void (*userFunc)(void), void *args, uint32_t threshold) +{ + __touchConfigInterrupt(pin, userFunc, args, threshold, true); +} + +// new additional API to dettach touch ISR +static void __touchDettachInterrupt(uint8_t pin) +{ + __touchConfigInterrupt(pin, NULL, NULL, 0, false); // userFunc as NULL acts as dettaching +} +/* + External Public Touch API Functions +*/ + +#if SOC_TOUCH_VERSION_1 // Only for ESP32 SoC +void touchInterruptSetThresholdDirection(bool mustbeLower) { + if (mustbeLower) { + touch_pad_set_trigger_mode(TOUCH_TRIGGER_BELOW); + } else { + touch_pad_set_trigger_mode(TOUCH_TRIGGER_ABOVE); + } +} +#endif + +#if SOC_TOUCH_VERSION_2 // Only for ESP32S2 and ESP32S3 +// returns true if touch pad has been and continues pressed and false otherwise +bool touchInterruptGetLastStatus(uint8_t pin) { + int8_t pad = digitalPinToTouchChannel(pin); + if(pad < 0){ + return false; + } + + return __touchInterruptHandlers[pad].lastStatusIsPressed; +} #endif + + + +extern uint32_t touchRead(uint8_t) __attribute__ ((weak, alias("__touchRead"))); +extern void touchAttachInterrupt(uint8_t, voidFuncPtr, uint32_t) __attribute__ ((weak, alias("__touchAttachInterrupt"))); +extern void touchAttachInterruptArg(uint8_t, voidArgFuncPtr, void *, uint32_t) __attribute__ ((weak, alias("__touchAttachArgsInterrupt"))); +extern void touchDetachInterrupt(uint8_t) __attribute__ ((weak, alias("__touchDettachInterrupt"))); +extern void touchSetCycles(uint16_t, uint16_t) __attribute__ ((weak, alias("__touchSetCycles"))); + +#endif // #if SOC_TOUCH_SENSOR_NUM > 0 diff --git a/cores/esp32/esp32-hal-touch.h b/cores/esp32/esp32-hal-touch.h index 6f3b0fd0f7d..629babbb62f 100644 --- a/cores/esp32/esp32-hal-touch.h +++ b/cores/esp32/esp32-hal-touch.h @@ -24,6 +24,9 @@ extern "C" { #endif +#include "soc/soc_caps.h" + +#if SOC_TOUCH_SENSOR_NUM > 0 #include "esp32-hal.h" /* @@ -40,17 +43,44 @@ void touchSetCycles(uint16_t measure, uint16_t sleep); * You can use this method to chose a good threshold value * to use as value for touchAttachInterrupt * */ -uint16_t touchRead(uint8_t pin); +uint32_t touchRead(uint8_t pin); /* - * Set function to be called if touch pad value falls - * below the given threshold. Use touchRead to determine - * a proper threshold between touched and untouched state + * Set function to be called if touch pad value falls (ESP32) + * below the given threshold / rises (ESP32-S2/S3) by given increment (threshold). + * Use touchRead to determine a proper threshold between touched and untouched state * */ -void touchAttachInterrupt(uint8_t pin, void (*userFunc)(void), uint16_t threshold); +void touchAttachInterrupt(uint8_t pin, void (*userFunc)(void), uint32_t threshold); +void touchAttachInterruptArg(uint8_t pin, void (*userFunc)(void*), void *arg, uint32_t threshold); +void touchDetachInterrupt(uint8_t pin); + +/* + * Specific functions to ESP32 + * Tells the driver if it shall activate the ISR if the sensor is Lower or Higher than the Threshold + * Default if Lower. + **/ + +#if SOC_TOUCH_VERSION_1 // Only for ESP32 SoC +void touchInterruptSetThresholdDirection(bool mustbeLower); +#endif + + +/* + * Specific functions to ESP32-S2 and ESP32-S3 + * Returns true when the latest ISR status for the Touchpad is that it is touched (Active) + * and false when the Touchpad is untoouched (Inactive) + * This function can be used in conjunction with ISR User callback in order to take action + * as soon as the touchpad is touched and/or released + **/ + +#if SOC_TOUCH_VERSION_2 // Only for ESP32S2 and ESP32S3 +// returns true if touch pad has been and continues pressed and false otherwise +bool touchInterruptGetLastStatus(uint8_t pin); +#endif + #ifdef __cplusplus } #endif - +#endif // SOC_TOUCH_SENSOR_NUM > 0 #endif /* MAIN_ESP32_HAL_TOUCH_H_ */ diff --git a/libraries/ESP32/examples/Touch/TouchButton/.skip.esp32S2 b/libraries/ESP32/examples/Touch/TouchButton/.skip.esp32S2 new file mode 100644 index 00000000000..e69de29bb2d diff --git a/libraries/ESP32/examples/Touch/TouchButton/.skip.esp32S3 b/libraries/ESP32/examples/Touch/TouchButton/.skip.esp32S3 new file mode 100644 index 00000000000..e69de29bb2d diff --git a/libraries/ESP32/examples/Touch/TouchButton/.skip.esp32c3 b/libraries/ESP32/examples/Touch/TouchButton/.skip.esp32c3 new file mode 100644 index 00000000000..e69de29bb2d diff --git a/libraries/ESP32/examples/Touch/TouchButton/TouchButton.ino b/libraries/ESP32/examples/Touch/TouchButton/TouchButton.ino new file mode 100644 index 00000000000..4b6777a3aee --- /dev/null +++ b/libraries/ESP32/examples/Touch/TouchButton/TouchButton.ino @@ -0,0 +1,46 @@ +/* + +This is an example how to use Touch Intrrerupts +The sketh will tell when it is touched and then relesased as like a push-button + +This method based on touchInterruptSetThresholdDirection() is only available for ESP32 +*/ + +#include + +int threshold = 40; +bool touchActive = false; +bool lastTouchActive = false; +bool testingLower = true; + +void gotTouchEvent(){ + if (lastTouchActive != testingLower) { + touchActive = !touchActive; + testingLower = !testingLower; + // Touch ISR will be inverted: Lower <--> Higher than the Threshold after ISR event is noticed + touchInterruptSetThresholdDirection(testingLower); + } +} + +void setup() { + Serial.begin(115200); + delay(1000); // give me time to bring up serial monitor + Serial.println("ESP32 Touch Interrupt Test"); + touchAttachInterrupt(T2, gotTouchEvent, threshold); + + // Touch ISR will be activated when touchRead is lower than the Threshold + touchInterruptSetThresholdDirection(testingLower); +} + +void loop(){ + if(lastTouchActive != touchActive){ + lastTouchActive = touchActive; + if (touchActive) { + Serial.println(" ---- Touch was Pressed"); + } else { + Serial.println(" ---- Touch was Released"); + } + } + Serial.printf("T2 pin2 = %d \n", touchRead(T2)); + delay(125); +} diff --git a/libraries/ESP32/examples/Touch/TouchButtonV2/.skip.esp32 b/libraries/ESP32/examples/Touch/TouchButtonV2/.skip.esp32 new file mode 100644 index 00000000000..e69de29bb2d diff --git a/libraries/ESP32/examples/Touch/TouchButtonV2/.skip.esp32c3 b/libraries/ESP32/examples/Touch/TouchButtonV2/.skip.esp32c3 new file mode 100644 index 00000000000..e69de29bb2d diff --git a/libraries/ESP32/examples/Touch/TouchButtonV2/TouchButtonV2.ino b/libraries/ESP32/examples/Touch/TouchButtonV2/TouchButtonV2.ino new file mode 100644 index 00000000000..b58d2a7d0dc --- /dev/null +++ b/libraries/ESP32/examples/Touch/TouchButtonV2/TouchButtonV2.ino @@ -0,0 +1,53 @@ +/* + +This is an example how to use Touch Intrrerupts +The sketh will tell when it is touched and then relesased as like a push-button + +This method based on touchInterruptGetLastStatus() is only available for ESP32 S2 and S3 +*/ + +#include + +int threshold = 1500; // ESP32S2 +bool touch1detected = false; +bool touch2detected = false; + +void gotTouch1() { + touch1detected = true; +} + +void gotTouch2() { + touch2detected = true; +} + +void setup() { + Serial.begin(115200); + delay(1000); // give me time to bring up serial monitor + + Serial.println("\n ESP32 Touch Interrupt Test\n"); + touchAttachInterrupt(T1, gotTouch1, threshold); + touchAttachInterrupt(T2, gotTouch2, threshold); +} + +void loop() { + static uint32_t count = 0; + + if (touch1detected) { + touch1detected = false; + if (touchInterruptGetLastStatus(T1)) { + Serial.println(" --- T1 Touched"); + } else { + Serial.println(" --- T1 Released"); + } + } + if (touch2detected) { + touch2detected = false; + if (touchInterruptGetLastStatus(T2)) { + Serial.println(" --- T2 Touched"); + } else { + Serial.println(" --- T2 Released"); + } + } + + delay(80); +} From f8bed6893fb8d9871ec4391869762dc5545a4900 Mon Sep 17 00:00:00 2001 From: Rodrigo Garcia Date: Tue, 25 Jan 2022 21:24:24 -0300 Subject: [PATCH 02/11] Touch Sensor IDF Refactoring --- cores/esp32/esp32-hal-touch.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cores/esp32/esp32-hal-touch.c b/cores/esp32/esp32-hal-touch.c index b7b3b1c385c..72905715c32 100644 --- a/cores/esp32/esp32-hal-touch.c +++ b/cores/esp32/esp32-hal-touch.c @@ -216,6 +216,8 @@ static void __touchDettachInterrupt(uint8_t pin) { __touchConfigInterrupt(pin, NULL, NULL, 0, false); // userFunc as NULL acts as dettaching } + + /* External Public Touch API Functions */ @@ -242,8 +244,6 @@ bool touchInterruptGetLastStatus(uint8_t pin) { } #endif - - extern uint32_t touchRead(uint8_t) __attribute__ ((weak, alias("__touchRead"))); extern void touchAttachInterrupt(uint8_t, voidFuncPtr, uint32_t) __attribute__ ((weak, alias("__touchAttachInterrupt"))); extern void touchAttachInterruptArg(uint8_t, voidArgFuncPtr, void *, uint32_t) __attribute__ ((weak, alias("__touchAttachArgsInterrupt"))); From a5507a29699eb140195a46d6aca1416f8af4eb2c Mon Sep 17 00:00:00 2001 From: Rodrigo Garcia Date: Tue, 25 Jan 2022 21:59:31 -0300 Subject: [PATCH 03/11] Touch Sensor IDF Refactoring --- libraries/ESP32/examples/Touch/TouchButton/.skip.esp32S2 | 0 libraries/ESP32/examples/Touch/TouchButton/.skip.esp32S3 | 0 2 files changed, 0 insertions(+), 0 deletions(-) delete mode 100644 libraries/ESP32/examples/Touch/TouchButton/.skip.esp32S2 delete mode 100644 libraries/ESP32/examples/Touch/TouchButton/.skip.esp32S3 diff --git a/libraries/ESP32/examples/Touch/TouchButton/.skip.esp32S2 b/libraries/ESP32/examples/Touch/TouchButton/.skip.esp32S2 deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/libraries/ESP32/examples/Touch/TouchButton/.skip.esp32S3 b/libraries/ESP32/examples/Touch/TouchButton/.skip.esp32S3 deleted file mode 100644 index e69de29bb2d..00000000000 From 4fa91deaa0e445f75b41cfd41d4d90dfc3646027 Mon Sep 17 00:00:00 2001 From: Rodrigo Garcia Date: Tue, 25 Jan 2022 22:01:26 -0300 Subject: [PATCH 04/11] Touch Sensor IDF Refactoring --- libraries/ESP32/examples/Touch/TouchButton/.skip.esp32s2 | 0 libraries/ESP32/examples/Touch/TouchButton/.skip.esp32s3 | 0 2 files changed, 0 insertions(+), 0 deletions(-) create mode 100644 libraries/ESP32/examples/Touch/TouchButton/.skip.esp32s2 create mode 100644 libraries/ESP32/examples/Touch/TouchButton/.skip.esp32s3 diff --git a/libraries/ESP32/examples/Touch/TouchButton/.skip.esp32s2 b/libraries/ESP32/examples/Touch/TouchButton/.skip.esp32s2 new file mode 100644 index 00000000000..e69de29bb2d diff --git a/libraries/ESP32/examples/Touch/TouchButton/.skip.esp32s3 b/libraries/ESP32/examples/Touch/TouchButton/.skip.esp32s3 new file mode 100644 index 00000000000..e69de29bb2d From be005fe9e8c0de077856fc051c0c72e71e929eb8 Mon Sep 17 00:00:00 2001 From: Rodrigo Garcia Date: Tue, 25 Jan 2022 22:18:01 -0300 Subject: [PATCH 05/11] Touch Sensor IDF Refactoring --- libraries/ESP32/examples/Touch/TouchButton/TouchButton.ino | 2 +- libraries/ESP32/examples/Touch/TouchButtonV2/TouchButtonV2.ino | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/libraries/ESP32/examples/Touch/TouchButton/TouchButton.ino b/libraries/ESP32/examples/Touch/TouchButton/TouchButton.ino index 4b6777a3aee..b1dbc63fdcb 100644 --- a/libraries/ESP32/examples/Touch/TouchButton/TouchButton.ino +++ b/libraries/ESP32/examples/Touch/TouchButton/TouchButton.ino @@ -6,7 +6,7 @@ The sketh will tell when it is touched and then relesased as like a push-button This method based on touchInterruptSetThresholdDirection() is only available for ESP32 */ -#include +#include "Arduino.h" int threshold = 40; bool touchActive = false; diff --git a/libraries/ESP32/examples/Touch/TouchButtonV2/TouchButtonV2.ino b/libraries/ESP32/examples/Touch/TouchButtonV2/TouchButtonV2.ino index b58d2a7d0dc..640fd2d3b59 100644 --- a/libraries/ESP32/examples/Touch/TouchButtonV2/TouchButtonV2.ino +++ b/libraries/ESP32/examples/Touch/TouchButtonV2/TouchButtonV2.ino @@ -6,7 +6,7 @@ The sketh will tell when it is touched and then relesased as like a push-button This method based on touchInterruptGetLastStatus() is only available for ESP32 S2 and S3 */ -#include +#include "Arduino.h" int threshold = 1500; // ESP32S2 bool touch1detected = false; From b5c791c2507af61313bfe02d21083d45e9ea9cbe Mon Sep 17 00:00:00 2001 From: Rodrigo Garcia Date: Wed, 26 Jan 2022 08:16:45 -0300 Subject: [PATCH 06/11] Touch Sensor IDF Refactoring --- cores/esp32/esp32-hal-touch.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cores/esp32/esp32-hal-touch.h b/cores/esp32/esp32-hal-touch.h index 629babbb62f..7968fee378c 100644 --- a/cores/esp32/esp32-hal-touch.h +++ b/cores/esp32/esp32-hal-touch.h @@ -79,8 +79,9 @@ bool touchInterruptGetLastStatus(uint8_t pin); #endif +#endif // SOC_TOUCH_SENSOR_NUM > 0 + #ifdef __cplusplus } #endif -#endif // SOC_TOUCH_SENSOR_NUM > 0 #endif /* MAIN_ESP32_HAL_TOUCH_H_ */ From c7ec9c50c5f39e87f1bf116c8acf27ef3544709f Mon Sep 17 00:00:00 2001 From: Rodrigo Garcia Date: Mon, 31 Jan 2022 09:05:32 -0300 Subject: [PATCH 07/11] fixes initialization return codes --- cores/esp32/esp32-hal-touch.c | 57 ++++++++++++++++++++++++----------- 1 file changed, 40 insertions(+), 17 deletions(-) diff --git a/cores/esp32/esp32-hal-touch.c b/cores/esp32/esp32-hal-touch.c index 72905715c32..9517b0e457f 100644 --- a/cores/esp32/esp32-hal-touch.c +++ b/cores/esp32/esp32-hal-touch.c @@ -101,29 +101,42 @@ static void __touchInit() if(initialized){ return; } - initialized = true; + + esp_err_t err = ESP_OK; #if SOC_TOUCH_VERSION_1 // ESP32 - touch_pad_init(); - // the next two lines will drive the touch reading values - touch_pad_set_voltage(TOUCH_HVOLT_2V7, TOUCH_LVOLT_0V5, TOUCH_HVOLT_ATTEN_0V); + err = touch_pad_init(); + if (err != ESP_OK) { + goto err; + } + // the next two lines will drive the touch reading values -- both will return ESP_OK + touch_pad_set_voltage(TOUCH_HVOLT_2V7, TOUCH_LVOLT_0V5, TOUCH_HVOLT_ATTEN_0V); touch_pad_set_meas_time(__touchMeasureCycles, __touchSleepCycles); // Touch Sensor Timer initiated - touch_pad_set_fsm_mode(TOUCH_FSM_MODE_TIMER); - touch_pad_filter_start(10); + touch_pad_set_fsm_mode(TOUCH_FSM_MODE_TIMER); // returns ESP_OK + err = touch_pad_filter_start(10); + if (err != ESP_OK) { + goto err; + } // Initial no Threshold and setup for (int i = 0; i < SOC_TOUCH_SENSOR_NUM; i++) { __touchInterruptHandlers[i].fn = NULL; - touch_pad_config(i, SOC_TOUCH_PAD_THRESHOLD_MAX); + touch_pad_config(i, SOC_TOUCH_PAD_THRESHOLD_MAX); // returns ESP_OK } // keep ISR activated - it can run all together (ISR + touchRead()) - touch_pad_isr_register(__touchISR, NULL); - touch_pad_intr_enable(); + err = touch_pad_isr_register(__touchISR, NULL); + if (err != ESP_OK) { + goto err; + } + touch_pad_intr_enable(); // returns ESP_OK #endif #if SOC_TOUCH_VERSION_2 // ESP32S2, ESP32S3 - touch_pad_init(); - // the next two lines will drive the touch reading values + err = touch_pad_init(); + if (err != ESP_OK) { + goto err; + } + // the next lines will drive the touch reading values -- all os them return ESP_OK touch_pad_set_meas_time(TOUCH_PAD_SLEEP_CYCLE_DEFAULT, TOUCH_PAD_MEASURE_CYCLE_DEFAULT); touch_pad_set_voltage(TOUCH_PAD_HIGH_VOLTAGE_THRESHOLD, TOUCH_PAD_LOW_VOLTAGE_THRESHOLD, TOUCH_PAD_ATTEN_VOLTAGE_THRESHOLD); touch_pad_set_idle_channel_connect(TOUCH_PAD_IDLE_CH_CONNECT_DEFAULT); @@ -134,18 +147,28 @@ static void __touchInit() touch_pad_denoise_set_config(&denoise); touch_pad_denoise_enable(); // Touch Sensor Timer initiated - touch_pad_set_fsm_mode(TOUCH_FSM_MODE_TIMER); - touch_pad_fsm_start(); + touch_pad_set_fsm_mode(TOUCH_FSM_MODE_TIMER); // returns ESP_OK + touch_pad_fsm_start(); // returns ESP_OK // Initial no Threshold and setup - TOUCH0 is internal denoise channel - for (int i = 0; i < SOC_TOUCH_SENSOR_NUM; i++) { + for (int i = 1; i < SOC_TOUCH_SENSOR_NUM; i++) { __touchInterruptHandlers[i].fn = NULL; - touch_pad_config(i); + touch_pad_config(i); // returns ESP_OK } // keep ISR activated - it can run all together (ISR + touchRead()) - touch_pad_isr_register(__touchISR, NULL, TOUCH_PAD_INTR_MASK_ACTIVE | TOUCH_PAD_INTR_MASK_INACTIVE); - touch_pad_intr_enable(TOUCH_PAD_INTR_MASK_ACTIVE | TOUCH_PAD_INTR_MASK_INACTIVE); + err = touch_pad_isr_register(__touchISR, NULL, TOUCH_PAD_INTR_MASK_ACTIVE | TOUCH_PAD_INTR_MASK_INACTIVE); + if (err != ESP_OK) { + goto err; + } + touch_pad_intr_enable(TOUCH_PAD_INTR_MASK_ACTIVE | TOUCH_PAD_INTR_MASK_INACTIVE); // returns ESP_OK #endif + + initialized = true; + return; +err: + log_e(" Touch sensor initialization error."); + initialized = false; + return; } static uint32_t __touchRead(uint8_t pin) From 6cab5b7a0a307a18d98d2295c1770baff237c008 Mon Sep 17 00:00:00 2001 From: Rodrigo Garcia Date: Wed, 2 Feb 2022 16:11:23 -0300 Subject: [PATCH 08/11] code review --- cores/esp32/esp32-hal-touch.c | 96 ++++++++++++++++------------------- cores/esp32/esp32-hal-touch.h | 12 +++-- 2 files changed, 52 insertions(+), 56 deletions(-) diff --git a/cores/esp32/esp32-hal-touch.c b/cores/esp32/esp32-hal-touch.c index 9517b0e457f..4c23bae687f 100644 --- a/cores/esp32/esp32-hal-touch.c +++ b/cores/esp32/esp32-hal-touch.c @@ -14,6 +14,10 @@ #include "soc/soc_caps.h" +#if !defined(SOC_TOUCH_VERSION_1) && !defined(SOC_TOUCH_VERSION_2) +#error CONFIG_IDF_TARGET is not supported for Touch IDF driver! +#endif + #if SOC_TOUCH_SENSOR_NUM > 0 #include "driver/touch_sensor.h" #include "esp32-hal-touch.h" @@ -22,8 +26,13 @@ Internal Private Touch Data Structure and Functions */ +#if SOC_TOUCH_VERSION_1 // ESP32 static uint16_t __touchSleepCycles = 0x1000; static uint16_t __touchMeasureCycles = 0x1000; +#elif SOC_TOUCH_VERSION_2 // ESP32S2, ESP32S3 +static uint16_t __touchSleepCycles = TOUCH_PAD_SLEEP_CYCLE_DEFAULT; +static uint16_t __touchMeasureCycles = TOUCH_PAD_MEASURE_CYCLE_DEFAULT; +#endif typedef void (*voidFuncPtr)(void); typedef void (*voidArgFuncPtr)(void *); @@ -58,27 +67,25 @@ static void ARDUINO_ISR_ATTR __touchISR(void * arg) } } } -#endif - -#if SOC_TOUCH_VERSION_2 // ESP32S2, ESP32S3 - touch_pad_intr_mask_t evt = touch_pad_read_intr_status_mask(); - uint8_t pad_num = touch_pad_get_current_meas_channel(); - if (evt & TOUCH_PAD_INTR_MASK_ACTIVE) { - // touch has been pressed / touched - __touchInterruptHandlers[pad_num].lastStatusIsPressed = true; - } - if (evt & TOUCH_PAD_INTR_MASK_INACTIVE) { - // touch has been released / untouched - __touchInterruptHandlers[pad_num].lastStatusIsPressed = false; - } - if(__touchInterruptHandlers[pad_num].fn){ - // keeping backward compatibility with "void cb(void)" and with new "void cb(vooid *)" - if (__touchInterruptHandlers[pad_num].callWithArgs) { - ((voidArgFuncPtr)__touchInterruptHandlers[pad_num].fn)(__touchInterruptHandlers[pad_num].arg); - } else { - __touchInterruptHandlers[pad_num].fn(); - } - } +#elif SOC_TOUCH_VERSION_2 // ESP32S2, ESP32S3 + touch_pad_intr_mask_t evt = touch_pad_read_intr_status_mask(); + uint8_t pad_num = touch_pad_get_current_meas_channel(); + if (evt & TOUCH_PAD_INTR_MASK_ACTIVE) { + // touch has been pressed / touched + __touchInterruptHandlers[pad_num].lastStatusIsPressed = true; + } + if (evt & TOUCH_PAD_INTR_MASK_INACTIVE) { + // touch has been released / untouched + __touchInterruptHandlers[pad_num].lastStatusIsPressed = false; + } + if(__touchInterruptHandlers[pad_num].fn){ + // keeping backward compatibility with "void cb(void)" and with new "void cb(vooid *)" + if (__touchInterruptHandlers[pad_num].callWithArgs) { + ((voidArgFuncPtr)__touchInterruptHandlers[pad_num].fn)(__touchInterruptHandlers[pad_num].arg); + } else { + __touchInterruptHandlers[pad_num].fn(); + } + } #endif } @@ -88,9 +95,7 @@ static void __touchSetCycles(uint16_t measure, uint16_t sleep) { __touchSleepCycles = sleep; __touchMeasureCycles = measure; -#if SOC_TOUCH_VERSION_1 || SOC_TOUCH_VERSION_2 // ESP32 || ESP32S2, ESP32S3 touch_pad_set_meas_time(sleep, measure); -#endif } @@ -129,15 +134,13 @@ static void __touchInit() goto err; } touch_pad_intr_enable(); // returns ESP_OK -#endif - -#if SOC_TOUCH_VERSION_2 // ESP32S2, ESP32S3 +#elif SOC_TOUCH_VERSION_2 // ESP32S2, ESP32S3 err = touch_pad_init(); if (err != ESP_OK) { goto err; } // the next lines will drive the touch reading values -- all os them return ESP_OK - touch_pad_set_meas_time(TOUCH_PAD_SLEEP_CYCLE_DEFAULT, TOUCH_PAD_MEASURE_CYCLE_DEFAULT); + touch_pad_set_meas_time(__touchSleepCycles, __touchMeasureCycles); touch_pad_set_voltage(TOUCH_PAD_HIGH_VOLTAGE_THRESHOLD, TOUCH_PAD_LOW_VOLTAGE_THRESHOLD, TOUCH_PAD_ATTEN_VOLTAGE_THRESHOLD); touch_pad_set_idle_channel_connect(TOUCH_PAD_IDLE_CH_CONNECT_DEFAULT); touch_pad_denoise_t denoise = { @@ -171,7 +174,7 @@ static void __touchInit() return; } -static uint32_t __touchRead(uint8_t pin) +static touch_value_t __touchRead(uint8_t pin) { int8_t pad = digitalPinToTouchChannel(pin); if(pad < 0){ @@ -179,22 +182,13 @@ static uint32_t __touchRead(uint8_t pin) } __touchInit(); -#if SOC_TOUCH_VERSION_1 // ESP32 - uint16_t touch_value; -#endif - -#if SOC_TOUCH_VERSION_2 // ESP32S2 ESP32S3 - uint32_t touch_value; -#endif - -#if SOC_TOUCH_VERSION_1 || SOC_TOUCH_VERSION_2 // ESP32 || ESP32S2, ESP32S3 + touch_value_t touch_value; touch_pad_read_raw_data(pad, &touch_value); -#endif - return (uint32_t) touch_value; + return touch_value; } -static void __touchConfigInterrupt(uint8_t pin, void (*userFunc)(void), void *Args, uint32_t threshold, bool callWithArgs) +static void __touchConfigInterrupt(uint8_t pin, void (*userFunc)(void), void *Args, touch_value_t threshold, bool callWithArgs) { int8_t pad = digitalPinToTouchChannel(pin); if(pad < 0){ @@ -214,22 +208,20 @@ static void __touchConfigInterrupt(uint8_t pin, void (*userFunc)(void), void *Ar } #if SOC_TOUCH_VERSION_1 // ESP32 - touch_pad_config(pad, (uint16_t) threshold); -#endif - -#if SOC_TOUCH_VERSION_2 // ESP32S2, ESP32S3 + touch_pad_config(pad, threshold); +#elif SOC_TOUCH_VERSION_2 // ESP32S2, ESP32S3 touch_pad_set_thresh(pad, threshold); #endif } // it keeps backwards compatibility -static void __touchAttachInterrupt(uint8_t pin, void (*userFunc)(void), uint32_t threshold) +static void __touchAttachInterrupt(uint8_t pin, void (*userFunc)(void), touch_value_t threshold) { __touchConfigInterrupt(pin, userFunc, NULL, threshold, false); } // new additional version of the API with User Args -static void __touchAttachArgsInterrupt(uint8_t pin, void (*userFunc)(void), void *args, uint32_t threshold) +static void __touchAttachArgsInterrupt(uint8_t pin, void (*userFunc)(void), void *args, touch_value_t threshold) { __touchConfigInterrupt(pin, userFunc, args, threshold, true); } @@ -245,7 +237,7 @@ static void __touchDettachInterrupt(uint8_t pin) External Public Touch API Functions */ -#if SOC_TOUCH_VERSION_1 // Only for ESP32 SoC +#if SOC_TOUCH_VERSION_1 // Only for ESP32 SoC void touchInterruptSetThresholdDirection(bool mustbeLower) { if (mustbeLower) { touch_pad_set_trigger_mode(TOUCH_TRIGGER_BELOW); @@ -253,9 +245,7 @@ void touchInterruptSetThresholdDirection(bool mustbeLower) { touch_pad_set_trigger_mode(TOUCH_TRIGGER_ABOVE); } } -#endif - -#if SOC_TOUCH_VERSION_2 // Only for ESP32S2 and ESP32S3 +#elif SOC_TOUCH_VERSION_2 // Only for ESP32S2 and ESP32S3 // returns true if touch pad has been and continues pressed and false otherwise bool touchInterruptGetLastStatus(uint8_t pin) { int8_t pad = digitalPinToTouchChannel(pin); @@ -267,9 +257,9 @@ bool touchInterruptGetLastStatus(uint8_t pin) { } #endif -extern uint32_t touchRead(uint8_t) __attribute__ ((weak, alias("__touchRead"))); -extern void touchAttachInterrupt(uint8_t, voidFuncPtr, uint32_t) __attribute__ ((weak, alias("__touchAttachInterrupt"))); -extern void touchAttachInterruptArg(uint8_t, voidArgFuncPtr, void *, uint32_t) __attribute__ ((weak, alias("__touchAttachArgsInterrupt"))); +extern touch_value_t touchRead(uint8_t) __attribute__ ((weak, alias("__touchRead"))); +extern void touchAttachInterrupt(uint8_t, voidFuncPtr, touch_value_t) __attribute__ ((weak, alias("__touchAttachInterrupt"))); +extern void touchAttachInterruptArg(uint8_t, voidArgFuncPtr, void *, touch_value_t) __attribute__ ((weak, alias("__touchAttachArgsInterrupt"))); extern void touchDetachInterrupt(uint8_t) __attribute__ ((weak, alias("__touchDettachInterrupt"))); extern void touchSetCycles(uint16_t, uint16_t) __attribute__ ((weak, alias("__touchSetCycles"))); diff --git a/cores/esp32/esp32-hal-touch.h b/cores/esp32/esp32-hal-touch.h index 7968fee378c..85d9034a65d 100644 --- a/cores/esp32/esp32-hal-touch.h +++ b/cores/esp32/esp32-hal-touch.h @@ -29,6 +29,12 @@ extern "C" { #if SOC_TOUCH_SENSOR_NUM > 0 #include "esp32-hal.h" +#if SOC_TOUCH_VERSION_1 // ESP32 +typedef uint16_t touch_value_t; +#elif SOC_TOUCH_VERSION_2 // ESP32S2 ESP32S3 +typedef uint32_t touch_value_t; +#endif + /* * Set cycles that measurement operation takes * The result from touchRead, threshold and detection @@ -43,15 +49,15 @@ void touchSetCycles(uint16_t measure, uint16_t sleep); * You can use this method to chose a good threshold value * to use as value for touchAttachInterrupt * */ -uint32_t touchRead(uint8_t pin); +touch_value_t touchRead(uint8_t pin); /* * Set function to be called if touch pad value falls (ESP32) * below the given threshold / rises (ESP32-S2/S3) by given increment (threshold). * Use touchRead to determine a proper threshold between touched and untouched state * */ -void touchAttachInterrupt(uint8_t pin, void (*userFunc)(void), uint32_t threshold); -void touchAttachInterruptArg(uint8_t pin, void (*userFunc)(void*), void *arg, uint32_t threshold); +void touchAttachInterrupt(uint8_t pin, void (*userFunc)(void), touch_value_t threshold); +void touchAttachInterruptArg(uint8_t pin, void (*userFunc)(void*), void *arg, touch_value_t threshold); void touchDetachInterrupt(uint8_t pin); /* From d1702cded222af31bdde2da2b4607f22f3eb48ec Mon Sep 17 00:00:00 2001 From: Rodrigo Garcia Date: Wed, 2 Feb 2022 16:26:08 -0300 Subject: [PATCH 09/11] code review --- cores/esp32/esp32-hal-touch.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cores/esp32/esp32-hal-touch.c b/cores/esp32/esp32-hal-touch.c index 4c23bae687f..8721afdf7e8 100644 --- a/cores/esp32/esp32-hal-touch.c +++ b/cores/esp32/esp32-hal-touch.c @@ -15,7 +15,7 @@ #include "soc/soc_caps.h" #if !defined(SOC_TOUCH_VERSION_1) && !defined(SOC_TOUCH_VERSION_2) -#error CONFIG_IDF_TARGET is not supported for Touch IDF driver! +#warning CONFIG_IDF_TARGET is not supported for Touch IDF driver! #endif #if SOC_TOUCH_SENSOR_NUM > 0 From 8cd18cc4d4e8f9f074f80825f1dbca75029c5da6 Mon Sep 17 00:00:00 2001 From: Rodrigo Garcia Date: Thu, 3 Feb 2022 12:13:31 -0300 Subject: [PATCH 10/11] preprocessor messages review --- cores/esp32/esp32-hal-touch.c | 10 +++++----- cores/esp32/esp32-hal-touch.h | 7 +++++-- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/cores/esp32/esp32-hal-touch.c b/cores/esp32/esp32-hal-touch.c index 8721afdf7e8..b29333a6822 100644 --- a/cores/esp32/esp32-hal-touch.c +++ b/cores/esp32/esp32-hal-touch.c @@ -13,15 +13,15 @@ // limitations under the License. #include "soc/soc_caps.h" - -#if !defined(SOC_TOUCH_VERSION_1) && !defined(SOC_TOUCH_VERSION_2) -#warning CONFIG_IDF_TARGET is not supported for Touch IDF driver! -#endif - #if SOC_TOUCH_SENSOR_NUM > 0 + #include "driver/touch_sensor.h" #include "esp32-hal-touch.h" +#if !defined(SOC_TOUCH_VERSION_1) && !defined(SOC_TOUCH_VERSION_2) +#error Touch IDF driver Not supported! +#endif + /* Internal Private Touch Data Structure and Functions */ diff --git a/cores/esp32/esp32-hal-touch.h b/cores/esp32/esp32-hal-touch.h index 85d9034a65d..280e9bebea0 100644 --- a/cores/esp32/esp32-hal-touch.h +++ b/cores/esp32/esp32-hal-touch.h @@ -25,9 +25,13 @@ extern "C" { #endif #include "soc/soc_caps.h" +#include "esp32-hal.h" #if SOC_TOUCH_SENSOR_NUM > 0 -#include "esp32-hal.h" + +#if !defined(CONFIG_IDF_TARGET_ESP32) && !defined(CONFIG_IDF_TARGET_ESP32S2) && !defined(CONFIG_IDF_TARGET_ESP32S3) +#warning This SoC may not support the current Touch Sensor implementation! +#endif #if SOC_TOUCH_VERSION_1 // ESP32 typedef uint16_t touch_value_t; @@ -84,7 +88,6 @@ void touchInterruptSetThresholdDirection(bool mustbeLower); bool touchInterruptGetLastStatus(uint8_t pin); #endif - #endif // SOC_TOUCH_SENSOR_NUM > 0 #ifdef __cplusplus From a243f891c99975a7347792362565be3785597313 Mon Sep 17 00:00:00 2001 From: Rodrigo Garcia Date: Thu, 3 Feb 2022 13:53:18 -0300 Subject: [PATCH 11/11] preprocessor messages review --- cores/esp32/esp32-hal-touch.c | 4 ---- cores/esp32/esp32-hal-touch.h | 4 ++-- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/cores/esp32/esp32-hal-touch.c b/cores/esp32/esp32-hal-touch.c index b29333a6822..e5eaf497b5e 100644 --- a/cores/esp32/esp32-hal-touch.c +++ b/cores/esp32/esp32-hal-touch.c @@ -18,10 +18,6 @@ #include "driver/touch_sensor.h" #include "esp32-hal-touch.h" -#if !defined(SOC_TOUCH_VERSION_1) && !defined(SOC_TOUCH_VERSION_2) -#error Touch IDF driver Not supported! -#endif - /* Internal Private Touch Data Structure and Functions */ diff --git a/cores/esp32/esp32-hal-touch.h b/cores/esp32/esp32-hal-touch.h index 280e9bebea0..a8cda191c31 100644 --- a/cores/esp32/esp32-hal-touch.h +++ b/cores/esp32/esp32-hal-touch.h @@ -29,8 +29,8 @@ extern "C" { #if SOC_TOUCH_SENSOR_NUM > 0 -#if !defined(CONFIG_IDF_TARGET_ESP32) && !defined(CONFIG_IDF_TARGET_ESP32S2) && !defined(CONFIG_IDF_TARGET_ESP32S3) -#warning This SoC may not support the current Touch Sensor implementation! +#if !defined(SOC_TOUCH_VERSION_1) && !defined(SOC_TOUCH_VERSION_2) +#error Touch IDF driver Not supported! #endif #if SOC_TOUCH_VERSION_1 // ESP32