libxr  1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
LibXR::STM32Endpoint Class Reference
Inheritance diagram for LibXR::STM32Endpoint:
[legend]
Collaboration diagram for LibXR::STM32Endpoint:
[legend]

Public Member Functions

void Configure (const Config &cfg) override
 二次初始化/配置端点协议参数(由Pool/Manager分配后调用) Configure endpoint protocol parameters (call after pool allocation)
 
void Close () override
 关闭端点(软禁用/资源复位) Close (soft disable)
 
ErrorCode Transfer (size_t size) override
 传输数据 Transfer data
 
ErrorCode Stall () override
 停止端点传输 Stop endpoint transfer
 
ErrorCode ClearStall () override
 清除端点停止状态 Clear endpoint stop status
 
size_t MaxTransferSize () const override
 返回最大可传输字节数 Return the maximum transferable size at this time
 
- Public Member Functions inherited from LibXR::USB::Endpoint
 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 ErrorCode TransferZLP ()
 传输空包 Transfer zero length packet
 
void OnTransferCompleteCallback (bool in_isr, size_t actual_transfer_size)
 

Data Fields

PCD_HandleTypeDef * hpcd_
 
stm32_usb_dev_id_t id_
 

Additional Inherited Members

- Public Types inherited from LibXR::USB::Endpoint
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...
 
- Static Public Member Functions inherited from LibXR::USB::Endpoint
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 inherited from LibXR::USB::Endpoint
ConfigGetConfig ()
 获取当前配置 Get endpoint config
 
virtual void SwitchBuffer ()
 切换缓冲区 Switch buffer
 
virtual void SetActiveBlock (bool active_block)
 设置当前活动缓冲区 Set active buffer
 

Detailed Description

Definition at line 14 of file stm32_usb_ep.hpp.

Member Function Documentation

◆ ClearStall()

ErrorCode STM32Endpoint::ClearStall ( )
overridevirtual

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

Returns
ErrorCode

Implements LibXR::USB::Endpoint.

Definition at line 290 of file stm32_usb_ep.cpp.

291{
292 if (GetState() != State::STALLED)
293 {
294 return ErrorCode::FAILED;
295 }
296
297 uint8_t addr = EPNumberToAddr(GetNumber(), GetDirection());
298
300 {
302 return ErrorCode::OK;
303 }
304
305 if (HAL_PCD_EP_ClrStall(hpcd_, addr) == HAL_OK)
306 {
308 return ErrorCode::OK;
309 }
310 else
311 {
313 return ErrorCode::FAILED;
314 }
315}
@ EP0
端点0 / Endpoint 0
static constexpr uint8_t EPNumberToAddr(EPNumber ep, Direction dir)
端点号转换为端点地址 / Convert endpoint number to endpoint address
Definition ep.hpp:104
Direction GetDirection() const
获取端点方向 Get endpoint direction
Definition ep.hpp:186
EPNumber GetNumber() const
获取端点号 Get endpoint number
Definition ep.hpp:174
@ STALLED
挂起 / Stalled
void SetState(State state)
设置端点状态 Set endpoint state
Definition ep.hpp:220
State GetState() const
获取端点状态 Get endpoint state
Definition ep.hpp:212

◆ Close()

void STM32Endpoint::Close ( )
overridevirtual

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

Implements LibXR::USB::Endpoint.

Definition at line 184 of file stm32_usb_ep.cpp.

185{
186 uint8_t addr = EPNumberToAddr(GetNumber(), GetDirection());
187 HAL_PCD_EP_Close(hpcd_, addr);
189}
@ DISABLED
禁用 / Disabled

◆ Configure()

void STM32Endpoint::Configure ( const Config & cfg)
overridevirtual

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

Implements LibXR::USB::Endpoint.

Definition at line 74 of file stm32_usb_ep.cpp.

75{
76 ASSERT(cfg.direction == Direction::IN || cfg.direction == Direction::OUT);
77
78 uint8_t addr = EPNumberToAddr(GetNumber(), cfg.direction);
79 uint8_t type = static_cast<uint8_t>(cfg.type);
80 auto& ep_cfg = GetConfig();
81 size_t packet_size_limit = 0;
82
83 ep_cfg = cfg;
84
85 switch (cfg.type)
86 {
87 case Type::BULK:
88#if defined(PCD_SPEED_HIGH_IN_FULL)
89 if (hpcd_->Init.speed == PCD_SPEED_FULL ||
90 hpcd_->Init.speed == PCD_SPEED_HIGH_IN_FULL)
91#else
92 if (hpcd_->Init.speed == PCD_SPEED_FULL)
93#endif
94 {
95 packet_size_limit = 64;
96 }
97 else
98 {
99 packet_size_limit = 512;
100 }
101 break;
102 case Type::INTERRUPT:
103#if defined(PCD_SPEED_HIGH_IN_FULL)
104 if (hpcd_->Init.speed == PCD_SPEED_FULL ||
105 hpcd_->Init.speed == PCD_SPEED_HIGH_IN_FULL)
106#else
107 if (hpcd_->Init.speed == PCD_SPEED_FULL)
108#endif
109 {
110 packet_size_limit = 64;
111 }
112 else
113 {
114 packet_size_limit = 1024;
115 }
116 break;
118#if defined(PCD_SPEED_HIGH_IN_FULL)
119 if (hpcd_->Init.speed == PCD_SPEED_FULL ||
120 hpcd_->Init.speed == PCD_SPEED_HIGH_IN_FULL)
121#else
122 if (hpcd_->Init.speed == PCD_SPEED_FULL)
123#endif
124 {
125 packet_size_limit = 1023;
126 }
127 else
128 {
129 packet_size_limit = 1024;
130 }
131 break;
132 case Type::CONTROL:
133 packet_size_limit = 64;
134 break;
135 default:
136 break;
137 }
138
139#if defined(USB_OTG_FS) || defined(USB_OTG_HS)
140 if (packet_size_limit > fifo_size_)
141 {
142 packet_size_limit = fifo_size_;
143 }
144#endif
145
146#if defined(USB_BASE)
147 if (packet_size_limit > hw_buffer_size_)
148 {
149 packet_size_limit = hw_buffer_size_;
150 }
151#endif
152
153 auto buffer = GetBuffer();
154
155 if (packet_size_limit > buffer.size_)
156 {
157 packet_size_limit = buffer.size_;
158 }
159
160 size_t max_packet_size = cfg.max_packet_size;
161
162 if (max_packet_size > packet_size_limit)
163 {
164 max_packet_size = packet_size_limit;
165 }
166
167 ep_cfg.max_packet_size = max_packet_size;
168
169 if (max_packet_size < 8)
170 {
171 max_packet_size = 8;
172 }
173
174 if (HAL_PCD_EP_Open(hpcd_, addr, max_packet_size, type) == HAL_OK)
175 {
177 }
178 else
179 {
181 }
182}
@ IN
输入方向 / IN direction
@ OUT
输出方向 / OUT direction
Config & GetConfig()
获取当前配置 Get endpoint config
Definition ep.hpp:365
@ ISOCHRONOUS
等时端点 / Isochronous
@ BULK
批量端点 / Bulk
@ INTERRUPT
中断端点 / Interrupt
@ CONTROL
控制端点 / Control
RawData GetBuffer() const
获取端点缓冲区 Get endpoint buffer
Definition ep.hpp:256

◆ MaxTransferSize()

size_t STM32Endpoint::MaxTransferSize ( ) const
overridevirtual

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

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

Reimplemented from LibXR::USB::Endpoint.

Definition at line 317 of file stm32_usb_ep.cpp.

318{
320 {
321 return MaxPacketSize();
322 }
323 else
324 {
325 return GetBuffer().size_;
326 }
327}
size_t size_
数据大小(字节)。 The size of the data (in bytes).
uint16_t MaxPacketSize() const
获取最大包长 Get max packet size
Definition ep.hpp:232

◆ Stall()

ErrorCode STM32Endpoint::Stall ( )
overridevirtual

停止端点传输 Stop endpoint transfer

Returns
ErrorCode

Implements LibXR::USB::Endpoint.

Definition at line 270 of file stm32_usb_ep.cpp.

271{
272 if (GetState() != State::IDLE)
273 {
274 return ErrorCode::BUSY;
275 }
276
277 uint8_t addr = EPNumberToAddr(GetNumber(), GetDirection());
278 if (HAL_PCD_EP_SetStall(hpcd_, addr) == HAL_OK)
279 {
281 return ErrorCode::OK;
282 }
283 else
284 {
286 return ErrorCode::FAILED;
287 }
288}

◆ Transfer()

ErrorCode STM32Endpoint::Transfer ( size_t size)
overridevirtual

传输数据 Transfer data

Parameters
size传输大小 / Transfer size
Returns
ErrorCode

Implements LibXR::USB::Endpoint.

Definition at line 191 of file stm32_usb_ep.cpp.

192{
193 if (GetState() == State::BUSY)
194 {
195 return ErrorCode::BUSY;
196 }
197
198 bool is_in = GetDirection() == Direction::IN;
199 auto ep_addr = EPNumberToAddr(GetNumber(), GetDirection());
200
201 PCD_EPTypeDef* ep = is_in ? &hpcd_->IN_ep[ep_addr & EP_ADDR_MSK]
202 : &hpcd_->OUT_ep[ep_addr & EP_ADDR_MSK];
203
204 auto buffer = GetBuffer();
205
206 if (buffer.size_ < size)
207 {
208 return ErrorCode::NO_BUFF;
209 }
210
211 ep->xfer_buff = reinterpret_cast<uint8_t*>(buffer.addr_);
212
213 if (UseDoubleBuffer() && GetDirection() == Direction::IN && size > 0)
214 {
215 SwitchBuffer();
216 }
217
218 ep->xfer_len = size;
219 ep->xfer_count = 0U;
220 ep->is_in = is_in ? 1U : 0U;
221 ep->num = ep_addr & EP_ADDR_MSK;
222
223#if defined(USB_OTG_FS) || defined(USB_OTG_HS)
224 if (hpcd_->Init.dma_enable == 1U)
225 {
226 ep->dma_addr = reinterpret_cast<uint32_t>(ep->xfer_buff);
227
228#if defined(__DCACHE_PRESENT) && (__DCACHE_PRESENT == 1U)
229 if (is_in == true)
230 {
231 SCB_CleanDCache_by_Addr(reinterpret_cast<uint32_t*>(buffer.addr_),
232 static_cast<int32_t>(size));
233 }
234#endif
235 }
236#endif
237
238#if defined(USB_BASE)
239 if (is_in)
240 {
241 ep->xfer_fill_db = 1U;
242 ep->xfer_len_db = size;
243 }
244#endif
245
247
248#if defined(USB_OTG_FS) || defined(USB_OTG_HS)
249 auto ans = USB_EPStartXfer(hpcd_->Instance, ep, hpcd_->Init.dma_enable);
250#else
251 auto ans = USB_EPStartXfer(hpcd_->Instance, ep);
252 if (size == 0 && GetNumber() == USB::Endpoint::EPNumber::EP0 &&
254 {
255 OnTransferCompleteCallback(false, 0);
256 }
257#endif
258
259 if (ans == HAL_OK)
260 {
261 return ErrorCode::OK;
262 }
263 else
264 {
266 return ErrorCode::FAILED;
267 }
268}
bool UseDoubleBuffer() const
是否使用双缓冲区 / Use double buffer
Definition ep.hpp:248
virtual void SwitchBuffer()
切换缓冲区 Switch buffer
Definition ep.hpp:372

Field Documentation

◆ hpcd_

PCD_HandleTypeDef* LibXR::STM32Endpoint::hpcd_

Definition at line 36 of file stm32_usb_ep.hpp.

◆ id_

stm32_usb_dev_id_t LibXR::STM32Endpoint::id_

Definition at line 44 of file stm32_usb_ep.hpp.


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