Electrical – Open Error on host while using VCP on STM32

stm32stm32cubemxstm32f4-discovery

I use STM32F407G-DISC1 board. I have run some examples with VCP based on STD library. Everything is OK. Then I have tried to generate code by CubeMX. And something interesting happens.

If not to add any code, then VCP is defined by host and Terminal can connect that.

If I add CDC_Transmit_FS in endless loop of main(), it works

volatile uint32_t time_var = 3e7;
while (1)
{
    while(time_var) {
    time_var--;
}
CDC_Transmit_FS(b, 4);
time_var = 3e8;
}

If instead of internal while loop I use HAL_Delay function, then terminal can't open the port.

while (1) {
Hal_Delay(5e3);
CDC_Transmit_FS(b, 4);
}

So, COM port is defined in DeviceManager, but terminal can't connect that. It happens even before the first call of CDC_Transmit_FS, though hUsbDeviceFS is already initialized.

Best Answer

This is probably because your Hal_Delay function is not working properly. Hal_Delay function will enter a busy loop that will compare an interrupt incremented value with the value you specify as argument. The interrupt functions will get initiated when calling HAL_init(), otherwise you need to make this configuration yourself.

To summarize, make sure that the HAL_Tick function is getting called before using HAL_Delay.