5#include <zephyr/drivers/uart.h>
15static constexpr uint32_t FLUSH_TIMEOUT_MS = 500;
16static constexpr uint32_t LINE_STATE_POLL_MS = 100;
18#ifdef CONFIG_USB_DEVICE_STACK_NEXT
19#define TX_FIFO_SIZE_IF_DEVICE(node_id) \
20 if (dev == DEVICE_DT_GET(node_id)) \
21 return DT_PROP(node_id, tx_fifo_size);
24static uint32_t driver_tx_buffer_size(
const device *dev) {
25 DT_FOREACH_STATUS_OKAY(zephyr_cdc_acm_uart, TX_FIFO_SIZE_IF_DEVICE)
29static uint32_t driver_tx_buffer_size(
const device * ) {
return CONFIG_USB_CDC_ACM_RINGBUF_SIZE; }
47 int sent = uart_fifo_fill(this->
uart_dev_, data, send_len);
72 int rx = uart_fifo_read(this->
uart_dev_, data, recv_len);
74 ESP_LOGE(TAG,
"Failed to read UART FIFO, err %d", rx);
76 ring_buf_put_finish(&this->
rx_ringbuf_, rx > 0 ? rx : 0);
81 while (uart_irq_update(dev) && uart_irq_is_pending(dev)) {
82 if (uart_irq_rx_ready(dev)) {
88 if (uart_irq_tx_ready(dev)) {
96 ESP_LOGE(TAG,
"UART Bus %s: is not ready.", this->
uart_dev_->name);
101 ESP_LOGE(TAG,
"USB device setup failed: %d", err);
110#if defined(USE_ZEPHYR_CDC_ACM_RATE_CALLBACK)
135 uart_line_ctrl_get(this->
uart_dev_, UART_LINE_CTRL_DTR, &dtr_val);
136 uart_line_ctrl_get(this->
uart_dev_, UART_LINE_CTRL_RTS, &rts_val);
137 const bool dtr = dtr_val != 0;
138 const bool rts = rts_val != 0;
139 if (dtr != this->
dtr_) {
140 ESP_LOGD(TAG,
"UART Bus %s: %s", this->
uart_dev_->name,
141 dtr ? LOG_STR_LITERAL(
"Opened") : LOG_STR_LITERAL(
"Closed"));
143 if (dtr != this->
dtr_ || rts != this->
rts_) {
144 ESP_LOGV(TAG,
"Line state device %s: DTR=%d, RTS=%d", this->
uart_dev_->name, dtr, rts);
160 " Baud Rate: %" PRIu32
" baud\n"
170 ESP_LOGVV(TAG,
"UART Bus %s: available %" PRIu32, this->
uart_dev_->name, size);
191 ESP_LOGE(TAG,
"UART CDC unexpected size");
217#ifdef CONFIG_USB_DEVICE_STACK_NEXT
228 if (
millis() - start > FLUSH_TIMEOUT_MS) {
229 ESP_LOGW(TAG,
"Flush timeout");
241 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_
static 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_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")
int usb_device_enable()
Start the USB device on whichever Zephyr USB device stack the build uses (the legacy one or device_ne...
void HOT delay(uint32_t ms)
uint32_t IRAM_ATTR HOT millis()
Application App
Global storage of Application pointer - only one Application can exist.