I am developing a complex USB device with several endpoints, I have encountered the following problem: the DataIn class callback was passing the epnum parameter incorrectly, the 0x80 flag - which indicates the endpoint direction - wasn't set. This resulted in faulty operation when I checked against the endpoint number in my callback.
This can be obviously circumvented easily by always setting the 0x80 flag before evaluating the epnum, so it's not a critical bug. However, this flag is otherwise handled everywhere correctly within the HAL library, when using any of the HAL_PCD_EP_<operation>() functions. My argument is that if the USB class calls USBD_LL_Transmit() with a certain epnum value, the DataIn callback signalling the completion of that transfer shall have the same epnum value.
The fix in the HAL libraries is fairly easy, just set the 0x80 flag on the epnum on each occurrence of the DataInStageCallback, like so:
HAL_PCD_DataInStageCallback(hpcd, 0x80 | epnum);
This alone would break the STM32 USB Device Library, therefore this small change needs to be applied for correctness:
USBD_StatusTypeDef USBD_LL_DataInStage(USBD_HandleTypeDef *pdev ,uint8_t epnum, uint8_t *pdata)
if((epnum & 0x7F) == 0)
FYI for this and numerous other issues I am currently maintaining my own branch of the STM32 USB Device Library on github.