diff --git a/cpu/esp32/startup.c b/cpu/esp32/startup.c index e3d8ff2dc8..6311758f2a 100644 --- a/cpu/esp32/startup.c +++ b/cpu/esp32/startup.c @@ -150,6 +150,10 @@ static NORETURN void IRAM system_startup_cpu0(void) /* initialize system call tables of ESP32x rom and newlib */ syscalls_init(); + /* systemwide UART initialization */ + extern void uart_system_init (void); + uart_system_init(); + /* initialize stdio */ esp_rom_uart_tx_wait_idle(CONFIG_ESP_CONSOLE_UART_NUM); early_init(); @@ -234,10 +238,6 @@ static NORETURN void IRAM system_init (void) /* install exception handlers */ init_exceptions(); - /* systemwide UART initialization */ - extern void uart_system_init (void); - uart_system_init(); - /* set log levels for SDK library outputs */ extern void esp_log_level_set(const char* tag, esp_log_level_t level); esp_log_level_set("wifi", LOG_DEBUG); diff --git a/cpu/esp_common/periph/uart.c b/cpu/esp_common/periph/uart.c index 021bf2a3d0..91771d3752 100644 --- a/cpu/esp_common/periph/uart.c +++ b/cpu/esp_common/periph/uart.c @@ -60,6 +60,7 @@ #else /* defined(MCU_ESP8266) */ #include "esp_rom_gpio.h" +#include "esp_rom_uart.h" #include "hal/interrupt_controller_types.h" #include "hal/interrupt_controller_ll.h" #include "soc/gpio_reg.h" @@ -68,6 +69,7 @@ #include "soc/periph_defs.h" #include "soc/rtc.h" #include "soc/soc_caps.h" +#include "soc/uart_pins.h" #include "soc/uart_reg.h" #include "soc/uart_struct.h" @@ -166,7 +168,10 @@ int uart_init(uart_t uart, uint32_t baudrate, uart_rx_cb_t rx_cb, void *arg) assert(uart < UART_NUMOF); #ifndef MCU_ESP8266 - /* reset the pins when they were already used as UART pins */ + assert(uart_config[uart].txd != GPIO_UNDEF); + assert(uart_config[uart].rxd != GPIO_UNDEF); + + /* reset the pin usage 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); } @@ -186,6 +191,7 @@ int uart_init(uart_t uart, uint32_t baudrate, uart_rx_cb_t rx_cb, void *arg) gpio_set_pin_usage(uart_config[uart].txd, _UART); gpio_set_pin_usage(uart_config[uart].rxd, _UART); + esp_rom_uart_tx_wait_idle(uart); 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, @@ -247,6 +253,18 @@ void uart_system_init(void) /* reset all UART interrupt status registers */ _uarts[uart].regs->int_clr.val = ~0; } +#ifndef MCU_ESP8266 + /* reset the pin usage of the default UART0 pins to GPIO to allow + * reinitialization and usage for other purposes */ + gpio_set_pin_usage(U0TXD_GPIO_NUM, _GPIO); + gpio_set_pin_usage(U0RXD_GPIO_NUM, _GPIO); +#if defined(CONFIG_CONSOLE_UART_TX) && defined(CONFIG_CONSOLE_UART_TX) + /* reset the pin usage of the UART pins used by the bootloader to + * _GPIO to be able to use them later for other purposes */ + gpio_set_pin_usage(CONFIG_CONSOLE_UART_TX, _GPIO); + gpio_set_pin_usage(CONFIG_CONSOLE_UART_RX, _GPIO); +#endif +#endif } void uart_print_config(void)