cancel
Showing results for 
Search instead for 
Did you mean: 

STM32 as USB COM Device fails when debugging but works when running.

Hello dear supporters,

I had some issues setting up my STM32G0B1CET6N as USB Communication Device Class (Virtual Port COM). 

Clock Source for the USB is the internal "HSI48 RC" oscillator, designed for USB application.  

What happened? 
I used the default USB Settings (default USB STMCube MX generated).

I started debugging (step by step) and came to the 

MX_USB_Device_Init();

function inside the main.c 

Then I open the Windows11 "Gerätemanager (Device Manger)" and the USB device always failed to get recognized. 

NoobWhoIsGettingBetter_0-1749496674266.png

--------------------------------------------------------------------------------------------------------------------------------------------------

I relaunched the debugger and this time I just klicked on the "Run" button. No step by step debugging this time. The STM32 gets recognized as a COM device without any problem. 

NoobWhoIsGettingBetter_1-1749496861565.png

And the communication (sending data to the putty terminal) works without any problem. 

NoobWhoIsGettingBetter_2-1749496986005.png

-------------------------------------------------------------------------------------------------------------------------------------------

However when adding a debug breakpoint (for example in the while(1) loop) the STM32 gets no longer recognized as a COM device anymore. 

NoobWhoIsGettingBetter_3-1749497173156.png

Same problem as before:
NoobWhoIsGettingBetter_0-1749496674266.png

---------------------------------------------------------------------------------------------------------------------------

So is this a bug? Or is it simply not possible to have a working STM32 COM-device when working with the debugger? Is this problem caused by the fact, that the USB works via Interrupt and interrupts fail in the debugger?

NoobWhoIsGettingBetter_5-1749497512175.png

-------------------------------------------------------------------------------------------------------------------------------------------

Thanks for tipps and explanations 

1 ACCEPTED SOLUTION

Accepted Solutions
Andrew Neil
Super User

@NoobWhoIsGettingBetter wrote:

However when adding a debug breakpoint (for example in the while(1) loop) the STM32 gets no longer recognized as a COM device anymore. 


I don't think that's altogether unexpected?

By putting a breakpoint, you will disrupt the comms between the USB Host (your PC) - which could well cause the failure.

 


@NoobWhoIsGettingBetter wrote:

So is this a bug? Or is it simply not possible to have a working STM32 COM-device when working with the debugger?


You have to be very careful about breakpoints withing timing-critical code!

 


@NoobWhoIsGettingBetter wrote:

Is this problem caused by the fact, that the USB works via Interrupt and interrupts fail in the debugger?


It's not that they fail as such - but halting the core may cause deadlines to be missed.

 

This isn't specific to STM32 - it's an inherent limitation of halting debug.

 

Segger have a clever trick to help in these situations but, IIRC, not on Cortex-M0:

https://www.segger.com/products/debug-probes/j-link/technology/monitor-mode-debugging/ 

A complex system that works is invariably found to have evolved from a simple system that worked.
A complex system designed from scratch never works and cannot be patched up to make it work.

View solution in original post

1 REPLY 1
Andrew Neil
Super User

@NoobWhoIsGettingBetter wrote:

However when adding a debug breakpoint (for example in the while(1) loop) the STM32 gets no longer recognized as a COM device anymore. 


I don't think that's altogether unexpected?

By putting a breakpoint, you will disrupt the comms between the USB Host (your PC) - which could well cause the failure.

 


@NoobWhoIsGettingBetter wrote:

So is this a bug? Or is it simply not possible to have a working STM32 COM-device when working with the debugger?


You have to be very careful about breakpoints withing timing-critical code!

 


@NoobWhoIsGettingBetter wrote:

Is this problem caused by the fact, that the USB works via Interrupt and interrupts fail in the debugger?


It's not that they fail as such - but halting the core may cause deadlines to be missed.

 

This isn't specific to STM32 - it's an inherent limitation of halting debug.

 

Segger have a clever trick to help in these situations but, IIRC, not on Cortex-M0:

https://www.segger.com/products/debug-probes/j-link/technology/monitor-mode-debugging/ 

A complex system that works is invariably found to have evolved from a simple system that worked.
A complex system designed from scratch never works and cannot be patched up to make it work.