USB: unify and improve USB (dis)connect
This commit is contained in:
parent
08cba3cd71
commit
3c0130ba4f
|
@ -247,7 +247,7 @@ static const char* usb_strings[] = {
|
||||||
"DFU bootloader (runtime mode)", /**< DFU interface string */
|
"DFU bootloader (runtime mode)", /**< DFU interface string */
|
||||||
};
|
};
|
||||||
|
|
||||||
/** disconnect USB by pulling down D+ to for re-enumerate */
|
/** disconnect USB to force re-enumerate */
|
||||||
static void usb_disconnect(void)
|
static void usb_disconnect(void)
|
||||||
{
|
{
|
||||||
if (usb_device) {
|
if (usb_device) {
|
||||||
|
@ -258,18 +258,12 @@ static void usb_disconnect(void)
|
||||||
rcc_periph_clock_enable(RCC_GPIOB);
|
rcc_periph_clock_enable(RCC_GPIOB);
|
||||||
gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO9);
|
gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO9);
|
||||||
gpio_set(GPIOB, GPIO9);
|
gpio_set(GPIOB, GPIO9);
|
||||||
for (uint32_t i = 0; i < 0x2000; i++) { // wait for at least 10 ms
|
#endif
|
||||||
__asm__("nop");
|
|
||||||
}
|
|
||||||
#else
|
|
||||||
// pull USB D+ low for a short while
|
// pull USB D+ low for a short while
|
||||||
rcc_periph_clock_enable(RCC_GPIOA);
|
rcc_periph_clock_enable(RCC_GPIOA);
|
||||||
gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO12);
|
gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO12);
|
||||||
gpio_clear(GPIOA, GPIO12);
|
gpio_clear(GPIOA, GPIO12);
|
||||||
for (uint32_t i = 0; i < 0x2000; i++) { // wait for at least 10 ms
|
sleep_ms(50); // USB disconnected must be at least 10 ms long, at most 100 ms
|
||||||
__asm__("nop");
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** DFU detach (disconnect USB and perform core reset)
|
/** DFU detach (disconnect USB and perform core reset)
|
||||||
|
@ -447,6 +441,12 @@ void usb_cdcacm_setup(void)
|
||||||
// initialize USB
|
// initialize USB
|
||||||
rcc_periph_reset_pulse(RST_USB); // reset USB peripheral
|
rcc_periph_reset_pulse(RST_USB); // reset USB peripheral
|
||||||
usb_disconnect(); // disconnect to force re-enumeration
|
usb_disconnect(); // disconnect to force re-enumeration
|
||||||
|
#if defined(MAPLE_MINI)
|
||||||
|
// connect USB D+ using dedicated DISC line/circuit on PB9
|
||||||
|
rcc_periph_clock_enable(RCC_GPIOB);
|
||||||
|
gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO9);
|
||||||
|
gpio_clear(GPIOB, GPIO9);
|
||||||
|
#endif
|
||||||
rcc_periph_clock_enable(RCC_GPIOA); // enable clock for GPIO used for USB
|
rcc_periph_clock_enable(RCC_GPIOA); // enable clock for GPIO used for USB
|
||||||
rcc_periph_clock_enable(RCC_USB); // enable clock for USB domain
|
rcc_periph_clock_enable(RCC_USB); // enable clock for USB domain
|
||||||
usb_device = usbd_init(&st_usbfs_v1_usb_driver, &usb_cdcacm_device_descriptor, &usb_cdcacm_configuration_descriptor, usb_strings, LENGTH(usb_strings), usbd_control_buffer, sizeof(usbd_control_buffer));
|
usb_device = usbd_init(&st_usbfs_v1_usb_driver, &usb_cdcacm_device_descriptor, &usb_cdcacm_configuration_descriptor, usb_strings, LENGTH(usb_strings), usbd_control_buffer, sizeof(usbd_control_buffer));
|
||||||
|
|
|
@ -131,24 +131,20 @@ static const char *usb_dfu_strings[] = {
|
||||||
/** disconnect USB to force re-enumerate */
|
/** disconnect USB to force re-enumerate */
|
||||||
static void usb_disconnect(void)
|
static void usb_disconnect(void)
|
||||||
{
|
{
|
||||||
|
if (usb_device) {
|
||||||
|
usbd_disconnect(usb_device, true);
|
||||||
|
}
|
||||||
#if defined(MAPLE_MINI)
|
#if defined(MAPLE_MINI)
|
||||||
// disconnect USB D+ using dedicated DISC line/circuit on PB9
|
// disconnect USB D+ using dedicated DISC line/circuit on PB9
|
||||||
rcc_periph_clock_enable(RCC_GPIOB);
|
rcc_periph_clock_enable(RCC_GPIOB);
|
||||||
gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO9);
|
gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO9);
|
||||||
gpio_set(GPIOB, GPIO9);
|
gpio_set(GPIOB, GPIO9);
|
||||||
for (uint32_t i = 0; i < 0x2000; i++) {
|
#endif
|
||||||
__asm__("nop");
|
|
||||||
}
|
|
||||||
gpio_clear(GPIOB, GPIO9);
|
|
||||||
#else
|
|
||||||
// pull USB D+ low for a short while
|
// pull USB D+ low for a short while
|
||||||
rcc_periph_clock_enable(RCC_GPIOA);
|
rcc_periph_clock_enable(RCC_GPIOA);
|
||||||
gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO12);
|
gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO12);
|
||||||
gpio_clear(GPIOA, GPIO12);
|
gpio_clear(GPIOA, GPIO12);
|
||||||
for (uint32_t i = 0; i < 0x2000; i++) {
|
sleep_ms(50); // USB disconnected must be at least 10 ms long, at most 100 ms
|
||||||
__asm__("nop");
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** flash downloaded data block
|
/** flash downloaded data block
|
||||||
|
@ -311,6 +307,12 @@ void usb_dfu_setup(void)
|
||||||
|
|
||||||
rcc_periph_reset_pulse(RST_USB); // reset USB peripheral
|
rcc_periph_reset_pulse(RST_USB); // reset USB peripheral
|
||||||
usb_disconnect(); // disconnect to force re-enumeration
|
usb_disconnect(); // disconnect to force re-enumeration
|
||||||
|
#if defined(MAPLE_MINI)
|
||||||
|
// connect USB D+ using dedicated DISC line/circuit on PB9
|
||||||
|
rcc_periph_clock_enable(RCC_GPIOB);
|
||||||
|
gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO9);
|
||||||
|
gpio_clear(GPIOB, GPIO9);
|
||||||
|
#endif
|
||||||
rcc_periph_clock_enable(RCC_GPIOA); // enable clock for GPIO used for USB
|
rcc_periph_clock_enable(RCC_GPIOA); // enable clock for GPIO used for USB
|
||||||
rcc_periph_clock_enable(RCC_USB); // enable clock for USB domain
|
rcc_periph_clock_enable(RCC_USB); // enable clock for USB domain
|
||||||
usb_device = usbd_init(&st_usbfs_v1_usb_driver, &usb_dfu_device, &usb_dfu_configuration, usb_dfu_strings, LENGTH(usb_dfu_strings), usbd_control_buffer, sizeof(usbd_control_buffer)); // configure USB device
|
usb_device = usbd_init(&st_usbfs_v1_usb_driver, &usb_dfu_device, &usb_dfu_configuration, usb_dfu_strings, LENGTH(usb_dfu_strings), usbd_control_buffer, sizeof(usbd_control_buffer)); // configure USB device
|
||||||
|
|
Loading…
Reference in New Issue