From 773e3fad43c8094964d3b79eab89502fb92345d7 Mon Sep 17 00:00:00 2001 From: Lucas Saavedra Vaz Date: Tue, 13 Dec 2022 16:11:34 -0300 Subject: [PATCH] arch/xtensa/esp32: Add initial support for touch pad polling --- arch/xtensa/src/esp32/Make.defs | 4 + arch/xtensa/src/esp32/esp32_gpio.c | 376 ++++-- arch/xtensa/src/esp32/esp32_gpio.h | 81 +- arch/xtensa/src/esp32/esp32_rtc_gpio.h | 492 ++++++-- arch/xtensa/src/esp32/esp32_touch.c | 621 ++++++++++ arch/xtensa/src/esp32/esp32_touch.h | 145 +++ arch/xtensa/src/esp32/esp32_touch_lowerhalf.h | 1058 +++++++++++++++++ arch/xtensa/src/esp32/hardware/esp32_touch.h | 144 +++ 8 files changed, 2681 insertions(+), 240 deletions(-) create mode 100644 arch/xtensa/src/esp32/esp32_touch.c create mode 100644 arch/xtensa/src/esp32/esp32_touch.h create mode 100644 arch/xtensa/src/esp32/esp32_touch_lowerhalf.h create mode 100644 arch/xtensa/src/esp32/hardware/esp32_touch.h diff --git a/arch/xtensa/src/esp32/Make.defs b/arch/xtensa/src/esp32/Make.defs index 67ac01c447..4bed1702a1 100644 --- a/arch/xtensa/src/esp32/Make.defs +++ b/arch/xtensa/src/esp32/Make.defs @@ -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 diff --git a/arch/xtensa/src/esp32/esp32_gpio.c b/arch/xtensa/src/esp32/esp32_gpio.c index e6fa3a22db..e0afb12f45 100644 --- a/arch/xtensa/src/esp32/esp32_gpio.c +++ b/arch/xtensa/src/esp32/esp32_gpio.c @@ -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" @@ -47,9 +48,11 @@ * Pre-processor Definitions ****************************************************************************/ -#define NGPIO_HPINS (ESP32_NIRQ_GPIO - 32) -#define NGPIO_HMASK ((1ul << NGPIO_HPINS) - 1) -#define _NA_ 0xff +#define NGPIO_HPINS (ESP32_NIRQ_GPIO - 32) +#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,148 +222,251 @@ int esp32_configgpio(int pin, gpio_pinattr_t attr) func = 0; cntrl = 0; - if ((attr & INPUT) != 0) + if ((attr & FUNCTION_MASK) == FUNCTION_RTC) /* RTCIO */ { - if (pin < 32) + if (rtc_gpio_is_valid_gpio(pin) == 0) { - putreg32((1ul << pin), GPIO_ENABLE_W1TC_REG); + 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 { - putreg32((1ul << (pin - 32)), GPIO_ENABLE1_W1TC_REG); + resetbits(rtc_reg_desc.reg, rtc_reg_desc.ie); + putreg32((UINT32_C(1) << pin), RTC_GPIO_ENABLE_W1TC_REG); } - /* Input enable */ - - func |= FUN_IE; - - /* Some pins only support Pull-Up and Pull-Down resistor on RTC GPIO */ - - if (rtc_gpio_is_valid_gpio(pin)) + if ((attr & DRIVE_MASK) != 0) { - uint32_t rtc_gpio_idx = g_rtc_io_num_map[pin]; - - if (rtc_gpio_idx >= RTCIO_GPIO34_IDX) + if (rtc_reg_desc.drv_v) { - gpioerr("Pins 34-39 don't support PullUp/PullDown\n"); + 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((UINT32_C(1) << pin), GPIO_ENABLE_W1TC_REG); } else { - uint32_t regval; - uint32_t rtc_gpio_pin; - bool en_pu = false; - bool en_pd = false; + putreg32((UINT32_C(1) << (pin - 32)), GPIO_ENABLE1_W1TC_REG); + } - if ((attr & PULLUP) != 0) + /* Input enable */ + + func |= FUN_IE; + + /* Some pins only support Pull-Up/Pull-Down resistor on RTC GPIO */ + + if (rtc_gpio_is_valid_gpio(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; + + /* Disable RTC control */ + + resetbits(rtc_reg_desc.reg, rtc_reg_desc.mux); + + if (rtc_reg_desc.pullup == 0) { - en_pu = true; - } - else if ((attr & PULLDOWN) != 0) - { - en_pd = true; - } - - /* Get the pin register */ - - rtc_gpio_pin = g_rtc_io_desc[rtc_gpio_idx]; - - /* Read the current value from RTC GPIO pin */ - - regval = getreg32(rtc_gpio_pin); - - /* RTC_IO_X32P (GPIO32) uses different PU/PD bits */ - - if (rtc_gpio_idx == RTCIO_GPIO32_IDX) - { - /* First, disable PU/PD */ - - regval &= ~SPECIAL_RTC_PU_BIT; - regval &= ~SPECIAL_RTC_PD_BIT; - - /* Enable PU/PD, if needed */ - - regval |= en_pu ? SPECIAL_RTC_PU_BIT : 0; - regval |= en_pd ? SPECIAL_RTC_PD_BIT : 0; + gpioerr("Pins 34-39 don't support PullUp/PullDown\n"); + assert(0); } else { - /* First, disable PU/PD */ + uint32_t regval; + uint32_t rtc_gpio_reg; + bool en_pu = false; + bool en_pd = false; - regval &= ~DEFAULT_RTC_PU_BIT; - regval &= ~DEFAULT_RTC_PD_BIT; + if ((attr & PULLUP) != 0) + { + en_pu = true; + } + else if ((attr & PULLDOWN) != 0) + { + en_pd = true; + } - /* Enable PU/PD, if needed */ + /* Get the pin register */ - regval |= en_pu ? DEFAULT_RTC_PU_BIT : 0; - regval |= en_pd ? DEFAULT_RTC_PD_BIT : 0; + rtc_gpio_reg = g_rtc_io_desc[rtc_gpio_idx].reg; + + /* Read the current value from RTC GPIO pin */ + + regval = getreg32(rtc_gpio_reg); + + /* RTC_IO_X32P (GPIO32) uses different PU/PD bits */ + + if (rtc_gpio_idx == RTCIO_GPIO32_CHANNEL) + { + /* First, disable PU/PD */ + + regval &= ~SPECIAL_RTC_PU_BIT; + regval &= ~SPECIAL_RTC_PD_BIT; + + /* Enable PU/PD, if needed */ + + regval |= en_pu ? SPECIAL_RTC_PU_BIT : 0; + regval |= en_pd ? SPECIAL_RTC_PD_BIT : 0; + } + else + { + /* First, disable PU/PD */ + + regval &= ~DEFAULT_RTC_PU_BIT; + regval &= ~DEFAULT_RTC_PD_BIT; + + /* Enable PU/PD, if needed */ + + regval |= en_pu ? DEFAULT_RTC_PU_BIT : 0; + regval |= en_pd ? DEFAULT_RTC_PD_BIT : 0; + } + + putreg32(regval, rtc_gpio_reg); } - - putreg32(regval, rtc_gpio_pin); + } + else if ((attr & PULLUP) != 0) + { + func |= FUN_PU; + } + else if (attr & PULLDOWN) + { + func |= FUN_PD; } } - else if ((attr & PULLUP) != 0) - { - func |= FUN_PU; - } - else if (attr & PULLDOWN) - { - func |= FUN_PD; - } - } - /* Handle output pins */ + /* Handle output pins */ - if ((attr & OUTPUT) != 0) - { - if (pin < 32) + if ((attr & OUTPUT) != 0) { - putreg32((1ul << pin), GPIO_ENABLE_W1TS_REG); + if (pin < 32) + { + putreg32((UINT32_C(1) << pin), GPIO_ENABLE_W1TS_REG); + } + else + { + putreg32((UINT32_C(1) << (pin - 32)), GPIO_ENABLE1_W1TS_REG); + } + } + + /* Configure the pad's function */ + + if ((attr & FUNCTION_MASK) != 0) + { + uint32_t val = ((attr & FUNCTION_MASK) >> FUNCTION_SHIFT) - 1; + func |= val << MCU_SEL_S; } else { - putreg32((1ul << (pin - 32)), GPIO_ENABLE1_W1TS_REG); + /* Function not provided, assuming function GPIO by default */ + + func |= (uint32_t)(PIN_FUNC_GPIO << MCU_SEL_S); } + + /* Configure the pad's drive strength */ + + if ((attr & DRIVE_MASK) != 0) + { + uint32_t val = ((attr & DRIVE_MASK) >> DRIVE_SHIFT) - 1; + func |= val << FUN_DRV_S; + } + else + { + /* Drive strength not provided, assuming strength 2 by default */ + + func |= UINT32_C(2) << FUN_DRV_S; + } + + if ((attr & OPEN_DRAIN) != 0) + { + cntrl |= (1 << GPIO_PIN_PAD_DRIVER_S); + } + + regaddr = DR_REG_IO_MUX_BASE + g_pin2func[pin]; + putreg32(func, regaddr); + + regaddr = GPIO_REG(pin); + putreg32(cntrl, regaddr); + return OK; } - - /* Configure the pad's function */ - - if ((attr & FUNCTION_MASK) != 0) - { - uint32_t val = ((attr & FUNCTION_MASK) >> FUNCTION_SHIFT) - 1; - func |= val << MCU_SEL_S; - } - else - { - /* Function not provided, assuming function GPIO by default */ - - func |= (uint32_t)(PIN_FUNC_GPIO << MCU_SEL_S); - } - - /* Configure the pad's drive strength */ - - if ((attr & DRIVE_MASK) != 0) - { - uint32_t val = ((attr & DRIVE_MASK) >> DRIVE_SHIFT) - 1; - func |= val << FUN_DRV_S; - } - else - { - /* Drive strength not provided, assuming strength 2 by default */ - - func |= UINT32_C(2) << FUN_DRV_S; - } - - if ((attr & OPEN_DRAIN) != 0) - { - cntrl |= (1 << GPIO_PIN_PAD_DRIVER_S); - } - - regaddr = DR_REG_IO_MUX_BASE + g_pin2func[pin]; - putreg32(func, regaddr); - - regaddr = GPIO_REG(pin); - putreg32(cntrl, regaddr); - return OK; } /**************************************************************************** @@ -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) diff --git a/arch/xtensa/src/esp32/esp32_gpio.h b/arch/xtensa/src/esp32/esp32_gpio.h index 5d7cc8518e..650281c926 100644 --- a/arch/xtensa/src/esp32/esp32_gpio.h +++ b/arch/xtensa/src/esp32/esp32_gpio.h @@ -47,49 +47,52 @@ * FN FN FN OD PD PU F O I */ -#define PINMODE_SHIFT 0 -#define PINMODE_MASK (7 << PINMODE_SHIFT) -# define INPUT (1 << 0) -# define OUTPUT (1 << 1) -# define FUNCTION (1 << 2) +#define PINMODE_SHIFT 0 +#define PINMODE_MASK (7 << PINMODE_SHIFT) +# define INPUT (1 << 0) +# define OUTPUT (1 << 1) +# define FUNCTION (1 << 2) -#define PULLUP (1 << 3) -#define PULLDOWN (1 << 4) -#define OPEN_DRAIN (1 << 5) +#define PULLUP (1 << 3) +#define PULLDOWN (1 << 4) +#define OPEN_DRAIN (1 << 5) -#define FUNCTION_SHIFT 6 -#define FUNCTION_MASK (7 << FUNCTION_SHIFT) -# define FUNCTION_1 (1 << FUNCTION_SHIFT) -# define FUNCTION_2 (2 << FUNCTION_SHIFT) -# define FUNCTION_3 (3 << FUNCTION_SHIFT) -# define FUNCTION_4 (4 << FUNCTION_SHIFT) -# define FUNCTION_5 (5 << FUNCTION_SHIFT) -# define FUNCTION_6 (6 << FUNCTION_SHIFT) +#define FUNCTION_SHIFT 6 +#define FUNCTION_MASK (7 << FUNCTION_SHIFT) +# define FUNCTION_1 (1 << FUNCTION_SHIFT) +# define FUNCTION_2 (2 << FUNCTION_SHIFT) +# define FUNCTION_3 (3 << FUNCTION_SHIFT) +# 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) -# define DRIVE_0 (1 << DRIVE_SHIFT) -# define DRIVE_1 (2 << DRIVE_SHIFT) -# define DRIVE_2 (3 << DRIVE_SHIFT) -# define DRIVE_3 (4 << DRIVE_SHIFT) +#define DRIVE_SHIFT 9 +#define DRIVE_MASK (7 << DRIVE_SHIFT) +# define DRIVE_0 (1 << DRIVE_SHIFT) +# define DRIVE_1 (2 << DRIVE_SHIFT) +# define DRIVE_2 (3 << DRIVE_SHIFT) +# define DRIVE_3 (4 << DRIVE_SHIFT) -#define INPUT_PULLUP (INPUT | PULLUP) -#define INPUT_PULLDOWN (INPUT | PULLDOWN) -#define OUTPUT_OPEN_DRAIN (OUTPUT | OPEN_DRAIN) -#define INPUT_FUNCTION (INPUT | FUNCTION) -# define INPUT_FUNCTION_1 (INPUT_FUNCTION | FUNCTION_1) -# define INPUT_FUNCTION_2 (INPUT_FUNCTION | FUNCTION_2) -# define INPUT_FUNCTION_3 (INPUT_FUNCTION | FUNCTION_3) -# 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 OUTPUT_FUNCTION (OUTPUT | FUNCTION) -# define OUTPUT_FUNCTION_1 (OUTPUT_FUNCTION | FUNCTION_1) -# define OUTPUT_FUNCTION_2 (OUTPUT_FUNCTION | FUNCTION_2) -# define OUTPUT_FUNCTION_3 (OUTPUT_FUNCTION | FUNCTION_3) -# 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 INPUT_PULLUP (INPUT | PULLUP) +#define INPUT_PULLDOWN (INPUT | PULLDOWN) +#define OUTPUT_OPEN_DRAIN (OUTPUT | OPEN_DRAIN) +#define INPUT_FUNCTION (INPUT | FUNCTION) +# define INPUT_FUNCTION_1 (INPUT_FUNCTION | FUNCTION_1) +# define INPUT_FUNCTION_2 (INPUT_FUNCTION | FUNCTION_2) +# define INPUT_FUNCTION_3 (INPUT_FUNCTION | FUNCTION_3) +# 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) +# define OUTPUT_FUNCTION_3 (OUTPUT_FUNCTION | FUNCTION_3) +# 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() */ diff --git a/arch/xtensa/src/esp32/esp32_rtc_gpio.h b/arch/xtensa/src/esp32/esp32_rtc_gpio.h index 287e9029ec..62faeb8e2c 100644 --- a/arch/xtensa/src/esp32/esp32_rtc_gpio.h +++ b/arch/xtensa/src/esp32/esp32_rtc_gpio.h @@ -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,114 +38,444 @@ #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 */ +/* RTC GPIO channels */ -#define RTCIO_GPIO2_IDX 1 /* RTCIO_IDX_2 */ +#define RTC_GPIO_NUMBER 18 -#define RTCIO_GPIO4_IDX 2 /* RTCIO_IDX_4 */ +#define RTCIO_GPIO36_CHANNEL 0 /* RTCIO_CHANNEL_0 */ +#define RTCIO_CHANNEL_0_GPIO_NUM 36 -#define RTCIO_GPIO12_IDX 3 /* RTCIO_IDX_12 */ +#define RTCIO_GPIO37_CHANNEL 1 /* RTCIO_CHANNEL_1 */ +#define RTCIO_CHANNEL_1_GPIO_NUM 37 -#define RTCIO_GPIO13_IDX 4 /* RTCIO_IDX_13 */ +#define RTCIO_GPIO38_CHANNEL 2 /* RTCIO_CHANNEL_2 */ +#define RTCIO_CHANNEL_2_GPIO_NUM 38 -#define RTCIO_GPIO14_IDX 5 /* RTCIO_IDX_14 */ +#define RTCIO_GPIO39_CHANNEL 3 /* RTCIO_CHANNEL_3 */ +#define RTCIO_CHANNEL_3_GPIO_NUM 39 -#define RTCIO_GPIO15_IDX 6 /* RTCIO_IDX_15 */ +#define RTCIO_GPIO34_CHANNEL 4 /* RTCIO_CHANNEL_4 */ +#define RTCIO_CHANNEL_4_GPIO_NUM 34 -#define RTCIO_GPIO25_IDX 7 /* RTCIO_IDX_25 */ +#define RTCIO_GPIO35_CHANNEL 5 /* RTCIO_CHANNEL_5 */ +#define RTCIO_CHANNEL_5_GPIO_NUM 35 -#define RTCIO_GPIO26_IDX 8 /* RTCIO_IDX_26 */ +#define RTCIO_GPIO25_CHANNEL 6 /* RTCIO_CHANNEL_6 */ +#define RTCIO_CHANNEL_6_GPIO_NUM 25 -#define RTCIO_GPIO27_IDX 9 /* RTCIO_IDX_27 */ +#define RTCIO_GPIO26_CHANNEL 7 /* RTCIO_CHANNEL_7 */ +#define RTCIO_CHANNEL_7_GPIO_NUM 26 -#define RTCIO_GPIO32_IDX 10 /* RTCIO_IDX_32 */ +#define RTCIO_GPIO33_CHANNEL 8 /* RTCIO_CHANNEL_8 */ +#define RTCIO_CHANNEL_8_GPIO_NUM 33 -#define RTCIO_GPIO33_IDX 11 /* RTCIO_IDX_33 */ +#define RTCIO_GPIO32_CHANNEL 9 /* RTCIO_CHANNEL_9 */ +#define RTCIO_CHANNEL_9_GPIO_NUM 32 -#define RTCIO_GPIO34_IDX 12 /* RTCIO_IDX_34 */ +#define RTCIO_GPIO4_CHANNEL 10 /* RTCIO_CHANNEL_10 */ +#define RTCIO_CHANNEL_10_GPIO_NUM 4 -#define RTCIO_GPIO35_IDX 13 /* RTCIO_IDX_35 */ +#define RTCIO_GPIO0_CHANNEL 11 /* RTCIO_CHANNEL_11 */ +#define RTCIO_CHANNEL_11_GPIO_NUM 0 -#define RTCIO_GPIO36_IDX 14 /* RTCIO_IDX_36 */ +#define RTCIO_GPIO2_CHANNEL 12 /* RTCIO_CHANNEL_12 */ +#define RTCIO_CHANNEL_12_GPIO_NUM 2 -#define RTCIO_GPIO37_IDX 15 /* RTCIO_IDX_37 */ +#define RTCIO_GPIO15_CHANNEL 13 /* RTCIO_CHANNEL_13 */ +#define RTCIO_CHANNEL_13_GPIO_NUM 15 -#define RTCIO_GPIO38_IDX 16 /* RTCIO_IDX_38 */ +#define RTCIO_GPIO13_CHANNEL 14 /* RTCIO_CHANNEL_14 */ +#define RTCIO_CHANNEL_14_GPIO_NUM 13 -#define RTCIO_GPIO39_IDX 17 /* RTCIO_IDX_39 */ +#define RTCIO_GPIO12_CHANNEL 15 /* RTCIO_CHANNEL_15 */ +#define RTCIO_CHANNEL_15_GPIO_NUM 12 -#define RTC_GPIO_NUMBER 18 +#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 */ - -1, /* GPIO1 not supported */ - RTCIO_GPIO2_IDX, /* GPIO2 */ - -1, /* GPIO3 not supported */ - RTCIO_GPIO4_IDX, /* GPIO4 */ - -1, /* GPIO5 not supported */ - -1, /* GPIO6 not supported */ - -1, /* GPIO7 not supported */ - -1, /* GPIO8 not supported */ - -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 */ - -1, /* GPIO16 not supported */ - -1, /* GPIO17 not supported */ - -1, /* GPIO18 not supported */ - -1, /* GPIO19 not supported */ - -1, /* GPIO20 not supported */ - -1, /* GPIO21 not supported */ - -1, /* GPIO22 not supported */ - -1, /* GPIO23 not supported */ - -1, /* GPIO24 not supported */ - RTCIO_GPIO25_IDX, /* GPIO25 */ - RTCIO_GPIO26_IDX, /* GPIO26 */ - RTCIO_GPIO27_IDX, /* 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_GPIO0_CHANNEL, /* GPIO0 */ + -1, /* GPIO1 not supported */ + RTCIO_GPIO2_CHANNEL, /* GPIO2 */ + -1, /* GPIO3 not supported */ + RTCIO_GPIO4_CHANNEL, /* GPIO4 */ + -1, /* GPIO5 not supported */ + -1, /* GPIO6 not supported */ + -1, /* GPIO7 not supported */ + -1, /* GPIO8 not supported */ + -1, /* GPIO9 not supported */ + -1, /* GPIO10 not supported */ + -1, /* GPIO11 not supported */ + 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 */ + -1, /* GPIO19 not supported */ + -1, /* GPIO20 not supported */ + -1, /* GPIO21 not supported */ + -1, /* GPIO22 not supported */ + -1, /* GPIO23 not supported */ + -1, /* GPIO24 not supported */ + 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_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] = { - RTC_IO_TOUCH_PAD1_REG, - RTC_IO_TOUCH_PAD2_REG, - RTC_IO_TOUCH_PAD0_REG, - RTC_IO_TOUCH_PAD5_REG, - RTC_IO_TOUCH_PAD4_REG, - RTC_IO_TOUCH_PAD6_REG, - RTC_IO_TOUCH_PAD3_REG, - RTC_IO_PAD_DAC1_REG, - RTC_IO_PAD_DAC2_REG, - RTC_IO_TOUCH_PAD7_REG, - RTC_IO_XTAL_32K_PAD_REG, - RTC_IO_XTAL_32K_PAD_REG, - RTC_IO_ADC_PAD_REG, - RTC_IO_ADC_PAD_REG, - RTC_IO_SENSOR_PADS_REG, - RTC_IO_SENSOR_PADS_REG, - RTC_IO_SENSOR_PADS_REG, - RTC_IO_SENSOR_PADS_REG + /* 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, + 0, + 0, + RTC_IO_SENSE3_SLP_SEL_M, + RTC_IO_SENSE3_SLP_IE_M, + 0, + RTC_IO_SENSE3_HOLD_M, + RTC_CNTL_SENSE3_HOLD_FORCE_M, + 0, + 0, + RTCIO_CHANNEL_2_GPIO_NUM + }, + { + RTC_IO_SENSOR_PADS_REG, + RTC_IO_SENSE4_MUX_SEL_M, + RTC_IO_SENSE4_FUN_SEL_S, + RTC_IO_SENSE4_FUN_IE_M, + 0, + 0, + RTC_IO_SENSE4_SLP_SEL_M, + RTC_IO_SENSE4_SLP_IE_M, + 0, + RTC_IO_SENSE4_HOLD_M, + RTC_CNTL_SENSE4_HOLD_FORCE_M, + 0, + 0, + RTCIO_CHANNEL_3_GPIO_NUM + }, + { + RTC_IO_ADC_PAD_REG, + RTC_IO_ADC1_MUX_SEL_M, + RTC_IO_ADC1_FUN_SEL_S, + RTC_IO_ADC1_FUN_IE_M, + 0, + 0, + RTC_IO_ADC1_SLP_SEL_M, + RTC_IO_ADC1_SLP_IE_M, + 0, + RTC_IO_ADC1_HOLD_M, + RTC_CNTL_ADC1_HOLD_FORCE_M, + 0, + 0, + RTCIO_CHANNEL_4_GPIO_NUM + }, + { + RTC_IO_ADC_PAD_REG, + RTC_IO_ADC2_MUX_SEL_M, + RTC_IO_ADC2_FUN_SEL_S, + RTC_IO_ADC2_FUN_IE_M, + 0, + 0, + RTC_IO_ADC2_SLP_SEL_M, + RTC_IO_ADC2_SLP_IE_M, + 0, + RTC_IO_ADC2_HOLD_M, + RTC_CNTL_ADC2_HOLD_FORCE_M, + 0, + 0, + RTCIO_CHANNEL_5_GPIO_NUM + }, + { + RTC_IO_PAD_DAC1_REG, + RTC_IO_PDAC1_MUX_SEL_M, + RTC_IO_PDAC1_FUN_SEL_S, + RTC_IO_PDAC1_FUN_IE_M, + RTC_IO_PDAC1_RUE_M, + RTC_IO_PDAC1_RDE_M, + RTC_IO_PDAC1_SLP_SEL_M, + RTC_IO_PDAC1_SLP_IE_M, + 0, + RTC_IO_PDAC1_HOLD_M, + RTC_CNTL_PDAC1_HOLD_FORCE_M, + RTC_IO_PDAC1_DRV_V, + RTC_IO_PDAC1_DRV_S, + RTCIO_CHANNEL_6_GPIO_NUM + }, + { + RTC_IO_PAD_DAC2_REG, + RTC_IO_PDAC2_MUX_SEL_M, + RTC_IO_PDAC2_FUN_SEL_S, + RTC_IO_PDAC2_FUN_IE_M, + RTC_IO_PDAC2_RUE_M, + RTC_IO_PDAC2_RDE_M, + RTC_IO_PDAC2_SLP_SEL_M, + RTC_IO_PDAC2_SLP_IE_M, + 0, + RTC_IO_PDAC2_HOLD_M, + RTC_CNTL_PDAC2_HOLD_FORCE_M, + RTC_IO_PDAC2_DRV_V, + RTC_IO_PDAC2_DRV_S, + RTCIO_CHANNEL_7_GPIO_NUM + }, + { + RTC_IO_XTAL_32K_PAD_REG, + RTC_IO_X32N_MUX_SEL_M, + RTC_IO_X32N_FUN_SEL_S, + RTC_IO_X32N_FUN_IE_M, + RTC_IO_X32N_RUE_M, + RTC_IO_X32N_RDE_M, + RTC_IO_X32N_SLP_SEL_M, + RTC_IO_X32N_SLP_IE_M, + 0, + RTC_IO_X32N_HOLD_M, + RTC_CNTL_X32N_HOLD_FORCE_M, + RTC_IO_X32N_DRV_V, + RTC_IO_X32N_DRV_S, + RTCIO_CHANNEL_8_GPIO_NUM + }, + { + RTC_IO_XTAL_32K_PAD_REG, + RTC_IO_X32P_MUX_SEL_M, + RTC_IO_X32P_FUN_SEL_S, + RTC_IO_X32P_FUN_IE_M, + RTC_IO_X32P_RUE_M, + RTC_IO_X32P_RDE_M, + RTC_IO_X32P_SLP_SEL_M, + RTC_IO_X32P_SLP_IE_M, + 0, + RTC_IO_X32P_HOLD_M, + RTC_CNTL_X32P_HOLD_FORCE_M, + RTC_IO_X32P_DRV_V, + RTC_IO_X32P_DRV_S, + RTCIO_CHANNEL_9_GPIO_NUM + }, + { + RTC_IO_TOUCH_PAD0_REG, + RTC_IO_TOUCH_PAD0_MUX_SEL_M, + RTC_IO_TOUCH_PAD0_FUN_SEL_S, + RTC_IO_TOUCH_PAD0_FUN_IE_M, + RTC_IO_TOUCH_PAD0_RUE_M, + RTC_IO_TOUCH_PAD0_RDE_M, + RTC_IO_TOUCH_PAD0_SLP_SEL_M, + RTC_IO_TOUCH_PAD0_SLP_IE_M, + 0, + RTC_IO_TOUCH_PAD0_HOLD_M, + RTC_CNTL_TOUCH_PAD0_HOLD_FORCE_M, + RTC_IO_TOUCH_PAD0_DRV_V, + RTC_IO_TOUCH_PAD0_DRV_S, + RTCIO_CHANNEL_10_GPIO_NUM + }, + { + RTC_IO_TOUCH_PAD1_REG, + RTC_IO_TOUCH_PAD1_MUX_SEL_M, + RTC_IO_TOUCH_PAD1_FUN_SEL_S, + RTC_IO_TOUCH_PAD1_FUN_IE_M, + RTC_IO_TOUCH_PAD1_RUE_M, + RTC_IO_TOUCH_PAD1_RDE_M, + RTC_IO_TOUCH_PAD1_SLP_SEL_M, + RTC_IO_TOUCH_PAD1_SLP_IE_M, + 0, + RTC_IO_TOUCH_PAD1_HOLD_M, + RTC_CNTL_TOUCH_PAD1_HOLD_FORCE_M, + RTC_IO_TOUCH_PAD1_DRV_V, + RTC_IO_TOUCH_PAD1_DRV_S, + RTCIO_CHANNEL_11_GPIO_NUM + }, + { + RTC_IO_TOUCH_PAD2_REG, + RTC_IO_TOUCH_PAD2_MUX_SEL_M, + RTC_IO_TOUCH_PAD2_FUN_SEL_S, + RTC_IO_TOUCH_PAD2_FUN_IE_M, + RTC_IO_TOUCH_PAD2_RUE_M, + RTC_IO_TOUCH_PAD2_RDE_M, + RTC_IO_TOUCH_PAD2_SLP_SEL_M, + RTC_IO_TOUCH_PAD2_SLP_IE_M, + 0, + RTC_IO_TOUCH_PAD2_HOLD_M, + RTC_CNTL_TOUCH_PAD2_HOLD_FORCE_M, + RTC_IO_TOUCH_PAD2_DRV_V, + RTC_IO_TOUCH_PAD2_DRV_S, + RTCIO_CHANNEL_12_GPIO_NUM + }, + { + RTC_IO_TOUCH_PAD3_REG, + RTC_IO_TOUCH_PAD3_MUX_SEL_M, + RTC_IO_TOUCH_PAD3_FUN_SEL_S, + RTC_IO_TOUCH_PAD3_FUN_IE_M, + RTC_IO_TOUCH_PAD3_RUE_M, + RTC_IO_TOUCH_PAD3_RDE_M, + RTC_IO_TOUCH_PAD3_SLP_SEL_M, + RTC_IO_TOUCH_PAD3_SLP_IE_M, + 0, + RTC_IO_TOUCH_PAD3_HOLD_M, + RTC_CNTL_TOUCH_PAD3_HOLD_FORCE_M, + RTC_IO_TOUCH_PAD3_DRV_V, + RTC_IO_TOUCH_PAD3_DRV_S, + RTCIO_CHANNEL_13_GPIO_NUM + }, + { + RTC_IO_TOUCH_PAD4_REG, + RTC_IO_TOUCH_PAD4_MUX_SEL_M, + RTC_IO_TOUCH_PAD4_FUN_SEL_S, + RTC_IO_TOUCH_PAD4_FUN_IE_M, + RTC_IO_TOUCH_PAD4_RUE_M, + RTC_IO_TOUCH_PAD4_RDE_M, + RTC_IO_TOUCH_PAD4_SLP_SEL_M, + RTC_IO_TOUCH_PAD4_SLP_IE_M, + 0, + RTC_IO_TOUCH_PAD4_HOLD_M, + RTC_CNTL_TOUCH_PAD4_HOLD_FORCE_M, + RTC_IO_TOUCH_PAD4_DRV_V, + RTC_IO_TOUCH_PAD4_DRV_S, + RTCIO_CHANNEL_14_GPIO_NUM + }, + { + RTC_IO_TOUCH_PAD5_REG, + RTC_IO_TOUCH_PAD5_MUX_SEL_M, + RTC_IO_TOUCH_PAD5_FUN_SEL_S, + RTC_IO_TOUCH_PAD5_FUN_IE_M, + RTC_IO_TOUCH_PAD5_RUE_M, + RTC_IO_TOUCH_PAD5_RDE_M, + RTC_IO_TOUCH_PAD5_SLP_SEL_M, + RTC_IO_TOUCH_PAD5_SLP_IE_M, + 0, + RTC_IO_TOUCH_PAD5_HOLD_M, + RTC_CNTL_TOUCH_PAD5_HOLD_FORCE_M, + RTC_IO_TOUCH_PAD5_DRV_V, + RTC_IO_TOUCH_PAD5_DRV_S, + RTCIO_CHANNEL_15_GPIO_NUM + }, + { + RTC_IO_TOUCH_PAD6_REG, + RTC_IO_TOUCH_PAD6_MUX_SEL_M, + RTC_IO_TOUCH_PAD6_FUN_SEL_S, + RTC_IO_TOUCH_PAD6_FUN_IE_M, + RTC_IO_TOUCH_PAD6_RUE_M, + RTC_IO_TOUCH_PAD6_RDE_M, + RTC_IO_TOUCH_PAD6_SLP_SEL_M, + RTC_IO_TOUCH_PAD6_SLP_IE_M, + 0, + RTC_IO_TOUCH_PAD6_HOLD_M, + RTC_CNTL_TOUCH_PAD6_HOLD_FORCE_M, + RTC_IO_TOUCH_PAD6_DRV_V, + RTC_IO_TOUCH_PAD6_DRV_S, + RTCIO_CHANNEL_16_GPIO_NUM + }, + { + RTC_IO_TOUCH_PAD7_REG, + RTC_IO_TOUCH_PAD7_MUX_SEL_M, + RTC_IO_TOUCH_PAD7_FUN_SEL_S, + RTC_IO_TOUCH_PAD7_FUN_IE_M, + RTC_IO_TOUCH_PAD7_RUE_M, + RTC_IO_TOUCH_PAD7_RDE_M, + RTC_IO_TOUCH_PAD7_SLP_SEL_M, + RTC_IO_TOUCH_PAD7_SLP_IE_M, + 0, + RTC_IO_TOUCH_PAD7_HOLD_M, + RTC_CNTL_TOUCH_PAD7_HOLD_FORCE_M, + RTC_IO_TOUCH_PAD7_DRV_V, + RTC_IO_TOUCH_PAD7_DRV_S, + RTCIO_CHANNEL_17_GPIO_NUM + } }; #endif /* __ARCH_XTENSA_SRC_ESP32_ESP32_RTC_GPIO_H */ diff --git a/arch/xtensa/src/esp32/esp32_touch.c b/arch/xtensa/src/esp32/esp32_touch.c new file mode 100644 index 0000000000..c15ab206ab --- /dev/null +++ b/arch/xtensa/src/esp32/esp32_touch.c @@ -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 + +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +#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); +} diff --git a/arch/xtensa/src/esp32/esp32_touch.h b/arch/xtensa/src/esp32/esp32_touch.h new file mode 100644 index 0000000000..7dee46ae18 --- /dev/null +++ b/arch/xtensa/src/esp32/esp32_touch.h @@ -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 +#include +#include + +#include "esp32_touch_lowerhalf.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#define TOUCH_BIT_MASK_ALL ((1< + +#include +#include + +#include "xtensa.h" + +#include "hardware/esp32_rtc_io.h" +#include "hardware/esp32_rtccntl.h" +#include "hardware/esp32_touch.h" +#include "hardware/esp32_sens.h" + +#include "esp32_rt_timer.h" +#include "esp32_rtc_gpio.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/* Some register bits of touch sensor 8 and 9 are mismatched, + * we need to swap the bits. + */ + +#define TOUCH_LH_BITSWAP(data, n, m) ((((data) >> (n)) & 0x1) == \ + (((data) >> (m)) & 0x1) ? (data) : \ + ((data) ^ ((0x1 << (n)) | \ + (0x1 << (m))))) + +#define TOUCH_LH_BITS_SWAP(v) TOUCH_LH_BITSWAP(v, \ + TOUCH_PAD_NUM8, \ + TOUCH_PAD_NUM9) + +#define setbits(a, bs) modifyreg32(a, 0, bs) +#define resetbits(a, bs) modifyreg32(a, bs, 0) + +/**************************************************************************** + * Public Data + ****************************************************************************/ + +#ifndef __ASSEMBLY__ + +#undef EXTERN +#if defined(__cplusplus) +#define EXTERN extern "C" +extern "C" +{ +#else +#define EXTERN extern +#endif + +/**************************************************************************** + * Inline Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: touch_lh_num_wrap + * + * Description: + * Some registers have swapped bits. This function returns the correct + * touchpad id to read. + * + * Input Parameters: + * tp - The wanted touchpad. + * + * Returned Value: + * The actual ID to be read. + * + ****************************************************************************/ + +static inline enum touch_pad_e touch_lh_num_wrap(enum touch_pad_e tp) +{ + if (tp == TOUCH_PAD_NUM8) + { + return TOUCH_PAD_NUM9; + } + else if (tp == TOUCH_PAD_NUM9) + { + return TOUCH_PAD_NUM8; + } + + return tp; +} + +/**************************************************************************** + * Name: touch_lh_set_meas_time + * + * Description: + * Set the measurement time for the touch sensors. + * + * Input Parameters: + * meas_time - The desired measurement time. + * + * Returned Value: + * None. + * + ****************************************************************************/ + +static inline void touch_lh_set_meas_time(uint16_t meas_time) +{ + /* Touch sensor measure time = meas_cycle / 8Mhz */ + + REG_SET_FIELD(SENS_SAR_TOUCH_CTRL1_REG, SENS_TOUCH_MEAS_DELAY, meas_time); + + /* The waiting cycles (in 8MHz) between TOUCH_START and TOUCH_XPD */ + + REG_SET_FIELD(SENS_SAR_TOUCH_CTRL1_REG, + SENS_TOUCH_XPD_WAIT, + TOUCH_MEASURE_WAIT_MAX); +} + +/**************************************************************************** + * Name: touch_lh_get_meas_time + * + * Description: + * Get the measurement time for the touch sensors. + * + * Input Parameters: + * None. + * + * Returned Value: + * The current measurement time. + * + ****************************************************************************/ + +static inline uint16_t touch_lh_get_meas_time(void) +{ + return REG_GET_FIELD(SENS_SAR_TOUCH_CTRL1_REG, SENS_TOUCH_MEAS_DELAY); +} + +/**************************************************************************** + * Name: touch_lh_set_sleep_time + * + * Description: + * Set the sleep time for the touch sensors. + * + * Input Parameters: + * sleep_time - The desired sleep time. + * + * Returned Value: + * None. + * + ****************************************************************************/ + +static inline void touch_lh_set_sleep_time(uint16_t sleep_time) +{ + /* Touch sensor sleep cycle Time = sleep_cycle / RTC_SLOW_CLK + * (can be 150k or 32k depending on the options) + */ + + REG_SET_FIELD(SENS_SAR_TOUCH_CTRL2_REG, + SENS_TOUCH_SLEEP_CYCLES, + sleep_time); +} + +/**************************************************************************** + * Name: touch_lh_get_sleep_time + * + * Description: + * Get the sleep time for the touch sensors. + * + * Input Parameters: + * None. + * + * Returned Value: + * The current sleep time. + * + ****************************************************************************/ + +static inline uint16_t touch_lh_get_sleep_time(void) +{ + return REG_GET_FIELD(SENS_SAR_TOUCH_CTRL2_REG, SENS_TOUCH_SLEEP_CYCLES); +} + +/**************************************************************************** + * Name: touch_lh_set_voltage_high + * + * Description: + * Set the touch sensor high reference voltage. + * + * Input Parameters: + * refh - The desired enum touch_high_volt_e value. + * + * Returned Value: + * None. + * + ****************************************************************************/ + +static inline void touch_lh_set_voltage_high(enum touch_high_volt_e refh) +{ + REG_SET_FIELD(RTC_IO_TOUCH_CFG_REG, RTC_IO_TOUCH_DREFH, refh); +} + +/**************************************************************************** + * Name: touch_lh_get_voltage_high + * + * Description: + * Get the touch sensor high reference voltage. + * + * Input Parameters: + * None. + * + * Returned Value: + * The current enum touch_high_volt_e. + * + ****************************************************************************/ + +static inline enum touch_high_volt_e touch_lh_get_voltage_high(void) +{ + return (enum touch_high_volt_e) + REG_GET_FIELD(RTC_IO_TOUCH_CFG_REG, RTC_IO_TOUCH_DREFH); +} + +/**************************************************************************** + * Name: touch_lh_set_voltage_low + * + * Description: + * Set the touch sensor low reference voltage. + * + * Input Parameters: + * refl - The desired enum touch_low_volt_e value. + * + * Returned Value: + * None. + * + ****************************************************************************/ + +static inline void touch_lh_set_voltage_low(enum touch_low_volt_e refl) +{ + REG_SET_FIELD(RTC_IO_TOUCH_CFG_REG, RTC_IO_TOUCH_DREFL, refl); +} + +/**************************************************************************** + * Name: touch_lh_get_voltage_low + * + * Description: + * Get the touch sensor low reference voltage. + * + * Input Parameters: + * None. + * + * Returned Value: + * The current enum touch_low_volt_e. + * + ****************************************************************************/ + +static inline enum touch_low_volt_e touch_lh_get_voltage_low(void) +{ + return (enum touch_low_volt_e) + REG_GET_FIELD(RTC_IO_TOUCH_CFG_REG, RTC_IO_TOUCH_DREFL); +} + +/**************************************************************************** + * Name: touch_lh_set_voltage_attenuation + * + * Description: + * Set the touch sensor voltage attenuation. + * + * Input Parameters: + * atten - The desired enum touch_volt_atten_e value. + * + * Returned Value: + * None. + * + ****************************************************************************/ + +static inline void + touch_lh_set_voltage_attenuation (enum touch_volt_atten_e atten) +{ + REG_SET_FIELD(RTC_IO_TOUCH_CFG_REG, RTC_IO_TOUCH_DRANGE, atten); +} + +/**************************************************************************** + * Name: touch_lh_get_voltage_attenuation + * + * Description: + * Get the touch sensor voltage attenuation. + * + * Input Parameters: + * None. + * + * Returned Value: + * The current enum touch_volt_atten_e. + * + ****************************************************************************/ + +static inline enum touch_volt_atten_e touch_lh_get_voltage_attenuation(void) +{ + return (enum touch_volt_atten_e) + REG_GET_FIELD(RTC_IO_TOUCH_CFG_REG, RTC_IO_TOUCH_DRANGE); +} + +/**************************************************************************** + * Name: touch_lh_set_slope + * + * Description: + * Set the charge/discharge slope for a given touch pad. + * + * Input Parameters: + * tp - The touch pad channel; + * slope - The desired enum touch_cnt_slope_e value. + * + * Returned Value: + * None. + * + ****************************************************************************/ + +static inline void touch_lh_set_slope(enum touch_pad_e tp, + enum touch_cnt_slope_e slope) +{ + /* All touch pads have the same position for the DAC bits. + * We can use any RTC_IO_TOUCH_PADn_DAC. + */ + + const uint32_t reg_addr[] = + { + RTC_IO_TOUCH_PAD0_REG, + RTC_IO_TOUCH_PAD1_REG, + RTC_IO_TOUCH_PAD2_REG, + RTC_IO_TOUCH_PAD3_REG, + RTC_IO_TOUCH_PAD4_REG, + RTC_IO_TOUCH_PAD5_REG, + RTC_IO_TOUCH_PAD6_REG, + RTC_IO_TOUCH_PAD7_REG, + RTC_IO_TOUCH_PAD8_REG, + RTC_IO_TOUCH_PAD9_REG + }; + + REG_SET_FIELD(reg_addr[tp], RTC_IO_TOUCH_PAD0_DAC, slope); +} + +/**************************************************************************** + * Name: touch_lh_get_slope + * + * Description: + * Get the charge/discharge slope for a given touch pad. + * + * Input Parameters: + * tp - The touch pad channel. + * + * Returned Value: + * The current enum touch_cnt_slope_e for that touch pad. + * + ****************************************************************************/ + +static inline enum touch_cnt_slope_e touch_lh_get_slope(enum touch_pad_e tp) +{ + /* All touch pads have the same position for the DAC bits. + * We can use any RTC_IO_TOUCH_PADn_DAC. + */ + + const uint32_t reg_addr[] = + { + RTC_IO_TOUCH_PAD0_REG, + RTC_IO_TOUCH_PAD1_REG, + RTC_IO_TOUCH_PAD2_REG, + RTC_IO_TOUCH_PAD3_REG, + RTC_IO_TOUCH_PAD4_REG, + RTC_IO_TOUCH_PAD5_REG, + RTC_IO_TOUCH_PAD6_REG, + RTC_IO_TOUCH_PAD7_REG, + RTC_IO_TOUCH_PAD8_REG, + RTC_IO_TOUCH_PAD9_REG + }; + + return (enum touch_cnt_slope_e) REG_GET_FIELD(reg_addr[tp], + RTC_IO_TOUCH_PAD0_DAC); +} + +/**************************************************************************** + * Name: touch_lh_set_tie_option + * + * Description: + * Set the initial charging level for a given touch pad. + * + * Input Parameters: + * tp - The touch pad channel; + * opt - The desired enum touch_tie_opt_e value. + * + * Returned Value: + * None. + * + ****************************************************************************/ + +static inline void touch_lh_set_tie_option(enum touch_pad_e tp, + enum touch_tie_opt_e opt) +{ + enum touch_pad_e tp_wrap = touch_lh_num_wrap(tp); + + const uint32_t reg_addr[] = + { + RTC_IO_TOUCH_PAD0_REG, + RTC_IO_TOUCH_PAD1_REG, + RTC_IO_TOUCH_PAD2_REG, + RTC_IO_TOUCH_PAD3_REG, + RTC_IO_TOUCH_PAD4_REG, + RTC_IO_TOUCH_PAD5_REG, + RTC_IO_TOUCH_PAD6_REG, + RTC_IO_TOUCH_PAD7_REG, + RTC_IO_TOUCH_PAD8_REG, + RTC_IO_TOUCH_PAD9_REG + }; + + /* All touch pads have the same position for the TIE_OPT bits. + * We can use any RTC_IO_TOUCH_PADn_TIE_OPT. + */ + + REG_SET_FIELD(reg_addr[tp_wrap], RTC_IO_TOUCH_PAD0_TIE_OPT, opt); +} + +/**************************************************************************** + * Name: touch_lh_get_tie_option + * + * Description: + * Get the initial charging level for a given touch pad. + * + * Input Parameters: + * tp - The touch pad channel. + * + * Returned Value: + * The current enum touch_tie_opt_e for that touch pad. + * + ****************************************************************************/ + +static inline enum touch_tie_opt_e + touch_lh_get_tie_option(enum touch_pad_e tp) +{ + enum touch_pad_e tp_wrap = touch_lh_num_wrap(tp); + + const uint32_t reg_addr[] = + { + RTC_IO_TOUCH_PAD0_REG, + RTC_IO_TOUCH_PAD1_REG, + RTC_IO_TOUCH_PAD2_REG, + RTC_IO_TOUCH_PAD3_REG, + RTC_IO_TOUCH_PAD4_REG, + RTC_IO_TOUCH_PAD5_REG, + RTC_IO_TOUCH_PAD6_REG, + RTC_IO_TOUCH_PAD7_REG, + RTC_IO_TOUCH_PAD8_REG, + RTC_IO_TOUCH_PAD9_REG + }; + + /* All touch pads have the same position for the TIE_OPT bits. + * We can use any RTC_IO_TOUCH_PADn_TIE_OPT. + */ + + return (enum touch_tie_opt_e) REG_GET_FIELD(reg_addr[tp_wrap], + RTC_IO_TOUCH_PAD0_TIE_OPT); +} + +/**************************************************************************** + * Name: touch_lh_set_fsm_mode + * + * Description: + * Set the mode of the internal touch finite state machine. + * + * Input Parameters: + * mode - The desired enum touch_fsm_mode_e value. + * + * Returned Value: + * None. + * + ****************************************************************************/ + +static inline void touch_lh_set_fsm_mode(enum touch_fsm_mode_e mode) +{ + REG_SET_FIELD(SENS_SAR_TOUCH_CTRL2_REG, SENS_TOUCH_START_FSM_EN, true); + REG_SET_FIELD(SENS_SAR_TOUCH_CTRL2_REG, SENS_TOUCH_START_EN, false); + REG_SET_FIELD(SENS_SAR_TOUCH_CTRL2_REG, SENS_TOUCH_START_FORCE, mode); +} + +/**************************************************************************** + * Name: touch_lh_get_fsm_mode + * + * Description: + * Get the mode of the internal touch finite state machine. + * + * Input Parameters: + * None. + * + * Returned Value: + * The current enum touch_fsm_mode_e. + * + ****************************************************************************/ + +static inline enum touch_fsm_mode_e touch_lh_get_fsm_mode(void) +{ + return (enum touch_fsm_mode_e) + REG_GET_FIELD(SENS_SAR_TOUCH_CTRL2_REG, SENS_TOUCH_START_FORCE); +} + +/**************************************************************************** + * Name: touch_lh_start_fsm + * + * Description: + * Start the internal touch finite state machine. + * + * Input Parameters: + * None. + * + * Returned Value: + * None. + * + ****************************************************************************/ + +static inline void touch_lh_start_fsm(void) +{ + REG_SET_FIELD(RTC_CNTL_STATE0_REG, RTC_CNTL_TOUCH_SLP_TIMER_EN, true); +} + +/**************************************************************************** + * Name: touch_lh_stop_fsm + * + * Description: + * Stop the internal touch finite state machine. + * + * Input Parameters: + * None. + * + * Returned Value: + * None. + * + ****************************************************************************/ + +static inline void touch_lh_stop_fsm(void) +{ + REG_SET_FIELD(RTC_CNTL_STATE0_REG, RTC_CNTL_TOUCH_SLP_TIMER_EN, false); +} + +/**************************************************************************** + * Name: touch_lh_start_sw_meas + * + * Description: + * Start measurement controlled by software. + * + * Input Parameters: + * None. + * + * Returned Value: + * None. + * + ****************************************************************************/ + +static inline void touch_lh_start_sw_meas(void) +{ + REG_SET_FIELD(SENS_SAR_TOUCH_CTRL2_REG, SENS_TOUCH_START_EN, false); + REG_SET_FIELD(SENS_SAR_TOUCH_CTRL2_REG, SENS_TOUCH_START_EN, true); +} + +/**************************************************************************** + * Name: touch_lh_set_threshold + * + * Description: + * Set the touch interrupt threshold for a given touch pad. + * + * Input Parameters: + * tp - The touch pad channel; + * threshold - The desired threshold value. + * + * Returned Value: + * None. + * + ****************************************************************************/ + +static inline void touch_lh_set_threshold(enum touch_pad_e tp, + uint16_t threshold) +{ + enum touch_pad_e tp_wrap = touch_lh_num_wrap(tp); + + const uint32_t reg_addr[] = + { + SENS_SAR_TOUCH_THRES1_REG, + SENS_SAR_TOUCH_THRES2_REG, + SENS_SAR_TOUCH_THRES3_REG, + SENS_SAR_TOUCH_THRES4_REG, + SENS_SAR_TOUCH_THRES5_REG + }; + + if (tp_wrap & 0x1) /* Odd */ + { + /* All odd touch pads have the same position for the THN bits. + * We can use any odd SENS_TOUCH_OUT_THN. + */ + + REG_SET_FIELD(reg_addr[tp_wrap / 2], SENS_TOUCH_OUT_TH1, threshold); + } + else /* Even */ + { + /* All even touch pads have the same position for the THN bits. + * We can use any even SENS_TOUCH_OUT_THN. + */ + + REG_SET_FIELD(reg_addr[tp_wrap / 2], SENS_TOUCH_OUT_TH0, threshold); + } +} + +/**************************************************************************** + * Name: touch_lh_get_threshold + * + * Description: + * Get the touch interrupt threshold for a given touch pad. + * + * Input Parameters: + * tp - The touch pad channel. + * + * Returned Value: + * The current interrupt threshold for that touch pad. + * + ****************************************************************************/ + +static inline uint16_t touch_lh_get_threshold(enum touch_pad_e tp) +{ + enum touch_pad_e tp_wrap = touch_lh_num_wrap(tp); + + const uint32_t reg_addr[] = + { + SENS_SAR_TOUCH_THRES1_REG, + SENS_SAR_TOUCH_THRES2_REG, + SENS_SAR_TOUCH_THRES3_REG, + SENS_SAR_TOUCH_THRES4_REG, + SENS_SAR_TOUCH_THRES5_REG + }; + + if (tp_wrap & 0x1) /* Odd */ + { + /* All odd touch pads have the same position for the THN bits. + * We can use any odd SENS_TOUCH_OUT_THN. + */ + + return REG_GET_FIELD(reg_addr[tp_wrap / 2], SENS_TOUCH_OUT_TH1); + } + else /* Even */ + { + /* All even touch pads have the same position for the THN bits. + * We can use any even SENS_TOUCH_OUT_THN. + */ + + return REG_GET_FIELD(reg_addr[tp_wrap / 2], SENS_TOUCH_OUT_TH0); + } +} + +/**************************************************************************** + * Name: touch_lh_set_trigger_mode + * + * Description: + * Set the touch interrupt trigger mode. + * + * Input Parameters: + * mode - The desired enum touch_trigger_mode_e value. + * + * Returned Value: + * None. + * + ****************************************************************************/ + +static inline void touch_lh_set_trigger_mode(enum touch_trigger_mode_e mode) +{ + REG_SET_FIELD(SENS_SAR_TOUCH_CTRL1_REG, SENS_TOUCH_OUT_SEL, mode); +} + +/**************************************************************************** + * Name: touch_lh_get_trigger_mode + * + * Description: + * Get the touch interrupt trigger mode. + * + * Input Parameters: + * None. + * + * Returned Value: + * The current enum touch_trigger_mode_e. + * + ****************************************************************************/ + +static inline enum touch_trigger_mode_e touch_lh_get_trigger_mode(void) +{ + return (enum touch_trigger_mode_e) + REG_GET_FIELD(SENS_SAR_TOUCH_CTRL1_REG, SENS_TOUCH_OUT_SEL); +} + +/**************************************************************************** + * Name: touch_lh_set_trigger_source + * + * Description: + * Set the touch interrupt trigger source. + * + * Input Parameters: + * src - The desired enum touch_trigger_src_e value. + * + * Returned Value: + * None. + * + ****************************************************************************/ + +static inline void touch_lh_set_trigger_source(enum touch_trigger_src_e src) +{ + REG_SET_FIELD(SENS_SAR_TOUCH_CTRL1_REG, SENS_TOUCH_OUT_1EN, src); +} + +/**************************************************************************** + * Name: touch_lh_get_trigger_source + * + * Description: + * Get the touch interrupt trigger source. + * + * Input Parameters: + * None. + * + * Returned Value: + * The current enum touch_trigger_src_e. + * + ****************************************************************************/ + +static inline enum touch_trigger_src_e touch_lh_get_trigger_source(void) +{ + return (enum touch_trigger_src_e) + REG_GET_FIELD(SENS_SAR_TOUCH_CTRL1_REG, SENS_TOUCH_OUT_1EN); +} + +/**************************************************************************** + * Name: touch_lh_set_channel_mask + * + * Description: + * Enable touch channels to be measured. + * + * Input Parameters: + * enable_mask - Bitmask containing the desired channels to be activated. + * + * Returned Value: + * None. + * + ****************************************************************************/ + +static inline void touch_lh_set_channel_mask(uint16_t enable_mask) +{ + setbits(SENS_SAR_TOUCH_ENABLE_REG, (TOUCH_LH_BITS_SWAP(enable_mask) << + SENS_TOUCH_PAD_WORKEN_S)); +} + +/**************************************************************************** + * Name: touch_lh_get_channel_mask + * + * Description: + * Get the active touch channels to be measured. + * + * Input Parameters: + * None. + * + * Returned Value: + * Bitmask of the current chennels being measured. + * + ****************************************************************************/ + +static inline uint16_t touch_lh_get_channel_mask(void) +{ + return TOUCH_LH_BITS_SWAP( + REG_GET_FIELD(SENS_SAR_TOUCH_ENABLE_REG, SENS_TOUCH_PAD_WORKEN)); +} + +/**************************************************************************** + * Name: touch_lh_clear_channel_mask + * + * Description: + * Disable touch channels being measured. + * + * Input Parameters: + * disable_mask - Bitmask containing the desired channels to be disabled. + * + * Returned Value: + * None. + * + ****************************************************************************/ + +static inline void touch_lh_clear_channel_mask(uint16_t disable_mask) +{ + resetbits(SENS_SAR_TOUCH_ENABLE_REG, (TOUCH_LH_BITS_SWAP(disable_mask) << + SENS_TOUCH_PAD_WORKEN_S)); +} + +/**************************************************************************** + * Name: touch_lh_set_group_mask + * + * Description: + * Enable channels in touch interrupt groups. + * + * Input Parameters: + * group1_mask - Bitmask containing the desired channels to be + * added to SET1; + * group2_mask - Bitmask containing the desired channels to be + * added to SET2. + * + * Returned Value: + * None. + * + ****************************************************************************/ + +static inline void touch_lh_set_group_mask(uint16_t group1_mask, + uint16_t group2_mask) +{ + setbits(SENS_SAR_TOUCH_ENABLE_REG, (TOUCH_LH_BITS_SWAP(group1_mask) << + SENS_TOUCH_PAD_OUTEN1_S)); + + setbits(SENS_SAR_TOUCH_ENABLE_REG, (TOUCH_LH_BITS_SWAP(group2_mask) << + SENS_TOUCH_PAD_OUTEN2_S)); +} + +/**************************************************************************** + * Name: touch_lh_get_channel_mask + * + * Description: + * Get the active touch interrupt groups. + * + * Input Parameters: + * group1_mask - Pointer to allocated uint16_t that will be modified to + * return the current SET1 bitmask. + * group2_mask - Pointer to allocated uint16_t that will be modified to + * return the current SET2 bitmask. + * + * Returned Value: + * None. + * + ****************************************************************************/ + +static inline void touch_lh_get_group_mask(uint16_t *group1_mask, + uint16_t *group2_mask) +{ + *group1_mask = TOUCH_LH_BITS_SWAP( + REG_GET_FIELD(SENS_SAR_TOUCH_ENABLE_REG, SENS_TOUCH_PAD_OUTEN1)); + *group2_mask = TOUCH_LH_BITS_SWAP( + REG_GET_FIELD(SENS_SAR_TOUCH_ENABLE_REG, SENS_TOUCH_PAD_OUTEN2)); +} + +/**************************************************************************** + * Name: touch_lh_clear_group_mask + * + * Description: + * Remove channels in touch interrupt groups. + * + * Input Parameters: + * group1_mask - Bitmask containing the desired channels to be + * removed from SET1; + * group2_mask - Bitmask containing the desired channels to be + * removed from SET2. + * + * Returned Value: + * None. + * + ****************************************************************************/ + +static inline void touch_lh_clear_group_mask(uint16_t group1_mask, + uint16_t group2_mask) +{ + resetbits(SENS_SAR_TOUCH_ENABLE_REG, (TOUCH_LH_BITS_SWAP(group1_mask) << + SENS_TOUCH_PAD_OUTEN1_S)); + + resetbits(SENS_SAR_TOUCH_ENABLE_REG, (TOUCH_LH_BITS_SWAP(group2_mask) << + SENS_TOUCH_PAD_OUTEN2_S)); +} + +/**************************************************************************** + * Name: touch_lh_read_trigger_status_mask + * + * Description: + * Get the channels that triggered a touch interruption. + * + * Input Parameters: + * None. + * + * Returned Value: + * Bitmask of the channels that triggered a touch interruption. + * + ****************************************************************************/ + +static inline uint32_t touch_lh_read_trigger_status_mask(void) +{ + return TOUCH_LH_BITS_SWAP( + REG_GET_FIELD(SENS_SAR_TOUCH_CTRL2_REG, SENS_TOUCH_MEAS_EN)); +} + +/**************************************************************************** + * Name: touch_lh_clear_trigger_status_mask + * + * Description: + * Clear the touch interruption channels bitmask. + * + * Input Parameters: + * None. + * + * Returned Value: + * None. + * + ****************************************************************************/ + +static inline void touch_lh_clear_trigger_status_mask(void) +{ + REG_SET_FIELD(SENS_SAR_TOUCH_CTRL2_REG, SENS_TOUCH_MEAS_EN_CLR, true); +} + +/**************************************************************************** + * Name: touch_lh_intr_enable + * + * Description: + * Enable the touch interruption. + * + * Input Parameters: + * None. + * + * Returned Value: + * None. + * + ****************************************************************************/ + +static inline void touch_lh_intr_enable(void) +{ + REG_SET_FIELD(RTC_CNTL_INT_ENA_REG, RTC_CNTL_TOUCH_INT_ENA, true); +} + +/**************************************************************************** + * Name: touch_lh_intr_disable + * + * Description: + * Disable the touch interruption. + * + * Input Parameters: + * None. + * + * Returned Value: + * None. + * + ****************************************************************************/ + +static inline void touch_lh_intr_disable(void) +{ + REG_SET_FIELD(RTC_CNTL_INT_ENA_REG, RTC_CNTL_TOUCH_INT_ENA, false); +} + +/**************************************************************************** + * Name: touch_lh_intr_clear + * + * Description: + * Clear the touch interruption status. + * + * Input Parameters: + * None. + * + * Returned Value: + * None. + * + ****************************************************************************/ + +static inline void touch_lh_intr_clear(void) +{ + REG_SET_FIELD(RTC_CNTL_INT_CLR_REG, RTC_CNTL_TOUCH_INT_CLR, true); +} + +/**************************************************************************** + * Name: touch_lh_read_raw_data + * + * Description: + * Get the measured value for a given touch pad. + * + * Input Parameters: + * tp - The touch pad channel. + * + * Returned Value: + * The current measured value for that touch pad. + * + ****************************************************************************/ + +static inline uint16_t touch_lh_read_raw_data(enum touch_pad_e tp) +{ + enum touch_pad_e tp_wrap = touch_lh_num_wrap(tp); + + const uint32_t reg_addr[] = + { + SENS_SAR_TOUCH_OUT1_REG, + SENS_SAR_TOUCH_OUT2_REG, + SENS_SAR_TOUCH_OUT3_REG, + SENS_SAR_TOUCH_OUT4_REG, + SENS_SAR_TOUCH_OUT5_REG, + }; + + if (tp_wrap & 0x1) /* Odd */ + { + /* All odd touch pads have the same position for the MEAS_OUTN bits. + * We can use any odd SENS_TOUCH_MEAS_OUTN. + */ + + return REG_GET_FIELD(reg_addr[tp_wrap / 2], SENS_TOUCH_MEAS_OUT1); + } + else /* Even */ + { + /* All even touch pads have the same position for the MEAS_OUTN bits. + * We can use any even SENS_TOUCH_MEAS_OUTN. + */ + + return REG_GET_FIELD(reg_addr[tp_wrap / 2], SENS_TOUCH_MEAS_OUT0); + } +} + +/**************************************************************************** + * Name: touch_lh_meas_is_done + * + * Description: + * Check if measurement is done. + * + * Input Parameters: + * None. + * + * Returned Value: + * True if yes, false if no. + * + ****************************************************************************/ + +static inline bool touch_lh_meas_is_done(void) +{ + return (bool) REG_GET_FIELD(SENS_SAR_TOUCH_CTRL2_REG, + SENS_TOUCH_MEAS_DONE); +} + +/**************************************************************************** + * Public Function Prototypes + ****************************************************************************/ + +#ifdef __cplusplus +} +#endif +#undef EXTERN + +#endif /* __ASSEMBLY__ */ +#endif /* __ARCH_XTENSA_SRC_ESP32_ESP32_TOUCH_LOWERHALF_H */ diff --git a/arch/xtensa/src/esp32/hardware/esp32_touch.h b/arch/xtensa/src/esp32/hardware/esp32_touch.h new file mode 100644 index 0000000000..05aa32ef95 --- /dev/null +++ b/arch/xtensa/src/esp32/hardware/esp32_touch.h @@ -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 */