1 /*
2  * SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/sys/util.h>
8 #include "stubs.h"
9 #include "console_init.h"
10 #include "soc/uart_periph.h"
11 #include "soc/uart_channel.h"
12 #include "soc/io_mux_reg.h"
13 #include "soc/gpio_periph.h"
14 #include "soc/gpio_sig_map.h"
15 #include "soc/rtc.h"
16 #include "hal/clk_gate_ll.h"
17 #include "hal/gpio_hal.h"
18 #if CONFIG_SOC_SERIES_ESP32S2
19 #include "esp32s2/rom/usb/cdc_acm.h"
20 #include "esp32s2/rom/usb/usb_common.h"
21 #include "esp32s2/rom/usb/usb_persist.h"
22 #endif
23 #include "esp_rom_gpio.h"
24 #include "esp_rom_uart.h"
25 #include "esp_rom_sys.h"
26 #include "esp_rom_caps.h"
27 
esp_console_deinit(void)28 void esp_console_deinit(void)
29 {
30 #ifdef CONFIG_ESP_CONSOLE_UART
31 	/* Ensure any buffered log output is displayed */
32 	esp_rom_uart_flush_tx(CONFIG_ESP_CONSOLE_UART_NUM);
33 #endif /* CONFIG_ESP_CONSOLE_UART */
34 }
35 
36 #ifdef CONFIG_ESP_CONSOLE_UART
esp_console_init(void)37 void esp_console_init(void)
38 {
39 	const int uart_num = CONFIG_ESP_CONSOLE_UART_NUM;
40 
41 	esp_rom_install_uart_printf();
42 
43 	esp_rom_uart_tx_wait_idle(0);
44 
45 	/* Set configured UART console baud rate */
46 	uint32_t clock_hz = rtc_clk_apb_freq_get();
47 #if ESP_ROM_UART_CLK_IS_XTAL
48 	/* From esp32-s3 on, UART clk source is selected to XTAL in ROM */
49 	clock_hz = (uint32_t)rtc_clk_xtal_freq_get() * MHZ(1);
50 #endif
51 	esp_rom_uart_set_clock_baudrate(uart_num, clock_hz, CONFIG_ESP_CONSOLE_UART_BAUDRATE);
52 }
53 #endif /* CONFIG_ESP_CONSOLE_UART */
54 
55 #ifdef CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG
esp_console_init(void)56 void esp_console_init(void)
57 {
58 	esp_rom_uart_switch_buffer(ESP_ROM_USB_SERIAL_DEVICE_NUM);
59 }
60 #endif /* CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG */
61