Skip to content

Commit e142d86

Browse files
cumin777fpistm
authored andcommitted
fix(usb): defer HAL v2 reconnect until device start
Signed-off-by: cumin <13809292481@163.com>
1 parent 4745b23 commit e142d86

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
@@ -30,6 +30,28 @@ static void USBD_V2_ConfigPinsAndIrq(void)
3030
HAL_CORTEX_NVIC_EnableIRQ(USB_IRQn);
3131
}
3232

33+
#if !defined(USBD_REENUM_DISABLED)
34+
/* HAL v2 adapters own USB pull-up control. Keep the common re-enumeration
35+
* entry point, but implement it through the PCD API rather than the legacy
36+
* USB_DevConnect/USB_DevDisconnect helpers. */
37+
void USBD_reenumerate(void)
38+
{
39+
if ((g_hpcd.global_state == HAL_PCD_STATE_IDLE) ||
40+
(g_hpcd.global_state == HAL_PCD_STATE_ACTIVE)) {
41+
const uint32_t was_active = (g_hpcd.global_state == HAL_PCD_STATE_ACTIVE);
42+
43+
(void)HAL_PCD_DeviceDisconnect(&g_hpcd);
44+
HAL_Delay(USBD_ENUM_DELAY);
45+
46+
/* HAL_PCD_Start() connects the device when startup is still in the IDLE
47+
* state. Only reconnect here when the device was already running. */
48+
if (was_active != 0U) {
49+
(void)HAL_PCD_DeviceConnect(&g_hpcd);
50+
}
51+
}
52+
}
53+
#endif
54+
3355
void HAL_PCD_SetupStageCallback(hal_pcd_handle_t *hpcd)
3456
{
3557
USBD_LL_SetupStage(g_pdev, (uint8_t *)hpcd->setup);
@@ -133,12 +155,8 @@ USBD_StatusTypeDef USBD_LL_Init(USBD_HandleTypeDef *pdev)
133155
}
134156
}
135157

136-
/* HAL v2 exposes the USB pull-up through the PCD device-connect API. Do
137-
* the startup detach after the PCD has been initialized instead of reaching
138-
* into a board-specific GPIO pin from the common re-enumeration helper.
139-
* HAL_PCD_Start() performs the matching attach once USB is ready. */
140-
(void)HAL_PCD_DeviceDisconnect(&g_hpcd);
141-
HAL_Delay(USBD_ENUM_DELAY);
158+
/* HAL v2 exposes the USB pull-up through the PCD device-connect API. */
159+
USBD_reenumerate();
142160

143161
return USBD_OK;
144162
}

0 commit comments

Comments
 (0)