Skip to content

Commit 81603cc

Browse files
committed
WIP timeouts
1 parent c63f96c commit 81603cc

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

cores/arduino/USBCore.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,14 @@ void EPBuffer<L>::init(uint8_t ep)
133133
this->reset();
134134
this->rxWaiting = false;
135135
this->txWaiting = false;
136+
this->timedOut = false;
137+
this->timeout = 100;
138+
}
139+
140+
template<size_t L>
141+
void EPBuffer<L>::setTimeout(uint16_t timeout)
142+
{
143+
this->timeout = timeout;
136144
}
137145

138146
template<size_t L>
@@ -236,6 +244,9 @@ void EPBuffer<L>::flush()
236244
// fall through
237245
case USBD_CONFIGURED:
238246
case USBD_SUSPENDED: {
247+
if (this->timedOut) {
248+
break;
249+
}
239250
// This will temporarily reenable and disable interrupts
240251
auto canWrite = this->waitForWriteComplete();
241252
if (canWrite) {
@@ -246,6 +257,7 @@ void EPBuffer<L>::flush()
246257
// Only start the next transmission if the device hasn't been
247258
// reset.
248259
this->txWaiting = true;
260+
this->startTime = millis();
249261
usbd_ep_send(&USBCore().usbDev(), this->ep, (uint8_t *)this->buf, this->len());
250262
USBCore().logEP('>', this->ep, '>', this->len());
251263
}
@@ -287,6 +299,7 @@ template<size_t L>
287299
void EPBuffer<L>::transcIn()
288300
{
289301
this->txWaiting = false;
302+
this->timedOut = false;
290303
}
291304

292305
// Unused?
@@ -306,6 +319,12 @@ bool EPBuffer<L>::waitForWriteComplete()
306319
auto ok = true;
307320
do {
308321
usb_enable_interrupts();
322+
if (this->txWaiting && millis() - this->startTime > this->timeout) {
323+
ok = false;
324+
this->timedOut = true;
325+
USBCore().logEP('X', this->ep, '>', this->len());
326+
break;
327+
}
309328
switch (USBCore().usbDev().cur_status) {
310329
case USBD_DEFAULT:
311330
case USBD_ADDRESSED:
@@ -854,6 +873,11 @@ int USBCore_::flush(uint8_t ep)
854873
return 0;
855874
}
856875

876+
void USBCore_::setTimeout(uint8_t ep, uint16_t timeout)
877+
{
878+
EPBuffers().buf(ep).setTimeout(timeout);
879+
}
880+
857881
void USBCore_::transcSetupHelper(usb_dev* usbd, uint8_t ep)
858882
{
859883
USBCore_* core = (USBCore_*)usbd->user_data;

cores/arduino/USBCore.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ class EPBuffer
7878
void flush();
7979
uint8_t* ptr();
8080
void enableOutEndpoint();
81+
void setTimeout(uint16_t timeout);
8182

8283
void transcIn();
8384
void transcOut();
@@ -114,6 +115,10 @@ class EPBuffer
114115
*/
115116
volatile bool currentlyFlushing = false;
116117

118+
volatile uint32_t startTime;
119+
uint16_t timeout;
120+
volatile bool timedOut;
121+
117122
uint8_t ep;
118123
};
119124

@@ -155,6 +160,7 @@ class USBCore_
155160
int recv(uint8_t ep, void* data, int len);
156161
int recv(uint8_t ep);
157162
int flush(uint8_t ep);
163+
void setTimeout(uint8_t ep, uint16_t timeout);
158164

159165
// Debug counters
160166
volatile uint16_t nreset;

0 commit comments

Comments
 (0)