Skip to content

Commit a33a71f

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

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_pcd.c

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

12701270
ep = &hpcd->IN_ep[epnum];
1271+
#if 0
12711272
len = ep->xfer_len - ep->xfer_count;
12721273

12731274
if (len > ep->maxpacket)
@@ -1297,6 +1298,37 @@ static HAL_StatusTypeDef PCD_WriteEmptyTxFifo(PCD_HandleTypeDef *hpcd, uint32_t
12971298
ep->xfer_count += len;
12981299
}
12991300

1301+
#else
1302+
/* dpgeorge: modified above loop to:
1303+
* - allow to write the packet in the case it exactly fills the FIFO
1304+
* - recompute len32b before each check of this value against FIFO free space
1305+
*/
1306+
for (;;)
1307+
{
1308+
len = ep->xfer_len - ep->xfer_count;
1309+
if (len <= 0U)
1310+
{
1311+
/* Finished sending all data */
1312+
break;
1313+
}
1314+
if (len > ep->maxpacket)
1315+
{
1316+
len = ep->maxpacket;
1317+
}
1318+
1319+
len32b = (len + 3U) / 4U;
1320+
if (len32b > (USBx_INEP(epnum)->DTXFSTS & USB_OTG_DTXFSTS_INEPTFSAV))
1321+
{
1322+
/* No room left in FIFO */
1323+
break;
1324+
}
1325+
1326+
USB_WritePacket(USBx, ep->xfer_buff, epnum, len, hpcd->Init.dma_enable);
1327+
ep->xfer_buff += len;
1328+
ep->xfer_count += len;
1329+
}
1330+
#endif
1331+
13001332
if(len <= 0U)
13011333
{
13021334
fifoemptymsk = 0x1U << epnum;

0 commit comments

Comments
 (0)