F4_HAL/pcd: Optimise writing to USB FIFO to fill it as much as possible.

The existing HAL code had two issues:

- It used < instead of <= to compare INEPTFSAV with len32b, which meant
  that data would not be written to the FIFO if there was exactly enough
  room for the packet (it would require enough room plus one additional
  FIFO slot).  So for IN endpoints that had a FIFO size the same as the
  maximum packet size, packets of maximum size could never be written.

- If the write loop went around again it would use the old len32b to check
  if there was enough space, thereby missing some opportunities to write
  packets that were smaller than the previous len32b.

This patch fixes these two issues.
This commit is contained in:
Damien George 2018-09-26 11:48:08 +10:00 committed by Damien George
parent 1a008711be
commit fdb18f4f2e

View File

@ -1268,6 +1268,7 @@ static HAL_StatusTypeDef PCD_WriteEmptyTxFifo(PCD_HandleTypeDef *hpcd, uint32_t
uint32_t fifoemptymsk = 0U;
ep = &hpcd->IN_ep[epnum];
#if 0
len = ep->xfer_len - ep->xfer_count;
if (len > ep->maxpacket)
@ -1297,6 +1298,37 @@ static HAL_StatusTypeDef PCD_WriteEmptyTxFifo(PCD_HandleTypeDef *hpcd, uint32_t
ep->xfer_count += len;
}
#else
/* dpgeorge: modified above loop to:
* - allow to write the packet in the case it exactly fills the FIFO
* - recompute len32b before each check of this value against FIFO free space
*/
for (;;)
{
len = ep->xfer_len - ep->xfer_count;
if (len <= 0U)
{
/* Finished sending all data */
break;
}
if (len > ep->maxpacket)
{
len = ep->maxpacket;
}
len32b = (len + 3U) / 4U;
if (len32b > (USBx_INEP(epnum)->DTXFSTS & USB_OTG_DTXFSTS_INEPTFSAV))
{
/* No room left in FIFO */
break;
}
USB_WritePacket(USBx, ep->xfer_buff, epnum, len, hpcd->Init.dma_enable);
ep->xfer_buff += len;
ep->xfer_count += len;
}
#endif
if(len <= 0U)
{
fifoemptymsk = 0x1U << epnum;