Skip to content

Commit f2e276e

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 #1089
1 parent 82dc31d commit f2e276e

File tree

3 files changed

+14
-6
lines changed

3 files changed

+14
-6
lines changed

source/board/microbitv2/kl27z/power.c

Lines changed: 1 addition & 1 deletion
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -401,9 +401,13 @@ 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+
#if (USBD_HID_ENABLE)
408+
usbd_bulk_init();
409+
usbd_hid_init();
410+
#endif
407411
break;
408412

409413
case USB_CONNECTING:
@@ -435,12 +439,16 @@ void main_task(void * arg)
435439
break;
436440

437441
case USB_CONNECTED:
442+
if (!usbd_configured()) {
443+
usb_state = USB_DISCONNECTING;
444+
}
445+
break;
446+
438447
case USB_DISCONNECTED:
439448
if (usbd_configured()) {
440449
usb_state = USB_CONNECTED;
441450
}
442451
else {
443-
usb_state = USB_DISCONNECTED;
444452
usb_state_count = USB_CONNECT_DELAY;
445453
usb_no_config_count = USB_CONFIGURE_TIMEOUT;
446454
}

0 commit comments

Comments
 (0)