1#include "stm32_usb_ep.hpp"
5#if defined(HAL_PCD_MODULE_ENABLED)
7static inline bool is_power_of_two(
unsigned int n) {
return n > 0 && (n & (n - 1)) == 0; }
9#if defined(USB_OTG_HS) || defined(USB_OTG_FS)
11STM32Endpoint::STM32Endpoint(EPNumber ep_num, stm32_usb_dev_id_t
id,
12 PCD_HandleTypeDef* hpcd, Direction dir,
size_t fifo_size,
14 : Endpoint(ep_num, dir, buffer), hpcd_(hpcd), fifo_size_(fifo_size), id_(id)
16 ASSERT(fifo_size >= 8);
17 ASSERT(is_power_of_two(fifo_size) || fifo_size % 4 == 0);
18 ASSERT(is_power_of_two(buffer.
size_) || buffer.
size_ % 4 == 0);
20#if defined(USB_OTG_HS)
21 if (
id == STM32_USB_OTG_HS)
23 map_otg_hs_[EPNumberToInt8(GetNumber())][
static_cast<uint8_t
>(dir)] =
this;
26#if defined(USB_OTG_FS)
27 if (
id == STM32_USB_OTG_FS)
29 map_otg_fs_[EPNumberToInt8(GetNumber())][
static_cast<uint8_t
>(dir)] =
this;
33 if (dir == Direction::IN)
35 HAL_PCDEx_SetTxFiFo(hpcd_, EPNumberToInt8(GetNumber()), fifo_size / 4);
37 else if (dir == Direction::OUT && ep_num == USB::Endpoint::EPNumber::EP0)
39 HAL_PCDEx_SetRxFiFo(hpcd_, fifo_size / 4);
45STM32Endpoint::STM32Endpoint(EPNumber ep_num, stm32_usb_dev_id_t
id,
46 PCD_HandleTypeDef* hpcd, Direction dir,
47 size_t hw_buffer_offset,
size_t hw_buffer_size,
49 : Endpoint(ep_num, dir, buffer), hpcd_(hpcd), hw_buffer_size_(hw_buffer_size), id_(id)
51 ASSERT(hw_buffer_size >= 8);
53 ASSERT(is_power_of_two(hw_buffer_size));
54 ASSERT(is_power_of_two(buffer.
size_) || buffer.
size_ % 4 == 0);
56 map_fs_[EPNumberToInt8(GetNumber())][
static_cast<uint8_t
>(dir)] =
this;
58 size_t buffer_offset = hw_buffer_offset;
60 HAL_PCDEx_PMAConfig(hpcd_, EPNumberToAddr(GetNumber(), dir), PCD_SNG_BUF,
70 uint8_t type =
static_cast<uint8_t
>(cfg.
type);
72 size_t packet_size_limit = 0;
79#if defined(PCD_SPEED_HIGH_IN_FULL)
80 if (hpcd_->Init.speed == PCD_SPEED_FULL ||
81 hpcd_->Init.speed == PCD_SPEED_HIGH_IN_FULL)
83 if (hpcd_->Init.speed == PCD_SPEED_FULL)
86 packet_size_limit = 64;
90 packet_size_limit = 512;
94#if defined(PCD_SPEED_HIGH_IN_FULL)
95 if (hpcd_->Init.speed == PCD_SPEED_FULL ||
96 hpcd_->Init.speed == PCD_SPEED_HIGH_IN_FULL)
98 if (hpcd_->Init.speed == PCD_SPEED_FULL)
101 packet_size_limit = 64;
105 packet_size_limit = 1024;
109#if defined(PCD_SPEED_HIGH_IN_FULL)
110 if (hpcd_->Init.speed == PCD_SPEED_FULL ||
111 hpcd_->Init.speed == PCD_SPEED_HIGH_IN_FULL)
113 if (hpcd_->Init.speed == PCD_SPEED_FULL)
116 packet_size_limit = 1023;
120 packet_size_limit = 1024;
124 packet_size_limit = 64;
130#if defined(USB_OTG_FS) || defined(USB_OTG_HS)
131 if (packet_size_limit > fifo_size_)
133 packet_size_limit = fifo_size_;
138 if (packet_size_limit > hw_buffer_size_)
140 packet_size_limit = hw_buffer_size_;
146 if (packet_size_limit > buffer.
size_)
148 packet_size_limit = buffer.
size_;
153 if (max_packet_size > packet_size_limit)
155 max_packet_size = packet_size_limit;
158 ep_cfg.max_packet_size = max_packet_size;
160 if (max_packet_size < 8)
165 if (HAL_PCD_EP_Open(hpcd_, addr, max_packet_size, type) == HAL_OK)
178 HAL_PCD_EP_Close(hpcd_, addr);
186 return ErrorCode::BUSY;
192 PCD_EPTypeDef* ep = is_in ? &hpcd_->IN_ep[ep_addr & EP_ADDR_MSK]
193 : &hpcd_->OUT_ep[ep_addr & EP_ADDR_MSK];
197 if (buffer.
size_ < size)
199 return ErrorCode::NO_BUFF;
202 ep->xfer_buff =
reinterpret_cast<uint8_t*
>(buffer.
addr_);
211 ep->is_in = is_in ? 1U : 0U;
212 ep->num = ep_addr & EP_ADDR_MSK;
213 last_transfer_size_ = size;
215#if defined(USB_OTG_FS) || defined(USB_OTG_HS)
216 if (hpcd_->Init.dma_enable == 1U)
218 ep->dma_addr =
reinterpret_cast<uint32_t
>(ep->xfer_buff);
220#if defined(__DCACHE_PRESENT) && (__DCACHE_PRESENT == 1U)
223 SCB_CleanDCache_by_Addr(
reinterpret_cast<uint32_t*
>(buffer.
addr_),
224 static_cast<int32_t
>(size));
233 ep->xfer_fill_db = 0U;
234 ep->xfer_len_db = 0U;
240#if defined(USB_OTG_FS) || defined(USB_OTG_HS)
241 auto ans = USB_EPStartXfer(hpcd_->Instance, ep, hpcd_->Init.dma_enable);
243 auto ans = USB_EPStartXfer(hpcd_->Instance, ep);
253 return ErrorCode::OK;
258 return ErrorCode::FAILED;
266 return ErrorCode::BUSY;
270 if (HAL_PCD_EP_SetStall(hpcd_, addr) == HAL_OK)
273 return ErrorCode::OK;
278 return ErrorCode::FAILED;
286 return ErrorCode::FAILED;
294 return ErrorCode::OK;
297 if (HAL_PCD_EP_ClrStall(hpcd_, addr) == HAL_OK)
300 return ErrorCode::OK;
305 return ErrorCode::FAILED;
323static STM32Endpoint* GetEndpoint(PCD_HandleTypeDef* hpcd, uint8_t epnum,
bool is_in)
325 auto id = STM32USBDeviceGetID(hpcd);
326#if defined(USB_OTG_HS)
327 if (
id == STM32_USB_OTG_HS)
329 return STM32Endpoint::map_otg_hs_[epnum & 0x7F][
static_cast<uint8_t
>(is_in)];
332#if defined(USB_OTG_FS)
333 if (
id == STM32_USB_OTG_FS)
335 return STM32Endpoint::map_otg_fs_[epnum & 0x7F][
static_cast<uint8_t
>(is_in)];
339 if (
id == STM32_USB_FS_DEV)
341 return STM32Endpoint::map_fs_[epnum & 0x7F][
static_cast<uint8_t
>(is_in)];
347extern "C" void HAL_PCD_DataInStageCallback(PCD_HandleTypeDef* hpcd, uint8_t epnum)
349 auto id = STM32USBDeviceGetID(hpcd);
351 ASSERT(
id < STM32_USB_DEV_ID_NUM);
353 auto ep = GetEndpoint(hpcd, epnum,
true);
355 if (!ep || ep->hpcd_ != hpcd)
360 ep->OnTransferCompleteCallback(
true, ep->last_transfer_size_);
363extern "C" void HAL_PCD_DataOutStageCallback(PCD_HandleTypeDef* hpcd, uint8_t epnum)
365 auto id = STM32USBDeviceGetID(hpcd);
367 ASSERT(
id < STM32_USB_DEV_ID_NUM);
369 auto ep = GetEndpoint(hpcd, epnum,
false);
371 if (!ep || ep->hpcd_ != hpcd)
376 PCD_EPTypeDef* ep_handle = &hpcd->OUT_ep[epnum & EP_ADDR_MSK];
378 size_t actual_transfer_size = ep_handle->xfer_count;
380#if defined(__DCACHE_PRESENT) && (__DCACHE_PRESENT == 1U)
381 SCB_InvalidateDCache_by_Addr(ep->GetBuffer().addr_,
382 static_cast<int32_t
>(actual_transfer_size));
385 ep->OnTransferCompleteCallback(
true, actual_transfer_size);
388extern "C" void HAL_PCD_ISOINIncompleteCallback(PCD_HandleTypeDef* hpcd, uint8_t epnum)
390 auto id = STM32USBDeviceGetID(hpcd);
392 ASSERT(
id < STM32_USB_DEV_ID_NUM);
394 auto ep = GetEndpoint(hpcd, epnum,
true);
396 if (!ep || ep->hpcd_ != hpcd)
401 ep->OnTransferCompleteCallback(
true, 0);
404extern "C" void HAL_PCD_ISOOUTIncompleteCallback(PCD_HandleTypeDef* hpcd, uint8_t epnum)
406 auto id = STM32USBDeviceGetID(hpcd);
408 ASSERT(
id < STM32_USB_DEV_ID_NUM);
410 auto ep = GetEndpoint(hpcd, epnum,
false);
412 if (!ep || ep->hpcd_ != hpcd)
417 ep->OnTransferCompleteCallback(
true, 0);
原始数据封装类。 A class for encapsulating raw data.
size_t size_
数据大小(字节)。 The size of the data (in bytes).
void * addr_
数据存储地址。 The storage address of the data.
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
@ 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
端点配置参数 / Endpoint configuration parameters
Type type
端点类型 / Endpoint type
uint16_t max_packet_size
最大包长 / Max packet size
Direction direction
端点方向 / Endpoint direction