2024-02-08 04:00 AM
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.
2024-02-08 05:08 AM
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.
2024-02-08 05:34 AM
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.
2024-02-08 05:49 AM
dev_state remains at CONFIGURED even after you disconnect the USB cable...
2024-02-08 06:04 AM
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.
2024-02-09 04:59 AM
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.
2024-02-09 06:54 AM
@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.
2024-02-09 10:40 AM - edited 2024-02-09 10:42 AM
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.