Skip to content

Commit 59f7975

Browse files
authored
TOUCH - Peripheral manager implementation (#8129)
* Touch periman implemented * Deinit previous bus first
1 parent 3a282f6 commit 59f7975

File tree

2 files changed

+47
-8
lines changed

2 files changed

+47
-8
lines changed

cores/esp32/esp32-hal-periman.h

+3
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ typedef enum {
5151
#endif
5252
#if SOC_SDMMC_HOST_SUPPORTED
5353
ESP32_BUS_TYPE_SDMMC, // IO is used as SDMMC pin
54+
#endif
55+
#if SOC_TOUCH_SENSOR_SUPPORTED
56+
ESP32_BUS_TYPE_TOUCH, // IO is used as TOUCH pin
5457
#endif
5558
ESP32_BUS_TYPE_MAX
5659
} peripheral_bus_type_t;

cores/esp32/esp32-hal-touch.c

+44-8
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
#include "driver/touch_sensor.h"
1919
#include "esp32-hal-touch.h"
20+
#include "esp32-hal-periman.h"
2021

2122
/*
2223
Internal Private Touch Data Structure and Functions
@@ -44,6 +45,10 @@ typedef struct {
4445

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

48+
static uint8_t used_pads = 0;
49+
static bool initialized = false;
50+
static bool channels_initialized[SOC_TOUCH_SENSOR_NUM] = { false };
51+
4752
static void ARDUINO_ISR_ATTR __touchISR(void * arg)
4853
{
4954
#if SOC_TOUCH_VERSION_1 // ESP32
@@ -99,11 +104,23 @@ static void __touchSetCycles(uint16_t measure, uint16_t sleep)
99104
touch_pad_set_measurement_interval(sleep);
100105
}
101106

102-
107+
static bool touchDetachBus(void * pin){
108+
int8_t pad = digitalPinToTouchChannel((int)(pin-1));
109+
channels_initialized[pad] = false;
110+
used_pads--;
111+
if (used_pads == 0) {
112+
if (touch_pad_deinit() != ESP_OK) //deinit touch module, as no pads are used
113+
{
114+
log_e("Touch module deinit failed!");
115+
return false;
116+
}
117+
initialized = false;
118+
}
119+
return true;
120+
}
103121

104122
static void __touchInit()
105123
{
106-
static bool initialized = false;
107124
if(initialized){
108125
return;
109126
}
@@ -163,8 +180,7 @@ static void __touchInit()
163180

164181
static void __touchChannelInit(int pad)
165182
{
166-
static bool channels_initialized[SOC_TOUCH_SENSOR_NUM] = { false };
167-
if(channels_initialized[pad]){
183+
if(channels_initialized[pad]){
168184
return;
169185
}
170186

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

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

@@ -197,8 +214,19 @@ static touch_value_t __touchRead(uint8_t pin)
197214
return 0;
198215
}
199216

200-
__touchInit();
201-
__touchChannelInit(pad);
217+
if(perimanGetPinBus(pin, ESP32_BUS_TYPE_TOUCH) == NULL){
218+
perimanSetBusDeinit(ESP32_BUS_TYPE_TOUCH, touchDetachBus);
219+
if(!perimanSetPinBus(pin, ESP32_BUS_TYPE_INIT, NULL)){
220+
return 0;
221+
}
222+
__touchInit();
223+
__touchChannelInit(pad);
224+
225+
if(!perimanSetPinBus(pin, ESP32_BUS_TYPE_TOUCH, (void *)(pin+1))){
226+
touchDetachBus((void *)(pin+1));
227+
return 0;
228+
}
229+
}
202230

203231
touch_value_t touch_value;
204232
touch_pad_read_raw_data(pad, &touch_value);
@@ -280,9 +308,17 @@ void touchSleepWakeUpEnable(uint8_t pin, touch_value_t threshold)
280308
log_e(" No touch pad on selected pin!");
281309
return;
282310
}
283-
__touchInit();
284-
__touchChannelInit(pad);
285311

312+
if(perimanGetPinBus(pin, ESP32_BUS_TYPE_TOUCH) == NULL){
313+
perimanSetBusDeinit(ESP32_BUS_TYPE_TOUCH, touchDetachBus);
314+
__touchInit();
315+
__touchChannelInit(pad);
316+
if(!perimanSetPinBus(pin, ESP32_BUS_TYPE_TOUCH, (void *)(pin+1))){
317+
log_e("Failed to set bus to Peripheral manager");
318+
touchDetachBus((void *)(pin+1));
319+
return;
320+
}
321+
}
286322
#if SOC_TOUCH_VERSION_1 // Only for ESP32 SoC
287323
touch_pad_set_thresh(pad, threshold);
288324

0 commit comments

Comments
 (0)