cancel
Showing results for 
Search instead for 
Did you mean: 

Detect USB connections

OGhis
Senior

Hi,

We using the USB port on the STM32H7B0 (100pins) and he is configurated as a serial port (VCP).
The USB communication works fine but we would detect the follow statuses: 

  • When our USB plug is unplugged or not.
  • When we have opened or not the serial port from a external serial terminal program.

How we can do this?

Using STM32IDE V 1.16.1
H7 Library V1.11.2
OGhis_0-1728201116602.png

 

6 REPLIES 6
AScha.3
Chief III

Hi,

to check:

> When our USB plug is unplugged or not.

is easy: just watch the usb-vbus , about 5V or zero; -> then you know: host connected - or not.

 

To check, whether there is "someone" , most simple way: if someone want to get talking on terminal, he first sending anything, maybe just "return" or any charakter.

If receiving this, you "know" there is a working connection.

 

Or you send some "Hello \r\n" , some ms delay, after getting usb-vbus -> hi .

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

Dear,

This is not a good solution for us because I cannot poll every xx ms to check if the communication between the serial terminal is still established or not.

We have already tried with the “ep0_state” state.
Normally it has a 4 status when there is no connection and a 5 status when the connection has been established.
Now we had also seen that this is not correct with any serial terminal program. Why we don't understand either

So is there another solution?

TDK
Guru

> When we have opened or not the serial port from a external serial terminal program.

This isn't possible, unless your terminal program sends something to the chip. Nothing is sent over USB when this happens.

You can detect if the serial port is connected to the PC but that's it.

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

When a terminal is "connected" it will continuously send IN requests to the data endpoint. This is how the device knows that the host is receiving. 

 

remov-b4-flight
Associate II

In my Project (not CDC), Modifying 

HAL_PCD_SetupStageCallback() and HAL_PCD_DisconnectCallback() to set/clear connection flag.
But it's no /* USER CODE BEGIN ~ USER CODE END */ surrounded. Needs to add manually by every code generation.
ST Micro, can you do sonething about this inconvinience.
FBL
ST Employee

Hi @OGhis 

I suggest using redefine HAL_PCD_DisconnectCallback in main.c to handle cable disconnection

 

/**
  * @brief  Disconnection event callback.
  * @param  hpcd PCD handle
  * @retval None
  */
__weak void HAL_PCD_DisconnectCallback(PCD_HandleTypeDef *hpcd)
{
  /* Prevent unused argument(s) compilation warning */
  UNUSED(hpcd);

  /* NOTE : This function should not be modified, when the callback is needed,
            the HAL_PCD_DisconnectCallback could be implemented in the user file
   */
}

 

or enable USE_HAL_PCD_REGISTER_CALLBACKS in stm32h7xx_hal_conf.h file 

 

@remov-b4-flight HAL_PCD_SetupStageCallback() is weak callback you should redefine it in main.c without need of  /* USER CODE BEGIN ~ USER CODE END */ surrounded

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.