cancel
Showing results for 
Search instead for 
Did you mean: 

How to detect if usb connection is established

AlexandrosAA
Associate III

Hey. I'm trying to use my H7's usb interface and i've currently set it up as a cdc device.
What i'm trying to implement though is the following. 

The stm is supposed to create a virtual connection with either a python app or a tera term terminal.

Is there a function or a variable that detects if this kind of a connection is established ? For example, the pico equivalent is tud_cdc_connected()

 What i want to build is this

while(!exampleCOnnectionVariable)
{
   HAL_DELAY(500)
}

Meaning that my stm would wait during initialization, until a connection is established and then proceed with the super loop.

7 REPLIES 7
gbm
Lead III

With classic ST USB stack, you need to implement CDC_SET_CONTROL_LINE_STATE request in usbd_cdc_if.c. When ControlLineState value changes from anything different from 3 to 3, the connection is established. You may start transmitting data no sooner than 50 ms after that change.

My STM32 stuff on github - compact USB device stack and more: https://github.com/gbm-ii/gbmUSBdevice
TDK
Guru

Check dev_state for configured status.

if (hUsbDeviceFS.dev_state == USBD_STATE_CONFIGURED) {
    ...
}

 

This doesn't mean the program has the port open, just that the computer is connected. No way to detect when a program opens the serial port.

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

dev_state remains at CONFIGURED even after you disconnect the USB cable...

My STM32 stuff on github - compact USB device stack and more: https://github.com/gbm-ii/gbmUSBdevice
FBL
ST Employee

Hello @AlexandrosAA 

If I understand your request correctly, in usbd_conf.c, you can check device connection from ISR through USBD_LL_DevConnected in callback.

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

AlexandrosAA
Associate III

I tried something like this

while(CDC_Transmit_HS(messageOk,sizeof(messageOk))!=USBD_OK)
{

}

but it seems that the message is transmitted, even if the port is not open. Is there a similar approach to this ? The while loop should end only if the data are accepted by my computer.

@AlexandrosAA When stepping in debug, and you reach this line as previously mentioned.

if (hUsbDeviceFS.dev_state == USBD_STATE_CONFIGURED) {

 You are pretty sure your host is connected.

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

If your device is bus-powered, disconnecting the cable stops the program. No problems there.

If your device is self-powered, you are required (per USB standard) to monitor VBUS and de-initialize the peripheral when VBUS drops by calling MX_USB_DEVICE_DeInit(). This will reset the state machine back to USBD_STATE_DEFAULT.

Edit: If you ignore the standard, there's probably a state variable you could check somewhere. I may look into it.

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