Skip to content

Commit

Permalink
Merge pull request #12 from tlyu/fix-empty-read
Browse files Browse the repository at this point in the history
  • Loading branch information
obra authored Nov 13, 2022
2 parents 27dc0cf + f01b05b commit 4a3d515
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
11 changes: 11 additions & 0 deletions cores/arduino/USBCore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,10 @@ int USBCore_::sendControl(uint8_t flags, const void* data, int len)
this->flush(0);
}

if (wrote != 0) {
this->controlWritten = true;
}

// Return ‘len’, rather than ‘wrote’, because PluggableUSB
// calculates descriptor sizes by first having them write to an
// empty buffer (setting ‘this->maxWrite’ to 0). To accomodate
Expand Down Expand Up @@ -734,6 +738,7 @@ usb_dev& USBCore_::usbDev()
void USBCore_::transcSetup(usb_dev* usbd, uint8_t ep)
{
(void)ep;
this->controlWritten = false;

usb_reqsta reqstat = REQ_NOTSUPP;

Expand Down Expand Up @@ -764,6 +769,9 @@ void USBCore_::transcSetup(usb_dev* usbd, uint8_t ep)
if (reqstat == REQ_SUPP) {
if ((usbd->control.req.bmRequestType & USB_TRX_IN) != USB_TRX_IN) {
this->sendZLP(usbd, 0);
} else if (!this->controlWritten) {
// Send at least a ZLP if nothing was written, to avoid timeouts
this->sendZLP(usbd, 0);
}
} else {
usbd_ep_stall(usbd, 0);
Expand All @@ -784,6 +792,9 @@ void USBCore_::transcSetup(usb_dev* usbd, uint8_t ep)
if (reqstat == REQ_SUPP) {
if ((usbd->control.req.bmRequestType & USB_TRX_IN) != USB_TRX_IN) {
this->sendZLP(usbd, 0);
} else if (!this->controlWritten) {
// Send at least a ZLP if nothing was written, to avoid timeouts
this->sendZLP(usbd, 0);
}
} else {
usbd_ep_stall(usbd, 0);
Expand Down
2 changes: 2 additions & 0 deletions cores/arduino/USBCore.h
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,8 @@ class USBCore_
// TODO: verify that this only applies to the control endpoint’s use of wLength
// I think this is only on the setup packet, so it should be fine.
uint16_t maxWrite = 0;
// Whether a control write has occurred during a control transaction
bool controlWritten;

/*
* Pointers to the transaction routines specified by ‘usbd_init’.
Expand Down

0 comments on commit 4a3d515

Please sign in to comment.