cancel
Showing results for 
Search instead for 
Did you mean: 

How to sleep when USB_VBUS on stm32f105 is low?

CDew.1
Associate III

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?

 

1 ACCEPTED SOLUTION

Accepted Solutions

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.

 

View solution in original post

4 REPLIES 4
FBL
ST Employee

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

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.

CDew.1
Associate III

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.)

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.

 

More neatly:

 

#define USB_OTG_FS_VBUS_GPIO_Port PMU_LED_PWR_GPIO_Port

#define USB_OTG_FS_VBUS_Pin (PMU_LED_PWR_Pin >> 1)

 

int usb_plugged_in = HAL_GPIO_ReadPin(USB_OTG_FS_VBUS_GPIO_Port, USB_OTG_FS_VBUS_Pin);

 

where PMU_LED_PWR is the name of PA10.