Skip to main content
Visitor II
May 17, 2024
Solved

How to sleep when USB_VBUS on stm32f105 is low?

  • May 17, 2024
  • 1 reply
  • 1508 views

I have a self-powered device.  It appears as a USB CDC device /dev/ttyACM0, when plugged into a Linux host.  The device port is wired to USB_VBUS, so that pin will only be high when connected to a USB host.

I would like to detect the presence of the USB cable, so that the device can sleep if there is no cable and other conditions are met.

- How can I detect the presence of the USB cable?  I'm using CubeIDE and the HAL.

- How can I put the STM32 to sleep in such a way that it will wake up when the USB cable is plugged back in?

 

    This topic has been closed for replies.
    Best answer by CDew.1

    I've found a solution:

    int usb_plugged_in = HAL_GPIO_ReadPin(PMU_LED_PWR_GPIO_Port, PMU_LED_PWR_Pin / 2);

    where PMU_LED_PWR is the name of PA10.  PA9 is on the same port as PA10 and its pin mask is right-shifted.

     

    i.e. Setting PA9 to the role of USB_OTG_FS_VBUS makes it a GPIO input at a lower level, but doesn't give you a name to reference it.  By calculating USB_OTG_FS_VBUS's port struct and pin mask, you can access it.

     

    1 reply

    Technical Moderator
    May 20, 2024

    Hi @CDew.1 

    If your device is self-powered, you can configure a GPIO pin to detect the VBUS level. 

    Generally, to wake up a FS device from a suspend state, the host needs to drive low D+ to the K-State. Check section 28.5.2 Peripheral states in Reference Manual

    CDew.1Author
    Visitor II
    May 20, 2024

    Thanks for your reply.  My self-powered device is always USB device and never a host.

    I currently have PA9 configured as USB_OTG_FS_VBUS.

    - Will USB (device) continue to work if I change PA9 to a GPIO_Input?

    - Or do I need to rework the board to route VBUS to an *additional* GPIO pin?

    (I was hoping that there was a HAL_ function which I could poll to see if USB_OTG_FS_VBUS was high.)

    CDew.1AuthorAnswer
    Visitor II
    May 20, 2024

    I've found a solution:

    int usb_plugged_in = HAL_GPIO_ReadPin(PMU_LED_PWR_GPIO_Port, PMU_LED_PWR_Pin / 2);

    where PMU_LED_PWR is the name of PA10.  PA9 is on the same port as PA10 and its pin mask is right-shifted.

     

    i.e. Setting PA9 to the role of USB_OTG_FS_VBUS makes it a GPIO input at a lower level, but doesn't give you a name to reference it.  By calculating USB_OTG_FS_VBUS's port struct and pin mask, you can access it.