bootloader_console.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. #if CONFIG_IDF_TARGET_ESP32S2
  24. #include "esp32s2/rom/usb/cdc_acm.h"
  25. #include "esp32s2/rom/usb/usb_common.h"
  26. #endif
  27. #include "esp_rom_gpio.h"
  28. #include "esp_rom_uart.h"
  29. #include "esp_rom_sys.h"
  30. #ifdef CONFIG_ESP_CONSOLE_UART_NONE
  31. void bootloader_console_init(void)
  32. {
  33. esp_rom_install_channel_putc(1, NULL);
  34. esp_rom_install_channel_putc(2, NULL);
  35. }
  36. #endif // CONFIG_ESP_CONSOLE_UART_NONE
  37. #ifdef CONFIG_ESP_CONSOLE_UART
  38. void bootloader_console_init(void)
  39. {
  40. const int uart_num = CONFIG_ESP_CONSOLE_UART_NUM;
  41. esp_rom_install_channel_putc(1, esp_rom_uart_putc);
  42. // Wait for UART FIFO to be empty.
  43. esp_rom_uart_tx_wait_idle(0);
  44. #if CONFIG_ESP_CONSOLE_UART_CUSTOM
  45. // Some constants to make the following code less upper-case
  46. const int uart_tx_gpio = CONFIG_ESP_CONSOLE_UART_TX_GPIO;
  47. const int uart_rx_gpio = CONFIG_ESP_CONSOLE_UART_RX_GPIO;
  48. // Switch to the new UART (this just changes UART number used for esp_rom_printf in ROM code).
  49. esp_rom_uart_set_as_console(uart_num);
  50. // If console is attached to UART1 or if non-default pins are used,
  51. // need to reconfigure pins using GPIO matrix
  52. if (uart_num != 0 ||
  53. uart_tx_gpio != UART_NUM_0_TXD_DIRECT_GPIO_NUM ||
  54. uart_rx_gpio != UART_NUM_0_RXD_DIRECT_GPIO_NUM) {
  55. // Change default UART pins back to GPIOs
  56. PIN_FUNC_SELECT(PERIPHS_IO_MUX_U0RXD_U, PIN_FUNC_GPIO);
  57. PIN_FUNC_SELECT(PERIPHS_IO_MUX_U0TXD_U, PIN_FUNC_GPIO);
  58. // Route GPIO signals to/from pins
  59. const uint32_t tx_idx = uart_periph_signal[uart_num].tx_sig;
  60. const uint32_t rx_idx = uart_periph_signal[uart_num].rx_sig;
  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. // Enable the peripheral
  66. periph_ll_enable_clk_clear_rst(PERIPH_UART0_MODULE + uart_num);
  67. }
  68. #endif // CONFIG_ESP_CONSOLE_UART_CUSTOM
  69. // Set configured UART console baud rate
  70. uint32_t clock_hz = rtc_clk_apb_freq_get();
  71. #if CONFIG_IDF_TARGET_ESP32S3
  72. clock_hz = UART_CLK_FREQ_ROM; // From esp32-s3 on, UART clock source is selected to XTAL in ROM
  73. #endif
  74. esp_rom_uart_set_clock_baudrate(uart_num, clock_hz, CONFIG_ESP_CONSOLE_UART_BAUDRATE);
  75. }
  76. #endif // CONFIG_ESP_CONSOLE_UART
  77. #ifdef CONFIG_ESP_CONSOLE_USB_CDC
  78. /* Buffer for CDC data structures. No RX buffer allocated. */
  79. static char s_usb_cdc_buf[ESP_ROM_CDC_ACM_WORK_BUF_MIN];
  80. void bootloader_console_init(void)
  81. {
  82. #ifdef CONFIG_IDF_TARGET_ESP32S2
  83. /* ESP32-S2 specific patch to set the correct serial number in the descriptor.
  84. * Later chips don't need this.
  85. */
  86. rom_usb_cdc_set_descriptor_patch();
  87. #endif
  88. esp_rom_uart_usb_acm_init(s_usb_cdc_buf, sizeof(s_usb_cdc_buf));
  89. esp_rom_uart_set_as_console(ESP_ROM_UART_USB);
  90. esp_rom_install_channel_putc(1, bootloader_console_write_char_usb);
  91. }
  92. #endif //CONFIG_ESP_CONSOLE_USB_CDC