bootloader_console.c 4.3 KB

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