ESPHome  2024.5.4
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;
124  break;
126  this->uart_num_ = UART_NUM_1;
128  break;
129 #ifdef USE_ESP32_VARIANT_ESP32
131  this->uart_num_ = UART_NUM_2;
133  break;
134 #endif
135 #ifdef USE_LOGGER_USB_CDC
137  break;
138 #endif
139 #ifdef USE_LOGGER_USB_SERIAL_JTAG
141  init_usb_serial_jtag_();
142  break;
143 #endif
144  }
145 #endif // USE_ESP_IDF
146  }
147 
148  global_logger = this;
149 #if defined(USE_ESP_IDF) || defined(USE_ESP32_FRAMEWORK_ARDUINO)
150  esp_log_set_vprintf(esp_idf_log_vprintf_);
151  if (ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE) {
152  esp_log_level_set("*", ESP_LOG_VERBOSE);
153  }
154 #endif // USE_ESP_IDF || USE_ESP32_FRAMEWORK_ARDUINO
155 
156  ESP_LOGI(TAG, "Log initialized");
157 }
158 
159 #ifdef USE_ESP_IDF
160 void HOT Logger::write_msg_(const char *msg) {
161  if (
162 #if defined(USE_ESP32_VARIANT_ESP32S2)
164 #elif defined(USE_ESP32_VARIANT_ESP32C3) || defined(USE_ESP32_VARIANT_ESP32C6) || defined(USE_ESP32_VARIANT_ESP32H2)
166 #elif defined(USE_ESP32_VARIANT_ESP32S3)
168 #else
169  /* DISABLES CODE */ (false) // NOLINT
170 #endif
171  ) {
172  puts(msg);
173  } else {
174  uart_write_bytes(this->uart_num_, msg, strlen(msg));
175  uart_write_bytes(this->uart_num_, "\n", 1);
176  }
177 }
178 #else
179 void HOT Logger::write_msg_(const char *msg) { this->hw_serial_->println(msg); }
180 #endif
181 
182 const char *const UART_SELECTIONS[] = {
183  "UART0", "UART1",
184 #ifdef USE_ESP32_VARIANT_ESP32
185  "UART2",
186 #endif
187 #ifdef USE_LOGGER_USB_CDC
188  "USB_CDC",
189 #endif
190 #ifdef USE_LOGGER_USB_SERIAL_JTAG
191  "USB_SERIAL_JTAG",
192 #endif
193 };
194 
195 const char *Logger::get_uart_selection_() { return UART_SELECTIONS[this->uart_]; }
196 
197 } // namespace logger
198 } // namespace esphome
199 #endif
Logger * global_logger
Definition: logger.cpp:198
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