STMicroelectronics / STM32CubeF7

STM32Cube MCU Full Package for the STM32F7 series - (HAL + LL Drivers, CMSIS Core, CMSIS Device, MW libraries plus a set of Projects running on all boards provided by ST (Nucleo, Evaluation and Discovery Kits))
Other
331 stars 194 forks source link

USBD_LL_GetRxDataSize/HAL_PCD_EP_GetRxCount return wrong value #30

Closed davekeck closed 2 years ago

davekeck commented 3 years ago

When both of these conditions are true:

Under these circumstances, when the DataOut USB callback is called as a result of USBD_LL_PrepareReceive, USBD_LL_GetRxDataSize/HAL_PCD_EP_GetRxCount return an incorrect value for the number of bytes received.

The bug appears to be due to these lines in PCD_EP_OutXfrComplete_int:

https://github.com/STMicroelectronics/STM32CubeF7/blob/08376dce1b404687e4a86b73077f396bccfc9cb5/Drivers/STM32F7xx_HAL_Driver/Src/stm32f7xx_hal_pcd.c#L1986-L1990

Th above math incorrectly uses the the max packet size (instead of the transfer size), so that when the transfer size is larger than the max packet size, xfer_count is assigned to an incorrect value, which breaks USBD_LL_GetRxDataSize/HAL_PCD_EP_GetRxCount.

This replacement code fixes the issue for me:

const uint32_t xfer_len = hpcd->OUT_ep[epnum].xfer_len;
const uint32_t xfrsiz = (USBx_OUTEP(epnum)->DOEPTSIZ & USB_OTG_DOEPTSIZ_XFRSIZ);
if (xfer_len) {
    const uint32_t xfer_count = xfer_len - xfrsiz;
    hpcd->OUT_ep[epnum].xfer_count = xfer_count;
    hpcd->OUT_ep[epnum].xfer_buff += xfer_count;
} else {
    hpcd->OUT_ep[epnum].xfer_count = 0;
}
ALABSTM commented 3 years ago

Hi @davekeck,

Thank you for this clear and concise report and for the fix proposal. It has been forwarded to our development teams. I will get to you as soon as they provide their feedback.

With regards,

ALABSTM commented 2 years ago

ST Internal Reference: 120891