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 22 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 29 of file ep.hpp.

30 {
31 OUT = 0,
32 IN = 1,
33 BOTH = 2
34 };
@ 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 40 of file ep.hpp.

41 {
42 EP0 = 0,
43 EP1 = 1,
44 EP2 = 2,
45 EP3 = 3,
46 EP4 = 4,
47 EP5 = 5,
48 EP6 = 6,
49 EP7 = 7,
50 EP8 = 8,
51 EP9 = 9,
52 EP10 = 10,
53 EP11 = 11,
54 EP12 = 12,
55 EP13 = 13,
56 EP14 = 14,
57 EP15 = 15,
58 EP_MAX_NUM = 16,
59 EP_AUTO = 0xFE,
60 EP_INVALID = 0xFF
61 };
@ 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 79 of file ep.hpp.

80 {
81 DISABLED,
82 IDLE,
83 BUSY,
84 STALLED,
85 ERROR
86 };
@ 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 67 of file ep.hpp.

68 {
69 CONTROL = 0,
70 ISOCHRONOUS = 1,
71 BULK = 2,
72 INTERRUPT = 3
73 };
@ 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 152 of file ep.hpp.

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

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 116 of file ep.hpp.

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

◆ AvailableDirection()

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

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

Returns
Direction 允许方向 / Allowed direction

Definition at line 175 of file ep.hpp.

175{ 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 104 of file ep.hpp.

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

◆ 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 93 of file ep.hpp.

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

◆ GetActiveLength()

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

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

Returns
size_t 有效长度 / Valid length

Definition at line 276 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 194 of file ep.hpp.

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

◆ GetBuffer()

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

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

Returns
RawData 缓冲区视图 / Buffer view

Definition at line 245 of file ep.hpp.

246 {
248 {
250 }
251 else
252 {
253 return buffer_;
254 }
255 }
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:142

◆ GetConfig()

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

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

Returns
Config& 配置引用 / Configuration reference

Definition at line 515 of file ep.hpp.

515{ return config_; }

◆ GetDirection()

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

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

Returns
Direction 当前方向 / Current direction

Definition at line 181 of file ep.hpp.

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

◆ GetNumber()

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

获取端点号 / Get endpoint number

Returns
EPNumber 端点号 / Endpoint number

Definition at line 169 of file ep.hpp.

169{ return number_; }

◆ GetState()

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

获取端点状态 / Get endpoint state

Returns
State 当前状态 / Current state

Definition at line 207 of file ep.hpp.

207{ return state_; }

◆ GetType()

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

获取端点类型 / Get endpoint type

Returns
Type 端点类型 / Endpoint type

Definition at line 219 of file ep.hpp.

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

◆ IsStalled()

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

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

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

Definition at line 232 of file ep.hpp.

232{ 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 225 of file ep.hpp.

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

◆ 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 282 of file ep.hpp.

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

◆ 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 127 of file ep.hpp.

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

◆ 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 392 of file ep.hpp.

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

◆ 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 531 of file ep.hpp.

532 {
533 double_buffer_.SetActiveBlock(active_block);
535 }
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 270 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 261 of file ep.hpp.

262 {
264 }

◆ SetState()

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

设置端点状态 / Set endpoint state

Parameters
state状态 / State

Definition at line 213 of file ep.hpp.

213{ 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 520 of file ep.hpp.

521 {
524 }
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 320 of file ep.hpp.

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

◆ TransferZLP()

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

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

Returns
ErrorCode 错误码 / Error code

Definition at line 385 of file ep.hpp.

385{ return Transfer(0); }

◆ UseDoubleBuffer()

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

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

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

Definition at line 239 of file ep.hpp.

239{ return config_.double_buffer; }

Field Documentation

◆ avail_direction_

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

可配置方向 / Allowed direction

Definition at line 542 of file ep.hpp.

◆ buffer_

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

端点缓冲区 / Endpoint buffer

Definition at line 545 of file ep.hpp.

◆ config_

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

当前配置 / Current configuration

Definition at line 543 of file ep.hpp.

◆ double_buffer_

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

双缓冲管理 / Double buffer manager

Definition at line 546 of file ep.hpp.

◆ multi_bulk_

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

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

Definition at line 548 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 549 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 550 of file ep.hpp.

◆ number_

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

端点号 / Endpoint number

Definition at line 541 of file ep.hpp.

◆ on_transfer_complete_

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

传输完成回调 / Transfer complete callback

Definition at line 539 of file ep.hpp.

◆ state_

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

当前状态 / Current state

Definition at line 544 of file ep.hpp.


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