2021-05-09 22:35:07 -04:00
|
|
|
/*
|
2022-01-17 21:32:56 -05:00
|
|
|
* SPDX-FileCopyrightText: 2020-2022 Espressif Systems (Shanghai) CO LTD
|
2021-05-09 22:35:07 -04:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
2020-04-30 09:29:23 -04:00
|
|
|
|
|
|
|
#include "sdkconfig.h"
|
|
|
|
#include "bootloader_console.h"
|
|
|
|
#include "soc/uart_periph.h"
|
|
|
|
#include "soc/uart_channel.h"
|
|
|
|
#include "soc/io_mux_reg.h"
|
|
|
|
#include "soc/gpio_periph.h"
|
|
|
|
#include "soc/gpio_sig_map.h"
|
|
|
|
#include "soc/rtc.h"
|
|
|
|
#include "hal/clk_gate_ll.h"
|
2021-03-15 22:55:05 -04:00
|
|
|
#include "hal/gpio_hal.h"
|
2020-07-21 01:07:34 -04:00
|
|
|
#if CONFIG_IDF_TARGET_ESP32S2
|
2020-04-30 09:29:23 -04:00
|
|
|
#include "esp32s2/rom/usb/cdc_acm.h"
|
|
|
|
#include "esp32s2/rom/usb/usb_common.h"
|
2020-11-18 23:42:05 -05:00
|
|
|
#elif CONFIG_IDF_TARGET_ESP32C3
|
|
|
|
#include "esp32c3/rom/ets_sys.h"
|
2021-05-12 23:29:47 -04:00
|
|
|
#include "esp32c3/rom/uart.h"
|
2021-06-04 02:48:20 -04:00
|
|
|
#elif CONFIG_IDF_TARGET_ESP32S3
|
|
|
|
#include "esp32s3/rom/uart.h"
|
2021-06-10 07:28:18 -04:00
|
|
|
#elif CONFIG_IDF_TARGET_ESP32H2
|
|
|
|
#include "esp32h2/rom/ets_sys.h"
|
|
|
|
#include "esp32h2/rom/uart.h"
|
2022-01-17 21:32:56 -05:00
|
|
|
#elif CONFIG_IDF_TARGET_ESP32C2
|
|
|
|
#include "esp32c2/rom/ets_sys.h"
|
|
|
|
#include "esp32c2/rom/uart.h"
|
2020-04-30 09:29:23 -04:00
|
|
|
#endif
|
2020-06-19 00:00:58 -04:00
|
|
|
#include "esp_rom_gpio.h"
|
2020-07-13 09:33:23 -04:00
|
|
|
#include "esp_rom_uart.h"
|
2020-07-21 01:07:34 -04:00
|
|
|
#include "esp_rom_sys.h"
|
2020-11-18 23:42:05 -05:00
|
|
|
#include "esp_rom_caps.h"
|
2020-04-30 09:29:23 -04:00
|
|
|
|
2022-03-15 04:28:53 -04:00
|
|
|
#ifdef CONFIG_ESP_CONSOLE_NONE
|
2020-04-30 09:29:23 -04:00
|
|
|
void bootloader_console_init(void)
|
|
|
|
{
|
2020-07-21 01:07:34 -04:00
|
|
|
esp_rom_install_channel_putc(1, NULL);
|
|
|
|
esp_rom_install_channel_putc(2, NULL);
|
2020-04-30 09:29:23 -04:00
|
|
|
}
|
2022-03-15 04:28:53 -04:00
|
|
|
#endif // CONFIG_ESP_CONSOLE_NONE
|
2020-04-30 09:29:23 -04:00
|
|
|
|
|
|
|
#ifdef CONFIG_ESP_CONSOLE_UART
|
|
|
|
void bootloader_console_init(void)
|
|
|
|
{
|
|
|
|
const int uart_num = CONFIG_ESP_CONSOLE_UART_NUM;
|
|
|
|
|
2020-11-18 23:42:05 -05:00
|
|
|
#if !ESP_ROM_SUPPORT_MULTIPLE_UART
|
|
|
|
/* esp_rom_install_channel_put is not available unless multiple UARTs are supported */
|
2020-12-01 08:34:53 -05:00
|
|
|
esp_rom_install_uart_printf();
|
2020-11-18 23:42:05 -05:00
|
|
|
#else
|
|
|
|
esp_rom_install_channel_putc(1, esp_rom_uart_putc);
|
|
|
|
#endif
|
2020-04-30 09:29:23 -04:00
|
|
|
|
|
|
|
// Wait for UART FIFO to be empty.
|
2020-07-13 09:33:23 -04:00
|
|
|
esp_rom_uart_tx_wait_idle(0);
|
2020-04-30 09:29:23 -04:00
|
|
|
|
|
|
|
#if CONFIG_ESP_CONSOLE_UART_CUSTOM
|
|
|
|
// Some constants to make the following code less upper-case
|
|
|
|
const int uart_tx_gpio = CONFIG_ESP_CONSOLE_UART_TX_GPIO;
|
|
|
|
const int uart_rx_gpio = CONFIG_ESP_CONSOLE_UART_RX_GPIO;
|
2020-07-21 01:07:34 -04:00
|
|
|
// Switch to the new UART (this just changes UART number used for esp_rom_printf in ROM code).
|
2020-12-01 08:34:53 -05:00
|
|
|
#if ESP_ROM_SUPPORT_MULTIPLE_UART
|
2020-07-13 09:33:23 -04:00
|
|
|
esp_rom_uart_set_as_console(uart_num);
|
2020-12-01 08:34:53 -05:00
|
|
|
#endif
|
2020-04-30 09:29:23 -04:00
|
|
|
// If console is attached to UART1 or if non-default pins are used,
|
|
|
|
// need to reconfigure pins using GPIO matrix
|
|
|
|
if (uart_num != 0 ||
|
|
|
|
uart_tx_gpio != UART_NUM_0_TXD_DIRECT_GPIO_NUM ||
|
|
|
|
uart_rx_gpio != UART_NUM_0_RXD_DIRECT_GPIO_NUM) {
|
|
|
|
// Change default UART pins back to GPIOs
|
2021-03-15 22:55:05 -04:00
|
|
|
gpio_hal_iomux_func_sel(PERIPHS_IO_MUX_U0RXD_U, PIN_FUNC_GPIO);
|
|
|
|
gpio_hal_iomux_func_sel(PERIPHS_IO_MUX_U0TXD_U, PIN_FUNC_GPIO);
|
2020-04-30 09:29:23 -04:00
|
|
|
// Route GPIO signals to/from pins
|
2021-07-09 02:20:33 -04:00
|
|
|
const uint32_t tx_idx = UART_PERIPH_SIGNAL(uart_num, SOC_UART_TX_PIN_IDX);
|
|
|
|
const uint32_t rx_idx = UART_PERIPH_SIGNAL(uart_num, SOC_UART_RX_PIN_IDX);
|
2022-01-24 23:08:42 -05:00
|
|
|
gpio_hal_iomux_func_sel(GPIO_PIN_MUX_REG[uart_rx_gpio], PIN_FUNC_GPIO);
|
2020-04-30 09:29:23 -04:00
|
|
|
PIN_INPUT_ENABLE(GPIO_PIN_MUX_REG[uart_rx_gpio]);
|
2020-06-19 00:00:58 -04:00
|
|
|
esp_rom_gpio_pad_pullup_only(uart_rx_gpio);
|
|
|
|
esp_rom_gpio_connect_out_signal(uart_tx_gpio, tx_idx, 0, 0);
|
|
|
|
esp_rom_gpio_connect_in_signal(uart_rx_gpio, rx_idx, 0);
|
2022-01-24 23:08:42 -05:00
|
|
|
gpio_hal_iomux_func_sel(GPIO_PIN_MUX_REG[uart_tx_gpio], PIN_FUNC_GPIO);
|
2020-04-30 09:29:23 -04:00
|
|
|
// Enable the peripheral
|
|
|
|
periph_ll_enable_clk_clear_rst(PERIPH_UART0_MODULE + uart_num);
|
|
|
|
}
|
|
|
|
#endif // CONFIG_ESP_CONSOLE_UART_CUSTOM
|
|
|
|
|
|
|
|
// Set configured UART console baud rate
|
2020-07-29 10:03:46 -04:00
|
|
|
uint32_t clock_hz = rtc_clk_apb_freq_get();
|
2020-12-01 08:34:53 -05:00
|
|
|
#if ESP_ROM_UART_CLK_IS_XTAL
|
2020-07-29 10:03:46 -04:00
|
|
|
clock_hz = UART_CLK_FREQ_ROM; // From esp32-s3 on, UART clock source is selected to XTAL in ROM
|
|
|
|
#endif
|
|
|
|
esp_rom_uart_set_clock_baudrate(uart_num, clock_hz, CONFIG_ESP_CONSOLE_UART_BAUDRATE);
|
2020-04-30 09:29:23 -04:00
|
|
|
}
|
|
|
|
#endif // CONFIG_ESP_CONSOLE_UART
|
|
|
|
|
|
|
|
#ifdef CONFIG_ESP_CONSOLE_USB_CDC
|
|
|
|
/* Buffer for CDC data structures. No RX buffer allocated. */
|
2020-07-13 09:33:23 -04:00
|
|
|
static char s_usb_cdc_buf[ESP_ROM_CDC_ACM_WORK_BUF_MIN];
|
2020-04-30 09:29:23 -04:00
|
|
|
|
|
|
|
void bootloader_console_init(void)
|
|
|
|
{
|
|
|
|
#ifdef CONFIG_IDF_TARGET_ESP32S2
|
|
|
|
/* ESP32-S2 specific patch to set the correct serial number in the descriptor.
|
|
|
|
* Later chips don't need this.
|
|
|
|
*/
|
|
|
|
rom_usb_cdc_set_descriptor_patch();
|
|
|
|
#endif
|
|
|
|
|
2020-07-13 09:33:23 -04:00
|
|
|
esp_rom_uart_usb_acm_init(s_usb_cdc_buf, sizeof(s_usb_cdc_buf));
|
|
|
|
esp_rom_uart_set_as_console(ESP_ROM_UART_USB);
|
2020-07-21 01:07:34 -04:00
|
|
|
esp_rom_install_channel_putc(1, bootloader_console_write_char_usb);
|
2020-04-30 09:29:23 -04:00
|
|
|
}
|
|
|
|
#endif //CONFIG_ESP_CONSOLE_USB_CDC
|
2021-04-28 04:38:24 -04:00
|
|
|
|
|
|
|
#ifdef CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG
|
|
|
|
void bootloader_console_init(void)
|
|
|
|
{
|
2021-05-12 23:29:47 -04:00
|
|
|
UartDevice *uart = GetUartDevice();
|
|
|
|
uart->buff_uart_no = ESP_ROM_USB_SERIAL_DEVICE_NUM;
|
2021-04-28 04:38:24 -04:00
|
|
|
}
|
|
|
|
#endif //CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG
|