libxr  1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
LibXR::USB::Endpoint Class Referenceabstract

USB 端点基类 / USB Endpoint base class. More...

#include <ep.hpp>

Inheritance diagram for LibXR::USB::Endpoint:
[legend]
Collaboration diagram for LibXR::USB::Endpoint:
[legend]

Data Structures

struct  Config
 端点配置参数 / Endpoint configuration parameters More...
 

Public Types

enum class  Direction : uint8_t { OUT = 0 , IN = 1 , BOTH = 2 }
 端点方向 Endpoint direction More...
 
enum class  EPNumber : uint8_t {
  EP0 = 0 , EP1 = 1 , EP2 = 2 , EP3 = 3 ,
  EP4 = 4 , EP5 = 5 , EP6 = 6 , EP7 = 7 ,
  EP8 = 8 , EP9 = 9 , EP10 = 10 , EP11 = 11 ,
  EP12 = 12 , EP13 = 13 , EP14 = 14 , EP15 = 15 ,
  EP_MAX_NUM = 16 , EP_AUTO = 0xFE , EP_INVALID = 0xFF
}
 端点号 Endpoint number More...
 
enum class  Type : uint8_t { CONTROL = 0 , ISOCHRONOUS = 1 , BULK = 2 , INTERRUPT = 3 }
 端点类型 Endpoint type More...
 
enum class  State : uint8_t {
  DISABLED , IDLE , BUSY , STALLED ,
  ERROR
}
 端点状态 Endpoint state More...
 

Public Member Functions

 Endpoint (EPNumber number, Direction dir, RawData buffer)
 构造函数 / Constructor
 
virtual ~Endpoint ()=default
 虚析构函数 / Virtual destructor
 
 Endpoint (const Endpoint &)=delete
 
Endpointoperator= (const Endpoint &)=delete
 
EPNumber GetNumber () const
 获取端点号 / Get endpoint number
 
Direction AvailableDirection () const
 获取允许配置的方向 / Get allowed endpoint direction
 
Direction GetDirection () const
 获取当前端点方向 / Get current endpoint direction
 
uint8_t GetAddress () const
 获取端点地址(方向 + 号) / Get endpoint address (dir + num)
 
State GetState () const
 获取端点状态 / Get endpoint state
 
void SetState (State state)
 设置端点状态 / Set endpoint state
 
Type GetType () const
 获取端点类型 / Get endpoint type
 
uint16_t MaxPacketSize () const
 获取最大包长 / Get max packet size
 
bool IsStalled () const
 是否处于 STALL 状态 / Whether endpoint is stalled
 
bool UseDoubleBuffer () const
 是否启用双缓冲 / Whether double buffer is enabled
 
RawData GetBuffer () const
 获取当前可用于传输的缓冲区 / Get current transfer buffer
 
void SetOnTransferCompleteCallback (Callback< ConstRawData & > cb)
 设置传输完成回调 / Set transfer complete callback
 
void SetActiveLength (uint16_t len)
 设置当前活动缓冲区有效长度 / Set active buffer valid length
 
size_t GetActiveLength ()
 获取当前活动缓冲区有效长度 / Get active buffer valid length
 
virtual size_t MaxTransferSize () const
 返回当前最大可传输字节数 / Return maximum transferable size at this time
 
virtual void Configure (const Config &cfg)=0
 配置端点协议参数 / Configure endpoint protocol parameters
 
virtual void Close ()=0
 关闭端点 / Close endpoint
 
virtual ErrorCode Stall ()=0
 置 STALL / Stall endpoint
 
virtual ErrorCode ClearStall ()=0
 清除 STALL / Clear stall
 
virtual ErrorCode Transfer (size_t size)=0
 启动一次传输 / Start a transfer
 
virtual ErrorCode TransferMultiBulk (RawData &data)
 Bulk 多包传输辅助接口 / Helper for multi-packet bulk transfer.
 
virtual ErrorCode TransferZLP ()
 发送/接收 ZLP(零长度包) / Transfer zero length packet (ZLP)
 
void OnTransferCompleteCallback (bool in_isr, size_t actual_transfer_size)
 由底层在传输完成时调用 / Called by low-level driver when transfer completes
 

Static Public Member Functions

static constexpr uint8_t EPNumberToInt8 (EPNumber ep)
 端点号转换为 uint8_t / Convert endpoint number to uint8_t
 
static constexpr uint8_t EPNumberToAddr (EPNumber ep, Direction dir)
 端点号转换为端点地址 / Convert endpoint number to endpoint address
 
static constexpr EPNumber AddrToEPNumber (uint8_t addr, Direction &dir)
 端点地址转换为端点号 / Convert endpoint address to endpoint number
 
static constexpr EPNumber NextEPNumber (EPNumber ep)
 获取下一个端点号 / Get the next endpoint number
 

Protected Member Functions

ConfigGetConfig ()
 获取当前配置引用 / Get endpoint config reference
 
virtual void SwitchBuffer ()
 切换双缓冲 / Switch double buffer
 
virtual void SetActiveBlock (bool active_block)
 设置当前活动缓冲块 / Set active buffer block
 

Private Attributes

LibXR::Callback< LibXR::ConstRawData & > on_transfer_complete_
 传输完成回调 / Transfer complete callback
 
EPNumber number_
 端点号 / Endpoint number
 
Direction avail_direction_
 可配置方向 / Allowed direction
 
Config config_
 当前配置 / Current configuration
 
State state_ = State::DISABLED
 当前状态 / Current state
 
LibXR::RawData buffer_
 端点缓冲区 / Endpoint buffer
 
LibXR::DoubleBuffer double_buffer_
 双缓冲管理 / Double buffer manager
 
bool multi_bulk_ = false
 多包 bulk 状态机使能 / Multi-bulk state enabled
 
RawData multi_bulk_data_
 多包 bulk 应用层 buffer / App buffer for multi-bulk
 
size_t multi_bulk_remain_
 多包 bulk 剩余字节数 / Remaining bytes for multi-bulk
 

Detailed Description

USB 端点基类 / USB Endpoint base class.

用于描述与操作 USB 端点,提供端点配置、传输控制、状态管理与回调接口。 Used for describing and manipulating USB endpoints, providing endpoint configuration, transfer control, state management, and callback interfaces.

Definition at line 23 of file ep.hpp.

Member Enumeration Documentation

◆ Direction

enum class LibXR::USB::Endpoint::Direction : uint8_t
strong

端点方向 Endpoint direction

Enumerator
OUT 

输出方向 / OUT direction

IN 

输入方向 / IN direction

BOTH 

双向(可配置为 IN/OUT) / Both (configurable as IN/OUT)

Definition at line 30 of file ep.hpp.

31 {
32 OUT = 0,
33 IN = 1,
34 BOTH = 2
35 };
@ BOTH
双向(可配置为 IN/OUT) / Both (configurable as IN/OUT)
@ IN
输入方向 / IN direction
@ OUT
输出方向 / OUT direction

◆ EPNumber

enum class LibXR::USB::Endpoint::EPNumber : uint8_t
strong

端点号 Endpoint number

Enumerator
EP0 

端点 0 / Endpoint 0

EP1 

端点 1 / Endpoint 1

EP2 

端点 2 / Endpoint 2

EP3 

端点 3 / Endpoint 3

EP4 

端点 4 / Endpoint 4

EP5 

端点 5 / Endpoint 5

EP6 

端点 6 / Endpoint 6

EP7 

端点 7 / Endpoint 7

EP8 

端点 8 / Endpoint 8

EP9 

端点 9 / Endpoint 9

EP10 

端点 10 / Endpoint 10

EP11 

端点 11 / Endpoint 11

EP12 

端点 12 / Endpoint 12

EP13 

端点 13 / Endpoint 13

EP14 

端点 14 / Endpoint 14

EP15 

端点 15 / Endpoint 15

EP_MAX_NUM 

端点数量上限 / Maximum number of endpoints

EP_AUTO 

自动分配端点号 / Auto allocate

EP_INVALID 

非法端点号 / Invalid endpoint

Definition at line 41 of file ep.hpp.

42 {
43 EP0 = 0,
44 EP1 = 1,
45 EP2 = 2,
46 EP3 = 3,
47 EP4 = 4,
48 EP5 = 5,
49 EP6 = 6,
50 EP7 = 7,
51 EP8 = 8,
52 EP9 = 9,
53 EP10 = 10,
54 EP11 = 11,
55 EP12 = 12,
56 EP13 = 13,
57 EP14 = 14,
58 EP15 = 15,
59 EP_MAX_NUM = 16,
60 EP_AUTO = 0xFE,
61 EP_INVALID = 0xFF
62 };
@ EP0
端点 0 / Endpoint 0
@ EP11
端点 11 / Endpoint 11
@ EP5
端点 5 / Endpoint 5
@ EP14
端点 14 / Endpoint 14
@ EP1
端点 1 / Endpoint 1
@ EP_AUTO
自动分配端点号 / Auto allocate
@ EP8
端点 8 / Endpoint 8
@ EP2
端点 2 / Endpoint 2
@ EP7
端点 7 / Endpoint 7
@ EP4
端点 4 / Endpoint 4
@ EP15
端点 15 / Endpoint 15
@ EP_MAX_NUM
端点数量上限 / Maximum number of endpoints
@ EP13
端点 13 / Endpoint 13
@ EP10
端点 10 / Endpoint 10
@ EP3
端点 3 / Endpoint 3
@ EP6
端点 6 / Endpoint 6
@ EP9
端点 9 / Endpoint 9
@ EP_INVALID
非法端点号 / Invalid endpoint
@ EP12
端点 12 / Endpoint 12

◆ State

enum class LibXR::USB::Endpoint::State : uint8_t
strong

端点状态 Endpoint state

Enumerator
DISABLED 

禁用 / Disabled

IDLE 

空闲 / Idle

BUSY 

忙 / Busy

STALLED 

停止/挂起 / Stalled

ERROR 

错误 / Error

Definition at line 80 of file ep.hpp.

81 {
82 DISABLED,
83 IDLE,
84 BUSY,
85 STALLED,
86 ERROR
87 };
@ DISABLED
禁用 / Disabled
@ STALLED
停止/挂起 / Stalled

◆ Type

enum class LibXR::USB::Endpoint::Type : uint8_t
strong

端点类型 Endpoint type

Enumerator
CONTROL 

控制端点 / Control

ISOCHRONOUS 

等时端点 / Isochronous

BULK 

批量端点 / Bulk

INTERRUPT 

中断端点 / Interrupt

Definition at line 68 of file ep.hpp.

69 {
70 CONTROL = 0,
71 ISOCHRONOUS = 1,
72 BULK = 2,
73 INTERRUPT = 3
74 };
@ ISOCHRONOUS
等时端点 / Isochronous
@ BULK
批量端点 / Bulk
@ INTERRUPT
中断端点 / Interrupt
@ CONTROL
控制端点 / Control

Constructor & Destructor Documentation

◆ Endpoint()

LibXR::USB::Endpoint::Endpoint ( EPNumber number,
Direction dir,
RawData buffer )
inlineexplicit

构造函数 / Constructor

Parameters
number端点号 / Endpoint number
dir允许配置的方向 / Allowed direction
buffer端点缓冲区 / Endpoint buffer

Definition at line 153 of file ep.hpp.

154 : number_(number), avail_direction_(dir), buffer_(buffer), double_buffer_(buffer)
155 {
156 }
LibXR::DoubleBuffer double_buffer_
双缓冲管理 / Double buffer manager
Definition ep.hpp:547
Direction avail_direction_
可配置方向 / Allowed direction
Definition ep.hpp:543
EPNumber number_
端点号 / Endpoint number
Definition ep.hpp:542
LibXR::RawData buffer_
端点缓冲区 / Endpoint buffer
Definition ep.hpp:546

Member Function Documentation

◆ AddrToEPNumber()

static constexpr EPNumber LibXR::USB::Endpoint::AddrToEPNumber ( uint8_t addr,
Direction & dir )
inlinestaticconstexpr

端点地址转换为端点号 / Convert endpoint address to endpoint number

Parameters
addr端点地址 / Endpoint address
dir输出方向(输出参数) / Output direction (output)
Returns
EPNumber 端点号 / Endpoint number

Definition at line 117 of file ep.hpp.

118 {
119 dir = addr & 0x80 ? Direction::IN : Direction::OUT;
120 return static_cast<EPNumber>(addr & 0x7F);
121 }
EPNumber
端点号 Endpoint number
Definition ep.hpp:42

◆ AvailableDirection()

Direction LibXR::USB::Endpoint::AvailableDirection ( ) const
inline

获取允许配置的方向 / Get allowed endpoint direction

Returns
Direction 允许方向 / Allowed direction

Definition at line 176 of file ep.hpp.

176{ return avail_direction_; }

◆ ClearStall()

virtual ErrorCode LibXR::USB::Endpoint::ClearStall ( )
pure virtual

清除 STALL / Clear stall

Returns
ErrorCode 错误码 / Error code

Implemented in LibXR::STM32Endpoint.

◆ Close()

virtual void LibXR::USB::Endpoint::Close ( )
pure virtual

关闭端点 / Close endpoint

Implemented in LibXR::STM32Endpoint.

◆ Configure()

virtual void LibXR::USB::Endpoint::Configure ( const Config & cfg)
pure virtual

配置端点协议参数 / Configure endpoint protocol parameters

Parameters
cfg配置参数 / Configuration parameters

Implemented in LibXR::STM32Endpoint.

◆ EPNumberToAddr()

static constexpr uint8_t LibXR::USB::Endpoint::EPNumberToAddr ( EPNumber ep,
Direction dir )
inlinestaticconstexpr

端点号转换为端点地址 / Convert endpoint number to endpoint address

Parameters
ep端点号 / Endpoint number
dir端点方向 / Endpoint direction
Returns
uint8_t 端点地址 / Endpoint address

Definition at line 105 of file ep.hpp.

106 {
107 ASSERT(dir == Direction::IN || dir == Direction::OUT);
108 return static_cast<uint8_t>(ep) | (dir == Direction::IN ? 0x80 : 0x00);
109 }

◆ EPNumberToInt8()

static constexpr uint8_t LibXR::USB::Endpoint::EPNumberToInt8 ( EPNumber ep)
inlinestaticconstexpr

端点号转换为 uint8_t / Convert endpoint number to uint8_t

Parameters
ep端点号 / Endpoint number
Returns
uint8_t 端点号数值 / Endpoint number value

Definition at line 94 of file ep.hpp.

95 {
96 return static_cast<uint8_t>(ep);
97 }

◆ GetActiveLength()

size_t LibXR::USB::Endpoint::GetActiveLength ( )
inline

获取当前活动缓冲区有效长度 / Get active buffer valid length

Returns
size_t 有效长度 / Valid length

Definition at line 277 of file ep.hpp.

size_t GetActiveLength() const
获取当前活动缓冲区中准备好的数据长度 Gets the size of valid data in active buffer

◆ GetAddress()

uint8_t LibXR::USB::Endpoint::GetAddress ( ) const
inline

获取端点地址(方向 + 号) / Get endpoint address (dir + num)

Returns
uint8_t 端点地址 / Endpoint address

Definition at line 195 of file ep.hpp.

196 {
197 if (state_ == State::DISABLED)
198 {
199 return EPNumberToInt8(number_) & 0x0F;
200 }
202 }
static constexpr uint8_t EPNumberToInt8(EPNumber ep)
端点号转换为 uint8_t / Convert endpoint number to uint8_t
Definition ep.hpp:94
static constexpr uint8_t EPNumberToAddr(EPNumber ep, Direction dir)
端点号转换为端点地址 / Convert endpoint number to endpoint address
Definition ep.hpp:105
State state_
当前状态 / Current state
Definition ep.hpp:545
Config config_
当前配置 / Current configuration
Definition ep.hpp:544
Direction direction
端点方向 / Endpoint direction
Definition ep.hpp:140

◆ GetBuffer()

RawData LibXR::USB::Endpoint::GetBuffer ( ) const
inline

获取当前可用于传输的缓冲区 / Get current transfer buffer

Returns
RawData 缓冲区视图 / Buffer view

Definition at line 246 of file ep.hpp.

247 {
249 {
251 }
252 else
253 {
254 return buffer_;
255 }
256 }
size_t Size() const
获取每个缓冲区的大小(单位:字节) Gets the size of each buffer in bytes
uint8_t * ActiveBuffer() const
获取当前正在使用的缓冲区指针 Returns the currently active buffer
bool double_buffer
是否启用双缓冲 / Enable double buffer
Definition ep.hpp:143

◆ GetConfig()

Config & LibXR::USB::Endpoint::GetConfig ( )
inlineprotected

获取当前配置引用 / Get endpoint config reference

Returns
Config& 配置引用 / Configuration reference

Definition at line 516 of file ep.hpp.

516{ return config_; }

◆ GetDirection()

Direction LibXR::USB::Endpoint::GetDirection ( ) const
inline

获取当前端点方向 / Get current endpoint direction

Returns
Direction 当前方向 / Current direction

Definition at line 182 of file ep.hpp.

183 {
184 if (state_ == State::DISABLED)
185 {
186 return AvailableDirection();
187 }
188 return config_.direction;
189 }
Direction AvailableDirection() const
获取允许配置的方向 / Get allowed endpoint direction
Definition ep.hpp:176

◆ GetNumber()

EPNumber LibXR::USB::Endpoint::GetNumber ( ) const
inline

获取端点号 / Get endpoint number

Returns
EPNumber 端点号 / Endpoint number

Definition at line 170 of file ep.hpp.

170{ return number_; }

◆ GetState()

State LibXR::USB::Endpoint::GetState ( ) const
inline

获取端点状态 / Get endpoint state

Returns
State 当前状态 / Current state

Definition at line 208 of file ep.hpp.

208{ return state_; }

◆ GetType()

Type LibXR::USB::Endpoint::GetType ( ) const
inline

获取端点类型 / Get endpoint type

Returns
Type 端点类型 / Endpoint type

Definition at line 220 of file ep.hpp.

220{ return config_.type; }
Type type
端点类型 / Endpoint type
Definition ep.hpp:141

◆ IsStalled()

bool LibXR::USB::Endpoint::IsStalled ( ) const
inline

是否处于 STALL 状态 / Whether endpoint is stalled

Returns
true 已 STALL / Stalled
false 非 STALL / Not stalled

Definition at line 233 of file ep.hpp.

233{ return state_ == State::STALLED; }

◆ MaxPacketSize()

uint16_t LibXR::USB::Endpoint::MaxPacketSize ( ) const
inline

获取最大包长 / Get max packet size

Returns
uint16_t 最大包长 / Max packet size

Definition at line 226 of file ep.hpp.

226{ return config_.max_packet_size; }
uint16_t max_packet_size
最大包长 / Max packet size
Definition ep.hpp:142

◆ MaxTransferSize()

virtual size_t LibXR::USB::Endpoint::MaxTransferSize ( ) const
inlinevirtual

返回当前最大可传输字节数 / Return maximum transferable size at this time

Returns
size_t 最大可传输字节数 / Maximum transferable bytes

Reimplemented in LibXR::STM32Endpoint.

Definition at line 283 of file ep.hpp.

283{ return MaxPacketSize(); }
uint16_t MaxPacketSize() const
获取最大包长 / Get max packet size
Definition ep.hpp:226

◆ NextEPNumber()

static constexpr EPNumber LibXR::USB::Endpoint::NextEPNumber ( EPNumber ep)
inlinestaticconstexpr

获取下一个端点号 / Get the next endpoint number

Parameters
ep当前端点号 / Current endpoint number
Returns
EPNumber 下一个端点号 / Next endpoint number

Definition at line 128 of file ep.hpp.

129 {
130 ASSERT(ep <= EPNumber::EP15);
131 return static_cast<EPNumber>(EPNumberToInt8(ep) + 1);
132 }

◆ OnTransferCompleteCallback()

void LibXR::USB::Endpoint::OnTransferCompleteCallback ( bool in_isr,
size_t actual_transfer_size )
inline

由底层在传输完成时调用 / Called by low-level driver when transfer completes

Parameters
in_isr是否在中断上下文 / Whether in ISR context
actual_transfer_size实际传输长度 / Actual transferred size

Definition at line 393 of file ep.hpp.

394 {
395 if (GetState() != State::BUSY)
396 {
397 return;
398 }
399
400 bool callback_uses_app_buffer = false;
401 bool out_switched_before_cb = false;
402
403 const Direction DIR = GetDirection();
404 const size_t MAX_CHUNK = MaxTransferSize();
405 const bool DB = UseDoubleBuffer();
406
407 if (multi_bulk_)
408 {
409 if (DIR == Direction::IN)
410 {
411 if (multi_bulk_remain_ > 0)
412 {
413 auto ep_buf = GetBuffer(); // 此时应是“下一块 Active”(因为 Transfer 已切换)
414 const size_t SENT = multi_bulk_data_.size_ - multi_bulk_remain_;
415
416 size_t chunk = MAX_CHUNK;
417 if (chunk > multi_bulk_remain_) chunk = multi_bulk_remain_;
418
419 auto src = static_cast<const uint8_t*>(multi_bulk_data_.addr_) + SENT;
420 auto dst = static_cast<uint8_t*>(ep_buf.addr_);
421 Memory::FastCopy(dst, src, chunk);
422 multi_bulk_remain_ -= chunk;
423
425 (void)Transfer(chunk);
426 return;
427 }
428
429 // 结束:对上层报告 app buffer
430 multi_bulk_ = false;
431 callback_uses_app_buffer = true;
432 actual_transfer_size = multi_bulk_data_.size_;
433 }
434 else
435 {
436 // OUT:完成时 Active 里是数据
437 auto ep_buf = GetBuffer();
438 size_t prev_remain = multi_bulk_remain_;
439 size_t recvd = actual_transfer_size;
440 if (recvd > prev_remain)
441 {
442 recvd = prev_remain;
443 }
444
445 const size_t OFFSET = multi_bulk_data_.size_ - prev_remain;
446
447 auto dst = static_cast<uint8_t*>(multi_bulk_data_.addr_) + OFFSET;
448 auto src = static_cast<const uint8_t*>(ep_buf.addr_);
449 Memory::FastCopy(dst, src, recvd);
450
451 multi_bulk_remain_ = prev_remain - recvd;
452
453 const bool SHORT_PACKET = (recvd < MAX_CHUNK);
454 const bool BUFFER_FULL = (multi_bulk_remain_ == 0);
455
456 if (DB)
457 {
458 SwitchBuffer(); // 切换后 Pending = 刚接收的包;Active = 下次用
459 out_switched_before_cb = true;
460 }
461
462 if (!SHORT_PACKET && !BUFFER_FULL)
463 {
464 size_t chunk = MAX_CHUNK;
465 if (chunk > multi_bulk_remain_)
466 {
467 chunk = multi_bulk_remain_;
468 }
469
471 (void)Transfer(chunk); // 下一包将落到新的 Active(
472 return;
473 }
474
475 multi_bulk_ = false;
476 callback_uses_app_buffer = true;
477 actual_transfer_size = multi_bulk_data_.size_ - multi_bulk_remain_;
478 }
479 }
480
481 // 非 multi-bulk:OUT 也必须“回调前切换”
482 if (!multi_bulk_ && DB && DIR == Direction::OUT && !out_switched_before_cb)
483 {
484 SwitchBuffer();
485 out_switched_before_cb = true;
486 }
487
489
490 ConstRawData data;
491 if (callback_uses_app_buffer)
492 {
493 data = ConstRawData(multi_bulk_data_.addr_, actual_transfer_size);
494 }
495 else
496 {
497 if (DB)
498 {
499 // 回调里永远取 Pending = 刚刚完成的那包(IN/OUT 一致)
500 data = ConstRawData(double_buffer_.PendingBuffer(), actual_transfer_size);
501 }
502 else
503 {
504 data = ConstRawData(buffer_.addr_, actual_transfer_size);
505 }
506 }
507
508 on_transfer_complete_.Run(in_isr, data);
509 }
uint8_t * PendingBuffer() const
获取备用缓冲区的指针 Returns the pending (inactive) buffer
static void FastCopy(void *dst, const void *src, size_t size)
快速内存拷贝 / Fast memory copy
Definition libxr_mem.cpp:5
size_t size_
数据大小(字节)。 The size of the data (in bytes).
void * addr_
数据存储地址。 The storage address of the data.
RawData multi_bulk_data_
多包 bulk 应用层 buffer / App buffer for multi-bulk
Definition ep.hpp:550
Direction
端点方向 Endpoint direction
Definition ep.hpp:31
Direction GetDirection() const
获取当前端点方向 / Get current endpoint direction
Definition ep.hpp:182
bool UseDoubleBuffer() const
是否启用双缓冲 / Whether double buffer is enabled
Definition ep.hpp:240
virtual void SwitchBuffer()
切换双缓冲 / Switch double buffer
Definition ep.hpp:521
virtual size_t MaxTransferSize() const
返回当前最大可传输字节数 / Return maximum transferable size at this time
Definition ep.hpp:283
bool multi_bulk_
多包 bulk 状态机使能 / Multi-bulk state enabled
Definition ep.hpp:549
LibXR::Callback< LibXR::ConstRawData & > on_transfer_complete_
传输完成回调 / Transfer complete callback
Definition ep.hpp:540
void SetState(State state)
设置端点状态 / Set endpoint state
Definition ep.hpp:214
State GetState() const
获取端点状态 / Get endpoint state
Definition ep.hpp:208
virtual ErrorCode Transfer(size_t size)=0
启动一次传输 / Start a transfer
RawData GetBuffer() const
获取当前可用于传输的缓冲区 / Get current transfer buffer
Definition ep.hpp:246
size_t multi_bulk_remain_
多包 bulk 剩余字节数 / Remaining bytes for multi-bulk
Definition ep.hpp:551

◆ SetActiveBlock()

virtual void LibXR::USB::Endpoint::SetActiveBlock ( bool active_block)
inlineprotectedvirtual

设置当前活动缓冲块 / Set active buffer block

Parameters
active_blocktrue 使用第二块;false 使用第一块 / true selects second block; false selects first block

Definition at line 532 of file ep.hpp.

533 {
534 double_buffer_.SetActiveBlock(active_block);
536 }
void SetActiveBlock(bool block)
设置当前活动缓冲区 Sets the active buffer
void EnablePending()
手动启用 pending 状态 Manually sets the pending state to true

◆ SetActiveLength()

void LibXR::USB::Endpoint::SetActiveLength ( uint16_t len)
inline

设置当前活动缓冲区有效长度 / Set active buffer valid length

Parameters
len有效长度 / Valid length

Definition at line 271 of file ep.hpp.

void SetActiveLength(size_t length)
设置当前活动缓冲区的数据长度 Sets the size of the active buffer

◆ SetOnTransferCompleteCallback()

void LibXR::USB::Endpoint::SetOnTransferCompleteCallback ( Callback< ConstRawData & > cb)
inline

设置传输完成回调 / Set transfer complete callback

Parameters
cb回调函数 / Callback

Definition at line 262 of file ep.hpp.

263 {
265 }

◆ SetState()

void LibXR::USB::Endpoint::SetState ( State state)
inline

设置端点状态 / Set endpoint state

Parameters
state状态 / State

Definition at line 214 of file ep.hpp.

214{ state_ = state; }

◆ Stall()

virtual ErrorCode LibXR::USB::Endpoint::Stall ( )
pure virtual

置 STALL / Stall endpoint

Returns
ErrorCode 错误码 / Error code

Implemented in LibXR::STM32Endpoint.

◆ SwitchBuffer()

virtual void LibXR::USB::Endpoint::SwitchBuffer ( )
inlineprotectedvirtual

切换双缓冲 / Switch double buffer

Definition at line 521 of file ep.hpp.

522 {
525 }
void Switch()
切换到备用缓冲区(若其有效) Switches to the pending buffer if it's valid

◆ Transfer()

virtual ErrorCode LibXR::USB::Endpoint::Transfer ( size_t size)
pure virtual

启动一次传输 / Start a transfer

Parameters
size传输长度 / Transfer size
Returns
ErrorCode 错误码 / Error code

Implemented in LibXR::STM32Endpoint.

◆ TransferMultiBulk()

virtual ErrorCode LibXR::USB::Endpoint::TransferMultiBulk ( RawData & data)
inlinevirtual

Bulk 多包传输辅助接口 / Helper for multi-packet bulk transfer.

Parameters
data应用层缓冲区(IN:待发送数据;OUT:接收缓冲区) / App buffer (IN: data to send; OUT: receive buffer)
Returns
ErrorCode 错误码 / Error code

Definition at line 321 of file ep.hpp.

322 {
323 auto ep_buf = GetBuffer();
324 size_t max_chunk = MaxTransferSize();
325
326 ASSERT(max_chunk > 0);
327
328 // 单包就能搞定的情况:不进入 multi-bulk 状态机,保持原有行为
329 if (data.size_ <= max_chunk)
330 {
332 {
333 multi_bulk_ = false;
335 multi_bulk_data_ = {nullptr, 0};
336
337 auto src = static_cast<const uint8_t*>(data.addr_);
338 auto dst = static_cast<uint8_t*>(ep_buf.addr_);
339 Memory::FastCopy(dst, src, data.size_);
340
341 return Transfer(data.size_);
342 }
343
344 // OUT:接收——为了把数据回填到 data,单包也走 multi-bulk
346 {
347 multi_bulk_ = true;
348 multi_bulk_data_ = data;
349 multi_bulk_remain_ = data.size_; // OUT: 剩余可写容量
350 return Transfer(data.size_);
351 }
352
353 return ErrorCode::ARG_ERR;
354 }
355
356 // 需要多包处理的情况
357 multi_bulk_ = true;
358 multi_bulk_data_ = data; // 应用层 buffer 指针 + 最大容量 / 逻辑总长
359 multi_bulk_remain_ = data.size_; // IN: 剩余待发送;OUT: 剩余可写容量
360
361 // 第一包大小
362 size_t first = max_chunk;
363 if (first > multi_bulk_remain_)
364 {
365 first = multi_bulk_remain_;
366 }
367
369 {
370 // IN:发送时,先把第一 chunk 拷到 EP buffer
371 auto src = static_cast<const uint8_t*>(multi_bulk_data_.addr_);
372 auto dst = static_cast<uint8_t*>(ep_buf.addr_);
373 Memory::FastCopy(dst, src, first);
374
375 multi_bulk_remain_ -= first; // 已经准备好 first 字节要发
376 }
377 // OUT:接收时,先启动一次接收,稍后在回调里拷贝到 multi_bulk_data_
378
379 return Transfer(first);
380 }
@ ARG_ERR
参数错误 | Argument error

◆ TransferZLP()

virtual ErrorCode LibXR::USB::Endpoint::TransferZLP ( )
inlinevirtual

发送/接收 ZLP(零长度包) / Transfer zero length packet (ZLP)

Returns
ErrorCode 错误码 / Error code

Definition at line 386 of file ep.hpp.

386{ return Transfer(0); }

◆ UseDoubleBuffer()

bool LibXR::USB::Endpoint::UseDoubleBuffer ( ) const
inline

是否启用双缓冲 / Whether double buffer is enabled

Returns
true 启用 / Enabled
false 未启用 / Disabled

Definition at line 240 of file ep.hpp.

240{ return config_.double_buffer; }

Field Documentation

◆ avail_direction_

Direction LibXR::USB::Endpoint::avail_direction_
private

可配置方向 / Allowed direction

Definition at line 543 of file ep.hpp.

◆ buffer_

LibXR::RawData LibXR::USB::Endpoint::buffer_
private

端点缓冲区 / Endpoint buffer

Definition at line 546 of file ep.hpp.

◆ config_

Config LibXR::USB::Endpoint::config_
private

当前配置 / Current configuration

Definition at line 544 of file ep.hpp.

◆ double_buffer_

LibXR::DoubleBuffer LibXR::USB::Endpoint::double_buffer_
private

双缓冲管理 / Double buffer manager

Definition at line 547 of file ep.hpp.

◆ multi_bulk_

bool LibXR::USB::Endpoint::multi_bulk_ = false
private

多包 bulk 状态机使能 / Multi-bulk state enabled

Definition at line 549 of file ep.hpp.

◆ multi_bulk_data_

RawData LibXR::USB::Endpoint::multi_bulk_data_
private

多包 bulk 应用层 buffer / App buffer for multi-bulk

Definition at line 550 of file ep.hpp.

◆ multi_bulk_remain_

size_t LibXR::USB::Endpoint::multi_bulk_remain_
private
Initial value:
=
0

多包 bulk 剩余字节数 / Remaining bytes for multi-bulk

Definition at line 551 of file ep.hpp.

◆ number_

EPNumber LibXR::USB::Endpoint::number_
private

端点号 / Endpoint number

Definition at line 542 of file ep.hpp.

◆ on_transfer_complete_

LibXR::Callback<LibXR::ConstRawData&> LibXR::USB::Endpoint::on_transfer_complete_
private

传输完成回调 / Transfer complete callback

Definition at line 540 of file ep.hpp.

◆ state_

State LibXR::USB::Endpoint::state_ = State::DISABLED
private

当前状态 / Current state

Definition at line 545 of file ep.hpp.


The documentation for this class was generated from the following file: