bootloader_console.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // Copyright 2020 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include "sdkconfig.h"
  15. #include "bootloader_console.h"
  16. #include "soc/uart_periph.h"
  17. #include "soc/uart_channel.h"
  18. #include "soc/io_mux_reg.h"
  19. #include "soc/gpio_periph.h"
  20. #include "soc/gpio_sig_map.h"
  21. #include "soc/rtc.h"
  22. #include "hal/clk_gate_ll.h"
  23. #include "hal/gpio_hal.h"
  24. #if CONFIG_IDF_TARGET_ESP32S2
  25. #include "esp32s2/rom/usb/cdc_acm.h"
  26. #include "esp32s2/rom/usb/usb_common.h"
  27. #elif CONFIG_IDF_TARGET_ESP32C3
  28. #include "esp32c3/rom/ets_sys.h"
  29. #endif
  30. #include "esp_rom_gpio.h"
  31. #include "esp_rom_uart.h"
  32. #include "esp_rom_sys.h"
  33. #include "esp_rom_caps.h"
  34. #ifdef CONFIG_ESP_CONSOLE_UART_NONE
  35. void bootloader_console_init(void)
  36. {
  37. esp_rom_install_channel_putc(1, NULL);
  38. esp_rom_install_channel_putc(2, NULL);
  39. }
  40. #endif // CONFIG_ESP_CONSOLE_UART_NONE
  41. #ifdef CONFIG_ESP_CONSOLE_UART
  42. void bootloader_console_init(void)
  43. {
  44. const int uart_num = CONFIG_ESP_CONSOLE_UART_NUM;
  45. #if !ESP_ROM_SUPPORT_MULTIPLE_UART
  46. /* esp_rom_install_channel_put is not available unless multiple UARTs are supported */
  47. esp_rom_install_uart_printf();
  48. #else
  49. esp_rom_install_channel_putc(1, esp_rom_uart_putc);
  50. #endif
  51. // Wait for UART FIFO to be empty.
  52. esp_rom_uart_tx_wait_idle(0);
  53. #if CONFIG_ESP_CONSOLE_UART_CUSTOM
  54. // Some constants to make the following code less upper-case
  55. const int uart_tx_gpio = CONFIG_ESP_CONSOLE_UART_TX_GPIO;
  56. const int uart_rx_gpio = CONFIG_ESP_CONSOLE_UART_RX_GPIO;
  57. // Switch to the new UART (this just changes UART number used for esp_rom_printf in ROM code).
  58. #if ESP_ROM_SUPPORT_MULTIPLE_UART
  59. esp_rom_uart_set_as_console(uart_num);
  60. #endif
  61. // If console is attached to UART1 or if non-default pins are used,
  62. // need to reconfigure pins using GPIO matrix
  63. if (uart_num != 0 ||
  64. uart_tx_gpio != UART_NUM_0_TXD_DIRECT_GPIO_NUM ||
  65. uart_rx_gpio != UART_NUM_0_RXD_DIRECT_GPIO_NUM) {
  66. // Change default UART pins back to GPIOs
  67. gpio_hal_iomux_func_sel(PERIPHS_IO_MUX_U0RXD_U, PIN_FUNC_GPIO);
  68. gpio_hal_iomux_func_sel(PERIPHS_IO_MUX_U0TXD_U, PIN_FUNC_GPIO);
  69. // Route GPIO signals to/from pins
  70. const uint32_t tx_idx = uart_periph_signal[uart_num].tx_sig;
  71. const uint32_t rx_idx = uart_periph_signal[uart_num].rx_sig;
  72. PIN_INPUT_ENABLE(GPIO_PIN_MUX_REG[uart_rx_gpio]);
  73. esp_rom_gpio_pad_pullup_only(uart_rx_gpio);
  74. esp_rom_gpio_connect_out_signal(uart_tx_gpio, tx_idx, 0, 0);
  75. esp_rom_gpio_connect_in_signal(uart_rx_gpio, rx_idx, 0);
  76. // Enable the peripheral
  77. periph_ll_enable_clk_clear_rst(PERIPH_UART0_MODULE + uart_num);
  78. }
  79. #endif // CONFIG_ESP_CONSOLE_UART_CUSTOM
  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 = UART_CLK_FREQ_ROM; // From esp32-s3 on, UART clock 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. #ifdef CONFIG_ESP_CONSOLE_USB_CDC
  89. /* Buffer for CDC data structures. No RX buffer allocated. */
  90. static char s_usb_cdc_buf[ESP_ROM_CDC_ACM_WORK_BUF_MIN];
  91. void bootloader_console_init(void)
  92. {
  93. #ifdef CONFIG_IDF_TARGET_ESP32S2
  94. /* ESP32-S2 specific patch to set the correct serial number in the descriptor.
  95. * Later chips don't need this.
  96. */
  97. rom_usb_cdc_set_descriptor_patch();
  98. #endif
  99. esp_rom_uart_usb_acm_init(s_usb_cdc_buf, sizeof(s_usb_cdc_buf));
  100. esp_rom_uart_set_as_console(ESP_ROM_UART_USB);
  101. esp_rom_install_channel_putc(1, bootloader_console_write_char_usb);
  102. }
  103. #endif //CONFIG_ESP_CONSOLE_USB_CDC