1#if defined(USE_ESP32_VARIANT_ESP32P4) || defined(USE_ESP32_VARIANT_ESP32S2) || defined(USE_ESP32_VARIANT_ESP32S3) || \
2 defined(USE_ESP32_VARIANT_ESP32S31) || defined(USE_ESP32_VARIANT_ESP32H4)
10#include "freertos/FreeRTOS.h"
11#include "freertos/ringbuf.h"
12#include "freertos/task.h"
16#include "tinyusb_cdc_acm.h"
23static constexpr size_t USB_CDC_MAX_LOG_BYTES = 168;
25static constexpr size_t USB_TX_TASK_STACK_SIZE = 4096;
26static constexpr size_t USB_TX_TASK_STACK_SIZE_VV = 8192;
30static constexpr uint32_t FLUSH_TIMEOUT_MS = 100;
32static USBCDCACMInstance *get_instance_by_itf(
int itf) {
39static void tinyusb_cdc_rx_callback(
int itf, cdcacm_event_t *event) {
40 USBCDCACMInstance *instance = get_instance_by_itf(itf);
41 if (instance ==
nullptr) {
42 ESP_LOGE(TAG,
"RX callback: invalid interface %d", itf);
47 static uint8_t rx_buf[CONFIG_TINYUSB_CDC_RX_BUFSIZE] = {0};
51 tinyusb_cdcacm_read(
static_cast<tinyusb_cdcacm_itf_t
>(itf), rx_buf, CONFIG_TINYUSB_CDC_RX_BUFSIZE, &rx_size);
52 ESP_LOGV(TAG,
"tinyusb_cdc_rx_callback itf=%d (size: %u)", itf, rx_size);
53 ESP_LOGVV_HEX(TAG, rx_buf, rx_size, USB_CDC_MAX_LOG_BYTES,
"rx_buf = %s");
55 if (
ret == ESP_OK && rx_size > 0) {
56 RingbufHandle_t rx_ringbuf = instance->get_rx_ringbuf();
57 if (rx_ringbuf !=
nullptr) {
58 BaseType_t send_res = xRingbufferSend(rx_ringbuf, rx_buf, rx_size, 0);
59 if (send_res != pdTRUE) {
60 ESP_LOGE(TAG,
"USB RX itf=%d: buffer full, %u bytes lost", itf, rx_size);
62 ESP_LOGV(TAG,
"USB RX itf=%d: queued %u bytes", itf, rx_size);
68static void tinyusb_cdc_line_state_changed_callback(
int itf, cdcacm_event_t *event) {
69 USBCDCACMInstance *instance = get_instance_by_itf(itf);
70 if (instance ==
nullptr) {
71 ESP_LOGE(TAG,
"Line state callback: invalid interface %d", itf);
75 int dtr =
event->line_state_changed_data.dtr;
76 int rts =
event->line_state_changed_data.rts;
77 ESP_LOGV(TAG,
"Line state itf=%d: DTR=%d, RTS=%d", itf, dtr, rts);
80 instance->queue_line_state_event(dtr != 0, rts != 0);
83static void tinyusb_cdc_line_coding_changed_callback(
int itf, cdcacm_event_t *event) {
84 USBCDCACMInstance *instance = get_instance_by_itf(itf);
85 if (instance ==
nullptr) {
86 ESP_LOGE(TAG,
"Line coding callback: invalid interface %d", itf);
90 uint32_t bit_rate =
event->line_coding_changed_data.p_line_coding->bit_rate;
91 uint8_t stop_bits =
event->line_coding_changed_data.p_line_coding->stop_bits;
92 uint8_t parity =
event->line_coding_changed_data.p_line_coding->parity;
93 uint8_t data_bits =
event->line_coding_changed_data.p_line_coding->data_bits;
94 ESP_LOGV(TAG,
"Line coding itf=%d: bit_rate=%" PRIu32
" stop_bits=%u parity=%u data_bits=%u", itf, bit_rate,
95 stop_bits, parity, data_bits);
98 instance->queue_line_coding_event(bit_rate, stop_bits, parity, data_bits);
106 this->
usb_tx_ringbuf_ = xRingbufferCreate(CONFIG_TINYUSB_CDC_TX_BUFSIZE, RINGBUF_TYPE_BYTEBUF);
108 ESP_LOGE(TAG,
"USB TX buffer creation error for itf %d", this->
itf_);
113 this->
usb_rx_ringbuf_ = xRingbufferCreate(CONFIG_TINYUSB_CDC_RX_BUFSIZE, RINGBUF_TYPE_BYTEBUF);
115 ESP_LOGE(TAG,
"USB RX buffer creation error for itf %d", this->
itf_);
121 const tinyusb_config_cdcacm_t acm_cfg = {
122 .cdc_port =
static_cast<tinyusb_cdcacm_itf_t
>(this->
itf_),
123 .callback_rx = &tinyusb_cdc_rx_callback,
124 .callback_rx_wanted_char = NULL,
125 .callback_line_state_changed = &tinyusb_cdc_line_state_changed_callback,
126 .callback_line_coding_changed = &tinyusb_cdc_line_coding_changed_callback,
129 esp_err_t result = tinyusb_cdcacm_init(&acm_cfg);
130 if (result != ESP_OK) {
131 ESP_LOGE(TAG,
"tinyusb_cdcacm_init failed: %d", result);
137 constexpr size_t stack_size =
138 ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERY_VERBOSE ? USB_TX_TASK_STACK_SIZE_VV : USB_TX_TASK_STACK_SIZE;
141 char task_name[] =
"usb_tx_0";
146 ESP_LOGE(TAG,
"Failed to create USB TX task for itf %d", this->
itf_);
166 size_t tx_data_size = 0;
176 ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
186 ESP_LOGE(TAG,
"USB TX itf=%d: RingBuf read failed", this->
itf_);
188 }
else if (tx_data_size == 0) {
189 ESP_LOGD(TAG,
"USB TX itf=%d: RingBuf empty, skipping", this->
itf_);
193 ESP_LOGV(TAG,
"USB TX itf=%d: Read %d bytes from buffer", this->
itf_, tx_data_size);
194 ESP_LOGVV_HEX(TAG, data, tx_data_size, USB_CDC_MAX_LOG_BYTES,
"data = %s");
198 uint8_t *data_head = &data[0];
200 while (tx_data_size > 0) {
202 tinyusb_cdcacm_write_queue(
static_cast<tinyusb_cdcacm_itf_t
>(this->
itf_), data_head, tx_data_size);
203 ESP_LOGV(TAG,
"USB TX itf=%d: enqueued: size=%d, queued=%u", this->
itf_, tx_data_size, queued);
205 tx_data_size -= queued;
208 ESP_LOGV(TAG,
"USB TX itf=%d: waiting 10ms for flush", this->
itf_);
209 esp_err_t flush_ret =
210 tinyusb_cdcacm_write_flush(
static_cast<tinyusb_cdcacm_itf_t
>(this->
itf_), pdMS_TO_TICKS(10));
212 if (flush_ret == ESP_OK) {
219 const size_t pending = tx_data_size + (CFG_TUD_CDC_TX_BUFSIZE - tud_cdc_n_write_available(this->
itf_));
235 if (flush_ret == ESP_ERR_TIMEOUT && tud_cdc_n_connected(this->
itf_)) {
237 if ((now - stall_log_ms) >= LOG_THROTTLE_MS) {
239 ESP_LOGW(TAG,
"USB TX itf=%d: host not reading; %zu bytes pending", this->
itf_, pending);
244 if (flush_ret == ESP_ERR_TIMEOUT) {
249 ESP_LOGW(TAG,
"USB TX itf=%d: not connected; dropping %zu bytes", this->
itf_, pending);
251 ESP_LOGE(TAG,
"USB TX itf=%d: flush failed (%s); dropping %zu bytes", this->
itf_, esp_err_to_name(flush_ret),
254 tud_cdc_n_write_clear(this->
itf_);
271 if (send_res != pdTRUE) {
291 size_t original_len =
len;
292 size_t bytes_read = 0;
296 uint8_t *buf =
static_cast<uint8_t *
>(xRingbufferReceiveUpTo(this->
usb_rx_ringbuf_, &rx_size, 0,
len));
297 if (buf ==
nullptr) {
301 memcpy(data, buf, rx_size);
304 bytes_read += rx_size;
312 buf =
static_cast<uint8_t *
>(xRingbufferReceiveUpTo(this->
usb_rx_ringbuf_, &rx_size, 0,
len));
313 if (buf ==
nullptr) {
317 memcpy(data, buf, rx_size);
320 bytes_read += rx_size;
322 return bytes_read == original_len;
326 UBaseType_t waiting = 0;
328 vRingbufferGetInfo(this->
usb_rx_ringbuf_,
nullptr,
nullptr,
nullptr,
nullptr, &waiting);
330 return waiting + (this->
has_peek_ ? 1 : 0);
338 UBaseType_t waiting = 0;
339 vRingbufferGetInfo(this->
usb_tx_ringbuf_,
nullptr,
nullptr,
nullptr,
nullptr, &waiting);
353 TickType_t now = xTaskGetTickCount();
354 const TickType_t deadline = now + pdMS_TO_TICKS(FLUSH_TIMEOUT_MS);
356 if (
static_cast<int32_t
>(now - deadline) >= 0) {
359 vTaskDelay(pdMS_TO_TICKS(1));
360 now = xTaskGetTickCount();
368 const int32_t remaining =
static_cast<int32_t
>(deadline - now);
369 const TickType_t flush_ticks = remaining > 0 ?
static_cast<TickType_t
>(remaining) : 1;
370 switch (tinyusb_cdcacm_write_flush(
static_cast<tinyusb_cdcacm_itf_t
>(this->
itf_), flush_ticks)) {
373 case ESP_ERR_TIMEOUT:
376 case ESP_ERR_NOT_FINISHED:
USBCDCACMComponent * parent_
USBCDCACMInstance * get_interface_by_number(uint8_t itf)
Represents a single CDC ACM interface instance.
TaskHandle_t usb_tx_task_handle_
void report_tx_debug_(const uint8_t *data, size_t len)
size_t available() override
std::array< uint8_t, CONFIG_TINYUSB_CDC_TX_BUFSIZE > usb_tx_staging_
std::atomic< uint8_t > usb_tx_busy_
bool read_array(uint8_t *data, size_t len) override
void record_tx_drop_(size_t dropped)
RingbufHandle_t usb_tx_ringbuf_
uart::UARTFlushResult flush() override
bool consume_peek_(uint8_t *&data, size_t &len)
void report_rx_debug_(const uint8_t *data, size_t len)
static void usb_tx_task_fn(void *arg)
void write_array(const uint8_t *data, size_t len) override
RingbufHandle_t usb_rx_ringbuf_
UARTFlushResult
Result of a flush() call.
@ UART_FLUSH_RESULT_ASSUMED_SUCCESS
Platform cannot report result; success is assumed.
@ UART_FLUSH_RESULT_SUCCESS
Confirmed: all bytes left the TX FIFO.
@ UART_FLUSH_RESULT_FAILED
Confirmed: driver or hardware error.
@ UART_FLUSH_RESULT_TIMEOUT
Confirmed: timed out before TX completed.
ESPHOME_LOG_TAG(TAG, "usb_cdc_acm")
esp_err_t ringbuf_read_bytes(RingbufHandle_t ring_buf, uint8_t *out_buf, size_t out_buf_sz, size_t *rx_data_size, TickType_t x_ticks_to_wait)
USBCDCACMComponent * global_usb_cdc_component
ESPHOME_ALWAYS_INLINE char format_hex_char(uint8_t v, char base)
Convert a nibble (0-15) to hex char with specified base ('a' for lowercase, 'A' for uppercase)
uint32_t IRAM_ATTR HOT millis()