diff --git a/arch/xtensa/include/esp32s3/chip.h b/arch/xtensa/include/esp32s3/chip.h index c3473699fd..e0e5c74da7 100644 --- a/arch/xtensa/include/esp32s3/chip.h +++ b/arch/xtensa/include/esp32s3/chip.h @@ -31,10 +31,6 @@ * Pre-processor Definitions ****************************************************************************/ -#define ESP32S3_NGPIOS 45 /* GPIO0-44 */ - -/* Characterize each supported ESP32-S3 part */ - /**************************************************************************** * Public Types ****************************************************************************/ diff --git a/arch/xtensa/src/esp32s3/esp32s3_gpio.c b/arch/xtensa/src/esp32s3/esp32s3_gpio.c index 79923418d3..95674ddb19 100644 --- a/arch/xtensa/src/esp32s3/esp32s3_gpio.c +++ b/arch/xtensa/src/esp32s3/esp32s3_gpio.c @@ -32,7 +32,6 @@ #include #include #include -#include #include "xtensa.h" #include "esp32s3_irq.h" @@ -45,6 +44,8 @@ * Pre-processor Definitions ****************************************************************************/ +#define ESP32S3_NPINS 49 + /**************************************************************************** * Private Data ****************************************************************************/ @@ -53,6 +54,27 @@ * Private Functions ****************************************************************************/ +/**************************************************************************** + * Name: is_valid_gpio + * + * Description: + * Check if the requested pin is a valid GPIO pin. + * + * Input Parameters: + * pin - Pin to be checked for validity. + * + * Returned Value: + * True if the requested pin is a valid GPIO pin, false otherwise. + * + ****************************************************************************/ + +static inline bool is_valid_gpio(uint32_t pin) +{ + /* ESP32-S3 has 45 GPIO pins numbered from 0 to 21 and 26 to 48 */ + + return pin <= 21 || (pin >= 26 && pin < ESP32S3_NPINS); +} + /**************************************************************************** * Public Functions ****************************************************************************/ @@ -63,16 +85,30 @@ * Description: * Configure a GPIO pin based on encoded pin attributes. * + * Input Parameters: + * pin - GPIO pin to be configured. + * attr - Attributes to be configured for the selected GPIO pin. + * The following attributes are accepted: + * - Direction (OUTPUT or INPUT) + * - Pull (PULLUP, PULLDOWN or OPENDRAIN) + * - Function (if not provided, assume function GPIO by + * default) + * - Drive strength (if not provided, assume DRIVE_2 by + * default) + * + * Returned Value: + * Zero (OK) on success, or -1 (ERROR) in case of failure. + * ****************************************************************************/ -int esp32s3_configgpio(int pin, gpio_pinattr_t attr) +int esp32s3_configgpio(uint32_t pin, gpio_pinattr_t attr) { uintptr_t regaddr; uint32_t func; uint32_t cntrl; uint32_t pin2func; - DEBUGASSERT(pin >= 0 && pin <= ESP32S3_NGPIOS); + DEBUGASSERT(is_valid_gpio(pin)); func = 0; cntrl = 0; @@ -81,7 +117,14 @@ int esp32s3_configgpio(int pin, gpio_pinattr_t attr) if ((attr & INPUT) != 0) { - putreg32((1ul << pin), GPIO_ENABLE_W1TC_REG); + if (pin < 32) + { + putreg32((UINT32_C(1) << pin), GPIO_ENABLE_W1TC_REG); + } + else + { + putreg32((UINT32_C(1) << (pin - 32)), GPIO_ENABLE1_W1TC_REG); + } /* Input enable */ @@ -101,26 +144,37 @@ int esp32s3_configgpio(int pin, gpio_pinattr_t attr) if ((attr & OUTPUT) != 0) { - putreg32((1ul << pin), GPIO_ENABLE_W1TS_REG); + putreg32((UINT32_C(1) << pin), GPIO_ENABLE_W1TS_REG); } - /* Add drivers */ - - func |= (uint32_t)(2ul << FUN_DRV_S); - - /* Select the pad's function. If no function was given, consider it a - * normal input or output (i.e. function1). - */ + /* Configure the pad's function */ if ((attr & FUNCTION_MASK) != 0) { - func |= (uint32_t)(((attr >> FUNCTION_SHIFT) - 1) << MCU_SEL_S); + 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); @@ -141,25 +195,35 @@ int esp32s3_configgpio(int pin, gpio_pinattr_t attr) * Name: esp32s3_gpio_matrix_in * * Description: - * Set gpio input to a signal - * NOTE: one gpio can input to several signals - * If gpio == 0x3c, cancel input to the signal, input 0 to signal. - * If gpio == 0x3a, input nothing to signal. - * If gpio == 0x38, cancel input to the signal, input 1 to signal. + * Set GPIO input to a signal. + * NOTE: one GPIO can input to several signals. + * + * Input Parameters: + * pin - GPIO pin to be configured. + * - If pin == 0x3c, cancel input to the signal, input 0 + * to signal. + * - If pin == 0x3a, input nothing to signal. + * - If pin == 0x38, cancel input to the signal, input 1 + * to signal. + * signal_idx - Signal index. + * inv - Flag indicating whether the signal is inverted. + * + * Returned Value: + * None. * ****************************************************************************/ -void esp32s3_gpio_matrix_in(uint32_t gpio, uint32_t signal_idx, bool inv) +void esp32s3_gpio_matrix_in(uint32_t pin, uint32_t signal_idx, bool inv) { uint32_t regaddr = GPIO_FUNC0_IN_SEL_CFG_REG + (signal_idx * 4); - uint32_t regval = (gpio << GPIO_FUNC0_IN_SEL_S); + uint32_t regval = pin << GPIO_FUNC0_IN_SEL_S; if (inv) { regval |= GPIO_FUNC0_IN_INV_SEL; } - if (gpio != 0x3a) + if (pin != 0x3a) { regval |= GPIO_SIG0_IN_SEL; } @@ -171,24 +235,31 @@ void esp32s3_gpio_matrix_in(uint32_t gpio, uint32_t signal_idx, bool inv) * Name: esp32s3_gpio_matrix_out * * Description: - * Set signal output to gpio - * NOTE: one signal can output to several gpios - * If signal_idx == 0x100, cancel output put to the gpio + * Set signal output to GPIO. + * NOTE: one signal can output to several GPIOs. + * + * Input Parameters: + * pin - GPIO pin to be configured. + * signal_idx - Signal index. + * - If signal_idx == 0x100, cancel output to the GPIO. + * out_inv - Flag indicating whether the signal output is inverted. + * oen_inv - Flag indicating whether the signal output enable is + * inverted. + * + * Returned Value: + * None. * ****************************************************************************/ -void esp32s3_gpio_matrix_out(uint32_t gpio, uint32_t signal_idx, - bool out_inv, bool oen_inv) +void esp32s3_gpio_matrix_out(uint32_t pin, uint32_t signal_idx, bool out_inv, + bool oen_inv) { - uint32_t regaddr = GPIO_FUNC0_OUT_SEL_CFG_REG + (gpio * 4); + uint32_t regaddr = GPIO_FUNC0_OUT_SEL_CFG_REG + (pin * 4); uint32_t regval = signal_idx << GPIO_FUNC0_OUT_SEL_S; - if (gpio >= ESP32S3_NGPIOS) - { - return; - } + DEBUGASSERT(is_valid_gpio(pin)); - putreg32(1ul << gpio, GPIO_ENABLE_W1TS_REG); + putreg32(1ul << pin, GPIO_ENABLE_W1TS_REG); if (out_inv) { diff --git a/arch/xtensa/src/esp32s3/esp32s3_gpio.h b/arch/xtensa/src/esp32s3/esp32s3_gpio.h index b254dd3f40..b173e047d5 100644 --- a/arch/xtensa/src/esp32s3/esp32s3_gpio.h +++ b/arch/xtensa/src/esp32s3/esp32s3_gpio.h @@ -47,38 +47,51 @@ * FN FN FN OD PD PU F O I */ -#define PINMODE_SHIFT 0 -#define PINMODE_MASK (7 << PINMODE_SHIFT) +#define MODE_SHIFT 0 +#define MODE_MASK (7 << MODE_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 PULL_SHIFT 3 +#define PULL_MASK (7 << PULL_SHIFT) +# 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_0 (1 << FUNCTION_SHIFT) -# define FUNCTION_1 (2 << FUNCTION_SHIFT) -# define FUNCTION_2 (3 << FUNCTION_SHIFT) -# define FUNCTION_3 (4 << FUNCTION_SHIFT) -# define FUNCTION_4 (5 << 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 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_0 (INPUT_FUNCTION | FUNCTION_0) # 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_0 (OUTPUT_FUNCTION | FUNCTION_0) # 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) /* Interrupt type used with esp32s3_gpioirqenable() */ @@ -123,36 +136,70 @@ extern "C" * Description: * Configure a GPIO pin based on encoded pin attributes. * + * Input Parameters: + * pin - GPIO pin to be configured. + * attr - Attributes to be configured for the selected GPIO pin. + * The following attributes are accepted: + * - Direction (OUTPUT or INPUT) + * - Pull (PULLUP, PULLDOWN or OPENDRAIN) + * - Function (if not provided, assume function GPIO by + * default) + * - Drive strength (if not provided, assume DRIVE_2 by + * default) + * + * Returned Value: + * Zero (OK) on success, or -1 (ERROR) in case of failure. + * ****************************************************************************/ -int esp32s3_configgpio(int pin, gpio_pinattr_t attr); +int esp32s3_configgpio(uint32_t pin, gpio_pinattr_t attr); /**************************************************************************** * Name: esp32s3_gpio_matrix_in * * Description: - * Set gpio input to a signal - * NOTE: one gpio can input to several signals - * If gpio == 0x3c, cancel input to the signal, input 0 to signal. - * If gpio == 0x3a, input nothing to signal. - * If gpio == 0x38, cancel input to the signal, input 1 to signal. + * Set GPIO input to a signal. + * NOTE: one GPIO can input to several signals. + * + * Input Parameters: + * pin - GPIO pin to be configured. + * - If pin == 0x3c, cancel input to the signal, input 0 + * to signal. + * - If pin == 0x3a, input nothing to signal. + * - If pin == 0x38, cancel input to the signal, input 1 + * to signal. + * signal_idx - Signal index. + * inv - Flag indicating whether the signal is inverted. + * + * Returned Value: + * None. * ****************************************************************************/ -void esp32s3_gpio_matrix_in(uint32_t gpio, uint32_t signal_idx, bool inv); +void esp32s3_gpio_matrix_in(uint32_t pin, uint32_t signal_idx, bool inv); /**************************************************************************** * Name: esp32s3_gpio_matrix_out * * Description: - * Set signal output to gpio - * NOTE: one signal can output to several gpios - * If signal_idx == 0x100, cancel output put to the gpio + * Set signal output to GPIO. + * NOTE: one signal can output to several GPIOs. + * + * Input Parameters: + * pin - GPIO pin to be configured. + * signal_idx - Signal index. + * - If signal_idx == 0x100, cancel output to the GPIO. + * out_inv - Flag indicating whether the signal output is inverted. + * oen_inv - Flag indicating whether the signal output enable is + * inverted. + * + * Returned Value: + * None. * ****************************************************************************/ -void esp32s3_gpio_matrix_out(uint32_t gpio, uint32_t signal_idx, - bool out_inv, bool oen_inv); +void esp32s3_gpio_matrix_out(uint32_t pin, uint32_t signal_idx, bool out_inv, + bool oen_inv); #ifdef __cplusplus } diff --git a/arch/xtensa/src/esp32s3/esp32s3_lowputc.c b/arch/xtensa/src/esp32s3/esp32s3_lowputc.c index fe0f7210af..37cbd0b3c4 100644 --- a/arch/xtensa/src/esp32s3/esp32s3_lowputc.c +++ b/arch/xtensa/src/esp32s3/esp32s3_lowputc.c @@ -738,9 +738,9 @@ void esp32s3_lowputc_config_pins(const struct esp32s3_uart_s *priv) /* Configure the pins */ esp32s3_gpio_matrix_out(priv->txpin, priv->txsig, 0, 0); - esp32s3_configgpio(priv->txpin, OUTPUT_FUNCTION_1); + esp32s3_configgpio(priv->txpin, OUTPUT_FUNCTION_2); - esp32s3_configgpio(priv->rxpin, INPUT_FUNCTION_1); + esp32s3_configgpio(priv->rxpin, INPUT_FUNCTION_2); esp32s3_gpio_matrix_in(priv->rxpin, priv->rxsig, 0); #ifdef CONFIG_SERIAL_IFLOWCONTROL diff --git a/arch/xtensa/src/esp32s3/hardware/esp32s3_iomux.h b/arch/xtensa/src/esp32s3/hardware/esp32s3_iomux.h index 1ab574438e..82c23b810f 100644 --- a/arch/xtensa/src/esp32s3/hardware/esp32s3_iomux.h +++ b/arch/xtensa/src/esp32s3/hardware/esp32s3_iomux.h @@ -167,7 +167,6 @@ #define IO_MUX_GPIO47_REG PERIPHS_IO_MUX_SPICLK_P_U #define IO_MUX_GPIO48_REG PERIPHS_IO_MUX_SPICLK_N_U -#define FUNC_GPIO_GPIO 1 #define PIN_FUNC_GPIO 1 #define GPIO_PAD_PULLUP(num) do{PIN_PULLDWN_DIS(IOMUX_REG_GPIO##num);PIN_PULLUP_EN(IOMUX_REG_GPIO##num);}while(0)