更新USB反初始化相关内容

This commit is contained in:
不吃油炸鸡
2026-02-08 19:30:56 +08:00
parent 23b7f7d283
commit 45aaffdc08
7 changed files with 24885 additions and 24797 deletions

File diff suppressed because one or more lines are too long

File diff suppressed because it is too large Load Diff

View File

@@ -52,6 +52,13 @@ void USER_USB_DEVICE_DeInit(void)
{
USBD_Stop(&hUsbDeviceFS);
USBD_DeInit(&hUsbDeviceFS);
__HAL_RCC_USB_OTG_FS_FORCE_RESET();
HAL_Delay(10);
__HAL_RCC_USB_OTG_FS_RELEASE_RESET();
__HAL_RCC_USB_OTG_FS_CLK_DISABLE();
HAL_PCD_MspDeInit(hUsbDeviceFS.pData);
// 手动配置USB引脚为GPIO输出强制拉低DP线以模拟拔出
USER_USBD_LL_Simulate_Disconnect();
}
/* USER CODE END 0 */

View File

@@ -59,6 +59,28 @@ USBD_StatusTypeDef USBD_Get_USB_Status(HAL_StatusTypeDef hal_status);
/* USER CODE BEGIN 1 */
/**
* @brief Manually simulates a USB device disconnection by pulling D+ low.
* @retval None
*/
void USER_USBD_LL_Simulate_Disconnect(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
// 1. 确保 GPIOA 时钟已使能
__HAL_RCC_GPIOA_CLK_ENABLE();
// 2. 将 PA12 (USB_DP) 配置为推挽输出
GPIO_InitStruct.Pin = GPIO_PIN_12;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
// 3. 强制将 PA12 输出低电平,拉低 D+
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_12, GPIO_PIN_RESET);
}
/* USER CODE END 1 */
/*******************************************************************************

View File

@@ -36,6 +36,8 @@
/* USER CODE BEGIN INCLUDE */
void USER_USBD_LL_Simulate_Disconnect(void);
/* USER CODE END INCLUDE */
/** @addtogroup USBD_OTG_DRIVER

View File

@@ -30,6 +30,9 @@ void MessageTask(void *argument)
// set the EEPROM flag
EEPROM_UpdateCommand_Write(true);
HAL_Delay(100);
USER_USB_DEVICE_DeInit();
// 给予PC足够的时间来识别设备断开
HAL_Delay(500);
// reset
NVIC_SystemReset();
}