mirror of
https://github.com/esphome/esphome.git
synced 2026-02-24 10:18:23 -07:00
The logger loop disable optimization was guarded by USE_LOGGER_USB_CDC, which is a platform capability flag defined whenever the platform supports USB CDC. This meant the optimization was disabled on all ESP32-S2, S3, C3, C5, C6, C61, H2, and P4 variants regardless of whether USB CDC was actually selected as the hardware UART. Changed all guards to use USE_LOGGER_UART_SELECTION_USB_CDC which is only defined when USB CDC is the selected hardware UART. Also changed the guard logic from || to && so the loop only stays permanently active when both Zephyr AND USB CDC is selected (the only case that needs cdc_loop_() to poll port readiness). Additionally set USE_LOGGER_UART_SELECTION_USB_CDC in the Zephyr codepath when USB CDC is selected, and updated defines.h for static analysis coverage.
99 lines
2.4 KiB
C++
99 lines
2.4 KiB
C++
#ifdef USE_ZEPHYR
|
|
|
|
#include "esphome/core/application.h"
|
|
#include "esphome/core/log.h"
|
|
#include "logger.h"
|
|
|
|
#include <zephyr/device.h>
|
|
#include <zephyr/drivers/uart.h>
|
|
#include <zephyr/sys/printk.h>
|
|
#include <zephyr/usb/usb_device.h>
|
|
|
|
namespace esphome::logger {
|
|
|
|
static const char *const TAG = "logger";
|
|
|
|
#ifdef USE_LOGGER_UART_SELECTION_USB_CDC
|
|
void Logger::cdc_loop_() {
|
|
if (this->uart_ != UART_SELECTION_USB_CDC || this->uart_dev_ == nullptr) {
|
|
return;
|
|
}
|
|
static bool opened = false;
|
|
uint32_t dtr = 0;
|
|
uart_line_ctrl_get(this->uart_dev_, UART_LINE_CTRL_DTR, &dtr);
|
|
|
|
/* Poll if the DTR flag was set, optional */
|
|
if (opened == dtr) {
|
|
return;
|
|
}
|
|
|
|
if (!opened) {
|
|
App.schedule_dump_config();
|
|
}
|
|
opened = !opened;
|
|
}
|
|
#endif
|
|
|
|
void Logger::pre_setup() {
|
|
if (this->baud_rate_ > 0) {
|
|
static const struct device *uart_dev = nullptr;
|
|
switch (this->uart_) {
|
|
case UART_SELECTION_UART0:
|
|
uart_dev = DEVICE_DT_GET_OR_NULL(DT_NODELABEL(uart0));
|
|
break;
|
|
case UART_SELECTION_UART1:
|
|
uart_dev = DEVICE_DT_GET_OR_NULL(DT_NODELABEL(uart1));
|
|
break;
|
|
#ifdef USE_LOGGER_USB_CDC
|
|
case UART_SELECTION_USB_CDC:
|
|
uart_dev = DEVICE_DT_GET_OR_NULL(DT_NODELABEL(cdc_acm_uart0));
|
|
if (device_is_ready(uart_dev)) {
|
|
usb_enable(nullptr);
|
|
}
|
|
break;
|
|
#endif
|
|
}
|
|
if (!device_is_ready(uart_dev)) {
|
|
ESP_LOGE(TAG, "%s is not ready.", LOG_STR_ARG(get_uart_selection_()));
|
|
} else {
|
|
this->uart_dev_ = uart_dev;
|
|
}
|
|
}
|
|
global_logger = this;
|
|
ESP_LOGI(TAG, "Log initialized");
|
|
}
|
|
|
|
void HOT Logger::write_msg_(const char *msg, uint16_t len) {
|
|
// Single write with newline already in buffer (added by caller)
|
|
#ifdef CONFIG_PRINTK
|
|
// Requires the debug component and an active SWD connection.
|
|
// It is used for pyocd rtt -t nrf52840
|
|
printk("%.*s", static_cast<int>(len), msg);
|
|
#endif
|
|
if (this->uart_dev_ == nullptr) {
|
|
return;
|
|
}
|
|
for (uint16_t i = 0; i < len; ++i) {
|
|
uart_poll_out(this->uart_dev_, msg[i]);
|
|
}
|
|
}
|
|
|
|
const LogString *Logger::get_uart_selection_() {
|
|
switch (this->uart_) {
|
|
case UART_SELECTION_UART0:
|
|
return LOG_STR("UART0");
|
|
case UART_SELECTION_UART1:
|
|
return LOG_STR("UART1");
|
|
#ifdef USE_LOGGER_USB_CDC
|
|
case UART_SELECTION_USB_CDC:
|
|
return LOG_STR("USB_CDC");
|
|
#endif
|
|
default:
|
|
return LOG_STR("UNKNOWN");
|
|
}
|
|
}
|
|
|
|
} // namespace esphome::logger
|
|
|
|
#endif
|