Skip to content

Commit 57799d4

Browse files
Fix abrupt USB disconnection leaving stale data in DAP queue buffers.
Clearing the buffers in the USB_DISCONNECTING state, which then moves to the USB_DISCONNECTED state. The USB_DISCONNECTING state was never used, in the current codebase nor the initial repo commit (well, second commit, first release): bdacee7 It looks like this state might have been initialy designed as a way to trigger a USB disconnection from the interface chip, as it manually set usbd_connect(0) to disconnect. As it was never used it's now the pre-disconnected state to run code only once on state change: connected->disconnecting->disconnected Fixes ARMmbed#1089
1 parent 82dc31d commit 57799d4

File tree

3 files changed

+12
-6
lines changed

3 files changed

+12
-6
lines changed

source/board/microbitv2/kl27z/power.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ void PORTCD_IRQHandler(void)
106106
USBD_Reset();
107107
usbd_reset_core();
108108
usb_pc_connected = false;
109-
usb_state = USB_DISCONNECTED;
109+
usb_state = USB_DISCONNECTING;
110110
}
111111
else {
112112
// Cable inserted

source/board/microbitv2/nrf52820/power.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ void GPIOTE_IRQHandler(void)
247247
USBD_Reset();
248248
usbd_reset_core();
249249
usb_pc_connected = false;
250-
usb_state = USB_DISCONNECTED;
250+
usb_state = USB_DISCONNECTING;
251251
} else {
252252
// Cable inserted
253253
wake_from_usb = 1;
@@ -272,7 +272,7 @@ void POWER_CLOCK_IRQHandler(void)
272272
USBD_Reset();
273273
usbd_reset_core();
274274
usb_pc_connected = false;
275-
usb_state = USB_DISCONNECTED;
275+
usb_state = USB_DISCONNECTING;
276276
}
277277
}
278278
#endif // POWER_IRQ_USBDETECTED

source/daplink/interface/main_interface.c

+9-3
Original file line numberDiff line numberDiff line change
@@ -401,9 +401,11 @@ void main_task(void * arg)
401401
switch (usb_state) {
402402
case USB_DISCONNECTING:
403403
usb_state = USB_DISCONNECTED;
404-
// Disable board power before USB is disconnected.
404+
// Disable target power as USB was disconnected.
405405
gpio_set_board_power(false);
406-
usbd_connect(0);
406+
// Clear the DAP queue buffers
407+
usbd_bulk_init();
408+
usbd_hid_init();
407409
break;
408410

409411
case USB_CONNECTING:
@@ -435,12 +437,16 @@ void main_task(void * arg)
435437
break;
436438

437439
case USB_CONNECTED:
440+
if (!usbd_configured()) {
441+
usb_state = USB_DISCONNECTING;
442+
}
443+
break;
444+
438445
case USB_DISCONNECTED:
439446
if (usbd_configured()) {
440447
usb_state = USB_CONNECTED;
441448
}
442449
else {
443-
usb_state = USB_DISCONNECTED;
444450
usb_state_count = USB_CONNECT_DELAY;
445451
usb_no_config_count = USB_CONFIGURE_TIMEOUT;
446452
}

0 commit comments

Comments
 (0)