Skip to main content
Graduate
May 24, 2025
Question

STM32U5G9J-DK USBX MSC – How to Reliably Detect Host Connection & Disconnection (No USB PD)

  • May 24, 2025
  • 1 reply
  • 760 views

Hi all,

I'm working on the STM32U5G9J-DK development board and trying to enable USBX-based MSC (Mass Storage Class) device functionality.

My actual product is a battery-powered device, so USB Power Delivery (PD) is not an option. However, it's critical for my application to detect when a USB host (e.g., PC) connects or disconnects to/from the device, as I need to perform certain app-level tasks on both events.

Since the USBX MSC device example was missing in CubeIDE, I updated the USBX CDC ACM device example to function as an MSC device. The device starts and I receive the "USBD_STORAGE_Activate" callback when a host is connected (when I call HAL_PCD_Start directly after initialization), but I do not receive the "USBD_STORAGE_Deactivate" callback when the host disconnects.

I also tried enabling the VBUS sensing option, but the example started misbehaving when I selected that. So I had to disable it.

What I’m looking for:

  • A reliable method to detect both connection and disconnection events, especially in the absence of USB PD.

  • Is VBUS GPIO-based sensing possible on this DK? If yes, how can it be properly configured and used with USBX?

  • If not, are there any software polling techniques or PCD/USBX state checks I can use inside a task/thread to detect host presence?

  • Is there a way to manually trigger MSC device start/stop (e.g., HAL_PCD_Start/Stop) based on host detection?

Any help, examples, or guidance based on STM32U5 or this DK board would be greatly appreciated.

Thanks in advance!

    This topic has been closed for replies.

    1 reply

    Super User
    May 24, 2025

    Hi,

    in USB event loop you can see it -> i set a global info then, usb IN or not...

    AScha3_0-1748103376618.png

     

    ArunJohnAuthor
    Graduate
    May 30, 2025

    Hi, sorry if this was not clear in my post. I want my dev board to act as a mass storage device on to which a host PC can connect to. When a host PC connects to my dev board, I want to get a "connected" event and when the PC disconnects, I want a "disconnected" event.

    Visitor II
    June 18, 2025

    in my application on a STM32U575 I'm using the following code to detect when an USB is plugged in from a PC host (my device acts as USB MSC device like yours).

    static void PWR_InitUSBVDDSensing(void)
    {
     PWR_PVMTypeDef sConfigPVM = {0};
    
     /*
     * PVM Configuration
     */
     sConfigPVM.PVMType = PWR_UVM;
     sConfigPVM.Mode = PWR_PVM_MODE_IT_RISING_FALLING;
     HAL_PWREx_PVMConfig(&sConfigPVM);
    
     /*
     * Enable the PVM Output
     */
     HAL_PWREx_EnableUVM();
    
     /*
     * Disable the internal Pull-Up in Dead Battery pins of UCPD peripheral
     */
     HAL_PWREx_DisableUCPDDeadBattery();
     /* PVD_PVM_IRQn interrupt configuration */
     HAL_NVIC_SetPriority(PVD_PVM_IRQn, 1, 0);
     HAL_NVIC_EnableIRQ(PVD_PVM_IRQn);
    }

    in the handler I call:

    void PVD_PVM_IRQHandler(void)
    {
     HAL_PWREx_PVD_PVM_IRQHandler();
    }

    that triggers the callback

    void HAL_PWREx_UVMCallback(void)
    {
     isUSBConnected = LL_PWR_IsActiveFlag_VDDUSB();
    }

    so basically it detects the USB voltage and gives me a hook to call HAL_PCD_Start when i sense a connection and HAL_PCD_Stop when i sense a disconnection.

    I don't know if it's the proper way to handle it, but I found USBX callbacks to be unreliable.

    give it i try and let me know if windows always enumerate the USB successfully, because sometimes it fails to see the mass storage device.