ESPHome 2026.2.1
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 const int uart_buffer_size = tx_buffer_size;
81 // Install UART driver using an event queue here
82 uart_driver_install(uart_num, uart_buffer_size, uart_buffer_size, 10, nullptr, 0);
83}
84
86 if (this->baud_rate_ > 0) {
87 this->uart_num_ = UART_NUM_0;
88 switch (this->uart_) {
90 this->uart_num_ = UART_NUM_0;
92 break;
94 this->uart_num_ = UART_NUM_1;
96 break;
97#ifdef USE_ESP32_VARIANT_ESP32
99 this->uart_num_ = UART_NUM_2;
101 break;
102#endif
103#ifdef USE_LOGGER_USB_CDC
105 break;
106#endif
107#ifdef USE_LOGGER_USB_SERIAL_JTAG
109 init_usb_serial_jtag_();
110 break;
111#endif
112 }
113 }
114
115 global_logger = this;
116 esp_log_set_vprintf(esp_idf_log_vprintf_);
117
118 ESP_LOGI(TAG, "Log initialized");
119}
120
121void HOT Logger::write_msg_(const char *msg, uint16_t len) {
122#if defined(USE_LOGGER_UART_SELECTION_USB_CDC) || defined(USE_LOGGER_UART_SELECTION_USB_SERIAL_JTAG)
123 // USB CDC/JTAG - single write including newline (already in buffer)
124 // Use fwrite to stdout which goes through VFS to USB console
125 //
126 // Note: These defines indicate the user's YAML configuration choice (hardware_uart: USB_CDC/USB_SERIAL_JTAG).
127 // They are ONLY defined when the user explicitly selects USB as the logger output in their config.
128 // This is compile-time selection, not runtime detection - if USB is configured, it's always used.
129 // There is no fallback to regular UART if "USB isn't connected" - that's the user's responsibility
130 // to configure correctly for their hardware. This approach eliminates runtime overhead.
131 fwrite(msg, 1, len, stdout);
132#else
133 // Regular UART - single write including newline (already in buffer)
134 uart_write_bytes(this->uart_num_, msg, len);
135#endif
136}
137
138const LogString *Logger::get_uart_selection_() {
139 switch (this->uart_) {
141 return LOG_STR("UART0");
143 return LOG_STR("UART1");
144#ifdef USE_ESP32_VARIANT_ESP32
146 return LOG_STR("UART2");
147#endif
148#ifdef USE_LOGGER_USB_CDC
150 return LOG_STR("USB_CDC");
151#endif
152#ifdef USE_LOGGER_USB_SERIAL_JTAG
154 return LOG_STR("USB_SERIAL_JTAG");
155#endif
156 default:
157 return LOG_STR("UNKNOWN");
158 }
159}
160
161} // namespace esphome::logger
162#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:332
uint16_t tx_buffer_size_
Definition logger.h:355
@ UART_SELECTION_UART2
Definition logger.h:114
@ UART_SELECTION_USB_SERIAL_JTAG
Definition logger.h:120
@ UART_SELECTION_USB_CDC
Definition logger.h:117
@ UART_SELECTION_UART0
Definition logger.h:108
@ UART_SELECTION_UART1
Definition logger.h:112
Logger * global_logger
Definition logger.cpp:279
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:692