加入模拟拔出USB

This commit is contained in:
不吃油炸鸡
2026-02-08 17:28:04 +08:00
parent 19da4ff10f
commit b04803ece1
2 changed files with 24 additions and 0 deletions

View File

@@ -47,6 +47,28 @@ void SystemClock_Config(void);
/* USER CODE BEGIN 0 */
/**
* @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 0 */
/* USER CODE BEGIN PFP */