Recently, a USB client communicates with the host computer and the MCU needs to be reset by the software. If the USB is not plugged in after the reset, the PC will not reenumerate the USB to solve this problem, I made a software reset and simulated USB plug-in.
Here I use the software reset of the HAL database. Before resetting, I turn off the interrupt and then reset it. The Code is as follows:
_ Set_faultmask (1); // disable nvic_systemreset (); // Reset
As for how to implement analog USB plugging, you only need to perform a low-latency-high operation before USB initialization, generally, the PC can check the usb d + pin to determine whether the USB has changed. For the stm32 pa12, it corresponds to this pin, therefore, you can simulate the USB plugging operation by simulating this pin level change. First simulate the plugging, and then perform USB initialization, so that the PC can reenumerate the USB, the Code is as follows:
/** Configure pins as * analog * input * output * event_out * exti */void mx_gpio_init (void) {gpio_inittypedef gpio_initstruct; /* gpio ports clock enable */_ Export ();/* configure gpio pin output level */hal_gpio_writepin (gpioa, gpio_pin_12, gpio_pin_reset);/* configure gpio pin: pa12 */export = gpio_pin_12; export = export; export = gpio_pulldown; export = export; hal_gpio_init (gpioa, & gpio_initstruct); Aggregate (gpioa, gpio_pin_12, callback ); hal_delay (65); // pull the pa12 down first and then pull it up. Use D + to simulate USB plugging.
Hal_gpio_writepin (gpioa, gpio_pin_12, gpio_pin_set); hal_delay (65 );}
Only note that the analog plug-in is placed before the USB initialization, so that the plug-in can be simulated after each reset, and the PC can re-enumerate the USB.
Stm32 software reset and analog USB plugging