ESPHome  2024.4.1
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 #include <esp_vfs_dev.h>
14 #include <esp_vfs_usb_serial_jtag.h>
15 #endif
16 
17 #include "freertos/FreeRTOS.h"
18 #include "esp_idf_version.h"
19 
20 #include <cstdint>
21 #include <cstdio>
22 #include <fcntl.h>
23 
24 #endif // USE_ESP_IDF
25 
26 #include "esphome/core/log.h"
27 
28 namespace esphome {
29 namespace logger {
30 
31 static const char *const TAG = "logger";
32 
33 #ifdef USE_ESP_IDF
34 
35 #ifdef USE_LOGGER_USB_SERIAL_JTAG
36 static void init_usb_serial_jtag_() {
37  setvbuf(stdin, NULL, _IONBF, 0); // Disable buffering on stdin
38 
39  // Minicom, screen, idf_monitor send CR when ENTER key is pressed
40  esp_vfs_dev_usb_serial_jtag_set_rx_line_endings(ESP_LINE_ENDINGS_CR);
41  // Move the caret to the beginning of the next line on '\n'
42  esp_vfs_dev_usb_serial_jtag_set_tx_line_endings(ESP_LINE_ENDINGS_CRLF);
43 
44  // Enable non-blocking mode on stdin and stdout
45  fcntl(fileno(stdout), F_SETFL, 0);
46  fcntl(fileno(stdin), F_SETFL, 0);
47 
48  usb_serial_jtag_driver_config_t usb_serial_jtag_config{};
49  usb_serial_jtag_config.rx_buffer_size = 512;
50  usb_serial_jtag_config.tx_buffer_size = 512;
51 
52  esp_err_t ret = ESP_OK;
53  // Install USB-SERIAL-JTAG driver for interrupt-driven reads and writes
54  ret = usb_serial_jtag_driver_install(&usb_serial_jtag_config);
55  if (ret != ESP_OK) {
56  return;
57  }
58 
59  // Tell vfs to use usb-serial-jtag driver
60  esp_vfs_usb_serial_jtag_use_driver();
61 }
62 #endif
63 
64 void init_uart(uart_port_t uart_num, uint32_t baud_rate, int tx_buffer_size) {
65  uart_config_t uart_config{};
66  uart_config.baud_rate = (int) baud_rate;
67  uart_config.data_bits = UART_DATA_8_BITS;
68  uart_config.parity = UART_PARITY_DISABLE;
69  uart_config.stop_bits = UART_STOP_BITS_1;
70  uart_config.flow_ctrl = UART_HW_FLOWCTRL_DISABLE;
71 #if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0)
72  uart_config.source_clk = UART_SCLK_DEFAULT;
73 #endif
74  uart_param_config(uart_num, &uart_config);
75  const int uart_buffer_size = tx_buffer_size;
76  // Install UART driver using an event queue here
77  uart_driver_install(uart_num, uart_buffer_size, uart_buffer_size, 10, nullptr, 0);
78 }
79 
80 #endif // USE_ESP_IDF
81 
83  if (this->baud_rate_ > 0) {
84 #ifdef USE_ARDUINO
85  switch (this->uart_) {
87 #if ARDUINO_USB_CDC_ON_BOOT
88  this->hw_serial_ = &Serial0;
89  Serial0.begin(this->baud_rate_);
90 #else
91  this->hw_serial_ = &Serial;
92  Serial.begin(this->baud_rate_);
93 #endif
94  break;
96  this->hw_serial_ = &Serial1;
97  Serial1.begin(this->baud_rate_);
98  break;
99 #ifdef USE_ESP32_VARIANT_ESP32
101  this->hw_serial_ = &Serial2;
102  Serial2.begin(this->baud_rate_);
103  break;
104 #endif
105 
106 #ifdef USE_LOGGER_USB_CDC
108  this->hw_serial_ = &Serial;
109 #if ARDUINO_USB_CDC_ON_BOOT
110  Serial.setTxTimeoutMs(0); // workaround for 2.0.9 crash when there's no data connection
111 #endif
112  Serial.begin(this->baud_rate_);
113  break;
114 #endif
115  }
116 #endif // USE_ARDUINO
117 
118 #ifdef USE_ESP_IDF
119  this->uart_num_ = UART_NUM_0;
120  switch (this->uart_) {
122  this->uart_num_ = UART_NUM_0;
123  break;
125  this->uart_num_ = UART_NUM_1;
126  break;
127 #ifdef USE_ESP32_VARIANT_ESP32
129  this->uart_num_ = UART_NUM_2;
130  break;
131 #endif
132 #ifdef USE_LOGGER_USB_CDC
134  this->uart_num_ = -1;
135  break;
136 #endif
137 #ifdef USE_LOGGER_USB_SERIAL_JTAG
139  this->uart_num_ = -1;
140  init_usb_serial_jtag_();
141  break;
142 #endif
143  }
144  if (this->uart_num_ >= 0) {
146  }
147 #endif // USE_ESP_IDF
148  }
149 
150  global_logger = this;
151 #if defined(USE_ESP_IDF) || defined(USE_ESP32_FRAMEWORK_ARDUINO)
152  esp_log_set_vprintf(esp_idf_log_vprintf_);
153  if (ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE) {
154  esp_log_level_set("*", ESP_LOG_VERBOSE);
155  }
156 #endif // USE_ESP_IDF || USE_ESP32_FRAMEWORK_ARDUINO
157 
158  ESP_LOGI(TAG, "Log initialized");
159 }
160 
161 #ifdef USE_ESP_IDF
162 void HOT Logger::write_msg_(const char *msg) {
163  if (
164 #if defined(USE_ESP32_VARIANT_ESP32S2)
166 #elif defined(USE_ESP32_VARIANT_ESP32C3) || defined(USE_ESP32_VARIANT_ESP32C6) || defined(USE_ESP32_VARIANT_ESP32H2)
168 #elif defined(USE_ESP32_VARIANT_ESP32S3)
170 #else
171  /* DISABLES CODE */ (false) // NOLINT
172 #endif
173  ) {
174  puts(msg);
175  } else {
176  uart_write_bytes(this->uart_num_, msg, strlen(msg));
177  uart_write_bytes(this->uart_num_, "\n", 1);
178  }
179 }
180 #else
181 void HOT Logger::write_msg_(const char *msg) { this->hw_serial_->println(msg); }
182 #endif
183 
184 const char *const UART_SELECTIONS[] = {
185  "UART0", "UART1",
186 #ifdef USE_ESP32_VARIANT_ESP32
187  "UART2",
188 #endif
189 #ifdef USE_LOGGER_USB_CDC
190  "USB_CDC",
191 #endif
192 #ifdef USE_LOGGER_USB_SERIAL_JTAG
193  "USB_SERIAL_JTAG",
194 #endif
195 };
196 
197 const char *Logger::get_uart_selection_() { return UART_SELECTIONS[this->uart_]; }
198 
199 } // namespace logger
200 } // namespace esphome
201 #endif
Logger * global_logger
Definition: logger.cpp:179
uart_port_t uart_num_
Definition: logger.h:160
const char * get_uart_selection_()
void init_uart(uart_port_t uart_num, uint32_t baud_rate, int tx_buffer_size)
UARTSelection uart_
Definition: logger.h:151
void pre_setup()
Set up this component.
const char *const UART_SELECTIONS[]
This is a workaround until we can figure out a way to get the tflite-micro idf component code availab...
Definition: a01nyub.cpp:7
void write_msg_(const char *msg)
int HOT esp_idf_log_vprintf_(const char *format, va_list args)
Definition: log.cpp:50