1 /*
2 * SPDX-FileCopyrightText: 2020-2024 Espressif Systems (Shanghai) CO LTD
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include "sdkconfig.h"
8 #include "bootloader_console.h"
9 #include "soc/soc_caps.h"
10 #include "soc/uart_periph.h"
11 #include "soc/uart_channel.h"
12 #include "soc/io_mux_reg.h"
13 #include "soc/gpio_periph.h"
14 #include "soc/gpio_sig_map.h"
15 #include "soc/rtc.h"
16 #include "hal/clk_gate_ll.h"
17 #include "hal/clk_tree_ll.h" // MHZ definition
18 #include "hal/gpio_hal.h"
19 #if CONFIG_IDF_TARGET_ESP32S2
20 #include "esp32s2/rom/usb/cdc_acm.h"
21 #include "esp32s2/rom/usb/usb_common.h"
22 #endif
23 #if CONFIG_ESP_CONSOLE_USB_CDC
24 #include "hal/usb_wrap_ll.h"
25 #endif
26 #include "esp_rom_gpio.h"
27 #include "esp_rom_uart.h"
28 #include "esp_rom_sys.h"
29 #include "esp_rom_caps.h"
30
31 #ifdef CONFIG_ESP_CONSOLE_NONE
bootloader_console_init(void)32 void bootloader_console_init(void)
33 {
34 esp_rom_install_channel_putc(1, NULL);
35 esp_rom_install_channel_putc(2, NULL);
36 }
37 #endif // CONFIG_ESP_CONSOLE_NONE
38
39 #ifdef CONFIG_ESP_CONSOLE_UART
bootloader_console_init(void)40 void bootloader_console_init(void)
41 {
42 const int uart_num = CONFIG_ESP_CONSOLE_UART_NUM;
43
44 // Install rom uart printf as console.
45 esp_rom_install_uart_printf();
46
47 // Wait for UART FIFO to be empty.
48 esp_rom_uart_tx_wait_idle(0);
49
50 #if CONFIG_ESP_CONSOLE_UART_CUSTOM
51 // Some constants to make the following code less upper-case
52 const int uart_tx_gpio = CONFIG_ESP_CONSOLE_UART_TX_GPIO;
53 const int uart_rx_gpio = CONFIG_ESP_CONSOLE_UART_RX_GPIO;
54
55 // Switch to the new UART (this just changes UART number used for esp_rom_printf in ROM code).
56 esp_rom_uart_set_as_console(uart_num);
57
58 // If console is attached to UART1 or if non-default pins are used,
59 // need to reconfigure pins using GPIO matrix
60 if (uart_num != 0 ||
61 uart_tx_gpio != UART_NUM_0_TXD_DIRECT_GPIO_NUM ||
62 uart_rx_gpio != UART_NUM_0_RXD_DIRECT_GPIO_NUM) {
63 // Change default UART pins back to GPIOs
64 gpio_hal_iomux_func_sel(PERIPHS_IO_MUX_U0RXD_U, PIN_FUNC_GPIO);
65 gpio_hal_iomux_func_sel(PERIPHS_IO_MUX_U0TXD_U, PIN_FUNC_GPIO);
66 // Route GPIO signals to/from pins
67 const uint32_t tx_idx = UART_PERIPH_SIGNAL(uart_num, SOC_UART_TX_PIN_IDX);
68 const uint32_t rx_idx = UART_PERIPH_SIGNAL(uart_num, SOC_UART_RX_PIN_IDX);
69 gpio_hal_iomux_func_sel(GPIO_PIN_MUX_REG[uart_rx_gpio], PIN_FUNC_GPIO);
70 PIN_INPUT_ENABLE(GPIO_PIN_MUX_REG[uart_rx_gpio]);
71 esp_rom_gpio_pad_pullup_only(uart_rx_gpio);
72 esp_rom_gpio_connect_out_signal(uart_tx_gpio, tx_idx, 0, 0);
73 esp_rom_gpio_connect_in_signal(uart_rx_gpio, rx_idx, 0);
74 gpio_hal_iomux_func_sel(GPIO_PIN_MUX_REG[uart_tx_gpio], PIN_FUNC_GPIO);
75 // Enable the peripheral
76 periph_ll_enable_clk_clear_rst(PERIPH_UART0_MODULE + uart_num);
77 }
78 #endif // CONFIG_ESP_CONSOLE_UART_CUSTOM
79
80 // Set configured UART console baud rate
81 uint32_t clock_hz = rtc_clk_apb_freq_get();
82 #if ESP_ROM_UART_CLK_IS_XTAL
83 clock_hz = (uint32_t)rtc_clk_xtal_freq_get() * MHZ; // From esp32-s3 on, UART clk source is selected to XTAL in ROM
84 #endif
85 esp_rom_uart_set_clock_baudrate(uart_num, clock_hz, CONFIG_ESP_CONSOLE_UART_BAUDRATE);
86 }
87 #endif // CONFIG_ESP_CONSOLE_UART
88
89 #ifdef CONFIG_ESP_CONSOLE_USB_CDC
90 /* Buffer for CDC data structures. No RX buffer allocated. */
91 static char s_usb_cdc_buf[ESP_ROM_CDC_ACM_WORK_BUF_MIN];
92
bootloader_console_init(void)93 void bootloader_console_init(void)
94 {
95 #ifdef CONFIG_IDF_TARGET_ESP32S2
96 /* ESP32-S2 specific patch to set the correct serial number in the descriptor.
97 * Later chips don't need this.
98 */
99 rom_usb_cdc_set_descriptor_patch();
100 #endif
101
102 esp_rom_uart_usb_acm_init(s_usb_cdc_buf, sizeof(s_usb_cdc_buf));
103 esp_rom_uart_set_as_console(ESP_ROM_USB_OTG_NUM);
104 esp_rom_install_channel_putc(1, bootloader_console_write_char_usb);
105 // Ensure that the USB FSLS PHY is mapped to the USB WRAP
106 usb_wrap_ll_phy_enable_pad(&USB_WRAP, true);
107 usb_wrap_ll_phy_enable_external(&USB_WRAP, false);
108 }
109 #endif //CONFIG_ESP_CONSOLE_USB_CDC
110
111 #ifdef CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG
bootloader_console_init(void)112 void bootloader_console_init(void)
113 {
114 esp_rom_uart_switch_buffer(ESP_ROM_USB_SERIAL_DEVICE_NUM);
115 }
116 #endif //CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG
117