bootloader_console_loader.c 2.8 KB

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