Skip to content

Commit 21bdafc

Browse files
committed
Add ISO buffer allocation.
1 parent 07ad64a commit 21bdafc

File tree

3 files changed

+155
-17
lines changed

3 files changed

+155
-17
lines changed

src/class/audio/audio_device.c

Lines changed: 120 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,18 @@
106106
#define USE_LINEAR_BUFFER 1
107107
#endif
108108

109+
// Temporarily put the check here for stm32_fsdev
110+
#if CFG_TUSB_MCU == OPT_MCU_STM32F0 || \
111+
CFG_TUSB_MCU == OPT_MCU_STM32F3 || \
112+
CFG_TUSB_MCU == OPT_MCU_STM32L0 || \
113+
CFG_TUSB_MCU == OPT_MCU_STM32L1 || \
114+
CFG_TUSB_MCU == OPT_MCU_STM32G4 || \
115+
CFG_TUSB_MCU == OPT_MCU_STM32WB
116+
#define USE_ISO_EP_ALLOCATION 1
117+
#else
118+
#define USE_ISO_EP_ALLOCATION 0
119+
#endif
120+
109121
// Declaration of buffers
110122

111123
// Check for maximum supported numbers
@@ -1467,6 +1479,104 @@ uint16_t audiod_open(uint8_t rhport, tusb_desc_interface_t const * itf_desc, uin
14671479
#endif
14681480
}
14691481

1482+
#if USE_ISO_EP_ALLOCATION
1483+
#if CFG_TUD_AUDIO_ENABLE_EP_IN
1484+
uint8_t ep_in = 0;
1485+
uint16_t ep_in_size = 0;
1486+
#endif
1487+
#if CFG_TUD_AUDIO_ENABLE_EP_OUT
1488+
uint8_t ep_out = 0;
1489+
uint16_t ep_out_size = 0;
1490+
#endif
1491+
#if CFG_TUD_AUDIO_ENABLE_FEEDBACK_EP
1492+
uint8_t ep_fb = 0;
1493+
#endif
1494+
1495+
// First find EP addr
1496+
uint8_t const *p_desc = _audiod_fct[i].p_desc;
1497+
uint8_t const *p_desc_end = p_desc + _audiod_fct[i].desc_length - TUD_AUDIO_DESC_IAD_LEN;
1498+
while (p_desc < p_desc_end)
1499+
{
1500+
if (tu_desc_type(p_desc) == TUSB_DESC_ENDPOINT)
1501+
{
1502+
tusb_desc_endpoint_t const *desc_ep = (tusb_desc_endpoint_t const *) p_desc;
1503+
if (desc_ep->bmAttributes.xfer == TUSB_XFER_ISOCHRONOUS)
1504+
{
1505+
#if CFG_TUD_AUDIO_ENABLE_FEEDBACK_EP
1506+
// Explicit feedback EP
1507+
if (desc_ep->bmAttributes.usage == 1)
1508+
{
1509+
ep_fb = desc_ep->bEndpointAddress;
1510+
}
1511+
#endif
1512+
// Data EP
1513+
if (desc_ep->bmAttributes.usage == 0)
1514+
{
1515+
if (tu_edpt_dir(desc_ep->bEndpointAddress) == TUSB_DIR_IN)
1516+
{
1517+
#if CFG_TUD_AUDIO_ENABLE_EP_IN
1518+
ep_in = desc_ep->bEndpointAddress;
1519+
#endif
1520+
} else
1521+
{
1522+
#if CFG_TUD_AUDIO_ENABLE_EP_OUT
1523+
ep_out = desc_ep->bEndpointAddress;
1524+
#endif
1525+
}
1526+
}
1527+
1528+
}
1529+
}
1530+
p_desc = tu_desc_next(p_desc);
1531+
}
1532+
1533+
// Then find EP max size
1534+
p_desc = _audiod_fct[i].p_desc;
1535+
while (p_desc < p_desc_end)
1536+
{
1537+
if (tu_desc_type(p_desc) == TUSB_DESC_ENDPOINT)
1538+
{
1539+
tusb_desc_endpoint_t const *desc_ep = (tusb_desc_endpoint_t const *) p_desc;
1540+
if (desc_ep->bmAttributes.xfer == TUSB_XFER_ISOCHRONOUS)
1541+
{
1542+
#if CFG_TUD_AUDIO_ENABLE_EP_IN
1543+
if (desc_ep->bEndpointAddress == ep_in)
1544+
{
1545+
ep_in_size = TU_MAX(tu_edpt_packet_size(desc_ep), ep_in_size);
1546+
}
1547+
#endif
1548+
#if CFG_TUD_AUDIO_ENABLE_EP_OUT
1549+
if (desc_ep->bEndpointAddress == ep_out)
1550+
{
1551+
ep_out_size = TU_MAX(tu_edpt_packet_size(desc_ep), ep_out_size);
1552+
}
1553+
#endif
1554+
}
1555+
}
1556+
p_desc = tu_desc_next(p_desc);
1557+
}
1558+
1559+
#if CFG_TUD_AUDIO_ENABLE_EP_IN
1560+
if (ep_in)
1561+
{
1562+
usbd_edpt_iso_alloc(rhport, ep_in, ep_in_size);
1563+
}
1564+
#endif
1565+
#if CFG_TUD_AUDIO_ENABLE_EP_OUT
1566+
if (ep_out)
1567+
{
1568+
usbd_edpt_iso_alloc(rhport, ep_out, ep_out_size);
1569+
}
1570+
#endif
1571+
#if CFG_TUD_AUDIO_ENABLE_FEEDBACK_EP
1572+
if (ep_fb)
1573+
{
1574+
usbd_edpt_iso_alloc(rhport, ep_fb, 4);
1575+
}
1576+
#endif
1577+
1578+
#endif
1579+
14701580
break;
14711581
}
14721582
}
@@ -1528,8 +1638,9 @@ static bool audiod_set_interface(uint8_t rhport, tusb_control_request_t const *
15281638
if (audio->ep_in_as_intf_num == itf)
15291639
{
15301640
audio->ep_in_as_intf_num = 0;
1641+
#if !USE_ISO_EP_ALLOCATION
15311642
usbd_edpt_close(rhport, audio->ep_in);
1532-
1643+
#endif
15331644
// Clear FIFOs, since data is no longer valid
15341645
#if !CFG_TUD_AUDIO_ENABLE_ENCODING
15351646
tu_fifo_clear(&audio->ep_in_ff);
@@ -1552,8 +1663,9 @@ static bool audiod_set_interface(uint8_t rhport, tusb_control_request_t const *
15521663
if (audio->ep_out_as_intf_num == itf)
15531664
{
15541665
audio->ep_out_as_intf_num = 0;
1666+
#if !USE_ISO_EP_ALLOCATION
15551667
usbd_edpt_close(rhport, audio->ep_out);
1556-
1668+
#endif
15571669
// Clear FIFOs, since data is no longer valid
15581670
#if !CFG_TUD_AUDIO_ENABLE_DECODING
15591671
tu_fifo_clear(&audio->ep_out_ff);
@@ -1571,7 +1683,9 @@ static bool audiod_set_interface(uint8_t rhport, tusb_control_request_t const *
15711683

15721684
// Close corresponding feedback EP
15731685
#if CFG_TUD_AUDIO_ENABLE_FEEDBACK_EP
1686+
#if !USE_ISO_EP_ALLOCATION
15741687
usbd_edpt_close(rhport, audio->ep_fb);
1688+
#endif
15751689
audio->ep_fb = 0;
15761690
tu_memclr(&audio->feedback, sizeof(audio->feedback));
15771691
#endif
@@ -1601,8 +1715,11 @@ static bool audiod_set_interface(uint8_t rhport, tusb_control_request_t const *
16011715
if (tu_desc_type(p_desc) == TUSB_DESC_ENDPOINT)
16021716
{
16031717
tusb_desc_endpoint_t const* desc_ep = (tusb_desc_endpoint_t const *) p_desc;
1718+
#if USE_ISO_EP_ALLOCATION
1719+
TU_ASSERT(usbd_edpt_iso_activate(rhport, desc_ep));
1720+
#else
16041721
TU_ASSERT(usbd_edpt_open(rhport, desc_ep));
1605-
1722+
#endif
16061723
uint8_t const ep_addr = desc_ep->bEndpointAddress;
16071724

16081725
//TODO: We need to set EP non busy since this is not taken care of right now in ep_close() - THIS IS A WORKAROUND!

src/device/usbd.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1396,11 +1396,19 @@ bool usbd_edpt_iso_activate(uint8_t rhport, tusb_desc_endpoint_t const * desc_ep
13961396
{
13971397
rhport = _usbd_rhport;
13981398

1399-
TU_ASSERT(tu_edpt_number(desc_ep->bEndpointAddress) < CFG_TUD_ENDPPOINT_MAX);
1399+
uint8_t const epnum = tu_edpt_number(desc_ep->bEndpointAddress);
1400+
uint8_t const dir = tu_edpt_dir(desc_ep->bEndpointAddress);
1401+
1402+
TU_ASSERT(epnum < CFG_TUD_ENDPPOINT_MAX);
14001403
TU_ASSERT(tu_edpt_validate(desc_ep, (tusb_speed_t) _usbd_dev.speed));
1401-
1404+
14021405
if (dcd_edpt_iso_activate)
1406+
{
1407+
_usbd_dev.ep_status[epnum][dir].stalled = false;
1408+
_usbd_dev.ep_status[epnum][dir].busy = false;
1409+
_usbd_dev.ep_status[epnum][dir].claimed = false;
14031410
return dcd_edpt_iso_activate(rhport, desc_ep);
1411+
}
14041412
else
14051413
return false;
14061414
}

src/portable/st/stm32_fsdev/dcd_stm32_fsdev.c

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -812,6 +812,13 @@ static uint8_t dcd_ep_alloc(uint8_t ep_addr, uint8_t ep_type)
812812

813813
for(uint8_t i = 0; i < STFSDEV_EP_COUNT; i++)
814814
{
815+
// Check if already allocated
816+
if(ep_alloc_status[i].allocated[dir] &&
817+
ep_alloc_status[i].ep_type == ep_type &&
818+
ep_alloc_status[i].ep_num == epnum)
819+
{
820+
return i;
821+
}
815822
// If EP of current direction is not allocated
816823
// Except for ISO endpoint, both direction should be free
817824
if(!ep_alloc_status[i].allocated[dir] &&
@@ -971,7 +978,7 @@ void dcd_edpt_close (uint8_t rhport, uint8_t ep_addr)
971978

972979
if(dir == TUSB_DIR_IN)
973980
{
974-
pcd_set_ep_tx_status(USB,epnum, USB_EP_TX_DIS);
981+
pcd_set_ep_tx_status(USB, epnum, USB_EP_TX_DIS);
975982
}
976983
else
977984
{
@@ -994,36 +1001,42 @@ bool dcd_edpt_iso_alloc(uint8_t rhport, uint8_t ep_addr, uint16_t largest_packet
9941001

9951002
/* Create a packet memory buffer area. For isochronous endpoints,
9961003
* use the same buffer as the double buffer, essentially disabling double buffering */
997-
dcd_pma_alloc(ep_addr, buffer_size);
1004+
uint16_t pma_addr = dcd_pma_alloc(ep_addr, buffer_size);
9981005

9991006
xfer_ctl_ptr(ep_addr)->epnum = epnum;
10001007

1008+
pcd_set_eptype(USB, epnum, USB_EP_ISOCHRONOUS);
1009+
1010+
*pcd_ep_tx_address_ptr(USB, epnum) = pma_addr;
1011+
*pcd_ep_rx_address_ptr(USB, epnum) = pma_addr;
1012+
10011013
return true;
10021014
}
10031015

10041016
bool dcd_edpt_iso_activate(uint8_t rhport, tusb_desc_endpoint_t const * p_endpoint_desc)
10051017
{
10061018
(void)rhport;
10071019
uint8_t const epnum = xfer_ctl_ptr(p_endpoint_desc->bEndpointAddress)->epnum;
1008-
const uint16_t packet_size = tu_edpt_packet_size(p_endpoint_desc);
1009-
uint16_t pma_addr;
1020+
uint8_t const dir = tu_edpt_dir(p_endpoint_desc->bEndpointAddress);
1021+
const uint16_t packet_size = pcd_aligned_buffer_size(tu_edpt_packet_size(p_endpoint_desc));
10101022

10111023
/* 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);
1024+
if(dir == TUSB_DIR_IN)
1025+
{
1026+
pcd_set_ep_tx_status(USB, epnum, USB_EP_TX_DIS);
1027+
}
1028+
else
1029+
{
1030+
pcd_set_ep_rx_status(USB, epnum, USB_EP_RX_DIS);
1031+
}
1032+
10161033
pcd_set_ep_address(USB, epnum, tu_edpt_number(p_endpoint_desc->bEndpointAddress));
10171034
// Be normal, for now, instead of only accepting zero-byte packets (on control endpoint)
10181035
// or being double-buffered (bulk endpoints)
10191036
pcd_clear_ep_kind(USB,0);
10201037

1021-
pma_addr = xfer_ctl_ptr(p_endpoint_desc->bEndpointAddress)->pma_ptr;
1022-
1023-
*pcd_ep_tx_address_ptr(USB, epnum) = pma_addr;
10241038
pcd_clear_tx_dtog(USB, epnum);
10251039

1026-
*pcd_ep_rx_address_ptr(USB, epnum) = pma_addr;
10271040
pcd_set_ep_rx_bufsize(USB, epnum, packet_size);
10281041
pcd_clear_rx_dtog(USB, epnum);
10291042

0 commit comments

Comments
 (0)