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