libxr  1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
tinyusb_virtual_uart.cpp
1#include "tinyusb_virtual_uart.hpp"
2
3using namespace LibXR;
4
9extern "C" void tud_cdc_rx_cb(uint8_t itf)
10{
11 UNUSED(itf);
12 if (TinyUSBVirtualUART::self)
13 {
14 auto &port = TinyUSBVirtualUART::self->_read_port;
15 size_t avail = tud_cdc_available();
16 if (avail > 0)
17 {
18 port.ProcessPendingReads(true);
19 }
20 }
21}
22
23// ==================== TinyUSBUARTReadPort ====================
24
30{
31 // TinyUSB FIFO最大容量 - 已有数据
32 return CFG_TUD_CDC_RX_BUFSIZE - tud_cdc_available();
33}
34
39size_t TinyUSBUARTReadPort::Size() { return tud_cdc_available(); }
40
46{
47 BusyState curr = busy_.load(std::memory_order_relaxed);
48
49 if (curr == BusyState::Pending)
50 {
51 if (Size() >= info_.data.size_)
52 {
53 int len = tud_cdc_read(static_cast<uint8_t *>(info_.data.addr_), info_.data.size_);
54 busy_.store(BusyState::Idle, std::memory_order_release);
55
56 if (len == static_cast<int>(info_.data.size_))
57 {
58 Finish(in_isr, ErrorCode::OK, info_, info_.data.size_);
59 }
60 else
61 {
62 Finish(in_isr, ErrorCode::EMPTY, info_, len);
63 }
64 }
65 }
66 else if (curr == BusyState::Idle)
67 {
68 busy_.store(BusyState::Event, std::memory_order_release);
69 }
70}
71
72// ==================== TinyUSBUARTWritePort ====================
73
78size_t TinyUSBUARTWritePort::EmptySize() { return tud_cdc_write_available(); }
79
85{
86 return CFG_TUD_CDC_TX_BUFSIZE - tud_cdc_write_available();
87}
88
89// ==================== TinyUSBVirtualUART ====================
90
96 : UART(&_read_port, &_write_port), _read_port(this), _write_port(this)
97{
98 self = this;
99 _read_port = ReadFun;
100 _write_port = WriteFun;
101 tusb_init();
102}
103
110{
111 WriteInfoBlock info;
112 if (port.queue_info_->Pop(info) != ErrorCode::OK) return ErrorCode::EMPTY;
113
114 size_t space = tud_cdc_write_available();
115 if (space < info.data.size_) return ErrorCode::FULL;
116
117 size_t written =
118 tud_cdc_write(static_cast<const uint8_t *>(info.data.addr_), info.data.size_);
119 tud_cdc_write_flush();
120
121 if (written == info.data.size_)
122 {
123 port.Finish(false, ErrorCode::OK, info, written);
124 return ErrorCode::OK;
125 }
126 else
127 {
128 port.Finish(false, ErrorCode::FAILED, info, written);
129 return ErrorCode::FAILED;
130 }
131}
132
139{
140 if (tud_cdc_available() >= port.info_.data.size_)
141 {
142 int len = tud_cdc_read(static_cast<uint8_t *>(port.info_.data.addr_),
143 port.info_.data.size_);
144 port.read_size_ = len;
145 return ErrorCode::OK;
146 }
147 return ErrorCode::EMPTY;
148}
149
156{
157 // CDC无须配置
158 return ErrorCode::OK;
159}
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.
Definition libxr_rw.hpp:270
void Finish(bool in_isr, ErrorCode ans, ReadInfoBlock &info, uint32_t size)
更新读取操作的状态。 Updates the status of the read operation.
Definition libxr_rw.hpp:371
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)
Definition uart.hpp:19
WritePort class for handling write operations.
Definition libxr_rw.hpp:564
void Finish(bool in_isr, ErrorCode ans, WriteInfoBlock &info, uint32_t size)
更新写入操作的状态。 Updates the status of the write operation.
Definition libxr_rw.hpp:665
LibXR 命名空间
Definition ch32_gpio.hpp:9
ErrorCode(* ReadFun)(ReadPort &port)
Function pointer type for read operations.
Definition libxr_rw.hpp:247
ErrorCode(* WriteFun)(WritePort &port)
Function pointer type for write operations.
Definition libxr_rw.hpp:243
RawData data
Data buffer. 数据缓冲区。
Definition libxr_rw.hpp:255
UART 配置结构体 / UART configuration structure.
Definition uart.hpp:44