Skip to content

Commit 35f2774

Browse files
committed
fix(usb): defer HAL v2 reconnect until device start
1 parent dbddc54 commit 35f2774

1 file changed

Lines changed: 24 additions & 6 deletions

File tree

‎libraries/USBDevice/src/usbd_confv2.c‎

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,28 @@ static void USBD_V2_ConfigPinsAndIrq(void)
3939
HAL_CORTEX_NVIC_EnableIRQ(USB_IRQn);
4040
}
4141

42+
#if !defined(USBD_REENUM_DISABLED)
43+
/* HAL v2 adapters own USB pull-up control. Keep the common re-enumeration
44+
* entry point, but implement it through the PCD API rather than the legacy
45+
* USB_DevConnect/USB_DevDisconnect helpers. */
46+
void USBD_reenumerate(void)
47+
{
48+
if ((g_hpcd.global_state == HAL_PCD_STATE_IDLE) ||
49+
(g_hpcd.global_state == HAL_PCD_STATE_ACTIVE)) {
50+
const uint32_t was_active = (g_hpcd.global_state == HAL_PCD_STATE_ACTIVE);
51+
52+
(void)HAL_PCD_DeviceDisconnect(&g_hpcd);
53+
HAL_Delay(USBD_ENUM_DELAY);
54+
55+
/* HAL_PCD_Start() connects the device when startup is still in the IDLE
56+
* state. Only reconnect here when the device was already running. */
57+
if (was_active != 0U) {
58+
(void)HAL_PCD_DeviceConnect(&g_hpcd);
59+
}
60+
}
61+
}
62+
#endif
63+
4264
void HAL_PCD_SetupStageCallback(hal_pcd_handle_t *hpcd)
4365
{
4466
USBD_LL_SetupStage(g_pdev, (uint8_t *)hpcd->setup);
@@ -142,12 +164,8 @@ USBD_StatusTypeDef USBD_LL_Init(USBD_HandleTypeDef *pdev)
142164
}
143165
}
144166

145-
/* HAL v2 exposes the USB pull-up through the PCD device-connect API. Do
146-
* the startup detach after the PCD has been initialized instead of reaching
147-
* into a board-specific GPIO pin from the common re-enumeration helper.
148-
* HAL_PCD_Start() performs the matching attach once USB is ready. */
149-
(void)HAL_PCD_DeviceDisconnect(&g_hpcd);
150-
HAL_Delay(USBD_ENUM_DELAY);
167+
/* HAL v2 exposes the USB pull-up through the PCD device-connect API. */
168+
USBD_reenumerate();
151169

152170
return USBD_OK;
153171
}

0 commit comments

Comments
 (0)