4#if defined(USE_ESP32_FRAMEWORK_ARDUINO) || defined(USE_ESP_IDF)
9#include <driver/uart.h>
11#ifdef USE_LOGGER_USB_SERIAL_JTAG
12#include <driver/usb_serial_jtag.h>
13#if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 3, 0)
14#include <esp_vfs_dev.h>
15#include <esp_vfs_usb_serial_jtag.h>
17#include <driver/usb_serial_jtag_vfs.h>
21#include "esp_idf_version.h"
22#include "freertos/FreeRTOS.h"
34static const char *
const TAG =
"logger";
38#ifdef USE_LOGGER_USB_SERIAL_JTAG
39static void init_usb_serial_jtag_() {
40 setvbuf(stdin, NULL, _IONBF, 0);
42#if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 3, 0)
44 esp_vfs_dev_usb_serial_jtag_set_rx_line_endings(ESP_LINE_ENDINGS_CR);
46 esp_vfs_dev_usb_serial_jtag_set_tx_line_endings(ESP_LINE_ENDINGS_CRLF);
49 usb_serial_jtag_vfs_set_rx_line_endings(ESP_LINE_ENDINGS_CR);
51 usb_serial_jtag_vfs_set_tx_line_endings(ESP_LINE_ENDINGS_CRLF);
55 fcntl(fileno(stdout), F_SETFL, 0);
56 fcntl(fileno(stdin), F_SETFL, 0);
58 usb_serial_jtag_driver_config_t usb_serial_jtag_config{};
59 usb_serial_jtag_config.rx_buffer_size = 512;
60 usb_serial_jtag_config.tx_buffer_size = 512;
62 esp_err_t ret = ESP_OK;
64 ret = usb_serial_jtag_driver_install(&usb_serial_jtag_config);
70#if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 3, 0)
71 esp_vfs_usb_serial_jtag_use_driver();
73 usb_serial_jtag_vfs_use_driver();
78void init_uart(uart_port_t uart_num, uint32_t baud_rate,
int tx_buffer_size) {
79 uart_config_t uart_config{};
80 uart_config.baud_rate = (int) baud_rate;
81 uart_config.data_bits = UART_DATA_8_BITS;
82 uart_config.parity = UART_PARITY_DISABLE;
83 uart_config.stop_bits = UART_STOP_BITS_1;
84 uart_config.flow_ctrl = UART_HW_FLOWCTRL_DISABLE;
85 uart_config.source_clk = UART_SCLK_DEFAULT;
86 uart_param_config(uart_num, &uart_config);
87 const int uart_buffer_size = tx_buffer_size;
89 uart_driver_install(uart_num, uart_buffer_size, uart_buffer_size, 10,
nullptr, 0);
97 switch (this->
uart_) {
99#if ARDUINO_USB_CDC_ON_BOOT
111#ifdef USE_ESP32_VARIANT_ESP32
118#ifdef USE_LOGGER_USB_CDC
129 switch (this->
uart_) {
138#ifdef USE_ESP32_VARIANT_ESP32
144#ifdef USE_LOGGER_USB_CDC
148#ifdef USE_LOGGER_USB_SERIAL_JTAG
150 init_usb_serial_jtag_();
158#if defined(USE_ESP_IDF) || defined(USE_ESP32_FRAMEWORK_ARDUINO)
160 if (ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE) {
161 esp_log_level_set(
"*", ESP_LOG_VERBOSE);
165 ESP_LOGI(TAG,
"Log initialized");
171#
if defined(USE_LOGGER_USB_CDC) && !defined(USE_LOGGER_USB_SERIAL_JTAG)
173#elif defined(USE_LOGGER_USB_SERIAL_JTAG) && !defined(USE_LOGGER_USB_CDC)
175#elif defined(USE_LOGGER_USB_CDC) && defined(USE_LOGGER_USB_SERIAL_JTAG)
186 uart_write_bytes(this->
uart_num_,
"\n", 1);
195#ifdef USE_ESP32_VARIANT_ESP32
198#ifdef USE_LOGGER_USB_CDC
201#ifdef USE_LOGGER_USB_SERIAL_JTAG
const char * get_uart_selection_()
void pre_setup()
Set up this component.
void write_msg_(const char *msg)
const char *const UART_SELECTIONS[]
@ UART_SELECTION_USB_SERIAL_JTAG
void init_uart(uart_port_t uart_num, uint32_t baud_rate, int tx_buffer_size)
int HOT esp_idf_log_vprintf_(const char *format, va_list args)