Skip to content

Commit f8168ca

Browse files
committed
F7_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.
1 parent a33a71f commit f8168ca

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

STM32F7xx_HAL_Driver/Src/stm32f7xx_hal_pcd.c

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1256,6 +1256,7 @@ static HAL_StatusTypeDef PCD_WriteEmptyTxFifo(PCD_HandleTypeDef *hpcd, uint32_t
12561256
uint32_t fifoemptymsk = 0;
12571257

12581258
ep = &hpcd->IN_ep[epnum];
1259+
#if 0
12591260
len = ep->xfer_len - ep->xfer_count;
12601261

12611262
if (len > ep->maxpacket)
@@ -1285,6 +1286,37 @@ static HAL_StatusTypeDef PCD_WriteEmptyTxFifo(PCD_HandleTypeDef *hpcd, uint32_t
12851286
ep->xfer_count += len;
12861287
}
12871288

1289+
#else
1290+
/* dpgeorge: modified above loop to:
1291+
* - allow to write the packet in the case it exactly fills the FIFO
1292+
* - recompute len32b before each check of this value against FIFO free space
1293+
*/
1294+
for (;;)
1295+
{
1296+
len = ep->xfer_len - ep->xfer_count;
1297+
if (len <= 0U)
1298+
{
1299+
/* Finished sending all data */
1300+
break;
1301+
}
1302+
if (len > ep->maxpacket)
1303+
{
1304+
len = ep->maxpacket;
1305+
}
1306+
1307+
len32b = (len + 3U) / 4U;
1308+
if (len32b > (USBx_INEP(epnum)->DTXFSTS & USB_OTG_DTXFSTS_INEPTFSAV))
1309+
{
1310+
/* No room left in FIFO */
1311+
break;
1312+
}
1313+
1314+
USB_WritePacket(USBx, ep->xfer_buff, epnum, len, hpcd->Init.dma_enable);
1315+
ep->xfer_buff += len;
1316+
ep->xfer_count += len;
1317+
}
1318+
#endif
1319+
12881320
if(len <= 0)
12891321
{
12901322
fifoemptymsk = 0x1 << epnum;

0 commit comments

Comments
 (0)