ESPHome 2025.10.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) {
125 if (
126#if defined(USE_LOGGER_USB_CDC) && !defined(USE_LOGGER_USB_SERIAL_JTAG)
128#elif defined(USE_LOGGER_USB_SERIAL_JTAG) && !defined(USE_LOGGER_USB_CDC)
130#elif defined(USE_LOGGER_USB_CDC) && defined(USE_LOGGER_USB_SERIAL_JTAG)
132#else
133 /* DISABLES CODE */ (false) // NOLINT
134#endif
135 ) {
136 puts(msg);
137 } else {
138 // Use tx_buffer_at_ if msg points to tx_buffer_, otherwise fall back to strlen
139 size_t len = (msg == this->tx_buffer_) ? this->tx_buffer_at_ : strlen(msg);
140 uart_write_bytes(this->uart_num_, msg, len);
141 uart_write_bytes(this->uart_num_, "\n", 1);
142 }
143}
144
145const LogString *Logger::get_uart_selection_() {
146 switch (this->uart_) {
148 return LOG_STR("UART0");
150 return LOG_STR("UART1");
151#ifdef USE_ESP32_VARIANT_ESP32
153 return LOG_STR("UART2");
154#endif
155#ifdef USE_LOGGER_USB_CDC
157 return LOG_STR("USB_CDC");
158#endif
159#ifdef USE_LOGGER_USB_SERIAL_JTAG
161 return LOG_STR("USB_SERIAL_JTAG");
162#endif
163 default:
164 return LOG_STR("UNKNOWN");
165 }
166}
167
168} // namespace esphome::logger
169#endif
UARTSelection uart_
Definition logger.h:263
const LogString * get_uart_selection_()
void pre_setup()
Set up this component.
uart_port_t uart_num_
Definition logger.h:247
void write_msg_(const char *msg)
uint16_t tx_buffer_size_
Definition logger.h:260
@ UART_SELECTION_UART2
Definition logger.h:76
@ UART_SELECTION_USB_SERIAL_JTAG
Definition logger.h:82
@ UART_SELECTION_USB_CDC
Definition logger.h:79
@ UART_SELECTION_UART0
Definition logger.h:70
@ UART_SELECTION_UART1
Definition logger.h:74
Logger * global_logger
Definition logger.cpp:288
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:291