bootloader_console.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. * SPDX-FileCopyrightText: 2020-2022 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. #include "esp_rom_gpio.h"
  21. #include "esp_rom_uart.h"
  22. #include "esp_rom_sys.h"
  23. #include "esp_rom_caps.h"
  24. #ifdef CONFIG_ESP_CONSOLE_NONE
  25. void bootloader_console_init(void)
  26. {
  27. esp_rom_install_channel_putc(1, NULL);
  28. esp_rom_install_channel_putc(2, NULL);
  29. }
  30. #endif // CONFIG_ESP_CONSOLE_NONE
  31. #ifdef CONFIG_ESP_CONSOLE_UART
  32. void bootloader_console_init(void)
  33. {
  34. const int uart_num = CONFIG_ESP_CONSOLE_UART_NUM;
  35. // Install rom uart printf as console.
  36. esp_rom_install_uart_printf();
  37. // Wait for UART FIFO to be empty.
  38. esp_rom_uart_tx_wait_idle(0);
  39. #if CONFIG_ESP_CONSOLE_UART_CUSTOM
  40. // Some constants to make the following code less upper-case
  41. const int uart_tx_gpio = CONFIG_ESP_CONSOLE_UART_TX_GPIO;
  42. const int uart_rx_gpio = CONFIG_ESP_CONSOLE_UART_RX_GPIO;
  43. // Switch to the new UART (this just changes UART number used for esp_rom_printf in ROM code).
  44. esp_rom_uart_set_as_console(uart_num);
  45. // If console is attached to UART1 or if non-default pins are used,
  46. // need to reconfigure pins using GPIO matrix
  47. if (uart_num != 0 ||
  48. uart_tx_gpio != UART_NUM_0_TXD_DIRECT_GPIO_NUM ||
  49. uart_rx_gpio != UART_NUM_0_RXD_DIRECT_GPIO_NUM) {
  50. // Change default UART pins back to GPIOs
  51. gpio_hal_iomux_func_sel(PERIPHS_IO_MUX_U0RXD_U, PIN_FUNC_GPIO);
  52. gpio_hal_iomux_func_sel(PERIPHS_IO_MUX_U0TXD_U, PIN_FUNC_GPIO);
  53. // Route GPIO signals to/from pins
  54. const uint32_t tx_idx = UART_PERIPH_SIGNAL(uart_num, SOC_UART_TX_PIN_IDX);
  55. const uint32_t rx_idx = UART_PERIPH_SIGNAL(uart_num, SOC_UART_RX_PIN_IDX);
  56. gpio_hal_iomux_func_sel(GPIO_PIN_MUX_REG[uart_rx_gpio], PIN_FUNC_GPIO);
  57. PIN_INPUT_ENABLE(GPIO_PIN_MUX_REG[uart_rx_gpio]);
  58. esp_rom_gpio_pad_pullup_only(uart_rx_gpio);
  59. esp_rom_gpio_connect_out_signal(uart_tx_gpio, tx_idx, 0, 0);
  60. esp_rom_gpio_connect_in_signal(uart_rx_gpio, rx_idx, 0);
  61. gpio_hal_iomux_func_sel(GPIO_PIN_MUX_REG[uart_tx_gpio], PIN_FUNC_GPIO);
  62. // Enable the peripheral
  63. periph_ll_enable_clk_clear_rst(PERIPH_UART0_MODULE + uart_num);
  64. }
  65. #endif // CONFIG_ESP_CONSOLE_UART_CUSTOM
  66. // Set configured UART console baud rate
  67. uint32_t clock_hz = rtc_clk_apb_freq_get();
  68. #if ESP_ROM_UART_CLK_IS_XTAL
  69. clock_hz = (uint32_t)rtc_clk_xtal_freq_get() * MHZ; // From esp32-s3 on, UART clk source is selected to XTAL in ROM
  70. #endif
  71. int __DECLARE_RCC_ATOMIC_ENV __attribute__ ((unused)); // To avoid build errors/warnings about __DECLARE_RCC_ATOMIC_ENV
  72. esp_rom_uart_set_clock_baudrate(uart_num, clock_hz, CONFIG_ESP_CONSOLE_UART_BAUDRATE);
  73. }
  74. #endif // CONFIG_ESP_CONSOLE_UART
  75. #ifdef CONFIG_ESP_CONSOLE_USB_CDC
  76. /* Buffer for CDC data structures. No RX buffer allocated. */
  77. static char s_usb_cdc_buf[ESP_ROM_CDC_ACM_WORK_BUF_MIN];
  78. void bootloader_console_init(void)
  79. {
  80. #ifdef CONFIG_IDF_TARGET_ESP32S2
  81. /* ESP32-S2 specific patch to set the correct serial number in the descriptor.
  82. * Later chips don't need this.
  83. */
  84. rom_usb_cdc_set_descriptor_patch();
  85. #endif
  86. esp_rom_uart_usb_acm_init(s_usb_cdc_buf, sizeof(s_usb_cdc_buf));
  87. esp_rom_uart_set_as_console(ESP_ROM_USB_OTG_NUM);
  88. esp_rom_install_channel_putc(1, bootloader_console_write_char_usb);
  89. }
  90. #endif //CONFIG_ESP_CONSOLE_USB_CDC
  91. #ifdef CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG
  92. void bootloader_console_init(void)
  93. {
  94. esp_rom_uart_switch_buffer(ESP_ROM_USB_SERIAL_DEVICE_NUM);
  95. }
  96. #endif //CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG