2022-04-12 02:17 AM
The project consists of STM32F745 MCU and a desktop application. The desktop application sends the data over USB CDC and receives the response back. The problem happens if large data chunks are being sent. What happens is that if several packets of lets say 220 bytes for example are sent a few of them are received without any problems (sometimes 3-4, sometimes 20-30 packets go through, no rules whatsoever). When the communication fails the F745 doesn't receive the whole 220 bytes packet. Sometimes it receives 128 bytes, sometimes 192 bytes, a few time 64 bytes are received and the communication fails. The USB FS is used with max packet size of 64 bytes. The endpoints are bulk endpoints. Library version for F745 is 1.16.2. Any insights would be appreciated as there are no evident errors.
2022-04-13 03:41 AM
The 64/128/192 bytes you do receive indicate some pointer/buffer problem since the USB FIFO buffers are 64 bytes. Try identifying any pointer/buffer handling and make sure that the interrupt service routine doesn't interfere with the buffer pointers in your application. Make sure to check and handle any USB errors.
I used the CDC library in 2014 and have a vague memory that the application example had a bug where buffer wrapping didn't work correctly.
2022-04-13 07:24 AM
Standard USB device problem remark #1: Make sure you call CDC_Transmit from the ISR of the same priority as the USB interrupt. Do not try to call it from any other piece of code, like main() function loop.
2022-04-13 11:37 AM
> Do not try to call it from any other piece of code, like main() function loop
why not, with USBD interrupt disabled?
2022-04-14 04:06 AM
Already tried, nothing changes.
2022-04-14 04:10 AM
The only thing I do is to take the data from the Rx callback and later I process it from the main loop. No modifications to the generated HAL CDC library for USB except for Rx callback implementation and added functionality to echo back line coding for windows devices. It is hard to pinpoint where it actually fails as it is almost pure generated library code but thanks for the insights. I will try to look into the things you suggested.
EDIT: I failed to mention that HAL library was modified to also include dual CDC support. One interface is to send data to and receive data from F745 and other is to gather logs from F745, (just for receiving data from F745, nothing is being sent to the MCU).
2022-04-16 03:15 PM
> Make sure you call CDC_Transmit from the ISR of the same priority as the USB interrupt.
> with USBD interrupt disabled
What a "nice" USB stack... Guys, seriously - TinyUSB is not a broken bloatware, has a sane design, is actively developed and is easy to use.