bootloader_console_loader.c 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*
  2. * SPDX-FileCopyrightText: 2020-2021 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. /**
  7. * This file contains console-related functions which should be located in iram_loader_seg,
  8. * to be available in the "loader" phase, when iram_seg may be overwritten.
  9. */
  10. #include <stdint.h>
  11. #include <stddef.h>
  12. #include "sdkconfig.h"
  13. #include "bootloader_console.h"
  14. #include "esp_rom_uart.h"
  15. #include "esp_rom_sys.h"
  16. #if CONFIG_IDF_TARGET_ESP32S2
  17. #include "esp32s2/rom/usb/chip_usb_dw_wrapper.h"
  18. #include "esp32s2/rom/usb/usb_dc.h"
  19. #include "esp32s2/rom/usb/cdc_acm.h"
  20. #include "esp32s2/rom/usb/usb_persist.h"
  21. #elif CONFIG_IDF_TARGET_ESP32S3
  22. #include "esp32s3/rom/usb/chip_usb_dw_wrapper.h"
  23. #include "esp32s3/rom/usb/usb_dc.h"
  24. #include "esp32s3/rom/usb/cdc_acm.h"
  25. #include "esp32s3/rom/usb/usb_persist.h"
  26. #endif
  27. #ifdef CONFIG_ESP_CONSOLE_USB_CDC
  28. /* The following functions replace esp_rom_uart_putc, esp_rom_uart_tx_one_char,
  29. * and uart_tx_one_char_uart ROM functions. The main difference is that
  30. * uart_tx_one_char_uart calls cdc_acm_fifo_fill for each byte passed to it,
  31. * which results in very slow console output. The version here uses a TX buffer.
  32. * It also doesn't handle UART output, only works with USB.
  33. */
  34. static char cdc_txbuf[ACM_BYTES_PER_TX];
  35. static size_t cdc_txpos;
  36. static void bootloader_console_flush_usb(void)
  37. {
  38. cdc_acm_fifo_fill(uart_acm_dev, (const uint8_t *) cdc_txbuf, cdc_txpos);
  39. /* return value ignored — if bootloader fails to log something, proceed anyway */
  40. cdc_txpos = 0;
  41. }
  42. static void bootloader_console_write_one_char_usb(char ch)
  43. {
  44. cdc_txbuf[cdc_txpos++] = ch;
  45. if (ch == '\n' || cdc_txpos == sizeof(cdc_txbuf)) {
  46. bootloader_console_flush_usb();
  47. }
  48. }
  49. void bootloader_console_write_char_usb(char c)
  50. {
  51. if (c == '\n') {
  52. bootloader_console_write_one_char_usb('\r');
  53. bootloader_console_write_one_char_usb('\n');
  54. } else if (c == '\r') {
  55. } else {
  56. bootloader_console_write_one_char_usb(c);
  57. }
  58. }
  59. #endif //CONFIG_ESP_CONSOLE_USB_CDC
  60. void bootloader_console_deinit(void)
  61. {
  62. #ifdef CONFIG_ESP_CONSOLE_UART
  63. /* Ensure any buffered log output is displayed */
  64. esp_rom_uart_flush_tx(CONFIG_ESP_CONSOLE_UART_NUM);
  65. #endif // CONFIG_ESP_CONSOLE_UART
  66. #ifdef CONFIG_ESP_CONSOLE_USB_CDC
  67. bootloader_console_flush_usb();
  68. usb_dc_prepare_persist();
  69. chip_usb_set_persist_flags(USBDC_PERSIST_ENA);
  70. esp_rom_delay_us(100);
  71. for (int i = 0; i < 10; i++) {
  72. usb_dc_check_poll_for_interrupts();
  73. }
  74. esp_rom_install_channel_putc(1, NULL);
  75. #endif
  76. }