Skip to content

LPC USB stability fixes #5878

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 22, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 25 additions & 12 deletions features/unsupported/USBDevice/targets/TARGET_NXP/USBHAL_LPC17.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -275,11 +275,6 @@ uint32_t USBHAL::endpointReadcore(uint8_t endpoint, uint8_t *buffer) {

LPC_USB->USBCtrl = 0;

if ((endpoint >> 1) % 3 || (endpoint >> 1) == 0) {
SIEselectEndpoint(endpoint);
SIEclearBuffer();
}

return size;
}

Expand Down Expand Up @@ -431,7 +426,7 @@ void USBHAL::EP0setup(uint8_t *buffer) {
}

void USBHAL::EP0read(void) {
// Not required
endpointRead(EP0OUT, MAX_PACKET_SIZE_EP0);
}

void USBHAL::EP0readStage(void) {
Expand All @@ -456,6 +451,11 @@ void USBHAL::EP0stall(void) {
}

EP_STATUS USBHAL::endpointRead(uint8_t endpoint, uint32_t maximumSize) {
// Don't clear isochronous endpoints
if ((endpoint >> 1) % 3 || (endpoint >> 1) == 0) {
SIEselectEndpoint(endpoint);
SIEclearBuffer();
}
return EP_PENDING;
}

Expand Down Expand Up @@ -590,6 +590,25 @@ void USBHAL::usbisr(void) {
if (LPC_USB->USBDevIntSt & EP_SLOW) {
// (Slow) Endpoint Interrupt

// Process IN packets before SETUP packets
// Note - order of OUT and SETUP does not matter as OUT packets
// are clobbered by SETUP packets and thus ignored.
//
// A SETUP packet can arrive at any time where as an IN packet is
// only sent after calling EP0write and an OUT packet after EP0read.
// The functions EP0write and EP0read are called only in response to
// a setup packet or IN/OUT packets sent in response to that
// setup packet. Therefore, if an IN or OUT packet is pending
// at the same time as a SETUP packet, the IN or OUT packet belongs
// to the previous control transfer and should either be processed
// before the SETUP packet (in the case of IN) or dropped (in the
// case of OUT as SETUP clobbers the OUT data).
if (LPC_USB->USBEpIntSt & EP(EP0IN)) {
selectEndpointClearInterrupt(EP0IN);
LPC_USB->USBDevIntClr = EP_SLOW;
EP0in();
}

// Process each endpoint interrupt
if (LPC_USB->USBEpIntSt & EP(EP0OUT)) {
if (selectEndpointClearInterrupt(EP0OUT) & SIE_SE_STP) {
Expand All @@ -601,12 +620,6 @@ void USBHAL::usbisr(void) {
LPC_USB->USBDevIntClr = EP_SLOW;
}

if (LPC_USB->USBEpIntSt & EP(EP0IN)) {
selectEndpointClearInterrupt(EP0IN);
LPC_USB->USBDevIntClr = EP_SLOW;
EP0in();
}

for (uint8_t num = 2; num < 16*2; num++) {
if (LPC_USB->USBEpIntSt & EP(num)) {
selectEndpointClearInterrupt(num);
Expand Down
37 changes: 25 additions & 12 deletions features/unsupported/USBDevice/targets/TARGET_NXP/USBHAL_LPC40.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -276,11 +276,6 @@ uint32_t USBHAL::endpointReadcore(uint8_t endpoint, uint8_t *buffer) {

LPC_USB->Ctrl = 0;

if ((endpoint >> 1) % 3 || (endpoint >> 1) == 0) {
SIEselectEndpoint(endpoint);
SIEclearBuffer();
}

return size;
}

Expand Down Expand Up @@ -436,7 +431,7 @@ void USBHAL::EP0setup(uint8_t *buffer) {
}

void USBHAL::EP0read(void) {
// Not required
endpointRead(EP0OUT, MAX_PACKET_SIZE_EP0);
}

void USBHAL::EP0readStage(void) {
Expand All @@ -461,6 +456,11 @@ void USBHAL::EP0stall(void) {
}

EP_STATUS USBHAL::endpointRead(uint8_t endpoint, uint32_t maximumSize) {
// Don't clear isochronous endpoints
if ((endpoint >> 1) % 3 || (endpoint >> 1) == 0) {
SIEselectEndpoint(endpoint);
SIEclearBuffer();
}
return EP_PENDING;
}

Expand Down Expand Up @@ -595,6 +595,25 @@ void USBHAL::usbisr(void) {
if (LPC_USB->DevIntSt & EP_SLOW) {
// (Slow) Endpoint Interrupt

// Process IN packets before SETUP packets
// Note - order of OUT and SETUP does not matter as OUT packets
// are clobbered by SETUP packets and thus ignored.
//
// A SETUP packet can arrive at any time where as an IN packet is
// only sent after calling EP0write and an OUT packet after EP0read.
// The functions EP0write and EP0read are called only in response to
// a setup packet or IN/OUT packets sent in response to that
// setup packet. Therefore, if an IN or OUT packet is pending
// at the same time as a SETUP packet, the IN or OUT packet belongs
// to the previous control transfer and should either be processed
// before the SETUP packet (in the case of IN) or dropped (in the
// case of OUT as SETUP clobbers the OUT data).
if (LPC_USB->EpIntSt & EP(EP0IN)) {
selectEndpointClearInterrupt(EP0IN);
LPC_USB->DevIntClr = EP_SLOW;
EP0in();
}

// Process each endpoint interrupt
if (LPC_USB->EpIntSt & EP(EP0OUT)) {
if (selectEndpointClearInterrupt(EP0OUT) & SIE_SE_STP) {
Expand All @@ -606,12 +625,6 @@ void USBHAL::usbisr(void) {
LPC_USB->DevIntClr = EP_SLOW;
}

if (LPC_USB->EpIntSt & EP(EP0IN)) {
selectEndpointClearInterrupt(EP0IN);
LPC_USB->DevIntClr = EP_SLOW;
EP0in();
}

for (uint8_t num = 2; num < 16*2; num++) {
if (LPC_USB->EpIntSt & EP(num)) {
selectEndpointClearInterrupt(num);
Expand Down