Showing results for 
Search instead for 
Did you mean: 

USB CDC can't connect when some code added


I can get my USB CDC on Nucleo-144 recognized from Device Manager. It will appear like COM8 and Terminal Emulator can connect to it. I can even echo back data received.

Just when I add some minor functionality in the while loop in main() (like check for pressed button or incrementing some variables) it can not be connected any more from Terminal Emulator. It can still be seen in the Device Manager as COM8, just connection attempts fail.

It seems as if the MCU had resource problems. Running it on a F439 on sysclk of 168 MHz will allow me incrementing a variable in the endless loop and the serial port will be stilll functional. Adding some more code (checking user button) and it can not be connected any more.

On a slower F412 running on 72 MHz even this variable increment is too much and will prevent connecting as serial port.

The line

CDC_Transmit_FS(Buf, *Len) ;

added to CDC_Receive_FS(uint8_t* Buf, uint32_t *Len) will run and return entered characters on both boards as long as there is no serious business going on in the while loop.

If I implement user button detection as interrupt it will execute on the F439. If I do the same on the F412 board the serial port gets unaccessible again. The button itself has not to be pressed at all, just the sole fact some code has been added seems to stop the device responding normally!

Turning optimization on or off does not affect anything. I use STM32CubeIDE 1.4.0. Firmware package V1.25.1


This is probably one of those cases where even though adding something causes a failure, the bug was still present beforehand. I don't think there's going to be a simple answer and the bug is probably specific to your code. Debug as normal, look at return values from HAL and USB routines to ensure proper operation. Examine packets on the USB bus to find errors.

Ensure stack/heap are sufficient and do no clash or overrun.

If you feel a post has answered your question, please click "Accept as Solution".

There is no code beforehand - at least not from my side. It is the startup code just generated from cubeIDE after setting up pins, clocks and heap/stack. Just by adding a variable and incrementing it in the while loop stops serial port from responding. Stack starts at 203fff8 (+256kB) which is correct, since the size of the RAM. Heap starts after the declared variables. For heap I tried 0x0600..0x4000 - does not make any difference.