Skip to content

TOUCH - Peripheral manager implementation #8129

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions cores/esp32/esp32-hal-periman.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ typedef enum {
#endif
#if SOC_SDMMC_HOST_SUPPORTED
ESP32_BUS_TYPE_SDMMC, // IO is used as SDMMC pin
#endif
#if SOC_TOUCH_SENSOR_SUPPORTED
ESP32_BUS_TYPE_TOUCH, // IO is used as TOUCH pin
#endif
ESP32_BUS_TYPE_MAX
} peripheral_bus_type_t;
Expand Down
52 changes: 44 additions & 8 deletions cores/esp32/esp32-hal-touch.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

#include "driver/touch_sensor.h"
#include "esp32-hal-touch.h"
#include "esp32-hal-periman.h"

/*
Internal Private Touch Data Structure and Functions
Expand Down Expand Up @@ -44,6 +45,10 @@ typedef struct {

static TouchInterruptHandle_t __touchInterruptHandlers[SOC_TOUCH_SENSOR_NUM] = {0,};

static uint8_t used_pads = 0;
static bool initialized = false;
static bool channels_initialized[SOC_TOUCH_SENSOR_NUM] = { false };

static void ARDUINO_ISR_ATTR __touchISR(void * arg)
{
#if SOC_TOUCH_VERSION_1 // ESP32
Expand Down Expand Up @@ -99,11 +104,23 @@ static void __touchSetCycles(uint16_t measure, uint16_t sleep)
touch_pad_set_measurement_interval(sleep);
}


static bool touchDetachBus(void * pin){
int8_t pad = digitalPinToTouchChannel((int)(pin-1));
channels_initialized[pad] = false;
used_pads--;
if (used_pads == 0) {
if (touch_pad_deinit() != ESP_OK) //deinit touch module, as no pads are used
{
log_e("Touch module deinit failed!");
return false;
}
initialized = false;
}
return true;
}

static void __touchInit()
{
static bool initialized = false;
if(initialized){
return;
}
Expand Down Expand Up @@ -163,8 +180,7 @@ static void __touchInit()

static void __touchChannelInit(int pad)
{
static bool channels_initialized[SOC_TOUCH_SENSOR_NUM] = { false };
if(channels_initialized[pad]){
if(channels_initialized[pad]){
return;
}

Expand All @@ -186,6 +202,7 @@ static void __touchChannelInit(int pad)
#endif

channels_initialized[pad] = true;
used_pads++;
delay(20); //delay needed before reading from touch channel after config
}

Expand All @@ -197,8 +214,19 @@ static touch_value_t __touchRead(uint8_t pin)
return 0;
}

__touchInit();
__touchChannelInit(pad);
if(perimanGetPinBus(pin, ESP32_BUS_TYPE_TOUCH) == NULL){
perimanSetBusDeinit(ESP32_BUS_TYPE_TOUCH, touchDetachBus);
if(!perimanSetPinBus(pin, ESP32_BUS_TYPE_INIT, NULL)){
return 0;
}
__touchInit();
__touchChannelInit(pad);

if(!perimanSetPinBus(pin, ESP32_BUS_TYPE_TOUCH, (void *)(pin+1))){
touchDetachBus((void *)(pin+1));
return 0;
}
}

touch_value_t touch_value;
touch_pad_read_raw_data(pad, &touch_value);
Expand Down Expand Up @@ -280,9 +308,17 @@ void touchSleepWakeUpEnable(uint8_t pin, touch_value_t threshold)
log_e(" No touch pad on selected pin!");
return;
}
__touchInit();
__touchChannelInit(pad);

if(perimanGetPinBus(pin, ESP32_BUS_TYPE_TOUCH) == NULL){
perimanSetBusDeinit(ESP32_BUS_TYPE_TOUCH, touchDetachBus);
__touchInit();
__touchChannelInit(pad);
if(!perimanSetPinBus(pin, ESP32_BUS_TYPE_TOUCH, (void *)(pin+1))){
log_e("Failed to set bus to Peripheral manager");
touchDetachBus((void *)(pin+1));
return;
}
}
#if SOC_TOUCH_VERSION_1 // Only for ESP32 SoC
touch_pad_set_thresh(pad, threshold);

Expand Down