5#include <zephyr/usb/usb_device.h>
6#include <zephyr/drivers/uart.h>
10#ifdef CONFIG_USB_DEVICE_STACK_NEXT
11#error "usb_cdc_acm supports only the legacy USB device stack (CONFIG_USB_DEVICE_STACK_NEXT must be disabled)"
20static constexpr uint32_t FLUSH_TIMEOUT_MS = 500;
21static constexpr uint32_t LINE_STATE_POLL_MS = 100;
30 if (uart_irq_tx_ready(this->
uart_dev_) == CONFIG_USB_CDC_ACM_RINGBUF_SIZE) {
36 int sent = uart_fifo_fill(this->
uart_dev_, data, send_len);
61 int rx = uart_fifo_read(this->
uart_dev_, data, recv_len);
63 ESP_LOGE(TAG,
"Failed to read UART FIFO, err %d", rx);
65 ring_buf_put_finish(&this->
rx_ringbuf_, rx > 0 ? rx : 0);
70 while (uart_irq_update(dev) && uart_irq_is_pending(dev)) {
71 if (uart_irq_rx_ready(dev)) {
77 if (uart_irq_tx_ready(dev)) {
85 ESP_LOGE(TAG,
"UART Bus %s: is not ready.", this->
uart_dev_->name);
95#if defined(CONFIG_CDC_ACM_DTE_RATE_CALLBACK_SUPPORT)
120 uart_line_ctrl_get(this->
uart_dev_, UART_LINE_CTRL_DTR, &dtr_val);
121 uart_line_ctrl_get(this->
uart_dev_, UART_LINE_CTRL_RTS, &rts_val);
122 const bool dtr = dtr_val != 0;
123 const bool rts = rts_val != 0;
124 if (dtr != this->
dtr_) {
125 ESP_LOGD(TAG,
"UART Bus %s: %s", this->
uart_dev_->name,
126 dtr ? LOG_STR_LITERAL(
"Opened") : LOG_STR_LITERAL(
"Closed"));
128 if (dtr != this->
dtr_ || rts != this->
rts_) {
129 ESP_LOGV(TAG,
"Line state device %s: DTR=%d, RTS=%d", this->
uart_dev_->name, dtr, rts);
145 " Baud Rate: %" PRIu32
" baud\n"
155 ESP_LOGVV(TAG,
"UART Bus %s: available %" PRIu32, this->
uart_dev_->name, size);
176 ESP_LOGE(TAG,
"UART CDC unexpected size");
208 if (
millis() - start > FLUSH_TIMEOUT_MS) {
209 ESP_LOGW(TAG,
"Flush timeout");
221 if (recv_len <
len) {
uint32_t IRAM_ATTR HOT get_loop_component_start_time() const
Get the cached time in milliseconds from when the current component started its loop execution.
USBCDCACMComponent * get_parent() const
UARTParityOptions parity_
Represents a single CDC ACM interface instance.
std::atomic< bool > rx_irq_disabled_
void report_tx_debug_(const uint8_t *data, size_t len)
size_t available() override
uint8_t rx_ringbuf_data_[ESPHOME_CDC_RX_RING_BUFFER_SIZE]
bool read_array(uint8_t *data, size_t len) override
std::atomic< bool > tx_irq_disabled_
void record_tx_drop_(size_t dropped)
static void uart_irq_handler(const device *dev, void *instance)
uint32_t last_line_poll_ms_
LineStateCallback line_state_callback_
uart::UARTFlushResult flush() override
uint8_t tx_ringbuf_data_[ESPHOME_CDC_TX_RING_BUFFER_SIZE]
void queue_line_coding_event(uint32_t bit_rate, uint8_t stop_bits, uint8_t parity, uint8_t data_bits)
bool consume_peek_(uint8_t *&data, size_t &len)
void report_rx_debug_(const uint8_t *data, size_t len)
void write_array(const uint8_t *data, size_t len) override
std::atomic< bool > tx_drained_
void add_on_rate_callback(F &&callback)
Call from setup().
const LogString * parity_to_str(UARTParityOptions parity)
UARTFlushResult
Result of a flush() call.
@ 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")
CdcAcm *const global_cdc_acm
void HOT delay(uint32_t ms)
uint32_t IRAM_ATTR HOT millis()
Application App
Global storage of Application pointer - only one Application can exist.