cancel
Showing results for 
Search instead for 
Did you mean: 

How to detect disabling the USB virtual com port?

Alex Golubev
Associate II

Hi.

I have mk. stm32h743 self-powered from its own 3.3V power supply. With the help of a cube, I raised a virtual com port. I can not understand how you can understand that the device is disconnected from the USB? When you connect Usb, it calls "CDC_Init_FS" and hUsbDeviceFS.dev_state is set to 3, which corresponds to "USBD_STATE_CONFIGURED". But when you disconnect the USB there is no callback "HAL_PCD_DisconnectCallback (PCD_HandleTypeDef * hpcd);" ... Why "HAL_PCD_DisconnectCallback (PCD_HandleTypeDef * hpcd);" not called when USB is disabled?

3 REPLIES 3
BParh.1
Senior III

Hi,

When you install the driver stack for USB Host, there would be an internal state machine to handle connection/disconnection of USB stick. The state machine would notify these events to user via callback as following:

static void USBH_UserProcess  (USBH_HandleTypeDef *phost, uint8_t id)
{
  /* USER CODE BEGIN CALL_BACK_1 */
  switch(id)
  {
  case HOST_USER_SELECT_CONFIGURATION:
  break;
 
  case HOST_USER_DISCONNECTION:
	  Appli_state = APPLICATION_DISCONNECT;
	  break;
  case HOST_USER_CLASS_ACTIVE:
	  Appli_state = APPLICATION_READY;
	  break;
  case HOST_USER_CONNECTION:
	  Appli_state = APPLICATION_START;
	  break;
  default:
  break;
  }
  /* USER CODE END CALL_BACK_1 */
}

As you can see, when it is disconnected, the state machine would call this callback with id = HOST_USER_DISCONNECTION.

For this to work, remember to enable your interrupt in the setting.

Alex Golubev
Associate II

I use a virtual com port for communication between the microcontroller and the computer.  I don't have USBH_UserProcess

Oh Ya, my bad I missed that 😁