diff --git a/esphome/components/improv_serial/improv_serial_component.cpp b/esphome/components/improv_serial/improv_serial_component.cpp index 2937720496..02ffa9f31c 100644 --- a/esphome/components/improv_serial/improv_serial_component.cpp +++ b/esphome/components/improv_serial/improv_serial_component.cpp @@ -57,7 +57,7 @@ optional ImprovSerialComponent::read_byte_() { } } break; -#if defined(CONFIG_ESP_CONSOLE_USB_CDC) && (defined(USE_ESP32_VARIANT_ESP32S2) || defined(USE_ESP32_VARIANT_ESP32S3)) +#ifdef USE_LOGGER_USB_CDC case logger::UART_SELECTION_USB_CDC: #if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0) if (esp_usb_console_available_for_read()) { @@ -68,15 +68,15 @@ optional ImprovSerialComponent::read_byte_() { byte = data; } break; -#endif // USE_ESP32_VARIANT_ESP32S2 || USE_ESP32_VARIANT_ESP32S3 -#if defined(USE_ESP32_VARIANT_ESP32C3) || defined(USE_ESP32_VARIANT_ESP32C6) || defined(USE_ESP32_VARIANT_ESP32S3) +#endif // USE_LOGGER_USB_CDC +#ifdef USE_LOGGER_USB_SERIAL_JTAG case logger::UART_SELECTION_USB_SERIAL_JTAG: { if (usb_serial_jtag_read_bytes((char *) &data, 1, 0)) { byte = data; } break; } -#endif // USE_ESP32_VARIANT_ESP32C3 || USE_ESP32_VARIANT_ESP32C6 || USE_ESP32_VARIANT_ESP32S3 +#endif // USE_LOGGER_USB_SERIAL_JTAG default: break; } @@ -99,19 +99,19 @@ void ImprovSerialComponent::write_data_(std::vector &data) { #endif // !USE_ESP32_VARIANT_ESP32C3 && !USE_ESP32_VARIANT_ESP32S2 && !USE_ESP32_VARIANT_ESP32S3 uart_write_bytes(this->uart_num_, data.data(), data.size()); break; -#if defined(CONFIG_ESP_CONSOLE_USB_CDC) && (defined(USE_ESP32_VARIANT_ESP32S2) || defined(USE_ESP32_VARIANT_ESP32S3)) +#ifdef USE_LOGGER_USB_CDC case logger::UART_SELECTION_USB_CDC: { const char *msg = (char *) data.data(); esp_usb_console_write_buf(msg, data.size()); break; } -#endif // USE_ESP32_VARIANT_ESP32S2 || USE_ESP32_VARIANT_ESP32S3 -#if defined(USE_ESP32_VARIANT_ESP32C3) || defined(USE_ESP32_VARIANT_ESP32C6) || defined(USE_ESP32_VARIANT_ESP32S3) +#endif // USE_LOGGER_USB_CDC +#ifdef USE_LOGGER_USB_SERIAL_JTAG case logger::UART_SELECTION_USB_SERIAL_JTAG: usb_serial_jtag_write_bytes((char *) data.data(), data.size(), 20 / portTICK_PERIOD_MS); usb_serial_jtag_ll_txfifo_flush(); // fixes for issue in IDF 4.4.7 break; -#endif // USE_ESP32_VARIANT_ESP32C3 || USE_ESP32_VARIANT_ESP32S3 +#endif // USE_LOGGER_USB_SERIAL_JTAG default: break; } diff --git a/esphome/components/uart/uart_component_esp32_arduino.cpp b/esphome/components/uart/uart_component_esp32_arduino.cpp index f77783e20e..793c1d52f4 100644 --- a/esphome/components/uart/uart_component_esp32_arduino.cpp +++ b/esphome/components/uart/uart_component_esp32_arduino.cpp @@ -96,10 +96,24 @@ void ESP32ArduinoUARTComponent::setup() { next_uart_num++; } else { #ifdef USE_LOGGER - // The logger doesn't use this UART component, instead it targets the UARTs - // directly (i.e. Serial/Serial0, Serial1, and Serial2). If the logger is - // enabled, skip the UART that it is configured to use. - if (logger::global_logger->get_baud_rate() > 0 && logger::global_logger->get_uart() == next_uart_num) { + bool logger_uses_hardware_uart = true; + +#ifdef USE_LOGGER_USB_CDC + if (logger::global_logger->get_uart() == logger::UART_SELECTION_USB_CDC) { + // this is not a hardware UART, ignore it + logger_uses_hardware_uart = false; + } +#endif // USE_LOGGER_USB_CDC + +#ifdef USE_LOGGER_USB_SERIAL_JTAG + if (logger::global_logger->get_uart() == logger::UART_SELECTION_USB_SERIAL_JTAG) { + // this is not a hardware UART, ignore it + logger_uses_hardware_uart = false; + } +#endif // USE_LOGGER_USB_SERIAL_JTAG + + if (logger_uses_hardware_uart && logger::global_logger->get_baud_rate() > 0 && + logger::global_logger->get_uart() == next_uart_num) { next_uart_num++; } #endif // USE_LOGGER diff --git a/esphome/components/uart/uart_component_esp_idf.cpp b/esphome/components/uart/uart_component_esp_idf.cpp index c66753b0c4..6999dfb619 100644 --- a/esphome/components/uart/uart_component_esp_idf.cpp +++ b/esphome/components/uart/uart_component_esp_idf.cpp @@ -60,10 +60,30 @@ uart_config_t IDFUARTComponent::get_config_() { void IDFUARTComponent::setup() { static uint8_t next_uart_num = 0; + #ifdef USE_LOGGER - if (logger::global_logger->get_uart_num() == next_uart_num) + bool logger_uses_hardware_uart = true; + +#ifdef USE_LOGGER_USB_CDC + if (logger::global_logger->get_uart() == logger::UART_SELECTION_USB_CDC) { + // this is not a hardware UART, ignore it + logger_uses_hardware_uart = false; + } +#endif // USE_LOGGER_USB_CDC + +#ifdef USE_LOGGER_USB_SERIAL_JTAG + if (logger::global_logger->get_uart() == logger::UART_SELECTION_USB_SERIAL_JTAG) { + // this is not a hardware UART, ignore it + logger_uses_hardware_uart = false; + } +#endif // USE_LOGGER_USB_SERIAL_JTAG + + if (logger_uses_hardware_uart && logger::global_logger->get_baud_rate() > 0 && + logger::global_logger->get_uart_num() == next_uart_num) { next_uart_num++; -#endif + } +#endif // USE_LOGGER + if (next_uart_num >= UART_NUM_MAX) { ESP_LOGW(TAG, "Maximum number of UART components created already."); this->mark_failed(); diff --git a/tests/components/logger/common.yaml b/tests/components/logger/common-default_uart.yaml similarity index 100% rename from tests/components/logger/common.yaml rename to tests/components/logger/common-default_uart.yaml diff --git a/tests/components/logger/common-usb_cdc.yaml b/tests/components/logger/common-usb_cdc.yaml new file mode 100644 index 0000000000..4df320527d --- /dev/null +++ b/tests/components/logger/common-usb_cdc.yaml @@ -0,0 +1,8 @@ +esphome: + on_boot: + then: + - logger.log: Hello world + +logger: + level: DEBUG + hardware_uart: USB_CDC diff --git a/tests/components/logger/common-usb_serial_jtag.yaml b/tests/components/logger/common-usb_serial_jtag.yaml new file mode 100644 index 0000000000..5891c9ea40 --- /dev/null +++ b/tests/components/logger/common-usb_serial_jtag.yaml @@ -0,0 +1,8 @@ +esphome: + on_boot: + then: + - logger.log: Hello world + +logger: + level: DEBUG + hardware_uart: USB_SERIAL_JTAG diff --git a/tests/components/logger/test-usb_cdc.esp32-c3-ard.yaml b/tests/components/logger/test-usb_cdc.esp32-c3-ard.yaml new file mode 100644 index 0000000000..cfdaec9771 --- /dev/null +++ b/tests/components/logger/test-usb_cdc.esp32-c3-ard.yaml @@ -0,0 +1 @@ +<<: !include common-usb_cdc.yaml diff --git a/tests/components/logger/test-usb_cdc.esp32-s2-ard.yaml b/tests/components/logger/test-usb_cdc.esp32-s2-ard.yaml new file mode 100644 index 0000000000..cfdaec9771 --- /dev/null +++ b/tests/components/logger/test-usb_cdc.esp32-s2-ard.yaml @@ -0,0 +1 @@ +<<: !include common-usb_cdc.yaml diff --git a/tests/components/logger/test-usb_cdc.esp32-s2-idf.yaml b/tests/components/logger/test-usb_cdc.esp32-s2-idf.yaml new file mode 100644 index 0000000000..cfdaec9771 --- /dev/null +++ b/tests/components/logger/test-usb_cdc.esp32-s2-idf.yaml @@ -0,0 +1 @@ +<<: !include common-usb_cdc.yaml diff --git a/tests/components/logger/test-usb_cdc.esp32-s3-ard.yaml b/tests/components/logger/test-usb_cdc.esp32-s3-ard.yaml new file mode 100644 index 0000000000..cfdaec9771 --- /dev/null +++ b/tests/components/logger/test-usb_cdc.esp32-s3-ard.yaml @@ -0,0 +1 @@ +<<: !include common-usb_cdc.yaml diff --git a/tests/components/logger/test-usb_serial_jtag.esp32-c3-idf.yaml b/tests/components/logger/test-usb_serial_jtag.esp32-c3-idf.yaml new file mode 100644 index 0000000000..46a927ff10 --- /dev/null +++ b/tests/components/logger/test-usb_serial_jtag.esp32-c3-idf.yaml @@ -0,0 +1 @@ +<<: !include common-usb_serial_jtag.yaml diff --git a/tests/components/logger/test-usb_serial_jtag.esp32-s3-idf.yaml b/tests/components/logger/test-usb_serial_jtag.esp32-s3-idf.yaml new file mode 100644 index 0000000000..46a927ff10 --- /dev/null +++ b/tests/components/logger/test-usb_serial_jtag.esp32-s3-idf.yaml @@ -0,0 +1 @@ +<<: !include common-usb_serial_jtag.yaml diff --git a/tests/components/logger/test.esp32-ard.yaml b/tests/components/logger/test.esp32-ard.yaml index dade44d145..3fe04e18a3 100644 --- a/tests/components/logger/test.esp32-ard.yaml +++ b/tests/components/logger/test.esp32-ard.yaml @@ -1 +1 @@ -<<: !include common.yaml +<<: !include common-default_uart.yaml diff --git a/tests/components/logger/test.esp32-c3-ard.yaml b/tests/components/logger/test.esp32-c3-ard.yaml index dade44d145..3fe04e18a3 100644 --- a/tests/components/logger/test.esp32-c3-ard.yaml +++ b/tests/components/logger/test.esp32-c3-ard.yaml @@ -1 +1 @@ -<<: !include common.yaml +<<: !include common-default_uart.yaml diff --git a/tests/components/logger/test.esp32-c3-idf.yaml b/tests/components/logger/test.esp32-c3-idf.yaml index dade44d145..3fe04e18a3 100644 --- a/tests/components/logger/test.esp32-c3-idf.yaml +++ b/tests/components/logger/test.esp32-c3-idf.yaml @@ -1 +1 @@ -<<: !include common.yaml +<<: !include common-default_uart.yaml diff --git a/tests/components/logger/test.esp32-idf.yaml b/tests/components/logger/test.esp32-idf.yaml index dade44d145..3fe04e18a3 100644 --- a/tests/components/logger/test.esp32-idf.yaml +++ b/tests/components/logger/test.esp32-idf.yaml @@ -1 +1 @@ -<<: !include common.yaml +<<: !include common-default_uart.yaml diff --git a/tests/components/logger/test.esp8266-ard.yaml b/tests/components/logger/test.esp8266-ard.yaml index dade44d145..3fe04e18a3 100644 --- a/tests/components/logger/test.esp8266-ard.yaml +++ b/tests/components/logger/test.esp8266-ard.yaml @@ -1 +1 @@ -<<: !include common.yaml +<<: !include common-default_uart.yaml diff --git a/tests/components/logger/test.rp2040-ard.yaml b/tests/components/logger/test.rp2040-ard.yaml index dade44d145..3fe04e18a3 100644 --- a/tests/components/logger/test.rp2040-ard.yaml +++ b/tests/components/logger/test.rp2040-ard.yaml @@ -1 +1 @@ -<<: !include common.yaml +<<: !include common-default_uart.yaml diff --git a/tests/components/uart/test-uart_max_with_usb_cdc.esp32-c3-ard.yaml b/tests/components/uart/test-uart_max_with_usb_cdc.esp32-c3-ard.yaml new file mode 100644 index 0000000000..2a73826c51 --- /dev/null +++ b/tests/components/uart/test-uart_max_with_usb_cdc.esp32-c3-ard.yaml @@ -0,0 +1,30 @@ +<<: !include ../logger/common-usb_cdc.yaml + +esphome: + on_boot: + then: + - uart.write: + id: uart_1 + data: 'Hello World' + - uart.write: + id: uart_1 + data: [0x00, 0x20, 0x42] + +uart: + - id: uart_1 + tx_pin: 4 + rx_pin: 5 + baud_rate: 9600 + data_bits: 8 + rx_buffer_size: 512 + parity: EVEN + stop_bits: 2 + + - id: uart_2 + tx_pin: 6 + rx_pin: 7 + baud_rate: 9600 + data_bits: 8 + rx_buffer_size: 512 + parity: EVEN + stop_bits: 2 diff --git a/tests/components/uart/test-uart_max_with_usb_cdc.esp32-s2-ard.yaml b/tests/components/uart/test-uart_max_with_usb_cdc.esp32-s2-ard.yaml new file mode 100644 index 0000000000..2a73826c51 --- /dev/null +++ b/tests/components/uart/test-uart_max_with_usb_cdc.esp32-s2-ard.yaml @@ -0,0 +1,30 @@ +<<: !include ../logger/common-usb_cdc.yaml + +esphome: + on_boot: + then: + - uart.write: + id: uart_1 + data: 'Hello World' + - uart.write: + id: uart_1 + data: [0x00, 0x20, 0x42] + +uart: + - id: uart_1 + tx_pin: 4 + rx_pin: 5 + baud_rate: 9600 + data_bits: 8 + rx_buffer_size: 512 + parity: EVEN + stop_bits: 2 + + - id: uart_2 + tx_pin: 6 + rx_pin: 7 + baud_rate: 9600 + data_bits: 8 + rx_buffer_size: 512 + parity: EVEN + stop_bits: 2 diff --git a/tests/components/uart/test-uart_max_with_usb_cdc.esp32-s2-idf.yaml b/tests/components/uart/test-uart_max_with_usb_cdc.esp32-s2-idf.yaml new file mode 100644 index 0000000000..2a73826c51 --- /dev/null +++ b/tests/components/uart/test-uart_max_with_usb_cdc.esp32-s2-idf.yaml @@ -0,0 +1,30 @@ +<<: !include ../logger/common-usb_cdc.yaml + +esphome: + on_boot: + then: + - uart.write: + id: uart_1 + data: 'Hello World' + - uart.write: + id: uart_1 + data: [0x00, 0x20, 0x42] + +uart: + - id: uart_1 + tx_pin: 4 + rx_pin: 5 + baud_rate: 9600 + data_bits: 8 + rx_buffer_size: 512 + parity: EVEN + stop_bits: 2 + + - id: uart_2 + tx_pin: 6 + rx_pin: 7 + baud_rate: 9600 + data_bits: 8 + rx_buffer_size: 512 + parity: EVEN + stop_bits: 2 diff --git a/tests/components/uart/test-uart_max_with_usb_cdc.esp32-s3-ard.yaml b/tests/components/uart/test-uart_max_with_usb_cdc.esp32-s3-ard.yaml new file mode 100644 index 0000000000..2a73826c51 --- /dev/null +++ b/tests/components/uart/test-uart_max_with_usb_cdc.esp32-s3-ard.yaml @@ -0,0 +1,30 @@ +<<: !include ../logger/common-usb_cdc.yaml + +esphome: + on_boot: + then: + - uart.write: + id: uart_1 + data: 'Hello World' + - uart.write: + id: uart_1 + data: [0x00, 0x20, 0x42] + +uart: + - id: uart_1 + tx_pin: 4 + rx_pin: 5 + baud_rate: 9600 + data_bits: 8 + rx_buffer_size: 512 + parity: EVEN + stop_bits: 2 + + - id: uart_2 + tx_pin: 6 + rx_pin: 7 + baud_rate: 9600 + data_bits: 8 + rx_buffer_size: 512 + parity: EVEN + stop_bits: 2 diff --git a/tests/components/uart/test-uart_max_with_usb_serial_jtag.esp32-c3-idf.yaml b/tests/components/uart/test-uart_max_with_usb_serial_jtag.esp32-c3-idf.yaml new file mode 100644 index 0000000000..e0a07dde91 --- /dev/null +++ b/tests/components/uart/test-uart_max_with_usb_serial_jtag.esp32-c3-idf.yaml @@ -0,0 +1,30 @@ +<<: !include ../logger/common-usb_serial_jtag.yaml + +esphome: + on_boot: + then: + - uart.write: + id: uart_1 + data: 'Hello World' + - uart.write: + id: uart_1 + data: [0x00, 0x20, 0x42] + +uart: + - id: uart_1 + tx_pin: 4 + rx_pin: 5 + baud_rate: 9600 + data_bits: 8 + rx_buffer_size: 512 + parity: EVEN + stop_bits: 2 + + - id: uart_2 + tx_pin: 6 + rx_pin: 7 + baud_rate: 9600 + data_bits: 8 + rx_buffer_size: 512 + parity: EVEN + stop_bits: 2 diff --git a/tests/components/uart/test-uart_max_with_usb_serial_jtag.esp32-s3-idf.yaml b/tests/components/uart/test-uart_max_with_usb_serial_jtag.esp32-s3-idf.yaml new file mode 100644 index 0000000000..e0a07dde91 --- /dev/null +++ b/tests/components/uart/test-uart_max_with_usb_serial_jtag.esp32-s3-idf.yaml @@ -0,0 +1,30 @@ +<<: !include ../logger/common-usb_serial_jtag.yaml + +esphome: + on_boot: + then: + - uart.write: + id: uart_1 + data: 'Hello World' + - uart.write: + id: uart_1 + data: [0x00, 0x20, 0x42] + +uart: + - id: uart_1 + tx_pin: 4 + rx_pin: 5 + baud_rate: 9600 + data_bits: 8 + rx_buffer_size: 512 + parity: EVEN + stop_bits: 2 + + - id: uart_2 + tx_pin: 6 + rx_pin: 7 + baud_rate: 9600 + data_bits: 8 + rx_buffer_size: 512 + parity: EVEN + stop_bits: 2