ESPHome 2026.1.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 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 if (ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE) {
118 esp_log_level_set("*", ESP_LOG_VERBOSE);
119 }
120
121 ESP_LOGI(TAG, "Log initialized");
122}
123
124void HOT Logger::write_msg_(const char *msg, size_t len) {
125 // Length is now always passed explicitly - no strlen() fallback needed
126
127#if defined(USE_LOGGER_UART_SELECTION_USB_CDC) || defined(USE_LOGGER_UART_SELECTION_USB_SERIAL_JTAG)
128 // USB CDC/JTAG - single write including newline (already in buffer)
129 // Use fwrite to stdout which goes through VFS to USB console
130 //
131 // Note: These defines indicate the user's YAML configuration choice (hardware_uart: USB_CDC/USB_SERIAL_JTAG).
132 // They are ONLY defined when the user explicitly selects USB as the logger output in their config.
133 // This is compile-time selection, not runtime detection - if USB is configured, it's always used.
134 // There is no fallback to regular UART if "USB isn't connected" - that's the user's responsibility
135 // to configure correctly for their hardware. This approach eliminates runtime overhead.
136 fwrite(msg, 1, len, stdout);
137#else
138 // Regular UART - single write including newline (already in buffer)
139 uart_write_bytes(this->uart_num_, msg, len);
140#endif
141}
142
143const LogString *Logger::get_uart_selection_() {
144 switch (this->uart_) {
146 return LOG_STR("UART0");
148 return LOG_STR("UART1");
149#ifdef USE_ESP32_VARIANT_ESP32
151 return LOG_STR("UART2");
152#endif
153#ifdef USE_LOGGER_USB_CDC
155 return LOG_STR("USB_CDC");
156#endif
157#ifdef USE_LOGGER_USB_SERIAL_JTAG
159 return LOG_STR("USB_SERIAL_JTAG");
160#endif
161 default:
162 return LOG_STR("UNKNOWN");
163 }
164}
165
166} // namespace esphome::logger
167#endif
UARTSelection uart_
Definition logger.h:353
const LogString * get_uart_selection_()
void pre_setup()
Set up this component.
void write_msg_(const char *msg, size_t len)
uart_port_t uart_num_
Definition logger.h:333
uint16_t tx_buffer_size_
Definition logger.h:350
@ UART_SELECTION_UART2
Definition logger.h:134
@ UART_SELECTION_USB_SERIAL_JTAG
Definition logger.h:140
@ UART_SELECTION_USB_CDC
Definition logger.h:137
@ UART_SELECTION_UART0
Definition logger.h:128
@ UART_SELECTION_UART1
Definition logger.h:132
Logger * global_logger
Definition logger.cpp:297
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:533