@@ -133,6 +133,14 @@ void EPBuffer<L>::init(uint8_t ep)
133
133
this ->reset ();
134
134
this ->rxWaiting = false ;
135
135
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;
136
144
}
137
145
138
146
template <size_t L>
@@ -236,6 +244,9 @@ void EPBuffer<L>::flush()
236
244
// fall through
237
245
case USBD_CONFIGURED:
238
246
case USBD_SUSPENDED: {
247
+ if (this ->timedOut ) {
248
+ break ;
249
+ }
239
250
// This will temporarily reenable and disable interrupts
240
251
auto canWrite = this ->waitForWriteComplete ();
241
252
if (canWrite) {
@@ -246,6 +257,7 @@ void EPBuffer<L>::flush()
246
257
// Only start the next transmission if the device hasn't been
247
258
// reset.
248
259
this ->txWaiting = true ;
260
+ this ->startTime = millis ();
249
261
usbd_ep_send (&USBCore ().usbDev (), this ->ep , (uint8_t *)this ->buf , this ->len ());
250
262
USBCore ().logEP (' >' , this ->ep , ' >' , this ->len ());
251
263
}
@@ -287,6 +299,7 @@ template<size_t L>
287
299
void EPBuffer<L>::transcIn()
288
300
{
289
301
this ->txWaiting = false ;
302
+ this ->timedOut = false ;
290
303
}
291
304
292
305
// Unused?
@@ -306,6 +319,12 @@ bool EPBuffer<L>::waitForWriteComplete()
306
319
auto ok = true ;
307
320
do {
308
321
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
+ }
309
328
switch (USBCore ().usbDev ().cur_status ) {
310
329
case USBD_DEFAULT:
311
330
case USBD_ADDRESSED:
@@ -854,6 +873,11 @@ int USBCore_::flush(uint8_t ep)
854
873
return 0 ;
855
874
}
856
875
876
+ void USBCore_::setTimeout (uint8_t ep, uint16_t timeout)
877
+ {
878
+ EPBuffers ().buf (ep).setTimeout (timeout);
879
+ }
880
+
857
881
void USBCore_::transcSetupHelper (usb_dev* usbd, uint8_t ep)
858
882
{
859
883
USBCore_* core = (USBCore_*)usbd->user_data ;
0 commit comments