libxr
Embedded C++ framework for bare-metal, RTOS and Linux
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(Endpoint::EPNumber data_in_ep_num, Endpoint::EPNumber data_out_ep_num,
44 Endpoint::EPNumber comm_ep_num, LibXR::UART& uart,
45 size_t rx_buffer_size = 128, size_t tx_buffer_size = 128,
46 size_t tx_queue_size = 5)
47 : CDCUart(data_in_ep_num, data_out_ep_num, comm_ep_num, rx_buffer_size,
48 tx_buffer_size, tx_queue_size),
49 rx_buffer_(new uint8_t[rx_buffer_size], rx_buffer_size),
50 tx_buffer_(new uint8_t[tx_buffer_size], tx_buffer_size),
51 uart_(uart)
52 {
53 // CDC->UART:CDC RX 缓冲必须能写入 UART 的 data 队列。
54 // CDC->UART: the UART TX data queue must accept at least rx_buffer_size bytes.
55 ASSERT(uart_.write_port_->Capacity() >= rx_buffer_size);
56
57 // 1) CDC 读完成回调:从 CDC 读一段数据并写入 UART。
58 // 1) CDC read callback: read one chunk from CDC and write it into UART.
59 // Guarded to flatten the bridge pump chain.
61 [](bool in_isr, CDCToUart* cdc_to_uart, ErrorCode)
62 {
63 auto size =
64 LibXR::min(cdc_to_uart->read_port_->Size(), cdc_to_uart->rx_buffer_.size_);
65
66 static ReadOperation op_read_cdc_noblock;
67 [[maybe_unused]] auto ans = cdc_to_uart->Read(
68 {cdc_to_uart->rx_buffer_.addr_, size}, op_read_cdc_noblock, in_isr);
69 DEV_ASSERT_FROM_CALLBACK(ans == ErrorCode::OK, in_isr);
70
71 ans = cdc_to_uart->uart_.Write({cdc_to_uart->rx_buffer_.addr_, size},
72 cdc_to_uart->op_write_uart_, in_isr);
73 DEV_ASSERT_FROM_CALLBACK(ans == ErrorCode::OK, in_isr);
74 },
75 this);
76
78
79 // 2) UART 写完成回调:继续触发下一次 CDC 读。
80 // 2) UART write-complete callback: trigger the next CDC read.
81 // Guarded to flatten the bridge pump chain.
83 [](bool in_isr, CDCToUart* cdc_to_uart, ErrorCode)
84 {
85 [[maybe_unused]] auto ans =
86 cdc_to_uart->Read({nullptr, 0}, cdc_to_uart->op_read_cdc_, in_isr);
87 DEV_ASSERT_FROM_CALLBACK(ans == ErrorCode::OK, in_isr);
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 [[maybe_unused]] auto ans = cdc_to_uart->uart_.Read(
104 {cdc_to_uart->tx_buffer_.addr_, size}, op_read_uart_noblock, in_isr);
105 DEV_ASSERT_FROM_CALLBACK(ans == ErrorCode::OK, in_isr);
106
107 ans = cdc_to_uart->Write({cdc_to_uart->tx_buffer_.addr_, size},
108 cdc_to_uart->op_write_cdc_, in_isr);
109 DEV_ASSERT_FROM_CALLBACK(ans == ErrorCode::OK, in_isr);
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 [[maybe_unused]] auto ans_uart_read =
122 cdc_to_uart->uart_.Read({nullptr, 0}, cdc_to_uart->op_read_uart_, in_isr);
123 DEV_ASSERT_FROM_CALLBACK(ans_uart_read == ErrorCode::OK, in_isr);
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 in_isr, CDCToUart* self, LibXR::UART::Configuration cfg)
133 { self->uart_.SetConfig(cfg, in_isr); }, 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:143
static Callback Create(CallableType fun, BoundArgType arg)
创建回调对象并绑定回调函数与参数 / Create a callback instance with bound function and argument
Definition libxr_cb.hpp:166
static Callback CreateGuarded(CallableType fun, BoundArgType arg)
创建带防重入保护的回调 / Create a guarded callback
Definition libxr_cb.hpp:190
可写原始数据视图 / Mutable raw data view
size_t size_
数据字节数 / Data size in bytes
void * addr_
数据起始地址 / Data start address
size_t Size() const
获取当前排队的数据量 / Get the current queued data size.
Definition read_port.cpp:57
通用异步收发传输(UART)基类 / Abstract base class for Universal Asynchronous Receiver-Transmitter (UART)
Definition uart.hpp:19
ReadPort * read_port_
读取端口 / Read port
Definition uart.hpp:56
virtual ErrorCode SetConfig(Configuration config, bool in_isr=false)=0
设置 UART 配置 / Sets the UART configuration
WritePort * write_port_
写入端口 / Write port
Definition uart.hpp:57
void SetOnSetLineCodingCallback(LibXR::Callback< LibXR::UART::Configuration > cb)
设置线路编码变更回调 Set line coding change callback
Definition cdc_base.hpp:240
CDC 与 UART 双向桥接器(CDC<->UART) / Bidirectional bridge between USB CDC and UART.
LibXR::Callback< ErrorCode > cb_uart_read_
UART 读完成回调 / UART read callback.
LibXR::WriteOperation op_write_uart_
UART 写操作句柄 / UART write operation.
CDCToUart(Endpoint::EPNumber data_in_ep_num, Endpoint::EPNumber data_out_ep_num, Endpoint::EPNumber comm_ep_num, LibXR::UART &uart, size_t rx_buffer_size=128, size_t tx_buffer_size=128, size_t tx_queue_size=5)
构造函数 / Constructor
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:47
EPNumber
端点号 Endpoint number
Definition ep.hpp:42
size_t Capacity() const
获取字节队列总容量 / Get total byte queue capacity.
ErrorCode
定义错误码枚举 / Defines an enumeration for error codes
@ OK
操作成功 / Operation successful
Operation< ErrorCode > ReadOperation
读操作的完成通知描述符 / Read completion descriptor.
constexpr auto min(LeftType a, RightType b) -> std::common_type_t< LeftType, RightType >
计算两个数的最小值 / Computes the minimum of two numbers
Operation< ErrorCode > WriteOperation
写操作的完成通知描述符 / Write completion descriptor.
UART 配置结构体 / UART configuration structure.
Definition uart.hpp:44