2025-03-12 2:39 AM
I'm using the STM32H753ZI Nucleo board with USB_OTG_FS communication,
and I have enabled VBUS_SENSING.
I would like to detect when the physical connection is lost,
but I am not sure how to do it.
Could you kindly provide any reference materials or guidance on the best approach?
2025-03-26 5:47 AM - edited 2025-03-26 5:48 AM
Hi @HMsDobby2
Which stack are you using? If it is USBX x-cube-azrtos-h7, in order to implement detection of USB cable removal, you can add the following in app_usbx_device.c file.
static UINT USBD_ChangeFunction(ULONG Device_State)
{
UINT status = UX_SUCCESS;
/* USER CODE BEGIN USBD_ChangeFunction0 */
/* USER CODE END USBD_ChangeFunction0 */
switch (Device_State)
{
case UX_DEVICE_ATTACHED:
/* USER CODE BEGIN UX_DEVICE_ATTACHED */
/* USER CODE END UX_DEVICE_ATTACHED */
break;
case UX_DEVICE_REMOVED:
/* USER CODE BEGIN UX_DEVICE_REMOVED */
/* USER CODE END UX_DEVICE_REMOVED */
break;
case UX_DCD_STM32_DEVICE_CONNECTED:
/* USER CODE BEGIN UX_DCD_STM32_DEVICE_CONNECTED */
/* USER CODE END UX_DCD_STM32_DEVICE_CONNECTED */
break;
case UX_DCD_STM32_DEVICE_DISCONNECTED:
/* USER CODE BEGIN UX_DCD_STM32_DEVICE_DISCONNECTED */
HAL_PCD_DisconnectCallback(&hpcd_USB_OTG_HS);
/* USER CODE END UX_DCD_STM32_DEVICE_DISCONNECTED */
break;
case UX_DCD_STM32_DEVICE_SUSPENDED:
/* USER CODE BEGIN UX_DCD_STM32_DEVICE_SUSPENDED */
/* USER CODE END UX_DCD_STM32_DEVICE_SUSPENDED */
break;
case UX_DCD_STM32_DEVICE_RESUMED:
/* USER CODE BEGIN UX_DCD_STM32_DEVICE_RESUMED */
/* USER CODE END UX_DCD_STM32_DEVICE_RESUMED */
break;
case UX_DCD_STM32_SOF_RECEIVED:
/* USER CODE BEGIN UX_DCD_STM32_SOF_RECEIVED */
/* USER CODE END UX_DCD_STM32_SOF_RECEIVED */
break;
default:
/* USER CODE BEGIN DEFAULT */
/* USER CODE END DEFAULT */
break;
}
And update this call
/* Install the device portion of USBX */
if (ux_device_stack_initialize(device_framework_high_speed,
device_framework_hs_length,
device_framework_full_speed,
device_framework_fs_length,
string_framework,
string_framework_length,
language_id_framework,
language_id_framework_length,
USBD_ChangeFunction) != UX_SUCCESS)
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.