libxr  1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
stm32_usb_ep.cpp
1#include "stm32_usb_ep.hpp"
2
3#include "stm32_dcache.hpp"
4using namespace LibXR;
5
6#if defined(HAL_PCD_MODULE_ENABLED)
7
8static inline bool is_power_of_two(unsigned int n) { return n > 0 && (n & (n - 1)) == 0; }
9
10#if defined(USB_OTG_HS) || defined(USB_OTG_FS)
11
12STM32Endpoint::STM32Endpoint(EPNumber ep_num, stm32_usb_dev_id_t id,
13 PCD_HandleTypeDef* hpcd, Direction dir, size_t fifo_size,
14 LibXR::RawData buffer)
15 : Endpoint(ep_num, dir, buffer), hpcd_(hpcd), fifo_size_(fifo_size), id_(id)
16{
17 ASSERT(fifo_size >= 8);
18 ASSERT(is_power_of_two(fifo_size) || fifo_size % 4 == 0);
19 ASSERT(is_power_of_two(buffer.size_) || buffer.size_ % 4 == 0);
20
21#if defined(USB_OTG_HS)
22 if (id == STM32_USB_OTG_HS)
23 {
24 map_otg_hs_[EPNumberToInt8(GetNumber())][static_cast<uint8_t>(dir)] = this;
25 }
26#endif
27#if defined(USB_OTG_FS)
28 if (id == STM32_USB_OTG_FS)
29 {
30 map_otg_fs_[EPNumberToInt8(GetNumber())][static_cast<uint8_t>(dir)] = this;
31 }
32#endif
33
34 if (dir == Direction::IN)
35 {
36 HAL_PCDEx_SetTxFiFo(hpcd_, EPNumberToInt8(GetNumber()), fifo_size / 4);
37 }
38 else if (dir == Direction::OUT && ep_num == USB::Endpoint::EPNumber::EP0)
39 {
40 HAL_PCDEx_SetRxFiFo(hpcd_, fifo_size / 4);
41 }
42}
43#endif
44
45#if defined(USB_BASE)
46STM32Endpoint::STM32Endpoint(EPNumber ep_num, stm32_usb_dev_id_t id,
47 PCD_HandleTypeDef* hpcd, Direction dir,
48 size_t hw_buffer_offset, size_t hw_buffer_size,
49 LibXR::RawData buffer)
50 : Endpoint(ep_num, dir, buffer), hpcd_(hpcd), hw_buffer_size_(hw_buffer_size), id_(id)
51{
52 ASSERT(hw_buffer_size >= 8);
53
54 ASSERT(is_power_of_two(hw_buffer_size));
55 ASSERT(is_power_of_two(buffer.size_) || buffer.size_ % 4 == 0);
56
57 map_fs_[EPNumberToInt8(GetNumber())][static_cast<uint8_t>(dir)] = this;
58
59 size_t buffer_offset = hw_buffer_offset;
60
61 HAL_PCDEx_PMAConfig(hpcd_, EPNumberToAddr(GetNumber(), dir), PCD_SNG_BUF,
62 buffer_offset);
63}
64#endif
65
67{
68 ASSERT(cfg.direction == Direction::IN || cfg.direction == Direction::OUT);
69
70 uint8_t addr = EPNumberToAddr(GetNumber(), cfg.direction);
71 uint8_t type = static_cast<uint8_t>(cfg.type);
72 auto& ep_cfg = GetConfig();
73 size_t packet_size_limit = 0;
74
75 ep_cfg = cfg;
76
77 switch (cfg.type)
78 {
79 case Type::BULK:
80#if defined(PCD_SPEED_HIGH_IN_FULL)
81 if (hpcd_->Init.speed == PCD_SPEED_FULL ||
82 hpcd_->Init.speed == PCD_SPEED_HIGH_IN_FULL)
83#else
84 if (hpcd_->Init.speed == PCD_SPEED_FULL)
85#endif
86 {
87 packet_size_limit = 64;
88 }
89 else
90 {
91 packet_size_limit = 512;
92 }
93 break;
94 case Type::INTERRUPT:
95#if defined(PCD_SPEED_HIGH_IN_FULL)
96 if (hpcd_->Init.speed == PCD_SPEED_FULL ||
97 hpcd_->Init.speed == PCD_SPEED_HIGH_IN_FULL)
98#else
99 if (hpcd_->Init.speed == PCD_SPEED_FULL)
100#endif
101 {
102 packet_size_limit = 64;
103 }
104 else
105 {
106 packet_size_limit = 1024;
107 }
108 break;
110#if defined(PCD_SPEED_HIGH_IN_FULL)
111 if (hpcd_->Init.speed == PCD_SPEED_FULL ||
112 hpcd_->Init.speed == PCD_SPEED_HIGH_IN_FULL)
113#else
114 if (hpcd_->Init.speed == PCD_SPEED_FULL)
115#endif
116 {
117 packet_size_limit = 1023;
118 }
119 else
120 {
121 packet_size_limit = 1024;
122 }
123 break;
124 case Type::CONTROL:
125 packet_size_limit = 64;
126 break;
127 default:
128 break;
129 }
130
131#if defined(USB_OTG_FS) || defined(USB_OTG_HS)
132 if (packet_size_limit > fifo_size_)
133 {
134 packet_size_limit = fifo_size_;
135 }
136#endif
137
138#if defined(USB_BASE)
139 if (packet_size_limit > hw_buffer_size_)
140 {
141 packet_size_limit = hw_buffer_size_;
142 }
143#endif
144
145 auto buffer = GetBuffer();
146
147 if (packet_size_limit > buffer.size_)
148 {
149 packet_size_limit = buffer.size_;
150 }
151
152 size_t max_packet_size = cfg.max_packet_size;
153
154 if (max_packet_size > packet_size_limit)
155 {
156 max_packet_size = packet_size_limit;
157 }
158
159 ep_cfg.max_packet_size = max_packet_size;
160
161 if (max_packet_size < 8)
162 {
163 max_packet_size = 8;
164 }
165
166 if (HAL_PCD_EP_Open(hpcd_, addr, max_packet_size, type) == HAL_OK)
167 {
169 }
170 else
171 {
173 }
174}
175
177{
178 uint8_t addr = EPNumberToAddr(GetNumber(), GetDirection());
179 HAL_PCD_EP_Close(hpcd_, addr);
181}
182
184{
185 if (GetState() == State::BUSY)
186 {
187 return ErrorCode::BUSY;
188 }
189
190 bool is_in = GetDirection() == Direction::IN;
191 auto ep_addr = EPNumberToAddr(GetNumber(), GetDirection());
192
193 PCD_EPTypeDef* ep = is_in ? &hpcd_->IN_ep[ep_addr & EP_ADDR_MSK]
194 : &hpcd_->OUT_ep[ep_addr & EP_ADDR_MSK];
195
196 auto buffer = GetBuffer();
197
198 if (buffer.size_ < size)
199 {
200 return ErrorCode::NO_BUFF;
201 }
202
203 ep->xfer_buff = reinterpret_cast<uint8_t*>(buffer.addr_);
204
205 if (UseDoubleBuffer() && GetDirection() == Direction::IN && size > 0)
206 {
207 SwitchBuffer();
208 }
209
210 ep->xfer_len = size;
211 ep->xfer_count = 0U;
212 ep->is_in = is_in ? 1U : 0U;
213 ep->num = ep_addr & EP_ADDR_MSK;
214 last_transfer_size_ = size;
215
216#if defined(USB_OTG_FS) || defined(USB_OTG_HS)
217 if (hpcd_->Init.dma_enable == 1U)
218 {
219 ep->dma_addr = reinterpret_cast<uint32_t>(ep->xfer_buff);
220
221 if (is_in == true)
222 {
223 STM32_CleanDCacheByAddr(buffer.addr_, size);
224 }
225 }
226#endif
227
228#if defined(USB_BASE)
229 if (is_in)
230 {
231 ep->xfer_fill_db = 0U;
232 ep->xfer_len_db = 0U;
233 }
234#endif
235
237
238#if defined(USB_OTG_FS) || defined(USB_OTG_HS)
239 auto ans = USB_EPStartXfer(hpcd_->Instance, ep, hpcd_->Init.dma_enable);
240#else
241 auto ans = USB_EPStartXfer(hpcd_->Instance, ep);
242 if (size == 0 && GetNumber() == USB::Endpoint::EPNumber::EP0 &&
244 {
246 }
247#endif
248
249 if (ans == HAL_OK)
250 {
251 return ErrorCode::OK;
252 }
253 else
254 {
256 return ErrorCode::FAILED;
257 }
258}
259
261{
262 const bool is_in = (GetDirection() == Direction::IN);
263 if (GetState() != State::IDLE && !(GetState() == State::BUSY && !is_in))
264 {
265 return ErrorCode::BUSY;
266 }
267
268 uint8_t addr = EPNumberToAddr(GetNumber(), GetDirection());
269 if (HAL_PCD_EP_SetStall(hpcd_, addr) == HAL_OK)
270 {
272 return ErrorCode::OK;
273 }
274 else
275 {
277 return ErrorCode::FAILED;
278 }
279}
280
282{
283 if (GetState() != State::STALLED)
284 {
285 return ErrorCode::FAILED;
286 }
287
288 uint8_t addr = EPNumberToAddr(GetNumber(), GetDirection());
289
291 {
293 return ErrorCode::OK;
294 }
295
296 if (HAL_PCD_EP_ClrStall(hpcd_, addr) == HAL_OK)
297 {
299 return ErrorCode::OK;
300 }
301 else
302 {
304 return ErrorCode::FAILED;
305 }
306}
307
309{
311 {
312 return MaxPacketSize();
313 }
314 else
315 {
316 return GetBuffer().size_;
317 }
318}
319
320// --- HAL C 回调桥接 ---
321// NOLINTNEXTLINE
322static STM32Endpoint* GetEndpoint(PCD_HandleTypeDef* hpcd, uint8_t epnum, bool is_in)
323{
324 auto id = STM32USBDeviceGetID(hpcd);
325#if defined(USB_OTG_HS)
326 if (id == STM32_USB_OTG_HS)
327 {
328 return STM32Endpoint::map_otg_hs_[epnum & 0x7F][static_cast<uint8_t>(is_in)];
329 }
330#endif
331#if defined(USB_OTG_FS)
332 if (id == STM32_USB_OTG_FS)
333 {
334 return STM32Endpoint::map_otg_fs_[epnum & 0x7F][static_cast<uint8_t>(is_in)];
335 }
336#endif
337#if defined(USB_BASE)
338 if (id == STM32_USB_FS_DEV)
339 {
340 return STM32Endpoint::map_fs_[epnum & 0x7F][static_cast<uint8_t>(is_in)];
341 }
342#endif
343 return nullptr;
344}
345
346extern "C" void HAL_PCD_DataInStageCallback(PCD_HandleTypeDef* hpcd, uint8_t epnum)
347{
348 auto id = STM32USBDeviceGetID(hpcd);
349
350 if (id >= STM32_USB_DEV_ID_NUM || STM32USBDevice::map_[id] == nullptr)
351 {
352 return;
353 }
354
355 auto ep = GetEndpoint(hpcd, epnum, true);
356
357 if (!ep || ep->hpcd_ != hpcd)
358 {
359 return;
360 }
361
362 ep->OnTransferCompleteCallback(true, ep->last_transfer_size_);
363}
364
365extern "C" void HAL_PCD_DataOutStageCallback(PCD_HandleTypeDef* hpcd, uint8_t epnum)
366{
367 auto id = STM32USBDeviceGetID(hpcd);
368
369 if (id >= STM32_USB_DEV_ID_NUM || STM32USBDevice::map_[id] == nullptr)
370 {
371 return;
372 }
373
374 auto ep = GetEndpoint(hpcd, epnum, false);
375
376 if (!ep || ep->hpcd_ != hpcd)
377 {
378 return;
379 }
380
381 PCD_EPTypeDef* ep_handle = &hpcd->OUT_ep[epnum & EP_ADDR_MSK];
382
383 size_t actual_transfer_size = ep_handle->xfer_count;
384
385 if (STM32USBUsesDma(hpcd))
386 {
387 STM32_InvalidateDCacheByAddr(ep->GetBuffer().addr_, actual_transfer_size);
388 }
389
390 ep->OnTransferCompleteCallback(true, actual_transfer_size);
391}
392
393extern "C" void HAL_PCD_ISOINIncompleteCallback(PCD_HandleTypeDef* hpcd, uint8_t epnum)
394{
395 auto id = STM32USBDeviceGetID(hpcd);
396
397 if (id >= STM32_USB_DEV_ID_NUM || STM32USBDevice::map_[id] == nullptr)
398 {
399 return;
400 }
401
402 auto ep = GetEndpoint(hpcd, epnum, true);
403
404 if (!ep || ep->hpcd_ != hpcd)
405 {
406 return;
407 }
408
409 ep->OnTransferCompleteCallback(true, 0);
410}
411
412extern "C" void HAL_PCD_ISOOUTIncompleteCallback(PCD_HandleTypeDef* hpcd, uint8_t epnum)
413{
414 auto id = STM32USBDeviceGetID(hpcd);
415
416 if (id >= STM32_USB_DEV_ID_NUM || STM32USBDevice::map_[id] == nullptr)
417 {
418 return;
419 }
420
421 auto ep = GetEndpoint(hpcd, epnum, false);
422
423 if (!ep || ep->hpcd_ != hpcd)
424 {
425 return;
426 }
427
428 ep->OnTransferCompleteCallback(true, 0);
429}
430#endif
可写原始数据视图 / Mutable raw data view
size_t size_
数据字节数 / Data size in bytes
void * addr_
数据起始地址 / Data start address
STM32 USB 端点实现 / STM32 USB endpoint implementation.
ErrorCode Transfer(size_t size) override
启动一次传输 / Start a transfer
ErrorCode ClearStall() override
清除 STALL / Clear stall
void Configure(const Config &cfg) override
配置端点协议参数 / Configure endpoint protocol parameters
void Close() override
关闭端点 / Close endpoint
ErrorCode Stall() override
置 STALL / Stall endpoint
size_t MaxTransferSize() const override
返回当前最大可传输字节数 / Return maximum transferable size at this time
@ EP0
端点 0 / Endpoint 0
@ IN
输入方向 / IN direction
@ OUT
输出方向 / OUT direction
static constexpr uint8_t EPNumberToAddr(EPNumber ep, Direction dir)
端点号转换为端点地址 / Convert endpoint number to endpoint address
Definition ep.hpp:105
Direction GetDirection() const
获取当前端点方向 / Get current endpoint direction
Definition ep.hpp:182
bool UseDoubleBuffer() const
是否启用双缓冲 / Whether double buffer is enabled
Definition ep.hpp:240
virtual void SwitchBuffer()
切换双缓冲 / Switch double buffer
Definition ep.hpp:521
Config & GetConfig()
获取当前配置引用 / Get endpoint config reference
Definition ep.hpp:516
void OnTransferCompleteCallback(bool in_isr, size_t actual_transfer_size)
由底层在传输完成时调用 / Called by low-level driver when transfer completes
Definition ep.hpp:393
@ ISOCHRONOUS
等时端点 / Isochronous
@ BULK
批量端点 / Bulk
@ INTERRUPT
中断端点 / Interrupt
@ CONTROL
控制端点 / Control
EPNumber GetNumber() const
获取端点号 / Get endpoint number
Definition ep.hpp:170
uint16_t MaxPacketSize() const
获取最大包长 / Get max packet size
Definition ep.hpp:226
@ DISABLED
禁用 / Disabled
@ STALLED
停止/挂起 / Stalled
void SetState(State state)
设置端点状态 / Set endpoint state
Definition ep.hpp:214
State GetState() const
获取端点状态 / Get endpoint state
Definition ep.hpp:208
RawData GetBuffer() const
获取当前可用于传输的缓冲区 / Get current transfer buffer
Definition ep.hpp:246
LibXR 命名空间
Definition ch32_can.hpp:14
void STM32_InvalidateDCacheByAddr(const void *addr, size_t size)
Invalidates D-Cache lines covering the specified memory range.
ErrorCode
定义错误码枚举
@ BUSY
忙碌 | Busy
@ NO_BUFF
缓冲区不足 | Insufficient buffer
@ FAILED
操作失败 | Operation failed
@ OK
操作成功 | Operation successful
void STM32_CleanDCacheByAddr(const void *addr, size_t size)
Cleans D-Cache lines covering the specified memory range.
端点配置参数 / Endpoint configuration parameters
Definition ep.hpp:139
Type type
端点类型 / Endpoint type
Definition ep.hpp:141
uint16_t max_packet_size
最大包长 / Max packet size
Definition ep.hpp:142
Direction direction
端点方向 / Endpoint direction
Definition ep.hpp:140