AnsweredAssumed Answered

Cube USBD USBD_LL_PrepareReceive CDC Example

Question asked by squires.daniel.001 on Aug 11, 2015
Latest reply on Nov 13, 2015 by squires.daniel.001

I'm using the Cube F1 1.2.0 libraries with an STM32F105R8T6 device.

I have adapted the CDC example for the device but am having a problem with the the USB out bulk transfers. They work for some time until a point where no more out transfers succeed.

Having investigated and experimented with toggling LED's at various points In the code I have come to the conclusion that something within USBD_LL_PrepareReceive sometimes fails.

The code normally runs in a loop where having made an initial call to USBD_LL_PrepareReceive the PC is able to send data, this triggers USBD_CDC_DataOut which calls CDC_Itf_Receive which initiate a DMA transfer to the UART. When this transfer completes
HAL_UART_TxCpltCallback is called which calls 
USBD_CDC_ReceivePacket  which in turn calls USBD USBD_LL_PrepareReceive. The PC can then send another packet.

This all works for some time until at some point (apparently randomly) USBD_LL_PrepareReceive has been called but the PC is unable to transfer any more data. I can see in wireshark an URB status of -2 5 seconds after initially attempting the failed send.

I have noticed that if I schedule the writes regularly at say 2mS intervals it seems to work indefinitely, but if I emulate my real world scenario using a random time delay (i.e. 0 to 0.2 seconds) between writes it seems to fail after not very long.

Elsewhere in the forums I have seen comment about the loop being fragile in that if the UART TX callback doesn't fire for some reason the PC can no longer send, but in my case I have verified that the UART transaction completes and that USBD_LL_PrepareReceive is being called - I set an LED on immediately before calling it and off again in USBD_CDC_DataOut.

Any ideas?