Skip to content

Commit fe8c170

Browse files
committed
Add dcd_edpt_iso_alloc() / dcd_edpt_iso_activate() for ISO EP buffer management.
1 parent cece59d commit fe8c170

File tree

4 files changed

+94
-14
lines changed

4 files changed

+94
-14
lines changed

src/device/dcd.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,12 @@ void dcd_edpt_stall (uint8_t rhport, uint8_t ep_addr);
167167
// This API never calls with control endpoints, since it is auto cleared when receiving setup packet
168168
void dcd_edpt_clear_stall (uint8_t rhport, uint8_t ep_addr);
169169

170+
// Allocate packet buffer used by ISO endpoints
171+
// Some MCU need manual packet buffer allocation, we allocation largest size to avoid clustering
172+
bool dcd_edpt_iso_alloc(uint8_t rhport, uint8_t ep_addr, uint16_t largest_packet_size);
173+
174+
// Configure and enable an ISO endpoint according to descriptor
175+
bool dcd_edpt_iso_activate(uint8_t rhport, tusb_desc_endpoint_t const * p_endpoint_desc);
170176
//--------------------------------------------------------------------+
171177
// Event API (implemented by stack)
172178
//--------------------------------------------------------------------+

src/device/usbd.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1380,4 +1380,23 @@ void usbd_sof_enable(uint8_t rhport, bool en)
13801380
dcd_sof_enable(rhport, en);
13811381
}
13821382

1383+
bool usbd_edpt_iso_alloc(uint8_t rhport, uint8_t ep_addr, uint16_t largest_packet_size)
1384+
{
1385+
rhport = _usbd_rhport;
1386+
1387+
TU_ASSERT(tu_edpt_number(ep_addr) < CFG_TUD_ENDPPOINT_MAX);
1388+
1389+
return dcd_edpt_iso_alloc(rhport, ep_addr, largest_packet_size);
1390+
}
1391+
1392+
bool usbd_edpt_iso_activate(uint8_t rhport, tusb_desc_endpoint_t const * desc_ep)
1393+
{
1394+
rhport = _usbd_rhport;
1395+
1396+
TU_ASSERT(tu_edpt_number(desc_ep->bEndpointAddress) < CFG_TUD_ENDPPOINT_MAX);
1397+
TU_ASSERT(tu_edpt_validate(desc_ep, (tusb_speed_t) _usbd_dev.speed));
1398+
1399+
return dcd_edpt_iso_activate(rhport, desc_ep);
1400+
}
1401+
13831402
#endif

src/device/usbd_pvt.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,12 @@ void usbd_edpt_clear_stall(uint8_t rhport, uint8_t ep_addr);
9696
// Check if endpoint is stalled
9797
bool usbd_edpt_stalled(uint8_t rhport, uint8_t ep_addr);
9898

99+
// Allocate packet buffer used by ISO endpoints
100+
bool usbd_edpt_iso_alloc(uint8_t rhport, uint8_t ep_addr, uint16_t largest_packet_size);
101+
102+
// Configure and enable an ISO endpoint according to descriptor
103+
bool usbd_edpt_iso_activate(uint8_t rhport, tusb_desc_endpoint_t const * p_endpoint_desc);
104+
99105
// Check if endpoint is ready (not busy and not stalled)
100106
TU_ATTR_ALWAYS_INLINE static inline
101107
bool usbd_edpt_ready(uint8_t rhport, uint8_t ep_addr)

src/portable/st/stm32_fsdev/dcd_stm32_fsdev.c

Lines changed: 63 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ static void dcd_pma_alloc_reset(void);
209209
static uint16_t dcd_pma_alloc(uint8_t ep_addr, size_t length);
210210
static void dcd_pma_free(uint8_t ep_addr);
211211
static void dcd_ep_free(uint8_t ep_addr);
212-
static uint8_t dcd_ep_alloc(tusb_desc_endpoint_t const * p_endpoint_desc);
212+
static uint8_t dcd_ep_alloc(uint8_t ep_addr, uint8_t ep_type);
213213
static bool dcd_write_packet_memory(uint16_t dst, const void *__restrict src, size_t wNBytes);
214214
static bool dcd_read_packet_memory(void *__restrict dst, uint16_t src, size_t wNBytes);
215215

@@ -758,13 +758,15 @@ static uint16_t dcd_pma_alloc(uint8_t ep_addr, size_t length)
758758
TU_ASSERT(length <= epXferCtl->pma_alloc_size, 0xFFFF); // Verify no larger than previous alloc
759759
return epXferCtl->pma_ptr;
760760
}
761-
761+
762+
open_ep_count++;
763+
762764
uint16_t addr = ep_buf_ptr;
763765
ep_buf_ptr = (uint16_t)(ep_buf_ptr + length); // increment buffer pointer
764-
766+
765767
// Verify no overflow
766768
TU_ASSERT(ep_buf_ptr <= PMA_LENGTH, 0xFFFF);
767-
769+
768770
epXferCtl->pma_ptr = addr;
769771
epXferCtl->pma_alloc_size = length;
770772
//TU_LOG2("dcd_pma_alloc(%x,%x)=%x\r\n",ep_addr,length,addr);
@@ -802,29 +804,28 @@ static void dcd_pma_free(uint8_t ep_addr)
802804
/***
803805
* Allocate hardware endpoint
804806
*/
805-
static uint8_t dcd_ep_alloc(tusb_desc_endpoint_t const * p_endpoint_desc)
807+
static uint8_t dcd_ep_alloc(uint8_t ep_addr, uint8_t ep_type)
806808
{
807-
uint8_t const epnum = tu_edpt_number(p_endpoint_desc->bEndpointAddress);
808-
uint8_t const dir = tu_edpt_dir(p_endpoint_desc->bEndpointAddress);
809-
uint8_t const eptype = p_endpoint_desc->bmAttributes.xfer;
809+
uint8_t const epnum = tu_edpt_number(ep_addr);
810+
uint8_t const dir = tu_edpt_dir(ep_addr);
810811

811812
for(uint8_t i = 0; i < STFSDEV_EP_COUNT; i++)
812813
{
813814
// If EP of current direction is not allocated
814815
// Except for ISO endpoint, both direction should be free
815816
if(!ep_alloc_status[i].allocated[dir] &&
816-
(eptype != TUSB_XFER_ISOCHRONOUS || !ep_alloc_status[i].allocated[dir ^ 1]))
817+
(ep_type != TUSB_XFER_ISOCHRONOUS || !ep_alloc_status[i].allocated[dir ^ 1]))
817818
{
818819
// Check if EP number is the same
819820
if(ep_alloc_status[i].ep_num == 0xFF ||
820821
ep_alloc_status[i].ep_num == epnum)
821822
{
822823
// One EP pair has to be the same type
823824
if(ep_alloc_status[i].ep_type == 0xFF ||
824-
ep_alloc_status[i].ep_type == eptype)
825+
ep_alloc_status[i].ep_type == ep_type)
825826
{
826827
ep_alloc_status[i].ep_num = epnum;
827-
ep_alloc_status[i].ep_type = eptype;
828+
ep_alloc_status[i].ep_type = ep_type;
828829
ep_alloc_status[i].allocated[dir] = true;
829830

830831
return i;
@@ -871,7 +872,7 @@ static void dcd_ep_free(uint8_t ep_addr)
871872
bool dcd_edpt_open (uint8_t rhport, tusb_desc_endpoint_t const * p_endpoint_desc)
872873
{
873874
(void)rhport;
874-
uint8_t const epnum = dcd_ep_alloc(p_endpoint_desc);
875+
uint8_t const epnum = dcd_ep_alloc(p_endpoint_desc->bEndpointAddress, p_endpoint_desc->bmAttributes.xfer);
875876
uint8_t const dir = tu_edpt_dir(p_endpoint_desc->bEndpointAddress);
876877
const uint16_t buffer_size = pcd_aligned_buffer_size(tu_edpt_packet_size(p_endpoint_desc));
877878
uint16_t pma_addr;
@@ -900,8 +901,6 @@ bool dcd_edpt_open (uint8_t rhport, tusb_desc_endpoint_t const * p_endpoint_desc
900901
TU_ASSERT(false);
901902
}
902903

903-
open_ep_count++;
904-
905904
pcd_set_eptype(USB, epnum, wType);
906905
pcd_set_ep_address(USB, epnum, tu_edpt_number(p_endpoint_desc->bEndpointAddress));
907906
// Be normal, for now, instead of only accepting zero-byte packets (on control endpoint)
@@ -983,6 +982,56 @@ void dcd_edpt_close (uint8_t rhport, uint8_t ep_addr)
983982
dcd_pma_free(ep_addr);
984983
}
985984

985+
bool dcd_edpt_iso_alloc(uint8_t rhport, uint8_t ep_addr, uint16_t largest_packet_size)
986+
{
987+
(void)rhport;
988+
989+
TU_ASSERT(largest_packet_size <= 1024);
990+
991+
uint8_t const epnum = dcd_ep_alloc(ep_addr, TUSB_XFER_ISOCHRONOUS);
992+
const uint16_t buffer_size = pcd_aligned_buffer_size(largest_packet_size);
993+
994+
/* Create a packet memory buffer area. For isochronous endpoints,
995+
* use the same buffer as the double buffer, essentially disabling double buffering */
996+
dcd_pma_alloc(ep_addr, buffer_size);
997+
998+
xfer_ctl_ptr(ep_addr)->epnum = epnum;
999+
1000+
return true;
1001+
}
1002+
1003+
bool dcd_edpt_iso_activate(uint8_t rhport, tusb_desc_endpoint_t const * p_endpoint_desc)
1004+
{
1005+
(void)rhport;
1006+
uint8_t const epnum = xfer_ctl_ptr(p_endpoint_desc->bEndpointAddress)->epnum;
1007+
uint8_t const dir = tu_edpt_dir(p_endpoint_desc->bEndpointAddress);
1008+
const uint16_t packet_size = tu_edpt_packet_size(p_endpoint_desc);
1009+
uint16_t pma_addr;
1010+
1011+
/* Disable endpoint */
1012+
pcd_set_ep_tx_status(USB, epnum, USB_EP_TX_DIS);
1013+
pcd_set_ep_rx_status(USB, epnum, USB_EP_RX_DIS);
1014+
1015+
pcd_set_eptype(USB, epnum, USB_EP_ISOCHRONOUS);
1016+
pcd_set_ep_address(USB, epnum, tu_edpt_number(p_endpoint_desc->bEndpointAddress));
1017+
// Be normal, for now, instead of only accepting zero-byte packets (on control endpoint)
1018+
// or being double-buffered (bulk endpoints)
1019+
pcd_clear_ep_kind(USB,0);
1020+
1021+
pma_addr = xfer_ctl_ptr(p_endpoint_desc->bEndpointAddress)->pma_ptr;
1022+
1023+
*pcd_ep_tx_address_ptr(USB, epnum) = pma_addr;
1024+
pcd_clear_tx_dtog(USB, epnum);
1025+
1026+
*pcd_ep_rx_address_ptr(USB, epnum) = pma_addr;
1027+
pcd_set_ep_rx_bufsize(USB, epnum, packet_size);
1028+
pcd_clear_rx_dtog(USB, epnum);
1029+
1030+
xfer_ctl_ptr(p_endpoint_desc->bEndpointAddress)->max_packet_size = packet_size;
1031+
1032+
return true;
1033+
}
1034+
9861035
// Currently, single-buffered, and only 64 bytes at a time (max)
9871036

9881037
static void dcd_transmit_packet(xfer_ctl_t * xfer, uint16_t ep_ix)

0 commit comments

Comments
 (0)