libxr  1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
ep.hpp
1#pragma once
2
3#include <cstdint>
4
5#include "double_buffer.hpp"
6#include "libxr_cb.hpp"
7#include "libxr_def.hpp"
8#include "libxr_rw.hpp"
9#include "libxr_type.hpp"
10
11namespace LibXR::USB
12{
13
22{
23 public:
28 enum class Direction : uint8_t
29 {
30 OUT = 0,
31 IN = 1,
32 BOTH = 2
33 };
34
38 enum class EPNumber : uint8_t
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 };
60
65 enum class Type : uint8_t
66 {
67 CONTROL = 0,
68 ISOCHRONOUS = 1,
69 BULK = 2,
70 INTERRUPT = 3
71 };
72
77 enum class State : uint8_t
78 {
79 DISABLED,
80 IDLE,
81 BUSY,
82 STALLED,
83 ERROR
84 };
85
92 static constexpr uint8_t EPNumberToInt8(EPNumber ep)
93 {
94 return static_cast<uint8_t>(ep);
95 }
96
104 static constexpr uint8_t EPNumberToAddr(EPNumber ep, Direction dir)
105 {
106 ASSERT(dir == Direction::IN || dir == Direction::OUT);
107 return static_cast<uint8_t>(ep) | (dir == Direction::IN ? 0x80 : 0x00);
108 }
109
117 static constexpr EPNumber AddrToEPNumber(uint8_t addr, Direction& dir)
118 {
119 dir = addr & 0x80 ? Direction::IN : Direction::OUT;
120 return static_cast<EPNumber>(addr & 0x7F);
121 }
122
129 static constexpr EPNumber NextEPNumber(EPNumber ep)
130 {
131 ASSERT(ep <= EPNumber::EP15);
132 return static_cast<EPNumber>(EPNumberToInt8(ep) + 1);
133 }
134
139 struct Config
140 {
143 uint16_t max_packet_size = UINT16_MAX;
144 bool double_buffer = false;
145 uint8_t mult = 0;
146 };
147
155 explicit Endpoint(EPNumber number, Direction dir, RawData buffer)
156 : number_(number), avail_direction_(dir), buffer_(buffer), double_buffer_(buffer)
157 {
158 }
159
164 ~Endpoint() = default;
165
166 // 禁止拷贝
167 Endpoint(const Endpoint&) = delete;
168 Endpoint& operator=(const Endpoint&) = delete;
169
174 EPNumber GetNumber() const { return number_; }
175
181
187 {
188 if (state_ == State::DISABLED)
189 {
190 return AvailableDirection();
191 }
192 return config_.direction;
193 }
194
199 uint8_t GetAddress() const
200 {
201 if (state_ == State::DISABLED)
202 {
203 return EPNumberToInt8(number_) & 0x0F;
204 }
206 }
207
212 State GetState() const { return state_; }
213
220 void SetState(State state) { state_ = state; }
221
226 Type GetType() const { return config_.type; }
227
232 uint16_t MaxPacketSize() const { return config_.max_packet_size; }
233
240 bool IsStalled() const { return state_ == State::STALLED; }
241
248 bool UseDoubleBuffer() const { return config_.double_buffer; }
249
257 {
258 if (config_.double_buffer)
259 {
261 }
262 else
263 {
264 return buffer_;
265 }
266 }
267
277
278 void SetActiveLength(uint16_t len) { double_buffer_.SetActiveLength(len); }
279
280 size_t GetActiveLength() { return double_buffer_.GetActiveLength(); }
281
289 virtual size_t MaxTransferSize() const { return MaxPacketSize(); }
290
295 virtual void Configure(const Config& cfg) = 0;
296
301 virtual void Close() = 0;
302
309 ErrorCode virtual Stall() = 0;
310
317 ErrorCode virtual ClearStall() = 0;
318
326 virtual ErrorCode Transfer(size_t size) = 0;
327
334 virtual ErrorCode TransferZLP() { return Transfer(0); }
335
336 void OnTransferCompleteCallback(bool in_isr, size_t actual_transfer_size)
337 {
338 if (GetState() != State::BUSY)
339 {
340 return;
341 }
342
344
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 }
359
360 protected:
365 Config& GetConfig() { return config_; }
366
372 virtual void SwitchBuffer()
373 {
376 }
377
384 virtual void SetActiveBlock(bool active_block)
385 {
386 double_buffer_.SetActiveBlock(active_block);
388 }
389
390 private:
393
400}; // namespace LibXR::USB
401
402} // namespace LibXR::USB
提供一个通用的回调包装,支持动态参数传递。 Provides a generic callback wrapper, supporting dynamic argument passing.
Definition libxr_cb.hpp:124
void Run(bool in_isr, PassArgs &&...args) const
执行回调函数,并传递参数。 Executes the callback function, passing the arguments.
Definition libxr_cb.hpp:207
常量原始数据封装类。 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
原始数据封装类。 A class for encapsulating raw data.
USB端点基类 / USB Endpoint base class.
Definition ep.hpp:22
Endpoint(EPNumber number, Direction dir, RawData buffer)
构造函数 Constructor
Definition ep.hpp:155
EPNumber
端点号 / Endpoint number
Definition ep.hpp:39
@ 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
LibXR::DoubleBuffer double_buffer_
双缓冲区 / Double buffer
Definition ep.hpp:399
Direction AvailableDirection() const
获取允许配置的端点方向 Get allowed endpoint direction
Definition ep.hpp:180
Type GetType() const
获取端点类型 Get endpoint type
Definition ep.hpp:226
uint8_t GetAddress() const
获取端点地址(方向 + 号) Get endpoint address (dir + num)
Definition ep.hpp:199
bool IsStalled() const
是否挂起 / Is endpoint stalled
Definition ep.hpp:240
static constexpr uint8_t EPNumberToInt8(EPNumber ep)
端点号转换为uint8_t / Convert endpoint number to uint8_t
Definition ep.hpp:92
virtual ErrorCode Stall()=0
停止端点传输 Stop endpoint transfer
Direction avail_direction_
可配置方向 / Allowed direction
Definition ep.hpp:395
Direction
端点方向 Endpoint direction
Definition ep.hpp:29
@ BOTH
双向(可配置成IN/OUT) / Both (can be configured as IN/OUT)
@ IN
输入方向 / IN direction
@ OUT
输出方向 / OUT direction
static constexpr uint8_t EPNumberToAddr(EPNumber ep, Direction dir)
端点号转换为端点地址 / Convert endpoint number to endpoint address
Definition ep.hpp:104
~Endpoint()=default
析构函数 Destructor
Direction GetDirection() const
获取端点方向 Get endpoint direction
Definition ep.hpp:186
bool UseDoubleBuffer() const
是否使用双缓冲区 / Use double buffer
Definition ep.hpp:248
static constexpr EPNumber AddrToEPNumber(uint8_t addr, Direction &dir)
端点地址转换为端点号 / Convert endpoint address to endpoint number
Definition ep.hpp:117
State state_
当前状态 / Endpoint status
Definition ep.hpp:397
virtual void SwitchBuffer()
切换缓冲区 Switch buffer
Definition ep.hpp:372
Config & GetConfig()
获取当前配置 Get endpoint config
Definition ep.hpp:365
EPNumber number_
当前端点编号 / Endpoint number
Definition ep.hpp:394
virtual ErrorCode ClearStall()=0
清除端点停止状态 Clear endpoint stop status
virtual size_t MaxTransferSize() const
返回最大可传输字节数 Return the maximum transferable size at this time
Definition ep.hpp:289
LibXR::RawData buffer_
端点缓冲区 / Endpoint buffer
Definition ep.hpp:398
void SetOnTransferCompleteCallback(Callback< ConstRawData & > cb)
设置传输完成回调 / Set transfer complete callback
Definition ep.hpp:273
static constexpr EPNumber NextEPNumber(EPNumber ep)
获取下一个端点号 / Get the next endpoint number
Definition ep.hpp:129
LibXR::Callback< LibXR::ConstRawData & > on_transfer_complete_
传输完成回调 / Called when transfer completes
Definition ep.hpp:392
virtual void Configure(const Config &cfg)=0
二次初始化/配置端点协议参数(由Pool/Manager分配后调用) Configure endpoint protocol parameters (call after pool allocation...
Type
端点类型 Endpoint type
Definition ep.hpp:66
@ ISOCHRONOUS
等时端点 / Isochronous
@ BULK
批量端点 / Bulk
@ INTERRUPT
中断端点 / Interrupt
@ CONTROL
控制端点 / Control
EPNumber GetNumber() const
获取端点号 Get endpoint number
Definition ep.hpp:174
virtual void Close()=0
关闭端点(软禁用/资源复位) Close (soft disable)
uint16_t MaxPacketSize() const
获取最大包长 Get max packet size
Definition ep.hpp:232
State
端点状态 Endpoint state
Definition ep.hpp:78
@ DISABLED
禁用 / Disabled
@ STALLED
挂起 / Stalled
virtual void SetActiveBlock(bool active_block)
设置当前活动缓冲区 Set active buffer
Definition ep.hpp:384
void SetState(State state)
设置端点状态 Set endpoint state
Definition ep.hpp:220
State GetState() const
获取端点状态 Get endpoint state
Definition ep.hpp:212
virtual ErrorCode TransferZLP()
传输空包 Transfer zero length packet
Definition ep.hpp:334
virtual ErrorCode Transfer(size_t size)=0
传输数据 Transfer data
Config config_
当前端点配置 / Current configuration
Definition ep.hpp:396
RawData GetBuffer() const
获取端点缓冲区 Get endpoint buffer
Definition ep.hpp:256
端点配置结构体 Endpoint configuration struct
Definition ep.hpp:140
Type type
端点类型 / Endpoint type
Definition ep.hpp:142
uint8_t mult
多包,高带宽端点用 / Multiplier (high-bandwidth)
Definition ep.hpp:145
uint16_t max_packet_size
最大包长 / Max packet size
Definition ep.hpp:143
Direction direction
端点方向 / Endpoint direction
Definition ep.hpp:141