arch/xtensa/esp32: Add initial support for touch pad polling

This commit is contained in:
Lucas Saavedra Vaz 2022-12-13 16:11:34 -03:00 committed by Xiang Xiao
parent b8ef8daef9
commit 773e3fad43
8 changed files with 2681 additions and 240 deletions

View File

@ -179,6 +179,10 @@ ifeq ($(CONFIG_ESP32_RT_TIMER),y)
CHIP_CSRCS += esp32_rt_timer.c
endif
ifeq ($(CONFIG_ESP32_TOUCH),y)
CHIP_CSRCS += esp32_touch.c
endif
ifeq ($(CONFIG_ESP32_AES_ACCELERATOR),y)
CHIP_CSRCS += esp32_aes.c
endif

View File

@ -37,6 +37,7 @@
#include "hardware/esp32_iomux.h"
#include "hardware/esp32_gpio.h"
#include "hardware/esp32_soc.h"
#include "esp32_irq.h"
#include "esp32_rtc_gpio.h"
@ -48,8 +49,10 @@
****************************************************************************/
#define NGPIO_HPINS (ESP32_NIRQ_GPIO - 32)
#define NGPIO_HMASK ((1ul << NGPIO_HPINS) - 1)
#define NGPIO_HMASK ((UINT32_C(1) << NGPIO_HPINS) - 1)
#define _NA_ 0xff
#define setbits(a, bs) modifyreg32(a, 0, bs)
#define resetbits(a, bs) modifyreg32(a, bs, 0)
/****************************************************************************
* Private Data
@ -68,6 +71,30 @@ static const uint8_t g_pin2func[40] =
0x1c, 0x20, 0x14, 0x18, 0x04, 0x08, 0x0c, 0x10 /* 32-39 */
};
static const uint32_t rtc_gpio_to_addr[] =
{
RTC_GPIO_PIN0_REG,
RTC_GPIO_PIN1_REG,
RTC_GPIO_PIN2_REG,
RTC_GPIO_PIN3_REG,
RTC_GPIO_PIN4_REG,
RTC_GPIO_PIN5_REG,
RTC_GPIO_PIN6_REG,
RTC_GPIO_PIN7_REG,
RTC_GPIO_PIN8_REG,
RTC_GPIO_PIN9_REG,
RTC_GPIO_PIN10_REG,
RTC_GPIO_PIN11_REG,
RTC_GPIO_PIN12_REG,
RTC_GPIO_PIN13_REG,
RTC_GPIO_PIN14_REG,
RTC_GPIO_PIN15_REG,
RTC_GPIO_PIN16_REG,
RTC_GPIO_PIN17_REG
};
static bool g_pin_rtc_controlled[RTC_GPIO_NUMBER];
/****************************************************************************
* Private Functions
****************************************************************************/
@ -105,7 +132,7 @@ static void gpio_dispatch(int irq, uint32_t status, uint32_t *regs)
{
/* Check if there is an interrupt pending for this pin */
mask = (1ul << i);
mask = (UINT32_C(1) << i);
if ((status & mask) != 0)
{
/* Yes... perform the second level dispatch */
@ -185,6 +212,8 @@ int esp32_configgpio(int pin, gpio_pinattr_t attr)
uintptr_t regaddr;
uint32_t func;
uint32_t cntrl;
uint32_t rtc_gpio_idx;
rtc_io_desc_t rtc_reg_desc;
DEBUGASSERT(pin >= 0 && pin <= ESP32_NGPIOS);
@ -193,35 +222,137 @@ int esp32_configgpio(int pin, gpio_pinattr_t attr)
func = 0;
cntrl = 0;
if ((attr & FUNCTION_MASK) == FUNCTION_RTC) /* RTCIO */
{
if (rtc_gpio_is_valid_gpio(pin) == 0)
{
gpioerr("Pin %d is not a valid RTC pin!\n", pin);
return -1;
}
rtc_gpio_idx = g_rtc_io_num_map[pin];
rtc_reg_desc = g_rtc_io_desc[rtc_gpio_idx];
g_pin_rtc_controlled[rtc_gpio_idx] = true;
setbits(rtc_reg_desc.reg, rtc_reg_desc.mux);
modifyreg32(rtc_reg_desc.reg,
((RTC_IO_TOUCH_PAD1_FUN_SEL_V) << (rtc_reg_desc.func)),
(((RTCIO_PIN_FUNC) & RTC_IO_TOUCH_PAD1_FUN_SEL_V) <<
(rtc_reg_desc.func)));
resetbits(rtc_reg_desc.reg, rtc_reg_desc.pulldown);
resetbits(rtc_reg_desc.reg, rtc_reg_desc.pullup);
if ((attr & PULLUP) != 0)
{
if (rtc_reg_desc.pullup)
{
setbits(rtc_reg_desc.reg, rtc_reg_desc.pullup);
}
}
else if ((attr & PULLDOWN) != 0)
{
if (rtc_reg_desc.pulldown)
{
setbits(rtc_reg_desc.reg, rtc_reg_desc.pulldown);
}
}
if ((attr & INPUT) != 0)
{
/* Enable Input */
setbits(rtc_reg_desc.reg, rtc_reg_desc.ie);
/* Disable Output */
putreg32((UINT32_C(1) << pin), RTC_GPIO_ENABLE_W1TC_REG);
}
else if ((attr & OUTPUT) != 0)
{
/* Disable Input */
resetbits(rtc_reg_desc.reg, rtc_reg_desc.ie);
/* Enable Output */
putreg32((UINT32_C(1) << pin), RTC_GPIO_ENABLE_W1TS_REG);
}
else
{
resetbits(rtc_reg_desc.reg, rtc_reg_desc.ie);
putreg32((UINT32_C(1) << pin), RTC_GPIO_ENABLE_W1TC_REG);
}
if ((attr & DRIVE_MASK) != 0)
{
if (rtc_reg_desc.drv_v)
{
uint32_t val = ((attr & DRIVE_MASK) >> DRIVE_SHIFT) - 1;
modifyreg32(rtc_reg_desc.reg,
((rtc_reg_desc.drv_v) << (rtc_reg_desc.drv_s)),
(((val) & rtc_reg_desc.drv_v) << (rtc_reg_desc.drv_s)));
}
}
if ((attr & OPEN_DRAIN) != 0)
{
/* All RTC GPIOs have the same position for the drive bits.
* We can use any RTC_GPIO_PINn_PAD_DRIVER.
*/
REG_SET_FIELD(rtc_gpio_to_addr[rtc_gpio_idx],
RTC_GPIO_PIN0_PAD_DRIVER,
true);
}
else
{
REG_SET_FIELD(rtc_gpio_to_addr[rtc_gpio_idx],
RTC_GPIO_PIN0_PAD_DRIVER,
false);
}
return OK;
}
else /* GPIO */
{
if ((attr & INPUT) != 0)
{
if (pin < 32)
{
putreg32((1ul << pin), GPIO_ENABLE_W1TC_REG);
putreg32((UINT32_C(1) << pin), GPIO_ENABLE_W1TC_REG);
}
else
{
putreg32((1ul << (pin - 32)), GPIO_ENABLE1_W1TC_REG);
putreg32((UINT32_C(1) << (pin - 32)), GPIO_ENABLE1_W1TC_REG);
}
/* Input enable */
func |= FUN_IE;
/* Some pins only support Pull-Up and Pull-Down resistor on RTC GPIO */
/* Some pins only support Pull-Up/Pull-Down resistor on RTC GPIO */
if (rtc_gpio_is_valid_gpio(pin))
{
uint32_t rtc_gpio_idx = g_rtc_io_num_map[pin];
rtc_gpio_idx = g_rtc_io_num_map[pin];
rtc_reg_desc = g_rtc_io_desc[rtc_gpio_idx];
g_pin_rtc_controlled[rtc_gpio_idx] = false;
if (rtc_gpio_idx >= RTCIO_GPIO34_IDX)
/* Disable RTC control */
resetbits(rtc_reg_desc.reg, rtc_reg_desc.mux);
if (rtc_reg_desc.pullup == 0)
{
gpioerr("Pins 34-39 don't support PullUp/PullDown\n");
assert(0);
}
else
{
uint32_t regval;
uint32_t rtc_gpio_pin;
uint32_t rtc_gpio_reg;
bool en_pu = false;
bool en_pd = false;
@ -236,15 +367,15 @@ int esp32_configgpio(int pin, gpio_pinattr_t attr)
/* Get the pin register */
rtc_gpio_pin = g_rtc_io_desc[rtc_gpio_idx];
rtc_gpio_reg = g_rtc_io_desc[rtc_gpio_idx].reg;
/* Read the current value from RTC GPIO pin */
regval = getreg32(rtc_gpio_pin);
regval = getreg32(rtc_gpio_reg);
/* RTC_IO_X32P (GPIO32) uses different PU/PD bits */
if (rtc_gpio_idx == RTCIO_GPIO32_IDX)
if (rtc_gpio_idx == RTCIO_GPIO32_CHANNEL)
{
/* First, disable PU/PD */
@ -269,7 +400,7 @@ int esp32_configgpio(int pin, gpio_pinattr_t attr)
regval |= en_pd ? DEFAULT_RTC_PD_BIT : 0;
}
putreg32(regval, rtc_gpio_pin);
putreg32(regval, rtc_gpio_reg);
}
}
else if ((attr & PULLUP) != 0)
@ -288,11 +419,11 @@ int esp32_configgpio(int pin, gpio_pinattr_t attr)
{
if (pin < 32)
{
putreg32((1ul << pin), GPIO_ENABLE_W1TS_REG);
putreg32((UINT32_C(1) << pin), GPIO_ENABLE_W1TS_REG);
}
else
{
putreg32((1ul << (pin - 32)), GPIO_ENABLE1_W1TS_REG);
putreg32((UINT32_C(1) << (pin - 32)), GPIO_ENABLE1_W1TS_REG);
}
}
@ -336,6 +467,7 @@ int esp32_configgpio(int pin, gpio_pinattr_t attr)
putreg32(cntrl, regaddr);
return OK;
}
}
/****************************************************************************
* Name: esp32_gpiowrite
@ -353,22 +485,24 @@ void esp32_gpiowrite(int pin, bool value)
{
if (pin < 32)
{
putreg32((uint32_t)(1ul << pin), GPIO_OUT_W1TS_REG);
putreg32((uint32_t)(UINT32_C(1) << pin), GPIO_OUT_W1TS_REG);
}
else
{
putreg32((uint32_t)(1ul << (pin - 32)), GPIO_OUT1_W1TS_REG);
putreg32((uint32_t)(UINT32_C(1) << (pin - 32)),
GPIO_OUT1_W1TS_REG);
}
}
else
{
if (pin < 32)
{
putreg32((uint32_t)(1ul << pin), GPIO_OUT_W1TC_REG);
putreg32((uint32_t)(UINT32_C(1) << pin), GPIO_OUT_W1TC_REG);
}
else
{
putreg32((uint32_t)(1ul << (pin - 32)), GPIO_OUT1_W1TC_REG);
putreg32((uint32_t)(UINT32_C(1) << (pin - 32)),
GPIO_OUT1_W1TC_REG);
}
}
}
@ -579,11 +713,11 @@ void esp32_gpio_matrix_out(uint32_t gpio, uint32_t signal_idx, bool out_inv,
if (gpio < 32)
{
putreg32((1ul << gpio), GPIO_ENABLE_W1TS_REG);
putreg32((UINT32_C(1) << gpio), GPIO_ENABLE_W1TS_REG);
}
else
{
putreg32((1ul << (gpio - 32)), GPIO_ENABLE1_W1TS_REG);
putreg32((UINT32_C(1) << (gpio - 32)), GPIO_ENABLE1_W1TS_REG);
}
if (out_inv)

View File

@ -65,6 +65,7 @@
# define FUNCTION_4 (4 << FUNCTION_SHIFT)
# define FUNCTION_5 (5 << FUNCTION_SHIFT)
# define FUNCTION_6 (6 << FUNCTION_SHIFT)
# define FUNCTION_RTC (7 << FUNCTION_SHIFT)
#define DRIVE_SHIFT 9
#define DRIVE_MASK (7 << DRIVE_SHIFT)
@ -83,6 +84,7 @@
# define INPUT_FUNCTION_4 (INPUT_FUNCTION | FUNCTION_4)
# define INPUT_FUNCTION_5 (INPUT_FUNCTION | FUNCTION_5)
# define INPUT_FUNCTION_6 (INPUT_FUNCTION | FUNCTION_6)
# define INPUT_FUNCTION_RTC (INPUT_FUNCTION | FUNCTION_RTC)
#define OUTPUT_FUNCTION (OUTPUT | FUNCTION)
# define OUTPUT_FUNCTION_1 (OUTPUT_FUNCTION | FUNCTION_1)
# define OUTPUT_FUNCTION_2 (OUTPUT_FUNCTION | FUNCTION_2)
@ -90,6 +92,7 @@
# define OUTPUT_FUNCTION_4 (OUTPUT_FUNCTION | FUNCTION_4)
# define OUTPUT_FUNCTION_5 (OUTPUT_FUNCTION | FUNCTION_5)
# define OUTPUT_FUNCTION_6 (OUTPUT_FUNCTION | FUNCTION_6)
# define OUTPUT_FUNCTION_RTC (OUTPUT_FUNCTION | FUNCTION_RTC)
/* Interrupt type used with esp32_gpioirqenable() */

View File

@ -25,7 +25,9 @@
* Included Files
****************************************************************************/
#include "hardware/esp32_gpio.h"
#include "hardware/esp32_rtc_io.h"
#include "hardware/esp32_rtccntl.h"
/****************************************************************************
* Pre-processor Definitions
@ -36,57 +38,103 @@
#define SPECIAL_RTC_PU_BIT (1 << 22)
#define SPECIAL_RTC_PD_BIT (1 << 23)
/* RTC GPIO Index */
#define RTCIO_PIN_FUNC 0
#define RTCIO_GPIO0_IDX 0 /* RTCIO_IDX_0 */
#define RTCIO_GPIO2_IDX 1 /* RTCIO_IDX_2 */
#define RTCIO_GPIO4_IDX 2 /* RTCIO_IDX_4 */
#define RTCIO_GPIO12_IDX 3 /* RTCIO_IDX_12 */
#define RTCIO_GPIO13_IDX 4 /* RTCIO_IDX_13 */
#define RTCIO_GPIO14_IDX 5 /* RTCIO_IDX_14 */
#define RTCIO_GPIO15_IDX 6 /* RTCIO_IDX_15 */
#define RTCIO_GPIO25_IDX 7 /* RTCIO_IDX_25 */
#define RTCIO_GPIO26_IDX 8 /* RTCIO_IDX_26 */
#define RTCIO_GPIO27_IDX 9 /* RTCIO_IDX_27 */
#define RTCIO_GPIO32_IDX 10 /* RTCIO_IDX_32 */
#define RTCIO_GPIO33_IDX 11 /* RTCIO_IDX_33 */
#define RTCIO_GPIO34_IDX 12 /* RTCIO_IDX_34 */
#define RTCIO_GPIO35_IDX 13 /* RTCIO_IDX_35 */
#define RTCIO_GPIO36_IDX 14 /* RTCIO_IDX_36 */
#define RTCIO_GPIO37_IDX 15 /* RTCIO_IDX_37 */
#define RTCIO_GPIO38_IDX 16 /* RTCIO_IDX_38 */
#define RTCIO_GPIO39_IDX 17 /* RTCIO_IDX_39 */
/* RTC GPIO channels */
#define RTC_GPIO_NUMBER 18
#define RTCIO_GPIO36_CHANNEL 0 /* RTCIO_CHANNEL_0 */
#define RTCIO_CHANNEL_0_GPIO_NUM 36
#define RTCIO_GPIO37_CHANNEL 1 /* RTCIO_CHANNEL_1 */
#define RTCIO_CHANNEL_1_GPIO_NUM 37
#define RTCIO_GPIO38_CHANNEL 2 /* RTCIO_CHANNEL_2 */
#define RTCIO_CHANNEL_2_GPIO_NUM 38
#define RTCIO_GPIO39_CHANNEL 3 /* RTCIO_CHANNEL_3 */
#define RTCIO_CHANNEL_3_GPIO_NUM 39
#define RTCIO_GPIO34_CHANNEL 4 /* RTCIO_CHANNEL_4 */
#define RTCIO_CHANNEL_4_GPIO_NUM 34
#define RTCIO_GPIO35_CHANNEL 5 /* RTCIO_CHANNEL_5 */
#define RTCIO_CHANNEL_5_GPIO_NUM 35
#define RTCIO_GPIO25_CHANNEL 6 /* RTCIO_CHANNEL_6 */
#define RTCIO_CHANNEL_6_GPIO_NUM 25
#define RTCIO_GPIO26_CHANNEL 7 /* RTCIO_CHANNEL_7 */
#define RTCIO_CHANNEL_7_GPIO_NUM 26
#define RTCIO_GPIO33_CHANNEL 8 /* RTCIO_CHANNEL_8 */
#define RTCIO_CHANNEL_8_GPIO_NUM 33
#define RTCIO_GPIO32_CHANNEL 9 /* RTCIO_CHANNEL_9 */
#define RTCIO_CHANNEL_9_GPIO_NUM 32
#define RTCIO_GPIO4_CHANNEL 10 /* RTCIO_CHANNEL_10 */
#define RTCIO_CHANNEL_10_GPIO_NUM 4
#define RTCIO_GPIO0_CHANNEL 11 /* RTCIO_CHANNEL_11 */
#define RTCIO_CHANNEL_11_GPIO_NUM 0
#define RTCIO_GPIO2_CHANNEL 12 /* RTCIO_CHANNEL_12 */
#define RTCIO_CHANNEL_12_GPIO_NUM 2
#define RTCIO_GPIO15_CHANNEL 13 /* RTCIO_CHANNEL_13 */
#define RTCIO_CHANNEL_13_GPIO_NUM 15
#define RTCIO_GPIO13_CHANNEL 14 /* RTCIO_CHANNEL_14 */
#define RTCIO_CHANNEL_14_GPIO_NUM 13
#define RTCIO_GPIO12_CHANNEL 15 /* RTCIO_CHANNEL_15 */
#define RTCIO_CHANNEL_15_GPIO_NUM 12
#define RTCIO_GPIO14_CHANNEL 16 /* RTCIO_CHANNEL_16 */
#define RTCIO_CHANNEL_16_GPIO_NUM 14
#define RTCIO_GPIO27_CHANNEL 17 /* RTCIO_CHANNEL_17 */
#define RTCIO_CHANNEL_17_GPIO_NUM 27
/****************************************************************************
* Public Types
****************************************************************************/
typedef struct
{
uint32_t reg; /* Register of RTC pad, or 0 if not an RTC GPIO */
uint32_t mux; /* Bit mask for selecting digital pad or RTC pad */
uint32_t func; /* Shift of pad function (FUN_SEL) field */
uint32_t ie; /* Mask of input enable */
uint32_t pullup; /* Mask of pullup enable */
uint32_t pulldown; /* Mask of pulldown enable */
uint32_t slpsel; /* If slpsel bit is set, slpie will be used as pad
* input enabled signal in sleep mode
*/
uint32_t slpie; /* Mask of input enable in sleep mode */
uint32_t slpoe; /* Mask of output enable in sleep mode */
uint32_t hold; /* Mask of hold enable */
uint32_t hold_force; /* Mask of hold_force bit for RTC IO in
* RTC_CNTL_HOLD_REG
*/
uint32_t drv_v; /* Mask of drive capability */
uint32_t drv_s; /* Offset of drive capability */
int rtc_num; /* GPIO number (corresponds to RTC pad) */
} rtc_io_desc_t;
/****************************************************************************
* Public Data
****************************************************************************/
const int g_rtc_io_num_map[GPIO_PIN_COUNT + 1] =
static const int g_rtc_io_num_map[GPIO_PIN_COUNT + 1] =
{
RTCIO_GPIO0_IDX, /* GPIO0 */
RTCIO_GPIO0_CHANNEL, /* GPIO0 */
-1, /* GPIO1 not supported */
RTCIO_GPIO2_IDX, /* GPIO2 */
RTCIO_GPIO2_CHANNEL, /* GPIO2 */
-1, /* GPIO3 not supported */
RTCIO_GPIO4_IDX, /* GPIO4 */
RTCIO_GPIO4_CHANNEL, /* GPIO4 */
-1, /* GPIO5 not supported */
-1, /* GPIO6 not supported */
-1, /* GPIO7 not supported */
@ -94,10 +142,10 @@ const int g_rtc_io_num_map[GPIO_PIN_COUNT + 1] =
-1, /* GPIO9 not supported */
-1, /* GPIO10 not supported */
-1, /* GPIO11 not supported */
RTCIO_GPIO12_IDX, /* GPIO12 */
RTCIO_GPIO13_IDX, /* GPIO13 */
RTCIO_GPIO14_IDX, /* GPIO14 */
RTCIO_GPIO15_IDX, /* GPIO15 */
RTCIO_GPIO12_CHANNEL, /* GPIO12 */
RTCIO_GPIO13_CHANNEL, /* GPIO13 */
RTCIO_GPIO14_CHANNEL, /* GPIO14 */
RTCIO_GPIO15_CHANNEL, /* GPIO15 */
-1, /* GPIO16 not supported */
-1, /* GPIO17 not supported */
-1, /* GPIO18 not supported */
@ -107,43 +155,327 @@ const int g_rtc_io_num_map[GPIO_PIN_COUNT + 1] =
-1, /* GPIO22 not supported */
-1, /* GPIO23 not supported */
-1, /* GPIO24 not supported */
RTCIO_GPIO25_IDX, /* GPIO25 */
RTCIO_GPIO26_IDX, /* GPIO26 */
RTCIO_GPIO27_IDX, /* GPIO27 */
RTCIO_GPIO25_CHANNEL, /* GPIO25 */
RTCIO_GPIO26_CHANNEL, /* GPIO26 */
RTCIO_GPIO27_CHANNEL, /* GPIO27 */
-1, /* GPIO28 not supported */
-1, /* GPIO29 not supported */
-1, /* GPIO30 not supported */
-1, /* GPIO31 not supported */
RTCIO_GPIO32_IDX, /* GPIO32 */
RTCIO_GPIO33_IDX, /* GPIO33 */
RTCIO_GPIO34_IDX, /* GPIO34 */
RTCIO_GPIO35_IDX, /* GPIO35 */
RTCIO_GPIO36_IDX, /* GPIO36 */
RTCIO_GPIO37_IDX, /* GPIO37 */
RTCIO_GPIO38_IDX, /* GPIO38 */
RTCIO_GPIO39_IDX /* GPIO39 */
RTCIO_GPIO32_CHANNEL, /* GPIO32 */
RTCIO_GPIO33_CHANNEL, /* GPIO33 */
RTCIO_GPIO34_CHANNEL, /* GPIO34 */
RTCIO_GPIO35_CHANNEL, /* GPIO35 */
RTCIO_GPIO36_CHANNEL, /* GPIO36 */
RTCIO_GPIO37_CHANNEL, /* GPIO37 */
RTCIO_GPIO38_CHANNEL, /* GPIO38 */
RTCIO_GPIO39_CHANNEL /* GPIO39 */
};
const int g_rtc_io_desc[RTC_GPIO_NUMBER] =
static const rtc_io_desc_t g_rtc_io_desc[RTC_GPIO_NUMBER] =
{
/* REG
* MUX select
* Function select
* Input enable
* Pullup
* Pulldown
* Sleep select
* Sleep input enable
* PAD hold
* Pad force hold
* Mask of drive capability Offset
* GPIO number
*/
{
RTC_IO_SENSOR_PADS_REG,
RTC_IO_SENSE1_MUX_SEL_M,
RTC_IO_SENSE1_FUN_SEL_S,
RTC_IO_SENSE1_FUN_IE_M,
0,
0,
RTC_IO_SENSE1_SLP_SEL_M,
RTC_IO_SENSE1_SLP_IE_M,
0,
RTC_IO_SENSE1_HOLD_M,
RTC_CNTL_SENSE1_HOLD_FORCE_M,
0,
0,
RTCIO_CHANNEL_0_GPIO_NUM
},
{
RTC_IO_SENSOR_PADS_REG,
RTC_IO_SENSE2_MUX_SEL_M,
RTC_IO_SENSE2_FUN_SEL_S,
RTC_IO_SENSE2_FUN_IE_M,
0,
0,
RTC_IO_SENSE2_SLP_SEL_M,
RTC_IO_SENSE2_SLP_IE_M,
0,
RTC_IO_SENSE2_HOLD_M,
RTC_CNTL_SENSE2_HOLD_FORCE_M,
0,
0,
RTCIO_CHANNEL_1_GPIO_NUM
},
{
RTC_IO_SENSOR_PADS_REG,
RTC_IO_SENSE3_MUX_SEL_M,
RTC_IO_SENSE3_FUN_SEL_S,
RTC_IO_SENSE3_FUN_IE_M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A_SRC_ESP32_ESP32_RTC_GPIO_H */

View File

@ -0,0 +1,621 @@
/****************************************************************************
* arch/xtensa/src/esp32/esp32_touch.c
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <assert.h>
#include <debug.h>
#include <stdint.h>
#include <sys/types.h>
#include <nuttx/arch.h>
#include <nuttx/irq.h>
#include <nuttx/kmalloc.h>
#include <nuttx/spinlock.h>
#include <arch/irq.h>
#include "xtensa.h"
#include "esp32_gpio.h"
#include "esp32_irq.h"
#include "esp32_rt_timer.h"
#include "esp32_rtc.h"
#include "esp32_touch.h"
#include "esp32_touch_lowerhalf.h"
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#define TOUCH_PAD_FILTER_FACTOR_DEFAULT (4) /* IIR filter coefficient */
#define TOUCH_PAD_SHIFT_DEFAULT (4) /* Increase computing accuracy */
#define TOUCH_PAD_SHIFT_ROUND_DEFAULT (8) /* ROUND = 2^(n-1) */
#define TOUCH_GET_IO_NUM(channel) (touch_channel_to_gpio[channel])
/****************************************************************************
* Private Types
****************************************************************************/
struct touch_config_volt_s
{
enum touch_high_volt_e refh;
enum touch_low_volt_e refl;
enum touch_volt_atten_e atten;
};
struct touch_config_meas_mode_s
{
enum touch_cnt_slope_e slope;
enum touch_tie_opt_e tie_opt;
};
#ifdef CONFIG_ESP32_TOUCH_FILTER
struct touch_filter_s
{
struct rt_timer_args_s timer_args;
struct rt_timer_s *timer_handler;
uint16_t filtered_val[TOUCH_SENSOR_PINS];
uint16_t raw_val[TOUCH_SENSOR_PINS];
uint32_t period_ms;
};
#endif
/****************************************************************************
* Private Function Prototypes
****************************************************************************/
#ifdef CONFIG_ESP32_TOUCH_FILTER
static uint32_t touch_filter_iir(uint32_t in_now,
uint32_t out_last,
uint32_t k);
static void touch_filter_cb(void *arg);
static void touch_filter_start(uint32_t filter_period_ms);
#endif
static uint16_t touch_read(enum touch_pad_e tp);
static void touch_clear_group_mask(uint16_t set1_mask,
uint16_t set2_mask,
uint16_t en_mask);
static void touch_config(enum touch_pad_e tp, uint16_t threshold);
static void touch_init(void);
static void touch_set_group_mask(uint16_t set1_mask,
uint16_t set2_mask,
uint16_t en_mask);
static void touch_set_meas_mode(enum touch_pad_e tp,
struct touch_config_meas_mode_s *meas);
static void touch_set_voltage(struct touch_config_volt_s *volt);
static void touch_io_init(enum touch_pad_e tp);
/****************************************************************************
* Private Data
****************************************************************************/
#ifdef CONFIG_ESP32_TOUCH_FILTER
static struct touch_filter_s *touch_pad_filter = NULL;
#endif
static mutex_t *touch_mux = NULL;
static uint16_t touch_pad_init_bit = 0x0000;
static uint16_t touch_pad_logic_threshold = 0;
static spinlock_t lock;
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Name: touch_init
*
* Description:
* Initialize the touch pad driver.
*
* Input Parameters:
* None.
*
* Returned Value:
* None.
*
****************************************************************************/
static void touch_init(void)
{
if (touch_mux == NULL)
{
touch_mux = (mutex_t *) kmm_zalloc(sizeof(mutex_t));
if (touch_mux == NULL)
{
ierr("Failed to initialize touch pad driver.\n");
assert(0);
}
nxmutex_init(touch_mux);
irqstate_t flags = spin_lock_irqsave(&lock);
touch_lh_stop_fsm();
touch_lh_intr_disable();
touch_lh_intr_clear();
touch_lh_clear_channel_mask(TOUCH_BIT_MASK_ALL);
touch_lh_clear_group_mask(TOUCH_BIT_MASK_ALL, TOUCH_BIT_MASK_ALL);
touch_lh_set_trigger_mode(TOUCH_TRIGGER_MODE_DEFAULT);
touch_lh_set_trigger_source(TOUCH_TRIGGER_SOURCE_DEFAULT);
touch_lh_clear_trigger_status_mask();
touch_lh_set_meas_time(TOUCH_MEASURE_CYCLE_DEFAULT);
touch_lh_set_sleep_time(TOUCH_SLEEP_CYCLE_DEFAULT);
touch_lh_set_fsm_mode(TOUCH_FSM_MODE_DEFAULT);
touch_lh_start_fsm();
spin_unlock_irqrestore(&lock, flags);
}
}
/****************************************************************************
* Name: touch_filter_iir
*
* Description:
* Infinite Impulse Response filter function.
*
* Input Parameters:
* in_now - Raw value to be filtered;
* out_last - Last value outputed;
* k - The filter coefficient.
*
* Returned Value:
* Filtered value.
*
****************************************************************************/
#ifdef CONFIG_ESP32_TOUCH_FILTER
static uint32_t touch_filter_iir(uint32_t in_now,
uint32_t out_last,
uint32_t k)
{
if (k == 0)
{
return in_now;
}
else
{
uint32_t out_now = (in_now + (k - 1) * out_last) / k;
return out_now;
}
}
/****************************************************************************
* Name: touch_filter_cb
*
* Description:
* Filter timer callback.
* Measures channels, applies filter and restart timers.
*
* Input Parameters:
* arg - Pointer to a memory location containing the function arguments.
*
* Returned Value:
* None.
*
****************************************************************************/
static void touch_filter_cb(void *arg)
{
if (touch_pad_filter == NULL || touch_mux == NULL)
{
return;
}
uint16_t val = 0;
static uint32_t filtered_temp[TOUCH_SENSOR_PINS];
nxmutex_lock(touch_mux);
for (int i = 0; i < TOUCH_SENSOR_PINS; i++)
{
if ((touch_pad_init_bit >> i) & 0x1)
{
val = touch_read(i);
touch_pad_filter->raw_val[i] = val;
filtered_temp[i] = filtered_temp[i] == 0 ? ((uint32_t)val <<
TOUCH_PAD_SHIFT_DEFAULT) : filtered_temp[i];
filtered_temp[i] =
touch_filter_iir((val << TOUCH_PAD_SHIFT_DEFAULT),
filtered_temp[i],
TOUCH_PAD_FILTER_FACTOR_DEFAULT);
touch_pad_filter->filtered_val[i] = (filtered_temp[i] +
TOUCH_PAD_SHIFT_ROUND_DEFAULT) >> TOUCH_PAD_SHIFT_DEFAULT;
}
}
rt_timer_start(touch_pad_filter->timer_handler,
touch_pad_filter->period_ms * USEC_PER_MSEC,
false);
nxmutex_unlock(touch_mux);
}
/****************************************************************************
* Name: touch_filter_start
*
* Description:
* Start the touch pad filter.
*
* Input Parameters:
* filter_period_ms - The filter measurement interval in milliseconds.
*
* Returned Value:
* None.
*
****************************************************************************/
static void touch_filter_start(uint32_t filter_period_ms)
{
if (touch_mux == NULL)
{
ierr("ERROR: Touch pads not initialized.\n");
return;
}
nxmutex_lock(touch_mux);
if (touch_pad_filter == NULL)
{
touch_pad_filter = (struct touch_filter_s *)
kmm_zalloc(sizeof(struct touch_filter_s));
if (touch_pad_filter == NULL)
{
ierr("ERROR: Failed to initialize touch filter.\n");
nxmutex_unlock(touch_mux);
return;
}
touch_pad_filter->timer_args.arg = NULL;
touch_pad_filter->timer_args.callback = touch_filter_cb;
rt_timer_create(&(touch_pad_filter->timer_args),
&(touch_pad_filter->timer_handler));
touch_pad_filter->period_ms = filter_period_ms;
}
nxmutex_unlock(touch_mux);
touch_filter_cb(NULL);
}
#endif
/****************************************************************************
* Name: touch_set_group_mask
*
* Description:
* Activate channels in touch pad groups.
*
* Input Parameters:
* set1_mask - The SET1 group bitmask;
* set2_mask - The SET2 group bitmask;
* en_mask - The working group bitmask.
*
* Returned Value:
* None.
*
****************************************************************************/
static void touch_set_group_mask(uint16_t set1_mask,
uint16_t set2_mask,
uint16_t en_mask)
{
DEBUGASSERT(set1_mask <= TOUCH_BIT_MASK_ALL &&
set2_mask <= TOUCH_BIT_MASK_ALL &&
en_mask <= TOUCH_BIT_MASK_ALL);
irqstate_t flags = spin_lock_irqsave(&lock);
touch_lh_set_group_mask(set1_mask, set2_mask);
touch_lh_set_channel_mask(en_mask);
spin_unlock_irqrestore(&lock, flags);
}
/****************************************************************************
* Name: touch_clear_group_mask
*
* Description:
* Deactivate channels in touch pad groups.
*
* Input Parameters:
* set1_mask - The SET1 group bitmask;
* set2_mask - The SET2 group bitmask;
* en_mask - The working group bitmask.
*
* Returned Value:
* None.
*
****************************************************************************/
static void touch_clear_group_mask(uint16_t set1_mask,
uint16_t set2_mask,
uint16_t en_mask)
{
DEBUGASSERT(set1_mask <= TOUCH_BIT_MASK_ALL &&
set2_mask <= TOUCH_BIT_MASK_ALL &&
en_mask <= TOUCH_BIT_MASK_ALL);
irqstate_t flags = spin_lock_irqsave(&lock);
touch_lh_clear_channel_mask(en_mask);
touch_lh_clear_group_mask(set1_mask, set2_mask);
spin_unlock_irqrestore(&lock, flags);
}
/****************************************************************************
* Name: touch_config
*
* Description:
* Configure a touch pad channel.
*
* Input Parameters:
* tp - The touch pad channel;
* threshold - The interruption threshold value.
*
* Returned Value:
* None.
*
****************************************************************************/
static void touch_config(enum touch_pad_e tp, uint16_t threshold)
{
DEBUGASSERT(tp < TOUCH_SENSOR_PINS);
if (touch_mux == NULL)
{
ierr("ERROR: Touch pads not initialized.\n");
return;
}
touch_io_init(tp);
irqstate_t flags = spin_lock_irqsave(&lock);
touch_lh_set_slope(tp, TOUCH_SLOPE_DEFAULT);
touch_lh_set_tie_option(tp, TOUCH_TIE_OPT_DEFAULT);
touch_lh_set_threshold(tp, threshold);
spin_unlock_irqrestore(&lock, flags);
enum touch_fsm_mode_e mode = touch_lh_get_fsm_mode();
if (mode == TOUCH_FSM_MODE_SW)
{
touch_clear_group_mask((1 << tp), (1 << tp), (1 << tp));
touch_pad_init_bit |= (1 << tp);
}
else
{
uint32_t wait_time_ms = 0;
uint16_t sleep_time = touch_lh_get_sleep_time();
uint16_t meas_cycle = touch_lh_get_meas_time();
uint32_t rtc_slow_clk_freq = esp32_rtc_clk_slow_freq_get_hz();
uint32_t rtc_fast_clk_freq = esp32_rtc_clk_fast_freq_get_hz();
touch_set_group_mask((1 << tp), (1 << tp), (1 << tp));
/* If the FSM mode is 'TOUCH_FSM_MODE_TIMER', The data will be ready
* after one measurement cycle, after this function is executed.
* Otherwise, the "touch_value" by "touch_read" is 0.
*/
wait_time_ms = sleep_time / (rtc_slow_clk_freq / 1000) + meas_cycle /
(rtc_fast_clk_freq / 1000);
up_udelay(wait_time_ms ? wait_time_ms * USEC_PER_MSEC : 1);
touch_pad_init_bit |= (1 << tp);
}
}
/****************************************************************************
* Name: touch_read
*
* Description:
* Read a touch pad.
*
* Input Parameters:
* tp - The touch pad channel.
*
* Returned Value:
* The read value.
*
****************************************************************************/
static uint16_t touch_read(enum touch_pad_e tp)
{
DEBUGASSERT(tp < TOUCH_SENSOR_PINS);
enum touch_fsm_mode_e mode = touch_lh_get_fsm_mode();
uint16_t val = 0xffff;
if (mode == TOUCH_FSM_MODE_SW)
{
touch_set_group_mask((1 << tp), (1 << tp), (1 << tp));
irqstate_t flags = spin_lock_irqsave(&lock);
touch_lh_start_sw_meas();
spin_unlock_irqrestore(&lock, flags);
while (!touch_lh_meas_is_done());
val = touch_lh_read_raw_data(tp);
touch_clear_group_mask((1 << tp), (1 << tp), (1 << tp));
}
else
{
while (!touch_lh_meas_is_done());
val = touch_lh_read_raw_data(tp);
}
return val;
}
/****************************************************************************
* Name: touch_set_voltage
*
* Description:
* Set the touch pads voltage configuration.
*
* Input Parameters:
* volt - The new configuration struct.
*
* Returned Value:
* None.
*
****************************************************************************/
static void touch_set_voltage(struct touch_config_volt_s *volt)
{
irqstate_t flags = spin_lock_irqsave(&lock);
touch_lh_set_voltage_high(volt->refh);
touch_lh_set_voltage_low(volt->refl);
touch_lh_set_voltage_attenuation(volt->atten);
spin_unlock_irqrestore(&lock, flags);
}
/****************************************************************************
* Name: touch_set_voltage
*
* Description:
* Set the measurement mode for a given touch pad.
*
* Input Parameters:
* tp - The touch pad channel;
* meas - The new configuration struct.
*
* Returned Value:
* None.
*
****************************************************************************/
static void touch_set_meas_mode(enum touch_pad_e tp,
struct touch_config_meas_mode_s *meas)
{
DEBUGASSERT(tp < TOUCH_SENSOR_PINS);
irqstate_t flags = spin_lock_irqsave(&lock);
touch_lh_set_slope(tp, meas->slope);
touch_lh_set_tie_option(tp, meas->tie_opt);
spin_unlock_irqrestore(&lock, flags);
}
/****************************************************************************
* Name: touch_io_init
*
* Description:
* Initialize GPIOs for a given touch pad.
*
* Input Parameters:
* tp - The touch pad channel.
*
* Returned Value:
* None.
*
****************************************************************************/
static void touch_io_init(enum touch_pad_e tp)
{
DEBUGASSERT(tp < TOUCH_SENSOR_PINS);
uint8_t gpio_num = TOUCH_GET_IO_NUM(tp);
esp32_configgpio(gpio_num, FUNCTION_RTC);
}
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: esp32_configtouch
*
* Description:
* Configure a touch pad channel.
*
* Input Parameters:
* tp - The touch pad channel;
* config - The touch pad configuration structure.
*
* Returned Value:
* OK.
*
****************************************************************************/
int esp32_configtouch(enum touch_pad_e tp, struct touch_config_s config)
{
struct touch_config_volt_s volt_config =
{
.refh = config.refh,
.refl = config.refl,
.atten = config.atten
};
struct touch_config_meas_mode_s meas_config =
{
.slope = config.slope,
.tie_opt = config.tie_opt
};
touch_init();
touch_config(tp, config.interrupt_threshold);
touch_set_meas_mode(tp, &meas_config);
touch_lh_set_fsm_mode(config.fsm_mode);
touch_set_voltage(&volt_config);
touch_pad_logic_threshold = config.logic_threshold;
#ifdef CONFIG_ESP32_TOUCH_FILTER
touch_filter_start(config.filter_period);
#endif
return OK;
}
/****************************************************************************
* Name: esp32_touchread
*
* Description:
* Read a touch pad channel.
*
* Input Parameters:
* tp - The touch pad channel.
*
* Returned Value:
* 0 if touch pad pressed, 1 if released.
*
****************************************************************************/
bool esp32_touchread(enum touch_pad_e tp)
{
#ifdef CONFIG_ESP32_TOUCH_FILTER
uint16_t value = touch_pad_filter->filtered_val[tp];
#else
uint16_t value = touch_read(tp);
#endif
iinfo("Touch pad %d value: %u\n", tp, value);
return (value > touch_pad_logic_threshold);
}

View File

@ -0,0 +1,145 @@
/****************************************************************************
* arch/xtensa/src/esp32/esp32_touch.h
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
#ifndef __ARCH_XTENSA_SRC_ESP32_ESP32_TOUCH_H
#define __ARCH_XTENSA_SRC_ESP32_ESP32_TOUCH_H
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <stdint.h>
#include <stdbool.h>
#include "esp32_touch_lowerhalf.h"
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#define TOUCH_BIT_MASK_ALL ((1<<TOUCH_SENSOR_PINS)-1)
#define TOUCH_SLOPE_DEFAULT (TOUCH_SLOPE_7)
#define TOUCH_TIE_OPT_DEFAULT (TOUCH_TIE_OPT_LOW)
#define TOUCH_BIT_MASK_MAX (TOUCH_BIT_MASK_ALL)
#define TOUCH_HIGH_VOLTAGE_THRESHOLD (TOUCH_HVOLT_2V7)
#define TOUCH_LOW_VOLTAGE_THRESHOLD (TOUCH_LVOLT_0V5)
#define TOUCH_ATTEN_VOLTAGE_THRESHOLD (TOUCH_HVOLT_ATTEN_0V5)
#define TOUCH_THRESHOLD_MAX (0)
#define TOUCH_SLEEP_CYCLE_DEFAULT (0x1000)
#define TOUCH_MEASURE_CYCLE_DEFAULT (0x7fff)
#define TOUCH_FSM_MODE_DEFAULT (TOUCH_FSM_MODE_SW)
#define TOUCH_TRIGGER_MODE_DEFAULT (TOUCH_TRIGGER_BELOW)
#define TOUCH_TRIGGER_SOURCE_DEFAULT (TOUCH_TRIGGER_SOURCE_SET1)
/****************************************************************************
* Public Types
****************************************************************************/
#ifndef __ASSEMBLY__
struct touch_config_s
{
enum touch_high_volt_e refh;
enum touch_low_volt_e refl;
enum touch_volt_atten_e atten;
enum touch_cnt_slope_e slope;
enum touch_tie_opt_e tie_opt;
enum touch_fsm_mode_e fsm_mode;
uint16_t interrupt_threshold;
uint16_t logic_threshold;
uint32_t filter_period;
};
/****************************************************************************
* Public Data
****************************************************************************/
static const uint8_t touch_channel_to_gpio[] =
{
TOUCH_PAD_NUM0_GPIO_NUM,
TOUCH_PAD_NUM1_GPIO_NUM,
TOUCH_PAD_NUM2_GPIO_NUM,
TOUCH_PAD_NUM3_GPIO_NUM,
TOUCH_PAD_NUM4_GPIO_NUM,
TOUCH_PAD_NUM5_GPIO_NUM,
TOUCH_PAD_NUM6_GPIO_NUM,
TOUCH_PAD_NUM7_GPIO_NUM,
TOUCH_PAD_NUM8_GPIO_NUM,
TOUCH_PAD_NUM9_GPIO_NUM
};
#undef EXTERN
#if defined(__cplusplus)
#define EXTERN extern "C"
extern "C"
{
#else
#define EXTERN extern
#endif
/****************************************************************************
* Inline Functions
****************************************************************************/
/****************************************************************************
* Public Function Prototypes
****************************************************************************/
/****************************************************************************
* Name: esp32_configtouch
*
* Description:
* Configure a touch pad channel.
*
* Input Parameters:
* tp - The touch pad channel;
* config - The touch pad configuration structure.
*
* Returned Value:
* OK.
*
****************************************************************************/
int esp32_configtouch(enum touch_pad_e tp, struct touch_config_s config);
/****************************************************************************
* Name: esp32_touchread
*
* Description:
* Read a touch pad channel.
*
* Input Parameters:
* tp - The touch pad channel.
*
* Returned Value:
* 0 if touch pad pressed, 1 if released.
*
****************************************************************************/
bool esp32_touchread(enum touch_pad_e tp);
#ifdef __cplusplus
}
#endif
#undef EXTERN
#endif /* __ASSEMBLY__ */
#endif /* __ARCH_XTENSA_SRC_ESP32_ESP32_TOUCH_H */

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,144 @@
/****************************************************************************
* arch/xtensa/src/esp32/hardware/esp32_touch.h
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
#ifndef __ARCH_XTENSA_SRC_ESP32_HARDWARE_ESP32_TOUCH_H
#define __ARCH_XTENSA_SRC_ESP32_HARDWARE_ESP32_TOUCH_H
/****************************************************************************
* Included Files
****************************************************************************/
/****************************************************************************
* Pre-preprocessor Definitions
****************************************************************************/
#define TOUCH_SENSOR_PINS 10
#define TOUCH_MEASURE_WAIT_MAX (0xff)
/* Touch channel to GPIO */
#define TOUCH_PAD_NUM0_GPIO_NUM 4
#define TOUCH_PAD_NUM1_GPIO_NUM 0
#define TOUCH_PAD_NUM2_GPIO_NUM 2
#define TOUCH_PAD_NUM3_GPIO_NUM 15
#define TOUCH_PAD_NUM4_GPIO_NUM 13
#define TOUCH_PAD_NUM5_GPIO_NUM 12
#define TOUCH_PAD_NUM6_GPIO_NUM 14
#define TOUCH_PAD_NUM7_GPIO_NUM 27
#define TOUCH_PAD_NUM8_GPIO_NUM 33
#define TOUCH_PAD_NUM9_GPIO_NUM 32
/****************************************************************************
* Public Types
****************************************************************************/
/* Touch pad channel */
enum touch_pad_e
{
TOUCH_PAD_NUM0, /* Touch pad channel 0 is GPIO4 */
TOUCH_PAD_NUM1, /* Touch pad channel 1 is GPIO0 */
TOUCH_PAD_NUM2, /* Touch pad channel 2 is GPIO2 */
TOUCH_PAD_NUM3, /* Touch pad channel 3 is GPIO15 */
TOUCH_PAD_NUM4, /* Touch pad channel 4 is GPIO13 */
TOUCH_PAD_NUM5, /* Touch pad channel 5 is GPIO12 */
TOUCH_PAD_NUM6, /* Touch pad channel 6 is GPIO14 */
TOUCH_PAD_NUM7, /* Touch pad channel 7 is GPIO27 */
TOUCH_PAD_NUM8, /* Touch pad channel 8 is GPIO33 */
TOUCH_PAD_NUM9 /* Touch pad channel 9 is GPIO32 */
};
/* Touch sensor high reference voltage */
enum touch_high_volt_e
{
TOUCH_HVOLT_2V4, /* Touch sensor high reference voltage, 2.4V */
TOUCH_HVOLT_2V5, /* Touch sensor high reference voltage, 2.5V */
TOUCH_HVOLT_2V6, /* Touch sensor high reference voltage, 2.6V */
TOUCH_HVOLT_2V7 /* Touch sensor high reference voltage, 2.7V */
};
/* Touch sensor low reference voltage */
enum touch_low_volt_e
{
TOUCH_LVOLT_0V5, /* Touch sensor low reference voltage, 0.5V */
TOUCH_LVOLT_0V6, /* Touch sensor low reference voltage, 0.6V */
TOUCH_LVOLT_0V7, /* Touch sensor low reference voltage, 0.7V */
TOUCH_LVOLT_0V8 /* Touch sensor low reference voltage, 0.8V */
};
/* Touch sensor high reference voltage attenuation */
enum touch_volt_atten_e
{
TOUCH_HVOLT_ATTEN_1V5, /* 1.5V attenuation */
TOUCH_HVOLT_ATTEN_1V, /* 1.0V attenuation */
TOUCH_HVOLT_ATTEN_0V5, /* 0.5V attenuation */
TOUCH_HVOLT_ATTEN_0V /* 0V attenuation */
};
/* Touch sensor charge/discharge speed */
enum touch_cnt_slope_e
{
TOUCH_SLOPE_0, /* Touch sensor charge/discharge speed, always zero */
TOUCH_SLOPE_1, /* Touch sensor charge/discharge speed, slowest */
TOUCH_SLOPE_2, /* Touch sensor charge/discharge speed */
TOUCH_SLOPE_3, /* Touch sensor charge/discharge speed */
TOUCH_SLOPE_4, /* Touch sensor charge/discharge speed */
TOUCH_SLOPE_5, /* Touch sensor charge/discharge speed */
TOUCH_SLOPE_6, /* Touch sensor charge/discharge speed */
TOUCH_SLOPE_7 /* Touch sensor charge/discharge speed, fast */
};
/* Touch sensor initial charge level */
enum touch_tie_opt_e
{
TOUCH_TIE_OPT_LOW, /* Initial level of charging voltage, low level */
TOUCH_TIE_OPT_HIGH /* Initial level of charging voltage, high level */
};
/* Touch sensor FSM mode */
enum touch_fsm_mode_e
{
TOUCH_FSM_MODE_TIMER, /* To start touch FSM by timer */
TOUCH_FSM_MODE_SW /* To start touch FSM by software trigger */
};
/* Touch sensor touch IRQ trigger */
enum touch_trigger_mode_e
{
TOUCH_TRIGGER_BELOW, /* Interrupt if value is less than threshold. */
TOUCH_TRIGGER_ABOVE /* Interrupt if value is larger than threshold. */
};
/* Touch sensor wakeup IRQ trigger */
enum touch_trigger_src_e
{
TOUCH_TRIGGER_SOURCE_BOTH, /* Interrupt if SET1 and SET2 are "touched" */
TOUCH_TRIGGER_SOURCE_SET1 /* Interrupt if SET1 is "touched" */
};
#endif /* __ARCH_XTENSA_SRC_ESP32_HARDWARE_ESP32_TOUCH_H */