ESPHome 2025.9.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#if defined(USE_ESP32_FRAMEWORK_ARDUINO) || defined(USE_ESP_IDF)
5#include <esp_log.h>
6#endif // USE_ESP32_FRAMEWORK_ARDUINO || USE_ESP_IDF
7
8#ifdef USE_ESP_IDF
9#include <driver/uart.h>
10
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>
16#else
17#include <driver/usb_serial_jtag_vfs.h>
18#endif
19#endif
20
21#include "esp_idf_version.h"
22#include "freertos/FreeRTOS.h"
23
24#include <fcntl.h>
25#include <cstdint>
26#include <cstdio>
27
28#endif // USE_ESP_IDF
29
30#include "esphome/core/log.h"
31
32namespace esphome::logger {
33
34static const char *const TAG = "logger";
35
36#ifdef USE_ESP_IDF
37
38#ifdef USE_LOGGER_USB_SERIAL_JTAG
39static void init_usb_serial_jtag_() {
40 setvbuf(stdin, NULL, _IONBF, 0); // Disable buffering on stdin
41
42#if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 3, 0)
43 // Minicom, screen, idf_monitor send CR when ENTER key is pressed
44 esp_vfs_dev_usb_serial_jtag_set_rx_line_endings(ESP_LINE_ENDINGS_CR);
45 // Move the caret to the beginning of the next line on '\n'
46 esp_vfs_dev_usb_serial_jtag_set_tx_line_endings(ESP_LINE_ENDINGS_CRLF);
47#else
48 // Minicom, screen, idf_monitor send CR when ENTER key is pressed
49 usb_serial_jtag_vfs_set_rx_line_endings(ESP_LINE_ENDINGS_CR);
50 // Move the caret to the beginning of the next line on '\n'
51 usb_serial_jtag_vfs_set_tx_line_endings(ESP_LINE_ENDINGS_CRLF);
52#endif
53
54 // Enable non-blocking mode on stdin and stdout
55 fcntl(fileno(stdout), F_SETFL, 0);
56 fcntl(fileno(stdin), F_SETFL, 0);
57
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;
61
62 esp_err_t ret = ESP_OK;
63 // Install USB-SERIAL-JTAG driver for interrupt-driven reads and writes
64 ret = usb_serial_jtag_driver_install(&usb_serial_jtag_config);
65 if (ret != ESP_OK) {
66 return;
67 }
68
69 // Tell vfs to use usb-serial-jtag driver
70#if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 3, 0)
71 esp_vfs_usb_serial_jtag_use_driver();
72#else
73 usb_serial_jtag_vfs_use_driver();
74#endif
75}
76#endif
77
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;
88 // Install UART driver using an event queue here
89 uart_driver_install(uart_num, uart_buffer_size, uart_buffer_size, 10, nullptr, 0);
90}
91
92#endif // USE_ESP_IDF
93
95 if (this->baud_rate_ > 0) {
96#ifdef USE_ARDUINO
97 switch (this->uart_) {
99#if ARDUINO_USB_CDC_ON_BOOT
100 this->hw_serial_ = &Serial0;
101 Serial0.begin(this->baud_rate_);
102#else
103 this->hw_serial_ = &Serial;
104 Serial.begin(this->baud_rate_);
105#endif
106 break;
108 this->hw_serial_ = &Serial1;
109 Serial1.begin(this->baud_rate_);
110 break;
111#ifdef USE_ESP32_VARIANT_ESP32
113 this->hw_serial_ = &Serial2;
114 Serial2.begin(this->baud_rate_);
115 break;
116#endif
117
118#ifdef USE_LOGGER_USB_CDC
120 this->hw_serial_ = &Serial;
121 Serial.begin(this->baud_rate_);
122 break;
123#endif
124 }
125#endif // USE_ARDUINO
126
127#ifdef USE_ESP_IDF
128 this->uart_num_ = UART_NUM_0;
129 switch (this->uart_) {
131 this->uart_num_ = UART_NUM_0;
133 break;
135 this->uart_num_ = UART_NUM_1;
137 break;
138#ifdef USE_ESP32_VARIANT_ESP32
140 this->uart_num_ = UART_NUM_2;
142 break;
143#endif
144#ifdef USE_LOGGER_USB_CDC
146 break;
147#endif
148#ifdef USE_LOGGER_USB_SERIAL_JTAG
150 init_usb_serial_jtag_();
151 break;
152#endif
153 }
154#endif // USE_ESP_IDF
155 }
156
157 global_logger = this;
158#if defined(USE_ESP_IDF) || defined(USE_ESP32_FRAMEWORK_ARDUINO)
159 esp_log_set_vprintf(esp_idf_log_vprintf_);
160 if (ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE) {
161 esp_log_level_set("*", ESP_LOG_VERBOSE);
162 }
163#endif // USE_ESP_IDF || USE_ESP32_FRAMEWORK_ARDUINO
164
165 ESP_LOGI(TAG, "Log initialized");
166}
167
168#ifdef USE_ESP_IDF
169void HOT Logger::write_msg_(const char *msg) {
170 if (
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)
177#else
178 /* DISABLES CODE */ (false) // NOLINT
179#endif
180 ) {
181 puts(msg);
182 } else {
183 // Use tx_buffer_at_ if msg points to tx_buffer_, otherwise fall back to strlen
184 size_t len = (msg == this->tx_buffer_) ? this->tx_buffer_at_ : strlen(msg);
185 uart_write_bytes(this->uart_num_, msg, len);
186 uart_write_bytes(this->uart_num_, "\n", 1);
187 }
188}
189#else
190void HOT Logger::write_msg_(const char *msg) { this->hw_serial_->println(msg); }
191#endif
192
193const char *const UART_SELECTIONS[] = {
194 "UART0", "UART1",
195#ifdef USE_ESP32_VARIANT_ESP32
196 "UART2",
197#endif
198#ifdef USE_LOGGER_USB_CDC
199 "USB_CDC",
200#endif
201#ifdef USE_LOGGER_USB_SERIAL_JTAG
202 "USB_SERIAL_JTAG",
203#endif
204};
205
206const char *Logger::get_uart_selection_() { return UART_SELECTIONS[this->uart_]; }
207
208} // namespace esphome::logger
209#endif
UARTSelection uart_
Definition logger.h:267
const char * get_uart_selection_()
void pre_setup()
Set up this component.
uart_port_t uart_num_
Definition logger.h:251
void write_msg_(const char *msg)
uint16_t tx_buffer_size_
Definition logger.h:264
const char *const UART_SELECTIONS[]
@ 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:283
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:279