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 struct 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
 
 ~Endpoint ()=default
 析构函数 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 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
 是否挂起 / Is endpoint stalled
 
bool UseDoubleBuffer () const
 是否使用双缓冲区 / Use double buffer
 
RawData GetBuffer () const
 获取端点缓冲区 Get endpoint buffer
 
void SetOnTransferCompleteCallback (Callback< ConstRawData & > cb)
 设置传输完成回调 / Set transfer complete callback
 
void SetActiveLength (uint16_t len)
 
size_t GetActiveLength ()
 
virtual size_t MaxTransferSize () const
 返回最大可传输字节数 Return the maximum transferable size at this time
 
virtual void Configure (const Config &cfg)=0
 二次初始化/配置端点协议参数(由Pool/Manager分配后调用) Configure endpoint protocol parameters (call after pool allocation)
 
virtual void Close ()=0
 关闭端点(软禁用/资源复位) Close (soft disable)
 
virtual ErrorCode Stall ()=0
 停止端点传输 Stop endpoint transfer
 
virtual ErrorCode ClearStall ()=0
 清除端点停止状态 Clear endpoint stop status
 
virtual ErrorCode Transfer (size_t size)=0
 传输数据 Transfer data
 
virtual ErrorCode TransferZLP ()
 传输空包 Transfer zero length packet
 
void OnTransferCompleteCallback (bool in_isr, size_t actual_transfer_size)
 

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
 
virtual void SwitchBuffer ()
 切换缓冲区 Switch buffer
 
virtual void SetActiveBlock (bool active_block)
 设置当前活动缓冲区 Set active buffer
 

Private Attributes

LibXR::Callback< LibXR::ConstRawData & > on_transfer_complete_
 传输完成回调 / Called when transfer completes
 
EPNumber number_
 当前端点编号 / Endpoint number
 
Direction avail_direction_
 可配置方向 / Allowed direction
 
Config config_
 当前端点配置 / Current configuration
 
State state_ = State::DISABLED
 当前状态 / Endpoint status
 
LibXR::RawData buffer_
 端点缓冲区 / Endpoint buffer
 
LibXR::DoubleBuffer double_buffer_
 双缓冲区 / Double buffer
 

Detailed Description

USB端点基类 / USB Endpoint base class.

用于描述和操作USB端点,包括配置、数据读写、状态管理等接口。 Used for describing and manipulating USB endpoints, including configuration, data read/write, state management, etc.

Definition at line 21 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 (can be configured as IN/OUT)

Definition at line 28 of file ep.hpp.

29 {
30 OUT = 0,
31 IN = 1,
32 BOTH = 2
33 };
@ BOTH
双向(可配置成IN/OUT) / Both (can be configured 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 38 of file ep.hpp.

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

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

66 {
67 CONTROL = 0,
68 ISOCHRONOUS = 1,
69 BULK = 2,
70 INTERRUPT = 3
71 };
@ 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 endpoint direction

Definition at line 155 of file ep.hpp.

156 : number_(number), avail_direction_(dir), buffer_(buffer), double_buffer_(buffer)
157 {
158 }
LibXR::DoubleBuffer double_buffer_
双缓冲区 / Double buffer
Definition ep.hpp:399
Direction avail_direction_
可配置方向 / Allowed direction
Definition ep.hpp:395
EPNumber number_
当前端点编号 / Endpoint number
Definition ep.hpp:394
LibXR::RawData buffer_
端点缓冲区 / Endpoint buffer
Definition ep.hpp:398

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返回的端点方向 / Return endpoint direction
Returns
constexpr EPNumber

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:39

◆ AvailableDirection()

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

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

Definition at line 180 of file ep.hpp.

180{ return avail_direction_; }

◆ ClearStall()

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

清除端点停止状态 Clear endpoint stop status

Returns
ErrorCode

Implemented in LibXR::CH32EndpointOtgFs, LibXR::CH32EndpointOtgHs, and LibXR::STM32Endpoint.

◆ Close()

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

关闭端点(软禁用/资源复位) Close (soft disable)

Implemented in LibXR::CH32EndpointOtgFs, LibXR::CH32EndpointOtgHs, and LibXR::STM32Endpoint.

◆ Configure()

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

二次初始化/配置端点协议参数(由Pool/Manager分配后调用) Configure endpoint protocol parameters (call after pool allocation)

Implemented in LibXR::CH32EndpointOtgFs, LibXR::CH32EndpointOtgHs, and 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
constexpr uint8_t

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
constexpr uint8_t

Definition at line 92 of file ep.hpp.

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

◆ GetActiveLength()

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

Definition at line 280 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)

Definition at line 199 of file ep.hpp.

200 {
201 if (state_ == State::DISABLED)
202 {
203 return EPNumberToInt8(number_) & 0x0F;
204 }
206 }
static constexpr uint8_t EPNumberToInt8(EPNumber ep)
端点号转换为uint8_t / Convert endpoint number to uint8_t
Definition ep.hpp:92
static constexpr uint8_t EPNumberToAddr(EPNumber ep, Direction dir)
端点号转换为端点地址 / Convert endpoint number to endpoint address
Definition ep.hpp:104
State state_
当前状态 / Endpoint status
Definition ep.hpp:397
Config config_
当前端点配置 / Current configuration
Definition ep.hpp:396
Direction direction
端点方向 / Endpoint direction
Definition ep.hpp:141

◆ GetBuffer()

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

获取端点缓冲区 Get endpoint buffer

Returns
RawData

Definition at line 256 of file ep.hpp.

257 {
258 if (config_.double_buffer)
259 {
261 }
262 else
263 {
264 return buffer_;
265 }
266 }
size_t Size() const
获取每个缓冲区的大小(单位:字节) Gets the size of each buffer in bytes
uint8_t * ActiveBuffer() const
获取当前正在使用的缓冲区指针 Returns the currently active buffer

◆ GetConfig()

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

获取当前配置 Get endpoint config

Definition at line 365 of file ep.hpp.

365{ return config_; }

◆ GetDirection()

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

获取端点方向 Get endpoint direction

Definition at line 186 of file ep.hpp.

187 {
188 if (state_ == State::DISABLED)
189 {
190 return AvailableDirection();
191 }
192 return config_.direction;
193 }
Direction AvailableDirection() const
获取允许配置的端点方向 Get allowed endpoint direction
Definition ep.hpp:180

◆ GetNumber()

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

获取端点号 Get endpoint number

Definition at line 174 of file ep.hpp.

174{ return number_; }

◆ GetState()

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

获取端点状态 Get endpoint state

Definition at line 212 of file ep.hpp.

212{ return state_; }

◆ GetType()

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

获取端点类型 Get endpoint type

Definition at line 226 of file ep.hpp.

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

◆ IsStalled()

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

是否挂起 / Is endpoint stalled

Returns
true
false

Definition at line 240 of file ep.hpp.

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

◆ MaxPacketSize()

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

获取最大包长 Get max packet size

Definition at line 232 of file ep.hpp.

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

◆ MaxTransferSize()

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

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

默认为最大包长,可在支持 FIFO 优化的平台中重载为动态剩余容量。 Default equals MaxPacketSize; override for FIFO-aware implementation.

Reimplemented in LibXR::STM32Endpoint.

Definition at line 289 of file ep.hpp.

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

◆ NextEPNumber()

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

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

Parameters
ep当前端点号 / Current endpoint number
Returns
constexpr EPNumber

Definition at line 129 of file ep.hpp.

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

◆ OnTransferCompleteCallback()

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

Definition at line 336 of file ep.hpp.

337 {
338 if (GetState() != State::BUSY)
339 {
340 return;
341 }
342
344
345 ConstRawData data = UseDoubleBuffer()
346 ? ConstRawData(GetDirection() == Direction::OUT
349 actual_transfer_size)
350 : ConstRawData(buffer_.addr_, actual_transfer_size);
351
353 {
354 SwitchBuffer();
355 }
356
357 on_transfer_complete_.Run(in_isr, data);
358 }
void Run(bool in_isr, PassArgs &&...args) const
执行回调函数,并传递参数。 Executes the callback function, passing the arguments.
Definition libxr_cb.hpp:207
uint8_t * PendingBuffer() const
获取备用缓冲区的指针 Returns the pending (inactive) buffer
Direction GetDirection() const
获取端点方向 Get endpoint direction
Definition ep.hpp:186
bool UseDoubleBuffer() const
是否使用双缓冲区 / Use double buffer
Definition ep.hpp:248
virtual void SwitchBuffer()
切换缓冲区 Switch buffer
Definition ep.hpp:372
LibXR::Callback< LibXR::ConstRawData & > on_transfer_complete_
传输完成回调 / Called when transfer completes
Definition ep.hpp:392
void SetState(State state)
设置端点状态 Set endpoint state
Definition ep.hpp:220
State GetState() const
获取端点状态 Get endpoint state
Definition ep.hpp:212

◆ SetActiveBlock()

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

设置当前活动缓冲区 Set active buffer

Parameters
active_blocktrue 表示使用第二个缓冲区,false 表示使用第一个缓冲区

Definition at line 384 of file ep.hpp.

385 {
386 double_buffer_.SetActiveBlock(active_block);
388 }
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

Definition at line 278 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传输完成回调 / Transfer complete callback

Definition at line 273 of file ep.hpp.

274 {
276 }

◆ SetState()

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

设置端点状态 Set endpoint state

Parameters
state状态 / State

Definition at line 220 of file ep.hpp.

220{ state_ = state; }

◆ Stall()

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

停止端点传输 Stop endpoint transfer

Returns
ErrorCode

Implemented in LibXR::CH32EndpointOtgFs, LibXR::CH32EndpointOtgHs, and LibXR::STM32Endpoint.

◆ SwitchBuffer()

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

切换缓冲区 Switch buffer

Reimplemented in LibXR::CH32EndpointOtgFs, and LibXR::CH32EndpointOtgHs.

Definition at line 372 of file ep.hpp.

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

◆ Transfer()

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

传输数据 Transfer data

Parameters
size传输大小 / Transfer size
Returns
ErrorCode

Implemented in LibXR::CH32EndpointOtgFs, LibXR::CH32EndpointOtgHs, and LibXR::STM32Endpoint.

◆ TransferZLP()

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

传输空包 Transfer zero length packet

Returns
ErrorCode

Definition at line 334 of file ep.hpp.

334{ return Transfer(0); }
virtual ErrorCode Transfer(size_t size)=0
传输数据 Transfer data

◆ UseDoubleBuffer()

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

是否使用双缓冲区 / Use double buffer

Returns
true
false

Definition at line 248 of file ep.hpp.

248{ return config_.double_buffer; }

Field Documentation

◆ avail_direction_

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

可配置方向 / Allowed direction

Definition at line 395 of file ep.hpp.

◆ buffer_

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

端点缓冲区 / Endpoint buffer

Definition at line 398 of file ep.hpp.

◆ config_

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

当前端点配置 / Current configuration

Definition at line 396 of file ep.hpp.

◆ double_buffer_

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

双缓冲区 / Double buffer

Definition at line 399 of file ep.hpp.

◆ number_

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

当前端点编号 / Endpoint number

Definition at line 394 of file ep.hpp.

◆ on_transfer_complete_

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

传输完成回调 / Called when transfer completes

Definition at line 392 of file ep.hpp.

◆ state_

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

当前状态 / Endpoint status

Definition at line 397 of file ep.hpp.


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