libxr  1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
cdc_to_uart.hpp
1#pragma once
2
3#include "cdc_uart.hpp"
4#include "libxr_cb.hpp"
5#include "libxr_rw.hpp"
6#include "uart.hpp"
7
8namespace LibXR::USB
9{
10
26class CDCToUart : public CDCUart
27{
28 public:
43 CDCToUart(LibXR::UART& uart, size_t rx_buffer_size = 128, size_t tx_buffer_size = 128,
44 size_t tx_queue_size = 5,
48 : CDCUart(rx_buffer_size, tx_buffer_size, tx_queue_size, data_in_ep_num,
49 data_out_ep_num, comm_ep_num),
50 rx_buffer_(new uint8_t[rx_buffer_size], rx_buffer_size),
51 tx_buffer_(new uint8_t[tx_buffer_size], tx_buffer_size),
52 uart_(uart)
53 {
54 // CDC->UART:CDC RX 缓冲必须能写入 UART 的 data 队列。
55 // CDC->UART: the UART TX data queue must accept at least rx_buffer_size bytes.
56 ASSERT(uart_.write_port_->queue_data_->MaxSize() >= rx_buffer_size);
57
58 // 1) CDC 读完成回调:从 CDC 读一段数据并写入 UART。
59 // 1) CDC read callback: read one chunk from CDC and write it into UART.
60 // Guarded to flatten the bridge pump chain.
62 [](bool in_isr, CDCToUart* cdc_to_uart, ErrorCode)
63 {
64 auto size =
65 LibXR::min(cdc_to_uart->read_port_->Size(), cdc_to_uart->rx_buffer_.size_);
66
67 static ReadOperation op_read_cdc_noblock;
68 auto ans = cdc_to_uart->Read({cdc_to_uart->rx_buffer_.addr_, size},
69 op_read_cdc_noblock, in_isr);
70 ASSERT(ans == ErrorCode::OK);
71
72 ans = cdc_to_uart->uart_.Write({cdc_to_uart->rx_buffer_.addr_, size},
73 cdc_to_uart->op_write_uart_, in_isr);
74 ASSERT(ans == ErrorCode::OK);
75 },
76 this);
77
79
80 // 2) UART 写完成回调:继续触发下一次 CDC 读。
81 // 2) UART write-complete callback: trigger the next CDC read.
82 // Guarded to flatten the bridge pump chain.
84 [](bool in_isr, CDCToUart* cdc_to_uart, ErrorCode)
85 {
86 auto ans = cdc_to_uart->Read({nullptr, 0}, cdc_to_uart->op_read_cdc_, in_isr);
87 ASSERT(ans == ErrorCode::OK);
88 },
89 this);
90
92
93 // 3) UART 读完成回调:从 UART 读一段数据并写入 CDC。
94 // 3) UART read callback: read one chunk from UART and write it into CDC.
95 // Guarded to flatten the bridge pump chain.
97 [](bool in_isr, CDCToUart* cdc_to_uart, ErrorCode)
98 {
99 auto size = LibXR::min(cdc_to_uart->uart_.read_port_->Size(),
100 cdc_to_uart->tx_buffer_.size_);
101 static ReadOperation op_read_uart_noblock;
102
103 auto ans = cdc_to_uart->uart_.Read({cdc_to_uart->tx_buffer_.addr_, size},
104 op_read_uart_noblock, in_isr);
105 ASSERT(ans == ErrorCode::OK);
106
107 ans = cdc_to_uart->Write({cdc_to_uart->tx_buffer_.addr_, size},
108 cdc_to_uart->op_write_cdc_, in_isr);
109 ASSERT(ans == ErrorCode::OK);
110 },
111 this);
112
114
115 // 4) CDC 写完成回调:继续触发下一次 UART 读。
116 // 4) CDC write-complete callback: trigger the next UART read.
117 // Guarded to flatten the bridge pump chain.
119 [](bool in_isr, CDCToUart* cdc_to_uart, ErrorCode)
120 {
121 auto ans_uart_read =
122 cdc_to_uart->uart_.Read({nullptr, 0}, cdc_to_uart->op_read_uart_, in_isr);
123 ASSERT(ans_uart_read == ErrorCode::OK);
124 },
125 this);
126
128
129 // 5) LineCoding 回调:USB CDC 侧配置变化同步到 UART。
130 // 5) LineCoding callback: forward CDC line-coding changes to UART configuration.
132 [](bool, CDCToUart* self, LibXR::UART::Configuration cfg)
133 { self->uart_.SetConfig(cfg); }, this);
134
136
137 // 启动搬运:挂起一次 CDC 读与 UART 读,以进入回调链。
138 // Kick the pump: schedule one CDC read and one UART read to enter the callback chain.
139 this->Read({nullptr, 0}, op_read_cdc_, false);
140 uart_.Read({nullptr, 0}, op_read_uart_, false);
141 }
142
145
152
155
160
162};
163
164} // namespace LibXR::USB
通用回调包装,支持动态参数传递 / Generic callback wrapper supporting dynamic argument passing
Definition libxr_cb.hpp:142
static Callback Create(CallableType fun, BoundArgType arg)
创建回调对象并绑定回调函数与参数 / Create a callback instance with bound function and argument
Definition libxr_cb.hpp:159
static Callback CreateGuarded(CallableType fun, BoundArgType arg)
创建带防重入保护的回调 / Create a guarded callback
Definition libxr_cb.hpp:179
size_t MaxSize() const
获取队列的最大容量 / Returns the maximum capacity of the queue
原始数据封装类。 A class for encapsulating raw data.
size_t size_
数据大小(字节)。 The size of the data (in bytes).
void * addr_
数据存储地址。 The storage address of the data.
size_t Size()
获取当前队列的已使用大小。 Gets the currently used size of the queue.
Definition libxr_rw.cpp:24
通用异步收发传输(UART)基类 / Abstract base class for Universal Asynchronous Receiver-Transmitter (UART)
Definition uart.hpp:19
ReadPort * read_port_
读取端口 / Read port
Definition uart.hpp:53
virtual ErrorCode SetConfig(Configuration config)=0
设置 UART 配置 / Sets the UART configuration
WritePort * write_port_
写入端口 / Write port
Definition uart.hpp:54
void SetOnSetLineCodingCallback(LibXR::Callback< LibXR::UART::Configuration > cb)
设置线路编码变更回调 Set line coding change callback
Definition cdc_base.hpp:238
CDC 与 UART 双向桥接器(CDC<->UART) / Bidirectional bridge between USB CDC and UART.
CDCToUart(LibXR::UART &uart, 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)
构造函数 / Constructor
LibXR::Callback< ErrorCode > cb_uart_read_
UART 读完成回调 / UART read callback.
LibXR::WriteOperation op_write_uart_
UART 写操作句柄 / UART write operation.
LibXR::ReadOperation op_read_uart_
UART 读操作句柄 / UART read operation.
LibXR::ReadOperation op_read_cdc_
CDC 读操作句柄 / CDC read operation.
LibXR::Callback< ErrorCode > cb_cdc_write_
CDC 写完成回调 / CDC write-complete callback.
RawData tx_buffer_
UART->CDC 临时缓存 / Temp buffer for UART->CDC.
RawData rx_buffer_
CDC->UART 临时缓存 / Temp buffer for CDC->UART.
LibXR::Callback< LibXR::UART::Configuration > set_line_coding_cb_
LineCoding 同步回调 / Line coding sync callback.
LibXR::Callback< ErrorCode > cb_uart_write_
UART 写完成回调 / UART write-complete callback.
LibXR::Callback< ErrorCode > cb_read_cdc_
CDC 读完成回调 / CDC read callback.
LibXR::WriteOperation op_write_cdc_
CDC 写操作句柄 / CDC write operation.
LibXR::UART & uart_
被桥接的 UART 引用 / Bridged UART reference
USB CDC-ACM UART 适配器 USB CDC-ACM UART adapter.
Definition cdc_uart.hpp:223
EPNumber
端点号 Endpoint number
Definition ep.hpp:42
@ EP_AUTO
自动分配端点号 / Auto allocate
ErrorCode
定义错误码枚举
@ OK
操作成功 | Operation successful
Operation< ErrorCode > ReadOperation
Read operation type.
Definition libxr_rw.hpp:344
constexpr auto min(LeftType a, RightType b) -> std::common_type_t< LeftType, RightType >
计算两个数的最小值
Operation< ErrorCode > WriteOperation
Write operation type.
Definition libxr_rw.hpp:348
UART 配置结构体 / UART configuration structure.
Definition uart.hpp:44