Skip to content

drivers: usb: Add VBUS detector#103840

Closed
nandojve wants to merge 5 commits intozephyrproject-rtos:mainfrom
nandojve:usb/add_vbus_detector
Closed

drivers: usb: Add VBUS detector#103840
nandojve wants to merge 5 commits intozephyrproject-rtos:mainfrom
nandojve:usb/add_vbus_detector

Conversation

@nandojve
Copy link
Copy Markdown
Member

This introduce a generic VBUS detect mechanism that allow group USB interfaces.

Problem:

A board that is connected on a host were both host and device have their own battery to operate could require VBUS monitoring. This, in many cases, can be solved with the in tree solution. However, when multiple devices are connected to the host by the same USB interface it became more complicate to operate. Some device may need that the MCU monitor the VBUS not only for their own interface but should act on behalf of another. This could be more complicated when the MCU have multiple interfaces and then distinct control should be made and when external devices like modems with USB interfaces are in place and require software coordination.

Proposal:

The proposal is define a generic mechanism that monitor the VBUS in behalf of one or many interfaces. This central point then will configure, for instance, a GPIO pin to interrupt the main CPU when a VBUS detection/removal happens. This help the application to control a power domain or use regulators.

The VBUS detector act injecting the UDC_EVT_VBUS_READY / UDC_EVT_VBUS_REMOVED events direct in the USB interface. From a system perspective, it is like each interface have their own VBUS function. In this case, no changes in the USB stack are needed. The injection is made direct using the USB interface instance and should be compatible with any future USB improvements inside the core. The events are dispatch in the exact order that the interfaces are defined in the devicetree. A software de-bounce optional is available. The only requirement is that the detector should be initialized after the USB interfaces, which can be easy defined using KConfig. A sample was developed to help anyone understand the concept and tests are defined to make sure regressions can be detected.

Add devicetree binding for GPIO-based USB VBUS detection. This binding
allows boards to specify a GPIO pin for sensing VBUS presence and
associate it with a USB device controller.

Properties:
- vbus-gpios: GPIO pin for VBUS sensing
- udc: phandle to USB device controller
- debounce-ms: optional debounce interval (default 20ms)

Signed-off-by: BUDKE Gerson Fernando <gerson.budke@leica-geosystems.com>
Add a VBUS detection subsystem for the USB-next stack that provides:
- Minimal API (usb_vbus_ready/usb_vbus_removed) for detector drivers
- Wrapper around udc_submit_event() to isolate UDC dependency
- GPIO-based detection driver with debounce support

The GPIO driver monitors VBUS state via interrupt and notifies the USB
device controller of connection/disconnection events.

Signed-off-by: BUDKE Gerson Fernando <gerson.budke@leica-geosystems.com>
Add ztest suite for the VBUS detection GPIO driver covering:
- GPIO interrupt configuration verification
- VBUS ready event on GPIO high
- VBUS removed event on GPIO low
- Debounce behavior

Tests run on native_sim using the GPIO emulator.

Signed-off-by: BUDKE Gerson Fernando <gerson.budke@leica-geosystems.com>
Add sample application demonstrating VBUS detection usage. The sample
logs VBUS state changes to the console.

Also update the USB Common sample Kconfig to conditionally enable VBUS
detection when the devicetree binding is present.

Signed-off-by: BUDKE Gerson Fernando <gerson.budke@leica-geosystems.com>
Add VBUS detection node using PA9 GPIO for sensing USB cable connection.
This enables the board to properly handle USB plug/unplug events with
the USB-next stack.

Signed-off-by: BUDKE Gerson Fernando <gerson.budke@leica-geosystems.com>
@sonarqubecloud
Copy link
Copy Markdown

@josuah
Copy link
Copy Markdown
Contributor

josuah commented Feb 10, 2026

Some vendor-specifics of DWC2 also does some VBUS detection, in a different way:

/*
* On USBHS, we cannot access the DWC2 register until VBUS is detected and
* valid. If the user tries to force usbd_enable() and the corresponding
* udc_enable() without a "VBUS ready" notification, the event wait will block
* until a valid VBUS signal is detected or until the
* CONFIG_UDC_DWC2_USBHS_VBUS_READY_TIMEOUT timeout expires.
*/
static K_EVENT_DEFINE(usbhs_events);
#define USBHS_VBUS_READY BIT(0)

Not sure if this qualifies for this API as well.

@nandojve
Copy link
Copy Markdown
Member Author

Some vendor-specifics of DWC2 also does some VBUS detection, in a different way:

/*
* On USBHS, we cannot access the DWC2 register until VBUS is detected and
* valid. If the user tries to force usbd_enable() and the corresponding
* udc_enable() without a "VBUS ready" notification, the event wait will block
* until a valid VBUS signal is detected or until the
* CONFIG_UDC_DWC2_USBHS_VBUS_READY_TIMEOUT timeout expires.
*/
static K_EVENT_DEFINE(usbhs_events);
#define USBHS_VBUS_READY BIT(0)

Not sure if this qualifies for this API as well.

What you mean in a different way? Do you have something in mind?

@josuah
Copy link
Copy Markdown
Contributor

josuah commented Feb 11, 2026

in a different way?

I meant that UDC not using a GPIO for VBUS detection could also use this API.
For instance, DWC2 with nRF uses a HAL function for it: nrfs_err = nrfs_usb_init(usbhs_vbus_handler);.

  • nRF devices with DWC2 UDC would have nRF VBUS detection.
  • ESP32 devices with DWC2 UDC would have ESP32 VBUS detection.
  • ...

This is orthogonal though: only trying to anticipate possible use-cases.
This seems like a USB PHY API somehow.
Thank you.

#endif

/**
* @brief Notify USB stack that VBUS is ready.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this will need some versioning.

common:
tags:
- usb
- vbus
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible that other, different protocols also have something called "VBUS"? Or is it unambiguous enough?

Comment on lines +431 to +436
static inline void udc_set_can_detect_vbus(const struct device *dev)
{
struct udc_data *data = (struct udc_data *)dev->data;

data->caps.can_detect_vbus = true;
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How is this synchronized against UDC init and USB stack using the value?

@github-actions
Copy link
Copy Markdown

This pull request has been marked as stale because it has been open (more than) 30 days with no activity. Remove the stale label or add a comment saying that you would like to have the label removed otherwise this pull request will automatically be closed in 7 days. Note, that you can always re-open a closed pull request at any time.

@github-actions github-actions Bot added the Stale label Mar 22, 2026
@tmon-nordic
Copy link
Copy Markdown
Contributor

in a different way?

I meant that UDC not using a GPIO for VBUS detection could also use this API. For instance, DWC2 with nRF uses a HAL function for it: nrfs_err = nrfs_usb_init(usbhs_vbus_handler);.

* nRF devices with DWC2 UDC would have nRF VBUS detection.

This is a perfectly valid use-case. In fact nRF devices would have two separate VBUS detectors. nRF54H20 would use nrfs, while nRF54LM20 would use the VREGUSB regulator.

@github-actions
Copy link
Copy Markdown

This pull request has been marked as stale because it has been open (more than) 30 days with no activity. Remove the stale label or add a comment saying that you would like to have the label removed otherwise this pull request will automatically be closed in 7 days. Note, that you can always re-open a closed pull request at any time.

@github-actions github-actions Bot added the Stale label Apr 24, 2026
@github-actions github-actions Bot closed this May 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: Boards/SoCs area: Devicetree Binding PR modifies or adds a Device Tree binding area: Devicetree area: Samples Samples area: Tests Issues related to a particular existing or missing test area: USB Universal Serial Bus platform: STM32 ST Micro STM32 Stale

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants