1#include "stm32_usb_ep.hpp"
3#include "stm32_dcache.hpp"
6#if defined(HAL_PCD_MODULE_ENABLED)
8static inline bool is_power_of_two(
unsigned int n) {
return n > 0 && (n & (n - 1)) == 0; }
10#if defined(USB_OTG_HS) || defined(USB_OTG_FS)
12STM32Endpoint::STM32Endpoint(EPNumber ep_num, stm32_usb_dev_id_t
id,
13 PCD_HandleTypeDef* hpcd, Direction dir,
size_t fifo_size,
15 : Endpoint(ep_num, dir, buffer), hpcd_(hpcd), fifo_size_(fifo_size), id_(id)
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);
21#if defined(USB_OTG_HS)
22 if (
id == STM32_USB_OTG_HS)
24 map_otg_hs_[EPNumberToInt8(GetNumber())][
static_cast<uint8_t
>(dir)] =
this;
27#if defined(USB_OTG_FS)
28 if (
id == STM32_USB_OTG_FS)
30 map_otg_fs_[EPNumberToInt8(GetNumber())][
static_cast<uint8_t
>(dir)] =
this;
34 if (dir == Direction::IN)
36 HAL_PCDEx_SetTxFiFo(hpcd_, EPNumberToInt8(GetNumber()), fifo_size / 4);
38 else if (dir == Direction::OUT && ep_num == USB::Endpoint::EPNumber::EP0)
40 HAL_PCDEx_SetRxFiFo(hpcd_, fifo_size / 4);
45#if defined(USB_BASE) || defined(USB_DRD_FS)
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,
50 : Endpoint(ep_num, dir, buffer), hpcd_(hpcd), hw_buffer_size_(hw_buffer_size), id_(id)
52 ASSERT(hw_buffer_size >= 8);
54 ASSERT(is_power_of_two(hw_buffer_size));
55 ASSERT(is_power_of_two(buffer.
size_) || buffer.
size_ % 4 == 0);
57 map_fs_[EPNumberToInt8(GetNumber())][
static_cast<uint8_t
>(dir)] =
this;
59 size_t buffer_offset = hw_buffer_offset;
61 HAL_PCDEx_PMAConfig(hpcd_, EPNumberToAddr(GetNumber(), dir), PCD_SNG_BUF,
71 uint8_t type =
static_cast<uint8_t
>(cfg.
type);
73 size_t packet_size_limit = 0;
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)
84 if (hpcd_->Init.speed == PCD_SPEED_FULL)
87 packet_size_limit = 64;
91 packet_size_limit = 512;
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)
99 if (hpcd_->Init.speed == PCD_SPEED_FULL)
102 packet_size_limit = 64;
106 packet_size_limit = 1024;
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)
114 if (hpcd_->Init.speed == PCD_SPEED_FULL)
117 packet_size_limit = 1023;
121 packet_size_limit = 1024;
125 packet_size_limit = 64;
131#if defined(USB_OTG_FS) || defined(USB_OTG_HS)
132 if (packet_size_limit > fifo_size_)
134 packet_size_limit = fifo_size_;
138#if defined(USB_BASE) || defined(USB_DRD_FS)
139 if (packet_size_limit > hw_buffer_size_)
141 packet_size_limit = hw_buffer_size_;
147 if (packet_size_limit > buffer.
size_)
149 packet_size_limit = buffer.
size_;
154 if (max_packet_size > packet_size_limit)
156 max_packet_size = packet_size_limit;
159 ep_cfg.max_packet_size = max_packet_size;
161 if (max_packet_size < 8)
166 if (HAL_PCD_EP_Open(hpcd_, addr, max_packet_size, type) == HAL_OK)
176#if defined(USB_OTG_FS) || defined(USB_OTG_HS)
177static bool wait_otg_in_ep_flag(USB_OTG_INEndpointTypeDef* in_ep, uint32_t flag)
181 constexpr uint32_t MAX_POLLS = 10000U;
182 for (uint32_t i = 0U; i < MAX_POLLS; ++i)
184 if ((in_ep->DIEPINT & flag) != 0U)
192static void disable_otg_in_ep(PCD_HandleTypeDef* hpcd, uint8_t ep_num)
194 const uintptr_t base =
reinterpret_cast<uintptr_t
>(hpcd->Instance);
195 auto* in_ep =
reinterpret_cast<USB_OTG_INEndpointTypeDef*
>(
196 base + USB_OTG_IN_ENDPOINT_BASE + ep_num * USB_OTG_EP_REG_SIZE);
197 auto* device =
reinterpret_cast<USB_OTG_DeviceTypeDef*
>(base + USB_OTG_DEVICE_BASE);
199 if ((in_ep->DIEPCTL & USB_OTG_DIEPCTL_EPENA) == 0U)
211 device->DIEPEMPMSK &= ~(1UL << ep_num);
212 in_ep->DIEPCTL |= USB_OTG_DIEPCTL_SNAK;
213 (void)wait_otg_in_ep_flag(in_ep, USB_OTG_DIEPINT_INEPNE);
214 in_ep->DIEPCTL |= USB_OTG_DIEPCTL_EPDIS | USB_OTG_DIEPCTL_SNAK;
215 (void)wait_otg_in_ep_flag(in_ep, USB_OTG_DIEPINT_EPDISD);
216 in_ep->DIEPINT = USB_OTG_DIEPINT_INEPNE | USB_OTG_DIEPINT_EPDISD;
217 (void)USB_FlushTxFifo(hpcd->Instance, ep_num);
224#if defined(USB_OTG_FS) || defined(USB_OTG_HS)
226#if defined(USB_OTG_FS)
227 is_otg = is_otg || id_ == STM32_USB_OTG_FS;
229#if defined(USB_OTG_HS)
230 is_otg = is_otg || id_ == STM32_USB_OTG_HS;
237 HAL_PCD_EP_Close(hpcd_, addr);
251 PCD_EPTypeDef* ep = is_in ? &hpcd_->IN_ep[ep_addr & EP_ADDR_MSK]
252 : &hpcd_->OUT_ep[ep_addr & EP_ADDR_MSK];
256 if (buffer.
size_ < size)
261 ep->xfer_buff =
reinterpret_cast<uint8_t*
>(buffer.
addr_);
270 ep->is_in = is_in ? 1U : 0U;
271 ep->num = ep_addr & EP_ADDR_MSK;
272 last_transfer_size_ = size;
274#if defined(USB_OTG_FS) || defined(USB_OTG_HS)
275 if (hpcd_->Init.dma_enable == 1U)
277 ep->dma_addr =
reinterpret_cast<uint32_t
>(ep->xfer_buff);
286#if defined(USB_BASE) || defined(USB_DRD_FS)
289 ep->xfer_fill_db = 0U;
290 ep->xfer_len_db = 0U;
296#if defined(USB_OTG_FS) || defined(USB_OTG_HS)
297 auto ans = USB_EPStartXfer(hpcd_->Instance, ep, hpcd_->Init.dma_enable);
299 auto ans = USB_EPStartXfer(hpcd_->Instance, ep);
327 if (HAL_PCD_EP_SetStall(hpcd_, addr) == HAL_OK)
354 if (HAL_PCD_EP_ClrStall(hpcd_, addr) == HAL_OK)
380static STM32Endpoint* GetEndpoint(PCD_HandleTypeDef* hpcd, uint8_t epnum,
bool is_in)
382 auto id = STM32USBDeviceGetID(hpcd);
383#if defined(USB_OTG_HS)
384 if (
id == STM32_USB_OTG_HS)
386 return STM32Endpoint::map_otg_hs_[epnum & 0x7F][
static_cast<uint8_t
>(is_in)];
389#if defined(USB_OTG_FS)
390 if (
id == STM32_USB_OTG_FS)
392 return STM32Endpoint::map_otg_fs_[epnum & 0x7F][
static_cast<uint8_t
>(is_in)];
395#if defined(USB_BASE) || defined(USB_DRD_FS)
396 if (
id == STM32_USB_FS_DEV)
398 return STM32Endpoint::map_fs_[epnum & 0x7F][
static_cast<uint8_t
>(is_in)];
404extern "C" void HAL_PCD_DataInStageCallback(PCD_HandleTypeDef* hpcd, uint8_t epnum)
406 auto id = STM32USBDeviceGetID(hpcd);
408 if (
id >= STM32_USB_DEV_ID_NUM || STM32USBDevice::map_[
id] ==
nullptr)
413 auto ep = GetEndpoint(hpcd, epnum,
true);
415 if (!ep || ep->hpcd_ != hpcd)
420 ep->OnTransferCompleteCallback(
true, ep->last_transfer_size_);
423extern "C" void HAL_PCD_DataOutStageCallback(PCD_HandleTypeDef* hpcd, uint8_t epnum)
425 auto id = STM32USBDeviceGetID(hpcd);
427 if (
id >= STM32_USB_DEV_ID_NUM || STM32USBDevice::map_[
id] ==
nullptr)
432 auto ep = GetEndpoint(hpcd, epnum,
false);
434 if (!ep || ep->hpcd_ != hpcd)
439 PCD_EPTypeDef* ep_handle = &hpcd->OUT_ep[epnum & EP_ADDR_MSK];
441 size_t actual_transfer_size = ep_handle->xfer_count;
443 if (STM32USBUsesDma(hpcd))
448 ep->OnTransferCompleteCallback(
true, actual_transfer_size);
451extern "C" void HAL_PCD_ISOINIncompleteCallback(PCD_HandleTypeDef* hpcd, uint8_t epnum)
453 auto id = STM32USBDeviceGetID(hpcd);
455 if (
id >= STM32_USB_DEV_ID_NUM || STM32USBDevice::map_[
id] ==
nullptr)
460 auto ep = GetEndpoint(hpcd, epnum,
true);
462 if (!ep || ep->hpcd_ != hpcd)
467 ep->OnTransferCompleteCallback(
true, 0);
470extern "C" void HAL_PCD_ISOOUTIncompleteCallback(PCD_HandleTypeDef* hpcd, uint8_t epnum)
472 auto id = STM32USBDeviceGetID(hpcd);
474 if (
id >= STM32_USB_DEV_ID_NUM || STM32USBDevice::map_[
id] ==
nullptr)
479 auto ep = GetEndpoint(hpcd, epnum,
false);
481 if (!ep || ep->hpcd_ != hpcd)
486 ep->OnTransferCompleteCallback(
true, 0);
可写原始数据视图 / 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
static constexpr uint8_t EPNumberToInt8(EPNumber ep)
端点号转换为 uint8_t / Convert endpoint number to uint8_t
@ 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
bool UseDoubleBuffer() const
是否启用双缓冲 / Whether double buffer is enabled
virtual void SwitchBuffer()
切换双缓冲 / Switch double buffer
Config & GetConfig()
获取当前配置引用 / Get endpoint config reference
void OnTransferCompleteCallback(bool in_isr, size_t actual_transfer_size)
由底层在传输完成时调用 / Called by low-level driver when transfer completes
@ ISOCHRONOUS
等时端点 / Isochronous
@ INTERRUPT
中断端点 / Interrupt
EPNumber GetNumber() const
获取端点号 / Get endpoint number
uint16_t MaxPacketSize() const
获取最大包长 / Get max packet size
void SetState(State state)
设置端点状态 / Set endpoint state
State GetState() const
获取端点状态 / Get endpoint state
RawData GetBuffer() const
获取当前可用于传输的缓冲区 / Get current transfer buffer
LibXR 命名空间 / LibXR namespace.
void STM32_InvalidateDCacheByAddr(const void *addr, size_t size)
失效指定内存范围覆盖的 D-Cache cache line Invalidates D-Cache lines covering the specified memory range
ErrorCode
定义错误码枚举 / Defines an enumeration for error codes
@ NO_BUFF
缓冲区不足 / Insufficient buffer
@ FAILED
操作失败 / Operation failed
@ OK
操作成功 / Operation successful
void STM32_CleanDCacheByAddr(const void *addr, size_t size)
清理指定内存范围覆盖的 D-Cache cache line Cleans D-Cache lines covering the specified memory range
端点配置参数 / Endpoint configuration parameters
Type type
端点类型 / Endpoint type
uint16_t max_packet_size
最大包长 / Max packet size
Direction direction
端点方向 / Endpoint direction