Add ISO buffer allocation.

This commit is contained in:
Mengsk 2022-12-30 01:05:12 +01:00
parent 07ad64adfe
commit 21bdafcfef
3 changed files with 155 additions and 17 deletions

View File

@ -106,6 +106,18 @@
#define USE_LINEAR_BUFFER 1
#endif
// Temporarily put the check here for stm32_fsdev
#if CFG_TUSB_MCU == OPT_MCU_STM32F0 || \
CFG_TUSB_MCU == OPT_MCU_STM32F3 || \
CFG_TUSB_MCU == OPT_MCU_STM32L0 || \
CFG_TUSB_MCU == OPT_MCU_STM32L1 || \
CFG_TUSB_MCU == OPT_MCU_STM32G4 || \
CFG_TUSB_MCU == OPT_MCU_STM32WB
#define USE_ISO_EP_ALLOCATION 1
#else
#define USE_ISO_EP_ALLOCATION 0
#endif
// Declaration of buffers
// Check for maximum supported numbers
@ -1467,6 +1479,104 @@ uint16_t audiod_open(uint8_t rhport, tusb_desc_interface_t const * itf_desc, uin
#endif
}
#if USE_ISO_EP_ALLOCATION
#if CFG_TUD_AUDIO_ENABLE_EP_IN
uint8_t ep_in = 0;
uint16_t ep_in_size = 0;
#endif
#if CFG_TUD_AUDIO_ENABLE_EP_OUT
uint8_t ep_out = 0;
uint16_t ep_out_size = 0;
#endif
#if CFG_TUD_AUDIO_ENABLE_FEEDBACK_EP
uint8_t ep_fb = 0;
#endif
// First find EP addr
uint8_t const *p_desc = _audiod_fct[i].p_desc;
uint8_t const *p_desc_end = p_desc + _audiod_fct[i].desc_length - TUD_AUDIO_DESC_IAD_LEN;
while (p_desc < p_desc_end)
{
if (tu_desc_type(p_desc) == TUSB_DESC_ENDPOINT)
{
tusb_desc_endpoint_t const *desc_ep = (tusb_desc_endpoint_t const *) p_desc;
if (desc_ep->bmAttributes.xfer == TUSB_XFER_ISOCHRONOUS)
{
#if CFG_TUD_AUDIO_ENABLE_FEEDBACK_EP
// Explicit feedback EP
if (desc_ep->bmAttributes.usage == 1)
{
ep_fb = desc_ep->bEndpointAddress;
}
#endif
// Data EP
if (desc_ep->bmAttributes.usage == 0)
{
if (tu_edpt_dir(desc_ep->bEndpointAddress) == TUSB_DIR_IN)
{
#if CFG_TUD_AUDIO_ENABLE_EP_IN
ep_in = desc_ep->bEndpointAddress;
#endif
} else
{
#if CFG_TUD_AUDIO_ENABLE_EP_OUT
ep_out = desc_ep->bEndpointAddress;
#endif
}
}
}
}
p_desc = tu_desc_next(p_desc);
}
// Then find EP max size
p_desc = _audiod_fct[i].p_desc;
while (p_desc < p_desc_end)
{
if (tu_desc_type(p_desc) == TUSB_DESC_ENDPOINT)
{
tusb_desc_endpoint_t const *desc_ep = (tusb_desc_endpoint_t const *) p_desc;
if (desc_ep->bmAttributes.xfer == TUSB_XFER_ISOCHRONOUS)
{
#if CFG_TUD_AUDIO_ENABLE_EP_IN
if (desc_ep->bEndpointAddress == ep_in)
{
ep_in_size = TU_MAX(tu_edpt_packet_size(desc_ep), ep_in_size);
}
#endif
#if CFG_TUD_AUDIO_ENABLE_EP_OUT
if (desc_ep->bEndpointAddress == ep_out)
{
ep_out_size = TU_MAX(tu_edpt_packet_size(desc_ep), ep_out_size);
}
#endif
}
}
p_desc = tu_desc_next(p_desc);
}
#if CFG_TUD_AUDIO_ENABLE_EP_IN
if (ep_in)
{
usbd_edpt_iso_alloc(rhport, ep_in, ep_in_size);
}
#endif
#if CFG_TUD_AUDIO_ENABLE_EP_OUT
if (ep_out)
{
usbd_edpt_iso_alloc(rhport, ep_out, ep_out_size);
}
#endif
#if CFG_TUD_AUDIO_ENABLE_FEEDBACK_EP
if (ep_fb)
{
usbd_edpt_iso_alloc(rhport, ep_fb, 4);
}
#endif
#endif
break;
}
}
@ -1528,8 +1638,9 @@ static bool audiod_set_interface(uint8_t rhport, tusb_control_request_t const *
if (audio->ep_in_as_intf_num == itf)
{
audio->ep_in_as_intf_num = 0;
#if !USE_ISO_EP_ALLOCATION
usbd_edpt_close(rhport, audio->ep_in);
#endif
// Clear FIFOs, since data is no longer valid
#if !CFG_TUD_AUDIO_ENABLE_ENCODING
tu_fifo_clear(&audio->ep_in_ff);
@ -1552,8 +1663,9 @@ static bool audiod_set_interface(uint8_t rhport, tusb_control_request_t const *
if (audio->ep_out_as_intf_num == itf)
{
audio->ep_out_as_intf_num = 0;
#if !USE_ISO_EP_ALLOCATION
usbd_edpt_close(rhport, audio->ep_out);
#endif
// Clear FIFOs, since data is no longer valid
#if !CFG_TUD_AUDIO_ENABLE_DECODING
tu_fifo_clear(&audio->ep_out_ff);
@ -1571,7 +1683,9 @@ static bool audiod_set_interface(uint8_t rhport, tusb_control_request_t const *
// Close corresponding feedback EP
#if CFG_TUD_AUDIO_ENABLE_FEEDBACK_EP
#if !USE_ISO_EP_ALLOCATION
usbd_edpt_close(rhport, audio->ep_fb);
#endif
audio->ep_fb = 0;
tu_memclr(&audio->feedback, sizeof(audio->feedback));
#endif
@ -1601,8 +1715,11 @@ static bool audiod_set_interface(uint8_t rhport, tusb_control_request_t const *
if (tu_desc_type(p_desc) == TUSB_DESC_ENDPOINT)
{
tusb_desc_endpoint_t const* desc_ep = (tusb_desc_endpoint_t const *) p_desc;
#if USE_ISO_EP_ALLOCATION
TU_ASSERT(usbd_edpt_iso_activate(rhport, desc_ep));
#else
TU_ASSERT(usbd_edpt_open(rhport, desc_ep));
#endif
uint8_t const ep_addr = desc_ep->bEndpointAddress;
//TODO: We need to set EP non busy since this is not taken care of right now in ep_close() - THIS IS A WORKAROUND!

View File

@ -1396,11 +1396,19 @@ bool usbd_edpt_iso_activate(uint8_t rhport, tusb_desc_endpoint_t const * desc_ep
{
rhport = _usbd_rhport;
TU_ASSERT(tu_edpt_number(desc_ep->bEndpointAddress) < CFG_TUD_ENDPPOINT_MAX);
uint8_t const epnum = tu_edpt_number(desc_ep->bEndpointAddress);
uint8_t const dir = tu_edpt_dir(desc_ep->bEndpointAddress);
TU_ASSERT(epnum < CFG_TUD_ENDPPOINT_MAX);
TU_ASSERT(tu_edpt_validate(desc_ep, (tusb_speed_t) _usbd_dev.speed));
if (dcd_edpt_iso_activate)
{
_usbd_dev.ep_status[epnum][dir].stalled = false;
_usbd_dev.ep_status[epnum][dir].busy = false;
_usbd_dev.ep_status[epnum][dir].claimed = false;
return dcd_edpt_iso_activate(rhport, desc_ep);
}
else
return false;
}

View File

@ -812,6 +812,13 @@ static uint8_t dcd_ep_alloc(uint8_t ep_addr, uint8_t ep_type)
for(uint8_t i = 0; i < STFSDEV_EP_COUNT; i++)
{
// Check if already allocated
if(ep_alloc_status[i].allocated[dir] &&
ep_alloc_status[i].ep_type == ep_type &&
ep_alloc_status[i].ep_num == epnum)
{
return i;
}
// If EP of current direction is not allocated
// Except for ISO endpoint, both direction should be free
if(!ep_alloc_status[i].allocated[dir] &&
@ -971,7 +978,7 @@ void dcd_edpt_close (uint8_t rhport, uint8_t ep_addr)
if(dir == TUSB_DIR_IN)
{
pcd_set_ep_tx_status(USB,epnum, USB_EP_TX_DIS);
pcd_set_ep_tx_status(USB, epnum, USB_EP_TX_DIS);
}
else
{
@ -994,10 +1001,15 @@ bool dcd_edpt_iso_alloc(uint8_t rhport, uint8_t ep_addr, uint16_t largest_packet
/* Create a packet memory buffer area. For isochronous endpoints,
* use the same buffer as the double buffer, essentially disabling double buffering */
dcd_pma_alloc(ep_addr, buffer_size);
uint16_t pma_addr = dcd_pma_alloc(ep_addr, buffer_size);
xfer_ctl_ptr(ep_addr)->epnum = epnum;
pcd_set_eptype(USB, epnum, USB_EP_ISOCHRONOUS);
*pcd_ep_tx_address_ptr(USB, epnum) = pma_addr;
*pcd_ep_rx_address_ptr(USB, epnum) = pma_addr;
return true;
}
@ -1005,25 +1017,26 @@ bool dcd_edpt_iso_activate(uint8_t rhport, tusb_desc_endpoint_t const * p_endpo
{
(void)rhport;
uint8_t const epnum = xfer_ctl_ptr(p_endpoint_desc->bEndpointAddress)->epnum;
const uint16_t packet_size = tu_edpt_packet_size(p_endpoint_desc);
uint16_t pma_addr;
uint8_t const dir = tu_edpt_dir(p_endpoint_desc->bEndpointAddress);
const uint16_t packet_size = pcd_aligned_buffer_size(tu_edpt_packet_size(p_endpoint_desc));
/* Disable endpoint */
pcd_set_ep_tx_status(USB, epnum, USB_EP_TX_DIS);
pcd_set_ep_rx_status(USB, epnum, USB_EP_RX_DIS);
pcd_set_eptype(USB, epnum, USB_EP_ISOCHRONOUS);
if(dir == TUSB_DIR_IN)
{
pcd_set_ep_tx_status(USB, epnum, USB_EP_TX_DIS);
}
else
{
pcd_set_ep_rx_status(USB, epnum, USB_EP_RX_DIS);
}
pcd_set_ep_address(USB, epnum, tu_edpt_number(p_endpoint_desc->bEndpointAddress));
// Be normal, for now, instead of only accepting zero-byte packets (on control endpoint)
// or being double-buffered (bulk endpoints)
pcd_clear_ep_kind(USB,0);
pma_addr = xfer_ctl_ptr(p_endpoint_desc->bEndpointAddress)->pma_ptr;
*pcd_ep_tx_address_ptr(USB, epnum) = pma_addr;
pcd_clear_tx_dtog(USB, epnum);
*pcd_ep_rx_address_ptr(USB, epnum) = pma_addr;
pcd_set_ep_rx_bufsize(USB, epnum, packet_size);
pcd_clear_rx_dtog(USB, epnum);