From 4452b3627236065e0ef08e01c2599b7e88b86972 Mon Sep 17 00:00:00 2001 From: Taylor Yu Date: Sun, 20 Nov 2022 18:14:19 -0600 Subject: [PATCH 1/2] NAK instead of ACK when clearing stall When clearing an endpoint halt condition, set endpoint status to NAK instead of VALID. When Windows receives more data than expected from an interrupt endpoint, it resets the device and attempts to clear the endpoint halt condition on that endpoint. On an IN endpoint, setting the status to VALID will cause a retransmission of the offending packet, resulting in a loop of attempted recovery operations. Signed-off-by: Taylor Yu --- .../GD32F30x_usbd_library/usbd/Source/usbd_lld_core.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/system/GD32F30x_firmware/GD32F30x_usbd_library/usbd/Source/usbd_lld_core.c b/system/GD32F30x_firmware/GD32F30x_usbd_library/usbd/Source/usbd_lld_core.c index 77e0c3ba..15d69cdc 100644 --- a/system/GD32F30x_firmware/GD32F30x_usbd_library/usbd/Source/usbd_lld_core.c +++ b/system/GD32F30x_firmware/GD32F30x_usbd_library/usbd/Source/usbd_lld_core.c @@ -412,7 +412,7 @@ static void usbd_ep_stall_clear (usb_dev *udev, uint8_t ep_addr) udev->transc_in[ep_num].ep_stall = 0U; /* clear endpoint stall status */ - USBD_EP_TX_STAT_SET(ep_num, EPTX_VALID); + USBD_EP_TX_STAT_SET(ep_num, EPTX_NAK); } else { /* clear endpoint data toggle bit */ USBD_RX_DTG_CLEAR(ep_num); @@ -420,7 +420,7 @@ static void usbd_ep_stall_clear (usb_dev *udev, uint8_t ep_addr) udev->transc_out[ep_num].ep_stall = 0U; /* clear endpoint stall status */ - USBD_EP_RX_STAT_SET(ep_num, EPRX_VALID); + USBD_EP_RX_STAT_SET(ep_num, EPRX_NAK); } } From 1faa2e9caf6ed88e207d5c593b490c23d2d36b82 Mon Sep 17 00:00:00 2001 From: Taylor Yu Date: Tue, 22 Nov 2022 13:13:24 -0600 Subject: [PATCH 2/2] handle clearing endpoint stall Re-init the endpoint buffer state on ClearFeature(EndpointHalt). Signed-off-by: Taylor Yu --- cores/arduino/USBCore.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/cores/arduino/USBCore.cpp b/cores/arduino/USBCore.cpp index de4452a0..cb9cb397 100644 --- a/cores/arduino/USBCore.cpp +++ b/cores/arduino/USBCore.cpp @@ -450,6 +450,11 @@ class ClassCore } else if (sent < 0) { return REQ_NOTSUPP; } + } else if ((setup.bmRequestType & USB_RECPTYPE_MASK) == USB_RECPTYPE_EP) { + uint8_t ep = EP_ID(setup.wIndex); + // Reset endpoint state on ClearFeature(EndpointHalt) + EPBuffers().buf(ep).init(ep); + return REQ_SUPP; } else { #ifdef USBD_USE_CDC if (CDCACM().setup(setup))