28 using LibXR::UART::Read;
30 using LibXR::UART::Write;
44 CDCUart(
size_t rx_buffer_size = 128,
size_t tx_buffer_size = 128,
45 size_t tx_queue_size = 5,
49 :
CDCBase(data_in_ep_num, data_out_ep_num, comm_ep_num),
73 auto& line_coding = GetLineCoding();
79 line_coding.bCharFormat = 0;
82 line_coding.bCharFormat = 2;
85 return ErrorCode::ARG_ERR;
92 line_coding.bParityType = 0;
95 line_coding.bParityType = 1;
98 line_coding.bParityType = 2;
101 return ErrorCode::ARG_ERR;
108 line_coding.bDataBits = 5;
111 line_coding.bDataBits = 6;
114 line_coding.bDataBits = 7;
117 line_coding.bDataBits = 8;
120 line_coding.bDataBits = 16;
123 return ErrorCode::ARG_ERR;
127 line_coding.dwDTERate = cfg.
baudrate;
131 return ErrorCode::OK;
174 auto ep_data_in = cdc->GetDataInEndpoint();
177 if (!cdc->Inited() || !cdc->
IsDtrSet())
179 if (ep_data_in->GetActiveLength() == 0)
182 auto ans = port.queue_info_->Pop(info);
183 if (ans != ErrorCode::OK)
185 return ErrorCode::EMPTY;
188 port.
Finish(
true, ErrorCode::INIT_ERR, info, 0);
190 return ErrorCode::FAILED;
195 while (port.queue_info_->Size() > 0)
200 return ErrorCode::FAILED;
203 auto buffer = ep_data_in->GetBuffer();
208 if (port.queue_info_->Peek(info) != ErrorCode::OK)
210 return ErrorCode::EMPTY;
213 size_t need_write = info.
data.
size_ - dequeued;
217 return ErrorCode::OK;
221 if (need_write > buffer.size_)
223 need_write = buffer.size_;
227 if (port.queue_data_->
PopBatch(
reinterpret_cast<uint8_t*
>(buffer.addr_),
228 need_write) != ErrorCode::OK)
231 return ErrorCode::EMPTY;
234 dequeued += need_write;
240 port.queue_info_->Pop();
245 ep_data_in->SetActiveLength(need_write);
249 std::atomic_signal_fence(std::memory_order_seq_cst);
253 ep_data_in->GetActiveLength() != 0)
259 return ErrorCode::FAILED;
263 auto ans = ErrorCode::OK;
265 ep_data_in->SetActiveLength(0);
267 ans = ep_data_in->Transfer(need_write);
269 ASSERT(ans == ErrorCode::OK);
272 return ErrorCode::OK;
287 return ErrorCode::EMPTY;
303 auto ep_data_out = GetDataOutEndpoint();
304 ep_data_out->Transfer(ep_data_out->MaxTransferSize());
328 auto ep_data_in = GetDataInEndpoint();
330 size_t pending_len = ep_data_in->GetActiveLength();
332 if (pending_len == 0)
338 ep_data_in->SetActiveLength(0);
339 auto ans = ep_data_in->Transfer(pending_len);
340 ASSERT(ans == ErrorCode::OK);
360 auto buffer = ep_data_in->GetBuffer();
361 if (buffer.size_ < need_write)
363 need_write = buffer.size_;
368 ep_data_in->SetActiveLength(need_write);
常量原始数据封装类。 A class for encapsulating constant raw data.
size_t size_
数据大小(字节)。 The size of the data (in bytes).
const void * addr_
数据存储地址(常量)。 The storage address of the data (constant).
ErrorCode PushBatch(const Data *data, size_t size)
批量推入数据 / Pushes multiple elements into the queue
ErrorCode PopBatch(Data *data, size_t size)
批量弹出数据 / Pops multiple elements from the queue
ReadPort class for handling read operations.
virtual void ProcessPendingReads(bool in_isr)
Processes pending reads.
通用异步收发传输(UART)基类 / Abstract base class for Universal Asynchronous Receiver-Transmitter (UART)
ReadPort * read_port_
读取端口 / Read port
@ NO_PARITY
无校验 / No parity
WritePort * write_port_
写入端口 / Write port
USB CDC ACM (Abstract Control Model) 设备类实现 USB CDC ACM (Abstract Control Model) device class implemen...
virtual void Deinit(EndpointPool &endpoint_pool) override
反初始化CDC设备 Deinitialize CDC device
ErrorCode SendSerialState()
发送串行状态通知 Send serial state notification
bool IsDtrSet() const
检查DTR状态 Check DTR state
USB CDC ACM UART 适配类 USB CDC ACM UART adaptation class.
CDCUart(size_t rx_buffer_size=128, size_t tx_buffer_size=128, size_t tx_queue_size=5, Endpoint::EPNumber data_in_ep_num=Endpoint::EPNumber::EP_AUTO, Endpoint::EPNumber data_out_ep_num=Endpoint::EPNumber::EP_AUTO, Endpoint::EPNumber comm_ep_num=Endpoint::EPNumber::EP_AUTO)
CDCUart 构造函数 / CDCUart constructor.
static ErrorCode ReadFun(ReadPort &port)
读端口回调(占位) Read port callback (placeholder)
LibXR::ReadPort read_port_cdc_
读取端口 / Read port
size_t written_
已写入字节数 / Number of bytes written
void Deinit(EndpointPool &endpoint_pool) override
反初始化 CDC 设备并清理写队列 Deinitialize CDC device and drain TX queue
void OnDataInComplete(bool in_isr, ConstRawData &data) override
数据IN端点传输完成处理 Handle data IN endpoint transfer completion
void OnDataOutComplete(bool in_isr, ConstRawData &data) override
OUT 端点完成:预装下一次接收,并将本次数据推入软件缓冲 Data OUT complete: arm next receive and push to software buffer.
static ErrorCode WriteFun(WritePort &port)
写端口回调(从软件队列取数据并通过 CDC Data IN 端点发送) Write port callback (dequeue and transmit via CDC Data IN endpoin...
bool need_write_zlp_
需要写入ZLP标志 / Need to write ZLP flag
LibXR::WritePort write_port_cdc_
写入端口 / Write port
ErrorCode SetConfig(UART::Configuration cfg) override
设置 UART 配置并同步到 CDC line coding Set UART configuration and sync to CDC line coding
EPNumber
端点号 / Endpoint number
@ EP_AUTO
自动分配端点号 / Auto allocate
USB端点池类 / USB endpoint pool class.
WritePort class for handling write operations.
virtual void Reset()
Resets the WritePort.
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.
UART 配置结构体 / UART configuration structure.
uint8_t stop_bits
停止位长度 / Number of stop bits
Parity parity
校验模式 / Parity mode
uint8_t data_bits
数据位长度 / Number of data bits
uint32_t baudrate
波特率 / Baud rate
ConstRawData data
Data buffer. 数据缓冲区。