Skip to content

Commit eda332c

Browse files
committed
Fix LPC17XX and LPC40XX USB race condition
If a SETUP packet arrives shortly after an IN then the packets will be processed in the wrong order - SETUP first then IN. This causes the subsequent control transfer to fail. Fix this problem by processing IN packets before processing SETUP packets.
1 parent 635a824 commit eda332c

File tree

2 files changed

+38
-12
lines changed

2 files changed

+38
-12
lines changed

features/unsupported/USBDevice/targets/TARGET_NXP/USBHAL_LPC17.cpp

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -590,6 +590,25 @@ void USBHAL::usbisr(void) {
590590
if (LPC_USB->USBDevIntSt & EP_SLOW) {
591591
// (Slow) Endpoint Interrupt
592592

593+
// Process IN packets before SETUP packets
594+
// Note - order of OUT and SETUP does not matter as OUT packets
595+
// are clobbered by SETUP packets and thus ignored.
596+
//
597+
// A SETUP packet can arrive at any time where as an IN packet is
598+
// only sent after calling EP0write and an OUT packet after EP0read.
599+
// The functions EP0write and EP0read are called only in response to
600+
// a setup packet or IN/OUT packets sent in response to that
601+
// setup packet. Therefore, if an IN or OUT packet is pending
602+
// at the same time as a SETUP packet, the IN or OUT packet belongs
603+
// to the previous control transfer and should either be processed
604+
// before the SETUP packet (in the case of IN) or dropped (in the
605+
// case of OUT as SETUP clobbers the OUT data).
606+
if (LPC_USB->USBEpIntSt & EP(EP0IN)) {
607+
selectEndpointClearInterrupt(EP0IN);
608+
LPC_USB->USBDevIntClr = EP_SLOW;
609+
EP0in();
610+
}
611+
593612
// Process each endpoint interrupt
594613
if (LPC_USB->USBEpIntSt & EP(EP0OUT)) {
595614
if (selectEndpointClearInterrupt(EP0OUT) & SIE_SE_STP) {
@@ -601,12 +620,6 @@ void USBHAL::usbisr(void) {
601620
LPC_USB->USBDevIntClr = EP_SLOW;
602621
}
603622

604-
if (LPC_USB->USBEpIntSt & EP(EP0IN)) {
605-
selectEndpointClearInterrupt(EP0IN);
606-
LPC_USB->USBDevIntClr = EP_SLOW;
607-
EP0in();
608-
}
609-
610623
for (uint8_t num = 2; num < 16*2; num++) {
611624
if (LPC_USB->USBEpIntSt & EP(num)) {
612625
selectEndpointClearInterrupt(num);

features/unsupported/USBDevice/targets/TARGET_NXP/USBHAL_LPC40.cpp

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -595,6 +595,25 @@ void USBHAL::usbisr(void) {
595595
if (LPC_USB->DevIntSt & EP_SLOW) {
596596
// (Slow) Endpoint Interrupt
597597

598+
// Process IN packets before SETUP packets
599+
// Note - order of OUT and SETUP does not matter as OUT packets
600+
// are clobbered by SETUP packets and thus ignored.
601+
//
602+
// A SETUP packet can arrive at any time where as an IN packet is
603+
// only sent after calling EP0write and an OUT packet after EP0read.
604+
// The functions EP0write and EP0read are called only in response to
605+
// a setup packet or IN/OUT packets sent in response to that
606+
// setup packet. Therefore, if an IN or OUT packet is pending
607+
// at the same time as a SETUP packet, the IN or OUT packet belongs
608+
// to the previous control transfer and should either be processed
609+
// before the SETUP packet (in the case of IN) or dropped (in the
610+
// case of OUT as SETUP clobbers the OUT data).
611+
if (LPC_USB->EpIntSt & EP(EP0IN)) {
612+
selectEndpointClearInterrupt(EP0IN);
613+
LPC_USB->DevIntClr = EP_SLOW;
614+
EP0in();
615+
}
616+
598617
// Process each endpoint interrupt
599618
if (LPC_USB->EpIntSt & EP(EP0OUT)) {
600619
if (selectEndpointClearInterrupt(EP0OUT) & SIE_SE_STP) {
@@ -606,12 +625,6 @@ void USBHAL::usbisr(void) {
606625
LPC_USB->DevIntClr = EP_SLOW;
607626
}
608627

609-
if (LPC_USB->EpIntSt & EP(EP0IN)) {
610-
selectEndpointClearInterrupt(EP0IN);
611-
LPC_USB->DevIntClr = EP_SLOW;
612-
EP0in();
613-
}
614-
615628
for (uint8_t num = 2; num < 16*2; num++) {
616629
if (LPC_USB->EpIntSt & EP(num)) {
617630
selectEndpointClearInterrupt(num);

0 commit comments

Comments
 (0)