Skip to content

Commit 71a42d3

Browse files
committed
L4_HAL/pcd: Optimise writing to USB FIFO to fill it as much as possible.
The existing HAL code had one issue: - 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 this issue.
1 parent f8168ca commit 71a42d3

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_pcd.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2160,6 +2160,7 @@ static HAL_StatusTypeDef PCD_WriteEmptyTxFifo(PCD_HandleTypeDef *hpcd, uint32_t
21602160
return HAL_ERROR;
21612161
}
21622162

2163+
#if 0
21632164
len = ep->xfer_len - ep->xfer_count;
21642165

21652166
if (len > ep->maxpacket)
@@ -2187,6 +2188,36 @@ static HAL_StatusTypeDef PCD_WriteEmptyTxFifo(PCD_HandleTypeDef *hpcd, uint32_t
21872188
ep->xfer_count += len;
21882189
}
21892190

2191+
#else
2192+
/* dpgeorge: modified above loop to:
2193+
* - recompute len32b before each check of this value against FIFO free space
2194+
*/
2195+
for (;;)
2196+
{
2197+
len = ep->xfer_len - ep->xfer_count;
2198+
if (len <= 0U)
2199+
{
2200+
/* Finished sending all data */
2201+
break;
2202+
}
2203+
if (len > ep->maxpacket)
2204+
{
2205+
len = ep->maxpacket;
2206+
}
2207+
2208+
len32b = (len + 3U) / 4U;
2209+
if (len32b > (USBx_INEP(epnum)->DTXFSTS & USB_OTG_DTXFSTS_INEPTFSAV))
2210+
{
2211+
/* No room left in FIFO */
2212+
break;
2213+
}
2214+
2215+
USB_WritePacket(USBx, ep->xfer_buff, epnum, len);
2216+
ep->xfer_buff += len;
2217+
ep->xfer_count += len;
2218+
}
2219+
#endif
2220+
21902221
if (ep->xfer_len <= ep->xfer_count)
21912222
{
21922223
fifoemptymsk = (uint32_t)(0x1UL << (epnum & EP_ADDR_MSK));

0 commit comments

Comments
 (0)