Skip to content

Commit

Permalink
WIP timeouts
Browse files Browse the repository at this point in the history
  • Loading branch information
tlyu committed Dec 9, 2022
1 parent c63f96c commit 81603cc
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
24 changes: 24 additions & 0 deletions cores/arduino/USBCore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,14 @@ void EPBuffer<L>::init(uint8_t ep)
this->reset();
this->rxWaiting = false;
this->txWaiting = false;
this->timedOut = false;
this->timeout = 100;
}

template<size_t L>
void EPBuffer<L>::setTimeout(uint16_t timeout)
{
this->timeout = timeout;
}

template<size_t L>
Expand Down Expand Up @@ -236,6 +244,9 @@ void EPBuffer<L>::flush()
// fall through
case USBD_CONFIGURED:
case USBD_SUSPENDED: {
if (this->timedOut) {
break;
}
// This will temporarily reenable and disable interrupts
auto canWrite = this->waitForWriteComplete();
if (canWrite) {
Expand All @@ -246,6 +257,7 @@ void EPBuffer<L>::flush()
// Only start the next transmission if the device hasn't been
// reset.
this->txWaiting = true;
this->startTime = millis();
usbd_ep_send(&USBCore().usbDev(), this->ep, (uint8_t *)this->buf, this->len());
USBCore().logEP('>', this->ep, '>', this->len());
}
Expand Down Expand Up @@ -287,6 +299,7 @@ template<size_t L>
void EPBuffer<L>::transcIn()
{
this->txWaiting = false;
this->timedOut = false;
}

// Unused?
Expand All @@ -306,6 +319,12 @@ bool EPBuffer<L>::waitForWriteComplete()
auto ok = true;
do {
usb_enable_interrupts();
if (this->txWaiting && millis() - this->startTime > this->timeout) {
ok = false;
this->timedOut = true;
USBCore().logEP('X', this->ep, '>', this->len());
break;
}
switch (USBCore().usbDev().cur_status) {
case USBD_DEFAULT:
case USBD_ADDRESSED:
Expand Down Expand Up @@ -854,6 +873,11 @@ int USBCore_::flush(uint8_t ep)
return 0;
}

void USBCore_::setTimeout(uint8_t ep, uint16_t timeout)
{
EPBuffers().buf(ep).setTimeout(timeout);
}

void USBCore_::transcSetupHelper(usb_dev* usbd, uint8_t ep)
{
USBCore_* core = (USBCore_*)usbd->user_data;
Expand Down
6 changes: 6 additions & 0 deletions cores/arduino/USBCore.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ class EPBuffer
void flush();
uint8_t* ptr();
void enableOutEndpoint();
void setTimeout(uint16_t timeout);

void transcIn();
void transcOut();
Expand Down Expand Up @@ -114,6 +115,10 @@ class EPBuffer
*/
volatile bool currentlyFlushing = false;

volatile uint32_t startTime;
uint16_t timeout;
volatile bool timedOut;

uint8_t ep;
};

Expand Down Expand Up @@ -155,6 +160,7 @@ class USBCore_
int recv(uint8_t ep, void* data, int len);
int recv(uint8_t ep);
int flush(uint8_t ep);
void setTimeout(uint8_t ep, uint16_t timeout);

// Debug counters
volatile uint16_t nreset;
Expand Down

0 comments on commit 81603cc

Please sign in to comment.