5#include "double_buffer.hpp"
7#include "libxr_def.hpp"
9#include "libxr_type.hpp"
95 return static_cast<uint8_t
>(ep);
107 return static_cast<uint8_t
>(ep) | (dir ==
Direction::IN ? 0x80 : 0x00);
119 return static_cast<EPNumber>(addr & 0x7F);
327 return ErrorCode::ARG_ERR;
331 if (data.
size_ <= max_chunk)
340 auto src =
static_cast<const uint8_t*
>(data.
addr_);
341 auto dst =
static_cast<uint8_t*
>(ep_buf.addr_);
355 size_t first = max_chunk;
365 auto dst =
static_cast<uint8_t*
>(ep_buf.addr_);
393 bool callback_uses_app_buffer =
false;
408 size_t chunk = MAX_CHUNK;
415 auto dst =
static_cast<uint8_t*
>(ep_buf.addr_);
427 callback_uses_app_buffer =
true;
436 size_t recvd = actual_transfer_size;
438 if (recvd > prev_remain)
446 auto src =
static_cast<const uint8_t*
>(ep_buf.addr_);
452 const bool SHORT_PACKET = (recvd < MAX_CHUNK);
455 if (!SHORT_PACKET && !BUFFER_FULL)
457 size_t chunk = MAX_CHUNK;
470 callback_uses_app_buffer =
true;
481 if (callback_uses_app_buffer)
490 actual_transfer_size)
提供一个通用的回调包装,支持动态参数传递。 Provides a generic callback wrapper, supporting dynamic argument passing.
void Run(bool in_isr, PassArgs &&...args) const
执行回调函数,并传递参数。 Executes the callback function, passing the arguments.
常量原始数据封装类。 A class for encapsulating constant raw data.
双缓冲区管理类 / Double buffer manager class
void SetActiveLength(size_t length)
设置当前活动缓冲区的数据长度 Sets the size of the active buffer
void SetActiveBlock(bool block)
设置当前活动缓冲区 Sets the active buffer
void EnablePending()
手动启用 pending 状态 Manually sets the pending state to true
size_t Size() const
获取每个缓冲区的大小(单位:字节) Gets the size of each buffer in bytes
uint8_t * ActiveBuffer() const
获取当前正在使用的缓冲区指针 Returns the currently active buffer
size_t GetActiveLength() const
获取当前活动缓冲区中准备好的数据长度 Gets the size of valid data in active buffer
uint8_t * PendingBuffer() const
获取备用缓冲区的指针 Returns the pending (inactive) buffer
void Switch()
切换到备用缓冲区(若其有效) Switches to the pending buffer if it's valid
static void FastCopy(void *dst, const void *src, size_t size)
快速内存拷贝 / Fast memory copy
原始数据封装类。 A class for encapsulating raw data.
size_t size_
数据大小(字节)。 The size of the data (in bytes).
void * addr_
数据存储地址。 The storage address of the data.
USB 端点基类 / USB Endpoint base class.
Endpoint(EPNumber number, Direction dir, RawData buffer)
构造函数 / Constructor
EPNumber
端点号 Endpoint number
@ EP11
端点 11 / Endpoint 11
@ EP14
端点 14 / Endpoint 14
@ EP_AUTO
自动分配端点号 / Auto allocate
@ EP15
端点 15 / Endpoint 15
@ EP_MAX_NUM
端点数量上限 / Maximum number of endpoints
@ EP13
端点 13 / Endpoint 13
@ EP10
端点 10 / Endpoint 10
@ EP_INVALID
非法端点号 / Invalid endpoint
@ EP12
端点 12 / Endpoint 12
RawData multi_bulk_data_
多包 bulk 应用层 buffer / App buffer for multi-bulk
LibXR::DoubleBuffer double_buffer_
双缓冲管理 / Double buffer manager
Direction AvailableDirection() const
获取允许配置的方向 / Get allowed endpoint direction
Type GetType() const
获取端点类型 / Get endpoint type
uint8_t GetAddress() const
获取端点地址(方向 + 号) / Get endpoint address (dir + num)
bool IsStalled() const
是否处于 STALL 状态 / Whether endpoint is stalled
static constexpr uint8_t EPNumberToInt8(EPNumber ep)
端点号转换为 uint8_t / Convert endpoint number to uint8_t
virtual ErrorCode Stall()=0
置 STALL / Stall endpoint
Direction avail_direction_
可配置方向 / Allowed direction
Direction
端点方向 Endpoint direction
@ BOTH
双向(可配置为 IN/OUT) / Both (configurable as IN/OUT)
@ OUT
输出方向 / OUT direction
static constexpr uint8_t EPNumberToAddr(EPNumber ep, Direction dir)
端点号转换为端点地址 / Convert endpoint number to endpoint address
Direction GetDirection() const
获取当前端点方向 / Get current endpoint direction
virtual ~Endpoint()=default
虚析构函数 / Virtual destructor
bool UseDoubleBuffer() const
是否启用双缓冲 / Whether double buffer is enabled
static constexpr EPNumber AddrToEPNumber(uint8_t addr, Direction &dir)
端点地址转换为端点号 / Convert endpoint address to endpoint number
State state_
当前状态 / Current state
virtual ErrorCode TransferMultiBulk(RawData &data)
Bulk 多包传输辅助接口 / Helper for multi-packet bulk transfer.
virtual void SwitchBuffer()
切换双缓冲 / Switch double buffer
Config & GetConfig()
获取当前配置引用 / Get endpoint config reference
void SetActiveLength(uint16_t len)
设置当前活动缓冲区有效长度 / Set active buffer valid length
void OnTransferCompleteCallback(bool in_isr, size_t actual_transfer_size)
由底层在传输完成时调用 / Called by low-level driver when transfer completes
EPNumber number_
端点号 / Endpoint number
virtual ErrorCode ClearStall()=0
清除 STALL / Clear stall
virtual size_t MaxTransferSize() const
返回当前最大可传输字节数 / Return maximum transferable size at this time
LibXR::RawData buffer_
端点缓冲区 / Endpoint buffer
void SetOnTransferCompleteCallback(Callback< ConstRawData & > cb)
设置传输完成回调 / Set transfer complete callback
static constexpr EPNumber NextEPNumber(EPNumber ep)
获取下一个端点号 / Get the next endpoint number
bool multi_bulk_
多包 bulk 状态机使能 / Multi-bulk state enabled
LibXR::Callback< LibXR::ConstRawData & > on_transfer_complete_
传输完成回调 / Transfer complete callback
virtual void Configure(const Config &cfg)=0
配置端点协议参数 / Configure endpoint protocol parameters
@ ISOCHRONOUS
等时端点 / Isochronous
@ INTERRUPT
中断端点 / Interrupt
EPNumber GetNumber() const
获取端点号 / Get endpoint number
virtual void Close()=0
关闭端点 / Close endpoint
size_t GetActiveLength()
获取当前活动缓冲区有效长度 / Get active buffer valid length
uint16_t MaxPacketSize() const
获取最大包长 / Get max packet size
virtual void SetActiveBlock(bool active_block)
设置当前活动缓冲块 / Set active buffer block
void SetState(State state)
设置端点状态 / Set endpoint state
State GetState() const
获取端点状态 / Get endpoint state
virtual ErrorCode TransferZLP()
发送/接收 ZLP(零长度包) / Transfer zero length packet (ZLP)
virtual ErrorCode Transfer(size_t size)=0
启动一次传输 / Start a transfer
Config config_
当前配置 / Current configuration
RawData GetBuffer() const
获取当前可用于传输的缓冲区 / Get current transfer buffer
size_t multi_bulk_remain_
多包 bulk 剩余字节数 / Remaining bytes for multi-bulk
端点配置参数 / Endpoint configuration parameters
Type type
端点类型 / Endpoint type
bool double_buffer
是否启用双缓冲 / Enable double buffer
uint8_t mult
多包倍数(高带宽端点) / Multiplier (high-bandwidth)
uint16_t max_packet_size
最大包长 / Max packet size
Direction direction
端点方向 / Endpoint direction