ESPHome 2026.3.0-dev
Loading...
Searching...
No Matches
logger_esp32.cpp
Go to the documentation of this file.
1#ifdef USE_ESP32
2#include "logger.h"
3
4#include <esp_log.h>
5
6#include <driver/uart.h>
7
8#ifdef USE_LOGGER_USB_SERIAL_JTAG
9#include <driver/usb_serial_jtag.h>
10#if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 3, 0)
11#include <esp_vfs_dev.h>
12#include <esp_vfs_usb_serial_jtag.h>
13#else
14#include <driver/usb_serial_jtag_vfs.h>
15#endif
16#endif
17
18#include "esp_idf_version.h"
19#include "freertos/FreeRTOS.h"
20
21#include <fcntl.h>
22#include <cstdint>
23#include <cstdio>
24
25#include "esphome/core/log.h"
26
27namespace esphome::logger {
28
29static const char *const TAG = "logger";
30
31#ifdef USE_LOGGER_USB_SERIAL_JTAG
32static void init_usb_serial_jtag_() {
33 setvbuf(stdin, NULL, _IONBF, 0); // Disable buffering on stdin
34
35#if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 3, 0)
36 // Minicom, screen, idf_monitor send CR when ENTER key is pressed
37 esp_vfs_dev_usb_serial_jtag_set_rx_line_endings(ESP_LINE_ENDINGS_CR);
38 // Move the caret to the beginning of the next line on '\n'
39 esp_vfs_dev_usb_serial_jtag_set_tx_line_endings(ESP_LINE_ENDINGS_CRLF);
40#else
41 // Minicom, screen, idf_monitor send CR when ENTER key is pressed
42 usb_serial_jtag_vfs_set_rx_line_endings(ESP_LINE_ENDINGS_CR);
43 // Move the caret to the beginning of the next line on '\n'
44 usb_serial_jtag_vfs_set_tx_line_endings(ESP_LINE_ENDINGS_CRLF);
45#endif
46
47 // Enable non-blocking mode on stdin and stdout
48 fcntl(fileno(stdout), F_SETFL, 0);
49 fcntl(fileno(stdin), F_SETFL, 0);
50
51 usb_serial_jtag_driver_config_t usb_serial_jtag_config{};
52 usb_serial_jtag_config.rx_buffer_size = 512;
53 usb_serial_jtag_config.tx_buffer_size = 512;
54
55 esp_err_t ret = ESP_OK;
56 // Install USB-SERIAL-JTAG driver for interrupt-driven reads and writes
57 ret = usb_serial_jtag_driver_install(&usb_serial_jtag_config);
58 if (ret != ESP_OK) {
59 return;
60 }
61
62 // Tell vfs to use usb-serial-jtag driver
63#if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 3, 0)
64 esp_vfs_usb_serial_jtag_use_driver();
65#else
66 usb_serial_jtag_vfs_use_driver();
67#endif
68}
69#endif
70
71void init_uart(uart_port_t uart_num, uint32_t baud_rate, int tx_buffer_size) {
72 uart_config_t uart_config{};
73 uart_config.baud_rate = (int) baud_rate;
74 uart_config.data_bits = UART_DATA_8_BITS;
75 uart_config.parity = UART_PARITY_DISABLE;
76 uart_config.stop_bits = UART_STOP_BITS_1;
77 uart_config.flow_ctrl = UART_HW_FLOWCTRL_DISABLE;
78 uart_config.source_clk = UART_SCLK_DEFAULT;
79 uart_param_config(uart_num, &uart_config);
80 // The logger only writes to UART, never reads, so use the minimum RX buffer.
81 // ESP-IDF requires rx_buffer_size > UART_HW_FIFO_LEN (128 bytes).
82 const int min_rx_buffer_size = UART_HW_FIFO_LEN(uart_num) + 1;
83 uart_driver_install(uart_num, min_rx_buffer_size, tx_buffer_size, 0, nullptr, 0);
84}
85
87 if (this->baud_rate_ > 0) {
88 this->uart_num_ = UART_NUM_0;
89 switch (this->uart_) {
91 this->uart_num_ = UART_NUM_0;
92 init_uart(this->uart_num_, baud_rate_, ESPHOME_LOGGER_TX_BUFFER_SIZE);
93 break;
95 this->uart_num_ = UART_NUM_1;
96 init_uart(this->uart_num_, baud_rate_, ESPHOME_LOGGER_TX_BUFFER_SIZE);
97 break;
98#ifdef USE_ESP32_VARIANT_ESP32
100 this->uart_num_ = UART_NUM_2;
101 init_uart(this->uart_num_, baud_rate_, ESPHOME_LOGGER_TX_BUFFER_SIZE);
102 break;
103#endif
104#ifdef USE_LOGGER_USB_CDC
106 break;
107#endif
108#ifdef USE_LOGGER_USB_SERIAL_JTAG
110 init_usb_serial_jtag_();
111 break;
112#endif
113 }
114 }
115
116 global_logger = this;
117 esp_log_set_vprintf(esp_idf_log_vprintf_);
118
119 ESP_LOGI(TAG, "Log initialized");
120}
121
122void HOT Logger::write_msg_(const char *msg, uint16_t len) {
123#if defined(USE_LOGGER_UART_SELECTION_USB_CDC) || defined(USE_LOGGER_UART_SELECTION_USB_SERIAL_JTAG)
124 // USB CDC/JTAG - single write including newline (already in buffer)
125 // Use fwrite to stdout which goes through VFS to USB console
126 //
127 // Note: These defines indicate the user's YAML configuration choice (hardware_uart: USB_CDC/USB_SERIAL_JTAG).
128 // They are ONLY defined when the user explicitly selects USB as the logger output in their config.
129 // This is compile-time selection, not runtime detection - if USB is configured, it's always used.
130 // There is no fallback to regular UART if "USB isn't connected" - that's the user's responsibility
131 // to configure correctly for their hardware. This approach eliminates runtime overhead.
132 fwrite(msg, 1, len, stdout);
133#else
134 // Regular UART - single write including newline (already in buffer)
135 uart_write_bytes(this->uart_num_, msg, len);
136#endif
137}
138
139const LogString *Logger::get_uart_selection_() {
140 switch (this->uart_) {
142 return LOG_STR("UART0");
144 return LOG_STR("UART1");
145#ifdef USE_ESP32_VARIANT_ESP32
147 return LOG_STR("UART2");
148#endif
149#ifdef USE_LOGGER_USB_CDC
151 return LOG_STR("USB_CDC");
152#endif
153#ifdef USE_LOGGER_USB_SERIAL_JTAG
155 return LOG_STR("USB_SERIAL_JTAG");
156#endif
157 default:
158 return LOG_STR("UNKNOWN");
159 }
160}
161
162} // namespace esphome::logger
163#endif
UARTSelection uart_
Definition logger.h:358
void write_msg_(const char *msg, uint16_t len)
const LogString * get_uart_selection_()
void pre_setup()
Set up this component.
uart_port_t uart_num_
Definition logger.h:333
@ UART_SELECTION_UART2
Definition logger.h:113
@ UART_SELECTION_USB_SERIAL_JTAG
Definition logger.h:119
@ UART_SELECTION_USB_CDC
Definition logger.h:116
@ UART_SELECTION_UART0
Definition logger.h:107
@ UART_SELECTION_UART1
Definition logger.h:111
Logger * global_logger
Definition logger.cpp:278
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)
Definition log.cpp:50
std::string size_t len
Definition helpers.h:817