bootloader_console.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. * SPDX-FileCopyrightText: 2020-2021 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. #elif CONFIG_IDF_TARGET_ESP32C3
  20. #include "esp32c3/rom/ets_sys.h"
  21. #include "esp32c3/rom/uart.h"
  22. #elif CONFIG_IDF_TARGET_ESP32S3
  23. #include "esp32s3/rom/uart.h"
  24. #elif CONFIG_IDF_TARGET_ESP32H2
  25. #include "esp32h2/rom/ets_sys.h"
  26. #include "esp32h2/rom/uart.h"
  27. #endif
  28. #include "esp_rom_gpio.h"
  29. #include "esp_rom_uart.h"
  30. #include "esp_rom_sys.h"
  31. #include "esp_rom_caps.h"
  32. #ifdef CONFIG_ESP_CONSOLE_UART_NONE
  33. void bootloader_console_init(void)
  34. {
  35. esp_rom_install_channel_putc(1, NULL);
  36. esp_rom_install_channel_putc(2, NULL);
  37. }
  38. #endif // CONFIG_ESP_CONSOLE_UART_NONE
  39. #ifdef CONFIG_ESP_CONSOLE_UART
  40. void bootloader_console_init(void)
  41. {
  42. const int uart_num = CONFIG_ESP_CONSOLE_UART_NUM;
  43. #if !ESP_ROM_SUPPORT_MULTIPLE_UART
  44. /* esp_rom_install_channel_put is not available unless multiple UARTs are supported */
  45. esp_rom_install_uart_printf();
  46. #else
  47. esp_rom_install_channel_putc(1, esp_rom_uart_putc);
  48. #endif
  49. // Wait for UART FIFO to be empty.
  50. esp_rom_uart_tx_wait_idle(0);
  51. #if CONFIG_ESP_CONSOLE_UART_CUSTOM
  52. // Some constants to make the following code less upper-case
  53. const int uart_tx_gpio = CONFIG_ESP_CONSOLE_UART_TX_GPIO;
  54. const int uart_rx_gpio = CONFIG_ESP_CONSOLE_UART_RX_GPIO;
  55. // Switch to the new UART (this just changes UART number used for esp_rom_printf in ROM code).
  56. #if ESP_ROM_SUPPORT_MULTIPLE_UART
  57. esp_rom_uart_set_as_console(uart_num);
  58. #endif
  59. // If console is attached to UART1 or if non-default pins are used,
  60. // need to reconfigure pins using GPIO matrix
  61. if (uart_num != 0 ||
  62. uart_tx_gpio != UART_NUM_0_TXD_DIRECT_GPIO_NUM ||
  63. uart_rx_gpio != UART_NUM_0_RXD_DIRECT_GPIO_NUM) {
  64. // Change default UART pins back to GPIOs
  65. gpio_hal_iomux_func_sel(PERIPHS_IO_MUX_U0RXD_U, PIN_FUNC_GPIO);
  66. gpio_hal_iomux_func_sel(PERIPHS_IO_MUX_U0TXD_U, PIN_FUNC_GPIO);
  67. // Route GPIO signals to/from pins
  68. const uint32_t tx_idx = UART_PERIPH_SIGNAL(uart_num, SOC_UART_TX_PIN_IDX);
  69. const uint32_t rx_idx = UART_PERIPH_SIGNAL(uart_num, SOC_UART_RX_PIN_IDX);
  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. // Enable the peripheral
  75. periph_ll_enable_clk_clear_rst(PERIPH_UART0_MODULE + uart_num);
  76. }
  77. #endif // CONFIG_ESP_CONSOLE_UART_CUSTOM
  78. // Set configured UART console baud rate
  79. uint32_t clock_hz = rtc_clk_apb_freq_get();
  80. #if ESP_ROM_UART_CLK_IS_XTAL
  81. clock_hz = UART_CLK_FREQ_ROM; // From esp32-s3 on, UART clock source is selected to XTAL in ROM
  82. #endif
  83. esp_rom_uart_set_clock_baudrate(uart_num, clock_hz, CONFIG_ESP_CONSOLE_UART_BAUDRATE);
  84. }
  85. #endif // CONFIG_ESP_CONSOLE_UART
  86. #ifdef CONFIG_ESP_CONSOLE_USB_CDC
  87. /* Buffer for CDC data structures. No RX buffer allocated. */
  88. static char s_usb_cdc_buf[ESP_ROM_CDC_ACM_WORK_BUF_MIN];
  89. void bootloader_console_init(void)
  90. {
  91. #ifdef CONFIG_IDF_TARGET_ESP32S2
  92. /* ESP32-S2 specific patch to set the correct serial number in the descriptor.
  93. * Later chips don't need this.
  94. */
  95. rom_usb_cdc_set_descriptor_patch();
  96. #endif
  97. esp_rom_uart_usb_acm_init(s_usb_cdc_buf, sizeof(s_usb_cdc_buf));
  98. esp_rom_uart_set_as_console(ESP_ROM_UART_USB);
  99. esp_rom_install_channel_putc(1, bootloader_console_write_char_usb);
  100. }
  101. #endif //CONFIG_ESP_CONSOLE_USB_CDC
  102. #ifdef CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG
  103. void bootloader_console_init(void)
  104. {
  105. UartDevice *uart = GetUartDevice();
  106. uart->buff_uart_no = ESP_ROM_USB_SERIAL_DEVICE_NUM;
  107. }
  108. #endif //CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG