diff --git a/cpu/esp32/bootloader/sdkconfig.h b/cpu/esp32/bootloader/sdkconfig.h index 536a5c716307..93cab5f7f700 100644 --- a/cpu/esp32/bootloader/sdkconfig.h +++ b/cpu/esp32/bootloader/sdkconfig.h @@ -85,7 +85,25 @@ extern "C" { * Bootloader output baudrate, defined by the app settings as BAUD or * BOOTLOADER_BAUD. */ -#define CONFIG_ESP_CONSOLE_UART_BAUDRATE (RIOT_BOOTLOADER_BAUD) +#define CONFIG_ESP_CONSOLE_UART_BAUDRATE (RIOT_BOOTLOADER_BAUD) + +/* + * If custom TX and RX are defined, use custom UART configuration for 2nd stage + * bootloader. + */ +#if defined(CONFIG_CONSOLE_UART_RX) && defined(CONFIG_CONSOLE_UART_RX) +#define CONFIG_ESP_CONSOLE_UART_CUSTOM 1 +#define CONFIG_ESP_CONSOLE_UART_TX_GPIO CONFIG_CONSOLE_UART_TX +#define CONFIG_ESP_CONSOLE_UART_RX_GPIO CONFIG_CONSOLE_UART_RX +#else +#define CONFIG_ESP_CONSOLE_UART_DEFAULT 1 +#endif + +#if defined(CONFIG_CONSOLE_UART_NUM) +#define CONFIG_ESP_CONSOLE_UART_NUM CONFIG_CONSOLE_UART_NUM +#else +#define CONFIG_ESP_CONSOLE_UART_NUM 0 +#endif #ifdef __cplusplus } diff --git a/cpu/esp32/bootloader/sdkconfig_default_common.h b/cpu/esp32/bootloader/sdkconfig_default_common.h index ed2e75999872..9dcdd78d9328 100644 --- a/cpu/esp32/bootloader/sdkconfig_default_common.h +++ b/cpu/esp32/bootloader/sdkconfig_default_common.h @@ -33,11 +33,6 @@ extern "C" { #define CONFIG_BOOTLOADER_FLASH_XMC_SUPPORT 1 #define CONFIG_ESP_CONSOLE_UART 1 -#define CONFIG_ESP_CONSOLE_UART_DEFAULT 1 -#define CONFIG_ESP_CONSOLE_UART_NUM 0 - -#define CONFIG_CONSOLE_UART_NUM CONFIG_ESP_CONSOLE_UART_NUM -#define CONFIG_CONSOLE_UART_DEFAULT CONFIG_ESP_CONSOLE_UART_DEFAULT #define CONFIG_LOG_DEFAULT_LEVEL 3 #define CONFIG_LOG_TIMESTAMP_SOURCE_RTOS 1 diff --git a/cpu/esp32/include/periph_cpu.h b/cpu/esp32/include/periph_cpu.h index f813dfeefcb4..e90f857e4f6f 100644 --- a/cpu/esp32/include/periph_cpu.h +++ b/cpu/esp32/include/periph_cpu.h @@ -32,6 +32,11 @@ extern "C" { #endif +/** + * @brief UART device used for STDIO + */ +#define STDIO_UART_DEV CONFIG_ESP_CONSOLE_UART_NUM + /** * @name Power management configuration * @{ diff --git a/cpu/esp32/include/sdkconfig.h b/cpu/esp32/include/sdkconfig.h index e412962ab86f..d7a91d0d5d3d 100644 --- a/cpu/esp32/include/sdkconfig.h +++ b/cpu/esp32/include/sdkconfig.h @@ -55,15 +55,15 @@ /** * Default console configuration * - * STDIO_UART_BAUDRATE is used as CONFIG_CONSOLE_UART_BAUDRATE and + * STDIO_UART_BAUDRATE is used as CONFIG_ESP_CONSOLE_UART_BAUDRATE and * can be overridden by an application specific configuration. */ -#define CONFIG_CONSOLE_UART_NUM 0 -#define CONFIG_ESP_CONSOLE_UART_NUM CONFIG_CONSOLE_UART_NUM - -#ifndef CONFIG_CONSOLE_UART_BAUDRATE -#define CONFIG_CONSOLE_UART_BAUDRATE STDIO_UART_BAUDRATE +#ifdef CONFIG_CONSOLE_UART_NUM +#define CONFIG_ESP_CONSOLE_UART_NUM CONFIG_CONSOLE_UART_NUM +#else +#define CONFIG_ESP_CONSOLE_UART_NUM 0 #endif +#define CONFIG_ESP_CONSOLE_UART_BAUDRATE STDIO_UART_BAUDRATE /** * Log output configuration (DO NOT CHANGE) diff --git a/cpu/esp32/startup.c b/cpu/esp32/startup.c index 344bbae44ba0..9455f4033aeb 100644 --- a/cpu/esp32/startup.c +++ b/cpu/esp32/startup.c @@ -148,7 +148,7 @@ static NORETURN void IRAM system_startup_cpu0(void) #endif /* initialize stdio */ - esp_rom_uart_tx_wait_idle(CONFIG_CONSOLE_UART_NUM); + esp_rom_uart_tx_wait_idle(CONFIG_ESP_CONSOLE_UART_NUM); stdio_init(); RESET_REASON reset_reason = rtc_get_reset_reason(PRO_CPU_NUM); @@ -315,7 +315,7 @@ static NORETURN void IRAM system_init (void) /* starting RIOT */ #if IS_USED(MODULE_ESP_LOG_STARTUP) LOG_STARTUP("Starting RIOT kernel on PRO cpu\n"); - esp_rom_uart_tx_wait_idle(CONFIG_CONSOLE_UART_NUM); + esp_rom_uart_tx_wait_idle(CONFIG_ESP_CONSOLE_UART_NUM); #else puts(""); #endif diff --git a/cpu/esp_common/periph/uart.c b/cpu/esp_common/periph/uart.c index d6f836d253a1..021bf2a3d024 100644 --- a/cpu/esp_common/periph/uart.c +++ b/cpu/esp_common/periph/uart.c @@ -59,6 +59,7 @@ #else /* defined(MCU_ESP8266) */ +#include "esp_rom_gpio.h" #include "hal/interrupt_controller_types.h" #include "hal/interrupt_controller_ll.h" #include "soc/gpio_reg.h" @@ -164,53 +165,33 @@ int uart_init(uart_t uart, uint32_t baudrate, uart_rx_cb_t rx_cb, void *arg) assert(uart < UART_NUMOF_MAX); assert(uart < UART_NUMOF); - /* UART1 and UART2 have configurable pins */ - if ((UART_NUMOF > 1 && uart == UART_DEV(1)) || - (UART_NUMOF > 2 && uart == UART_DEV(2))) { - - /* reset the pins when they were already used as UART pins */ - if (gpio_get_pin_usage(uart_config[uart].txd) == _UART) { - gpio_set_pin_usage(uart_config[uart].txd, _GPIO); - } - if (gpio_get_pin_usage(uart_config[uart].rxd) == _UART) { - gpio_set_pin_usage(uart_config[uart].rxd, _GPIO); - } - - /* try to initialize the pins as GPIOs first */ - if ((uart_config[uart].txd != GPIO_UNDEF && - gpio_init(uart_config[uart].txd, GPIO_OUT)) || - (uart_config[uart].rxd != GPIO_UNDEF && - gpio_init(uart_config[uart].rxd, GPIO_IN))) { - return -1; - } +#ifndef MCU_ESP8266 + /* reset the pins when they were already used as UART pins */ + if (gpio_get_pin_usage(uart_config[uart].txd) == _UART) { + gpio_set_pin_usage(uart_config[uart].txd, _GPIO); + } + if (gpio_get_pin_usage(uart_config[uart].rxd) == _UART) { + gpio_set_pin_usage(uart_config[uart].rxd, _GPIO); + } - /* store the usage type in GPIO table */ - gpio_set_pin_usage(uart_config[uart].txd, _UART); - gpio_set_pin_usage(uart_config[uart].rxd, _UART); + /* try to initialize the pins as GPIOs first */ + if ((uart_config[uart].txd != GPIO_UNDEF && + gpio_init(uart_config[uart].txd, GPIO_OUT)) || + (uart_config[uart].rxd != GPIO_UNDEF && + gpio_init(uart_config[uart].rxd, GPIO_IN_PU))) { + return -1; + } -#ifdef MCU_ESP8266 - if (uart_config[uart].txd != GPIO_UNDEF) { - uint8_t mux = _gpio_to_iomux[uart_config[uart].txd]; - IOMUX.PIN[mux] = (IOMUX.PIN[mux] & ~IOMUX_PIN_FUNC_MASK) | - IOMUX_FUNC(uart_config[uart].txd == GPIO2 ? 2 : 4); - } - if (uart_config[uart].rxd != GPIO_UNDEF) { - /* There's really only GPIO8 / FUNC(4) for this, but it is normally - * unusable because it is used by the internal flash. */ - uint8_t mux = _gpio_to_iomux[uart_config[uart].rxd]; - IOMUX.PIN[mux] = (IOMUX.PIN[mux] & ~IOMUX_PIN_FUNC_MASK) | - IOMUX_FUNC(4); - } -#else /* MCU_ESP8266 */ - /* connect TxD pin to the TxD output signal through the GPIO matrix */ - GPIO.func_out_sel_cfg[uart_config[uart].txd].func_sel = _uarts[uart].signal_txd; + /* store the usage type in GPIO table */ + gpio_set_pin_usage(uart_config[uart].txd, _UART); + gpio_set_pin_usage(uart_config[uart].rxd, _UART); - /* connect RxD input signal to the RxD pin through the GPIO matrix */ - GPIO.func_in_sel_cfg[_uarts[uart].signal_rxd].sig_in_sel = 1; - GPIO.func_in_sel_cfg[_uarts[uart].signal_rxd].sig_in_inv = 0; - GPIO.func_in_sel_cfg[_uarts[uart].signal_rxd].func_sel = uart_config[uart].rxd; + esp_rom_gpio_connect_out_signal(uart_config[uart].txd, + _uarts[uart].signal_txd, false, false); + esp_rom_gpio_connect_in_signal(uart_config[uart].rxd, + _uarts[uart].signal_rxd, false); #endif /* MCU_ESP8266 */ - } + _uarts[uart].baudrate = baudrate; /* register interrupt context */