Skip to content

Commit 18fb3f9

Browse files
alstrzebonskidleach02
authored andcommitted
usb: dwc2: Allow for enabling USB if the cable is disconnected
Adds CONFIG_UDC_DWC2_USBHS_VBUS_READY_TIMEOUT_MS that allows for waiting for a USBHS VBUS ready event for a specified amount of time. Earlier it waited forever and because of that, the udc_enable() was blocked forever if the USB cable was disconnected. Now the function returns error on timeout. Signed-off-by: Aleksander Strzebonski <aleksander.strzebonski@nordicsemi.no>
1 parent ef45434 commit 18fb3f9

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

drivers/usb/udc/Kconfig.dwc2

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,13 @@ config UDC_DWC2_THREAD_PRIORITY
3838
default 8
3939
help
4040
DWC2 driver thread priority.
41+
42+
config UDC_DWC2_USBHS_VBUS_READY_TIMEOUT
43+
int "UDC DWC2 USBHS VBUS ready event timeout in ms"
44+
depends on UDC_DWC2
45+
depends on NRFS_HAS_VBUS_DETECTOR_SERVICE
46+
default 0
47+
help
48+
UDC DWC2 USBHS VBUS ready event timeout. If the VBUS is not ready
49+
and the Nordic USBHS controller is used, the udc_enable() is
50+
blocked for this amount of time. Set it to zero to wait forever.

drivers/usb/udc/udc_dwc2_vendor_quirks.h

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,8 @@ DT_INST_FOREACH_STATUS_OKAY(QUIRK_STM32F4_FSOTG_DEFINE)
121121
* On USBHS, we cannot access the DWC2 register until VBUS is detected and
122122
* valid. If the user tries to force usbd_enable() and the corresponding
123123
* udc_enable() without a "VBUS ready" notification, the event wait will block
124-
* until a valid VBUS signal is detected.
124+
* until a valid VBUS signal is detected or until the
125+
* CONFIG_UDC_DWC2_USBHS_VBUS_READY_TIMEOUT timeout expires.
125126
*/
126127
static K_EVENT_DEFINE(usbhs_events);
127128
#define USBHS_VBUS_READY BIT(0)
@@ -182,10 +183,19 @@ static inline int usbhs_enable_nrfs_service(const struct device *dev)
182183
static inline int usbhs_enable_core(const struct device *dev)
183184
{
184185
NRF_USBHS_Type *wrapper = USBHS_DT_WRAPPER_REG_ADDR(0);
186+
k_timeout_t timeout = K_FOREVER;
187+
188+
#if CONFIG_NRFS_HAS_VBUS_DETECTOR_SERVICE
189+
if (CONFIG_UDC_DWC2_USBHS_VBUS_READY_TIMEOUT) {
190+
timeout = K_MSEC(CONFIG_UDC_DWC2_USBHS_VBUS_READY_TIMEOUT);
191+
}
192+
#endif
185193

186194
if (!k_event_wait(&usbhs_events, USBHS_VBUS_READY, false, K_NO_WAIT)) {
187195
LOG_WRN("VBUS is not ready, block udc_enable()");
188-
k_event_wait(&usbhs_events, USBHS_VBUS_READY, false, K_FOREVER);
196+
if (!k_event_wait(&usbhs_events, USBHS_VBUS_READY, false, timeout)) {
197+
return -ETIMEDOUT;
198+
}
189199
}
190200

191201
wrapper->ENABLE = USBHS_ENABLE_PHY_Msk | USBHS_ENABLE_CORE_Msk;

include/zephyr/drivers/usb/udc.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,7 @@ int udc_init(const struct device *dev,
371371
* @return 0 on success, all other values should be treated as error.
372372
* @retval -EPERM controller is not initialized
373373
* @retval -EALREADY already enabled
374+
* @retval -ETIMEDOUT enable operation timed out
374375
*/
375376
int udc_enable(const struct device *dev);
376377

0 commit comments

Comments
 (0)