Skip to content

Commit 77e1190

Browse files
committed
WIP delay clearing EP_RX_ST
Avoid a race condition by delaying clearing EP_RX_ST on EP0 until the packet buffer has been read.
1 parent 0f44de4 commit 77e1190

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

system/GD32F30x_firmware/GD32F30x_usbd_library/device/Source/usbd_transc.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ OF SUCH DAMAGE.
3535

3636
#include "usbd_enum.h"
3737
#include "usbd_transc.h"
38+
#include "usbd_lld_regs.h"
3839

3940
/* local function prototypes ('static') */
4041
static inline void usb_stall_transc (usb_dev *udev);
@@ -56,8 +57,12 @@ void _usb_setup_transc (usb_dev *udev, uint8_t ep_num)
5657

5758
usb_reqsta reqstat = REQ_NOTSUPP;
5859

60+
/* Force to IDLE state, to cancel actions by any pending completion handlers. */
61+
udev->control.ctl_state = USBD_CTL_IDLE;
62+
5963
uint16_t count = udev->drv_handler->ep_read((uint8_t *)(&udev->control.req), 0U, (uint8_t)EP_BUF_SNG);
6064

65+
USBD_EP_RX_ST_CLEAR(ep_num);
6166
if (count != USB_SETUP_PACKET_LEN) {
6267
udev->drv_handler->err(count);
6368
usb_stall_transc(udev);

system/GD32F30x_firmware/GD32F30x_usbd_library/usbd/Source/usbd_lld_int.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,9 @@ void usbd_isr (void)
118118
/* handle the USB OUT direction transaction */
119119
if (USBD_EPxCS(ep_num) & EPxCS_RX_ST) {
120120
/* clear successful receive interrupt flag */
121-
USBD_EP_RX_ST_CLEAR(ep_num);
121+
if (0U != ep_num) {
122+
USBD_EP_RX_ST_CLEAR(ep_num);
123+
}
122124

123125
if (USBD_EPxCS(ep_num) & EPxCS_SETUP) {
124126

@@ -132,6 +134,10 @@ void usbd_isr (void)
132134

133135
uint16_t count = udev->drv_handler->ep_read (transc->xfer_buf, ep_num, (uint8_t)EP_BUF_SNG);
134136

137+
if (0U == ep_num) {
138+
USBD_EP_RX_ST_CLEAR(ep_num);
139+
}
140+
135141
transc->xfer_buf += count;
136142
transc->xfer_count += count;
137143

0 commit comments

Comments
 (0)