AnsweredAssumed Answered

Modifying code and recompiling randomly breaks USB CDC

Question asked by Teemu Ikonen on Sep 19, 2017
Latest reply on Sep 19, 2017 by Teemu Ikonen


I've generated basic USB CDC sample with the CubeMX and have lots of trouble getting it to work reliably. Device appears as COM port in Windows but Terminal app (like Putty) sometimes fails to open it.


The pattern seems to be that when I modify code, like add single irrelevant line or call things like 'puts' that is never executed, the app links and loads and device shows up as COM port but Terminal app suddenly fails to connect. When this happens it's consistent, it never works or always works.


Randomly adding dummy code lines (like 'int k = sizeof(int);')  or littering it with HAL_Delay sometimes cures it but I've not understood what is the root cause of this problem.


Here is main loop that appears to work, but for example if I uncomment any of the puts statements code sometimes fails. Note that even when puts is not ever called, just linking it can cause the issue.

int main(void)
initialise_monitor_handles(); // semihosting, required for prints to work

// Reset of all peripherals, Initializes the Flash interface and the Systick.

// Configure the system clock

// Initialize all configured peripherals

// set red led on
BSP_LED_Init(LED6); // blue
BSP_LED_Init(LED5); // red

// Need to check when CDC_Init_FS has been called instead of wait

uint8_t msg[] = "Hello World!\r\n";
int c = 0;



for(;;) {
     CDC_Transmit_FS(msg, sizeof(msg)-1);

     if(c++ > 500000) {
       // not called but just uncommenting this line will cause Terminal app to fail

return 0;