1#include "tinyusb_virtual_uart.hpp"
9extern "C" void tud_cdc_rx_cb(uint8_t itf)
12 if (TinyUSBVirtualUART::self)
14 auto &port = TinyUSBVirtualUART::self->_read_port;
15 size_t avail = tud_cdc_available();
18 port.ProcessPendingReads(
true);
32 return CFG_TUD_CDC_RX_BUFSIZE - tud_cdc_available();
47 BusyState curr = busy_.load(std::memory_order_relaxed);
49 if (curr == BusyState::Pending)
54 busy_.store(BusyState::Idle, std::memory_order_release);
56 if (len ==
static_cast<int>(info_.
data.
size_))
62 Finish(in_isr, ErrorCode::EMPTY, info_, len);
66 else if (curr == BusyState::Idle)
68 busy_.store(BusyState::Event, std::memory_order_release);
86 return CFG_TUD_CDC_TX_BUFSIZE - tud_cdc_write_available();
96 :
UART(&_read_port, &_write_port), _read_port(this), _write_port(this)
112 if (port.queue_info_->Pop(info) != ErrorCode::OK)
return ErrorCode::EMPTY;
114 size_t space = tud_cdc_write_available();
115 if (space < info.data.
size_)
return ErrorCode::FULL;
118 tud_cdc_write(
static_cast<const uint8_t *
>(info.data.
addr_), info.data.
size_);
119 tud_cdc_write_flush();
121 if (written == info.data.
size_)
123 port.
Finish(
false, ErrorCode::OK, info, written);
124 return ErrorCode::OK;
128 port.
Finish(
false, ErrorCode::FAILED, info, written);
129 return ErrorCode::FAILED;
140 if (tud_cdc_available() >= port.info_.
data.
size_)
142 int len = tud_cdc_read(
static_cast<uint8_t *
>(port.info_.
data.
addr_),
144 port.read_size_ = len;
145 return ErrorCode::OK;
147 return ErrorCode::EMPTY;
158 return ErrorCode::OK;
size_t size_
数据大小(字节)。 The size of the data (in bytes).
const void * addr_
数据存储地址(常量)。 The storage address of the data (constant).
size_t size_
数据大小(字节)。 The size of the data (in bytes).
void * addr_
数据存储地址。 The storage address of the data.
ReadPort class for handling read operations.
void Finish(bool in_isr, ErrorCode ans, ReadInfoBlock &info, uint32_t size)
更新读取操作的状态。 Updates the status of the read operation.
size_t Size()
获取当前已用空间(接收数据量)
size_t EmptySize()
返回FIFO可用空间(空余字节数)
void ProcessPendingReads(bool in_isr=true)
处理等待中的读操作
size_t Size()
获取已写入空间(写入但未发送的字节数)
size_t EmptySize()
返回FIFO可写空间(空余字节数)
static ErrorCode ReadFun(ReadPort &port)
读操作实现
ErrorCode SetConfig(UART::Configuration config) override
配置CDC,无实际动作
static ErrorCode WriteFun(WritePort &port)
写操作实现
TinyUSBVirtualUART()
TinyUSB虚拟串口构造
通用异步收发传输(UART)基类 / Abstract base class for Universal Asynchronous Receiver-Transmitter (UART)
WritePort class for handling write operations.
void Finish(bool in_isr, ErrorCode ans, WriteInfoBlock &info, uint32_t size)
更新写入操作的状态。 Updates the status of the write operation.
ErrorCode(* ReadFun)(ReadPort &port)
Function pointer type for read operations.
ErrorCode(* WriteFun)(WritePort &port)
Function pointer type for write operations.
RawData data
Data buffer. 数据缓冲区。
UART 配置结构体 / UART configuration structure.