cancel
Showing results for 
Search instead for 
Did you mean: 

USB communication stalls after some minutes of PC restart

raiyans
Associate II
Posted on May 09, 2017 at 12:20

Hello fellow developers,

I have an stm32f4 board connected to PC through a USB link. Basically, the board is linked through an SPI link to several sensors. It gathers data from the sensors, filters and processes it before it sends it to the PC through the USB link for further processing and to display the results. I am building the USB link on top of the ST cube USB library and all works well.

I have lately been adding further data to the processing algorithm. I am using an array of 8 elements to average the sensors data. I have two sensors connected that I need to do this to. When I add the averaging algorithm to one sensor, everything works great and no problems occur what so  ever. When I add the averaging to the second sensor I start to get a weird error. Basically, When I restart the PC, things work for some minutes before the USB communication stalls. To make the system run again I have to unplug and plug the USB cable. At this point, everything will work perfectly until I restart the PC again. The USB will then stall after some minutes and the cycle will continue.

I have used a USB analyser to understand the Problem furhter. I turned out that the board reports to the PC a USB error with reference 0xC0000011. The PC then reports to the board to abort the packet, clear and restart the session. The board sucessfully does this and just as things go back to normal, the board reports to the PC again the same error and the cycle continues.

has anybody have a clue what might be causing this strange error.

Warm Regards

#stm32f4 #stall #usb
1 ACCEPTED SOLUTION

Accepted Solutions
AvaTar
Lead
Posted on May 09, 2017 at 12:41

Perhaps an overrun caused by a spurious race condition, I would guess related to interrupts.

When I add the averaging to the second sensor I start to get a weird error.

Do you run mentioned averaging in the interrupt context ?

I suggest to instrument your code with GPIO toggles, probably in your interrupt routines.

Use a scope / logic analyzer to visualize the runtime behaviour when your issue occurs.

View solution in original post

4 REPLIES 4
AvaTar
Lead
Posted on May 09, 2017 at 12:41

Perhaps an overrun caused by a spurious race condition, I would guess related to interrupts.

When I add the averaging to the second sensor I start to get a weird error.

Do you run mentioned averaging in the interrupt context ?

I suggest to instrument your code with GPIO toggles, probably in your interrupt routines.

Use a scope / logic analyzer to visualize the runtime behaviour when your issue occurs.

Posted on May 10, 2017 at 13:36

Hi AvaTar, looks like your suggestion was correct. I reduced the speed by which I call the processing algorithm. So now in the calleback I introduced another if statement that would process the data every 3rd millisecond click instead of every millisecond and the problem disappeared.  So definitely calling the processing algorithm too fast and too often introduced some sort of an Overrun.

What would be actually the best way to lock an interrupt handler that it is not called multiple times. I am not using threading in the lower level, only the USB libraries provided by st so I would not prefer to introduce a thread lock function just yet!

Kind Regards

Raiyan

Posted on May 10, 2017 at 14:18

One way is to do as few as possible in the interrupt routine itself.

Just set a flag, and do the processing in the main loop, i.e. somewhere else. Some rules for thread-safe coding still apply.

Another possibility is to prioritize interrupts - which only works if you deal with different interrupts.

If you are chasing your tail (interrupts coming faster than you can process it), you have hit a hard limit of the core.

Either optimize or move code out of the interrupt (as above), increase the core clock, or switch to a faster variant.

Or, when applicable, use DMA to collect data and reduce the interrupt rate.

... I introduced another if statement that would process the data every 3rd millisecond click instead of every millisecond and the problem disappeared.

Times of about 1 millisecond are already 'on the long side'.

That could be fine if it works - which seems not to be the case with your application.

I would try to move code out of interrupt context. And be especially careful when calling functions that involve other interrupt-based stuff, like serial output (UART, USB, SPI, ...).

Posted on May 10, 2017 at 16:27

 Solved, moved the processing the to the main function

Thank you very much for your help and time!