diff --git a/esphome/components/logger/__init__.py b/esphome/components/logger/__init__.py index d11b00405d..43d87bcefe 100644 --- a/esphome/components/logger/__init__.py +++ b/esphome/components/logger/__init__.py @@ -19,8 +19,13 @@ from esphome.const import ( CONF_TX_BUFFER_SIZE, ) from esphome.core import CORE, EsphomeError, Lambda, coroutine_with_priority -from esphome.components.esp32 import get_esp32_variant -from esphome.components.esp32.const import VARIANT_ESP32S2, VARIANT_ESP32C3 +from esphome.components.esp32 import add_idf_sdkconfig_option, get_esp32_variant +from esphome.components.esp32.const import ( + VARIANT_ESP32, + VARIANT_ESP32S2, + VARIANT_ESP32C3, + VARIANT_ESP32S3, +) CODEOWNERS = ["@esphome/core"] logger_ns = cg.esphome_ns.namespace("logger") @@ -54,36 +59,51 @@ LOG_LEVEL_SEVERITY = [ "VERY_VERBOSE", ] -ESP32_REDUCED_VARIANTS = [VARIANT_ESP32C3, VARIANT_ESP32S2] +UART0 = "UART0" +UART1 = "UART1" +UART2 = "UART2" +UART0_SWAP = "UART0_SWAP" +USB_SERIAL_JTAG = "USB_SERIAL_JTAG" +USB_CDC = "USB_CDC" -UART_SELECTION_ESP32_REDUCED = ["UART0", "UART1"] +UART_SELECTION_ESP32 = { + VARIANT_ESP32: [UART0, UART1, UART2], + VARIANT_ESP32S2: [UART0, UART1, USB_CDC], + VARIANT_ESP32S3: [UART0, UART1, USB_CDC, USB_SERIAL_JTAG], + VARIANT_ESP32C3: [UART0, UART1, USB_SERIAL_JTAG], +} -UART_SELECTION_ESP32 = ["UART0", "UART1", "UART2"] +UART_SELECTION_ESP8266 = [UART0, UART0_SWAP, UART1] -UART_SELECTION_ESP8266 = ["UART0", "UART0_SWAP", "UART1"] +ESP_IDF_UARTS = [USB_CDC, USB_SERIAL_JTAG] HARDWARE_UART_TO_UART_SELECTION = { - "UART0": logger_ns.UART_SELECTION_UART0, - "UART0_SWAP": logger_ns.UART_SELECTION_UART0_SWAP, - "UART1": logger_ns.UART_SELECTION_UART1, - "UART2": logger_ns.UART_SELECTION_UART2, + UART0: logger_ns.UART_SELECTION_UART0, + UART0_SWAP: logger_ns.UART_SELECTION_UART0_SWAP, + UART1: logger_ns.UART_SELECTION_UART1, + UART2: logger_ns.UART_SELECTION_UART2, + USB_CDC: logger_ns.UART_SELECTION_USB_CDC, + USB_SERIAL_JTAG: logger_ns.UART_SELECTION_USB_SERIAL_JTAG, } HARDWARE_UART_TO_SERIAL = { - "UART0": cg.global_ns.Serial, - "UART0_SWAP": cg.global_ns.Serial, - "UART1": cg.global_ns.Serial1, - "UART2": cg.global_ns.Serial2, + UART0: cg.global_ns.Serial, + UART0_SWAP: cg.global_ns.Serial, + UART1: cg.global_ns.Serial1, + UART2: cg.global_ns.Serial2, } is_log_level = cv.one_of(*LOG_LEVELS, upper=True) def uart_selection(value): + if value.upper() in ESP_IDF_UARTS: + if not CORE.using_esp_idf: + raise cv.Invalid(f"Only esp-idf framework supports {value}.") if CORE.is_esp32: - if get_esp32_variant() in ESP32_REDUCED_VARIANTS: - return cv.one_of(*UART_SELECTION_ESP32_REDUCED, upper=True)(value) - return cv.one_of(*UART_SELECTION_ESP32, upper=True)(value) + variant = get_esp32_variant() + if variant in UART_SELECTION_ESP32: + return cv.one_of(*UART_SELECTION_ESP32[variant], upper=True)(value) if CORE.is_esp8266: return cv.one_of(*UART_SELECTION_ESP8266, upper=True)(value) raise NotImplementedError @@ -113,7 +133,7 @@ CONFIG_SCHEMA = cv.All( cv.Optional(CONF_BAUD_RATE, default=115200): cv.positive_int, cv.Optional(CONF_TX_BUFFER_SIZE, default=512): cv.validate_bytes, cv.Optional(CONF_DEASSERT_RTS_DTR, default=False): cv.boolean, - cv.Optional(CONF_HARDWARE_UART, default="UART0"): uart_selection, + cv.Optional(CONF_HARDWARE_UART, default=UART0): uart_selection, cv.Optional(CONF_LEVEL, default="DEBUG"): is_log_level, cv.Optional(CONF_LOGS, default={}): cv.Schema( { @@ -185,6 +205,12 @@ async def to_code(config): if config.get(CONF_ESP8266_STORE_LOG_STRINGS_IN_FLASH): cg.add_build_flag("-DUSE_STORE_LOG_STR_IN_FLASH") + if CORE.using_esp_idf: + if config[CONF_HARDWARE_UART] == USB_CDC: + add_idf_sdkconfig_option("CONFIG_ESP_CONSOLE_USB_CDC", True) + elif config[CONF_HARDWARE_UART] == USB_SERIAL_JTAG: + add_idf_sdkconfig_option("CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG", True) + # Register at end for safe mode await cg.register_component(log, config) diff --git a/esphome/components/logger/logger.cpp b/esphome/components/logger/logger.cpp index 3f4e4e7753..08c83035b6 100644 --- a/esphome/components/logger/logger.cpp +++ b/esphome/components/logger/logger.cpp @@ -116,8 +116,22 @@ void HOT Logger::log_message_(int level, const char *tag, int offset) { this->hw_serial_->println(msg); #endif // USE_ARDUINO #ifdef USE_ESP_IDF - uart_write_bytes(uart_num_, msg, strlen(msg)); - uart_write_bytes(uart_num_, "\n", 1); + if ( +#if defined(USE_ESP32_VARIANT_ESP32S2) + uart_ == UART_SELECTION_USB_CDC +#elif defined(USE_ESP32_VARIANT_ESP32C3) + uart_ == UART_SELECTION_USB_SERIAL_JTAG +#elif defined(USE_ESP32_VARIANT_ESP32S3) + uart_ == UART_SELECTION_USB_CDC || uart_ == UART_SELECTION_USB_SERIAL_JTAG +#else + /* DISABLES CODE */ (false) +#endif + ) { + puts(msg); + } else { + uart_write_bytes(uart_num_, msg, strlen(msg)); + uart_write_bytes(uart_num_, "\n", 1); + } #endif } @@ -181,29 +195,41 @@ void Logger::pre_setup() { case UART_SELECTION_UART1: uart_num_ = UART_NUM_1; break; -#if defined(USE_ESP32) && !defined(USE_ESP32_VARIANT_ESP32C3) && !defined(USE_ESP32_VARIANT_ESP32S2) +#if !defined(USE_ESP32_VARIANT_ESP32C3) && !defined(USE_ESP32_VARIANT_ESP32S2) && !defined(USE_ESP32_VARIANT_ESP32S3) case UART_SELECTION_UART2: uart_num_ = UART_NUM_2; break; -#endif +#endif // !USE_ESP32_VARIANT_ESP32C3 && !USE_ESP32_VARIANT_ESP32S2 && !USE_ESP32_VARIANT_ESP32S3 +#if defined(USE_ESP32_VARIANT_ESP32S2) || defined(USE_ESP32_VARIANT_ESP32S3) + case UART_SELECTION_USB_CDC: + uart_num_ = -1; + break; +#endif // USE_ESP32_VARIANT_ESP32S2 || USE_ESP32_VARIANT_ESP32S3 +#if defined(USE_ESP32_VARIANT_ESP32C3) || defined(USE_ESP32_VARIANT_ESP32S3) + case UART_SELECTION_USB_SERIAL_JTAG: + uart_num_ = -1; + break; +#endif // USE_ESP32_VARIANT_ESP32C3 || USE_ESP32_VARIANT_ESP32S3 } - uart_config_t uart_config{}; - uart_config.baud_rate = (int) baud_rate_; - uart_config.data_bits = UART_DATA_8_BITS; - uart_config.parity = UART_PARITY_DISABLE; - uart_config.stop_bits = UART_STOP_BITS_1; - uart_config.flow_ctrl = UART_HW_FLOWCTRL_DISABLE; - uart_param_config(uart_num_, &uart_config); - const int uart_buffer_size = tx_buffer_size_; - // Install UART driver using an event queue here - uart_driver_install(uart_num_, uart_buffer_size, uart_buffer_size, 10, nullptr, 0); -#endif + if (uart_num_ >= 0) { + uart_config_t uart_config{}; + uart_config.baud_rate = (int) baud_rate_; + uart_config.data_bits = UART_DATA_8_BITS; + uart_config.parity = UART_PARITY_DISABLE; + uart_config.stop_bits = UART_STOP_BITS_1; + uart_config.flow_ctrl = UART_HW_FLOWCTRL_DISABLE; + uart_param_config(uart_num_, &uart_config); + const int uart_buffer_size = tx_buffer_size_; + // Install UART driver using an event queue here + uart_driver_install(uart_num_, uart_buffer_size, uart_buffer_size, 10, nullptr, 0); + } +#endif // USE_ESP_IDF } #ifdef USE_ESP8266 else { uart_set_debug(UART_NO); } -#endif +#endif // USE_ESP8266 global_logger = this; #if defined(USE_ESP_IDF) || defined(USE_ESP32_FRAMEWORK_ARDUINO) @@ -211,7 +237,7 @@ void Logger::pre_setup() { if (ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE) { esp_log_level_set("*", ESP_LOG_VERBOSE); } -#endif +#endif // USE_ESP_IDF || USE_ESP32_FRAMEWORK_ARDUINO ESP_LOGI(TAG, "Log initialized"); } @@ -226,11 +252,24 @@ void Logger::add_on_log_callback(std::function