Skip to content

Commit a912722

Browse files
committed
USB: Slightly refactored EP OUT handler
Now the release() function only performs the action that is called for, i.e. release the endpoint and let it receive data. All the buffers handling has been inlined in the callers, this also slightly improves performance because it allows to remove some redundant checks.
1 parent 87bf6e1 commit a912722

File tree

1 file changed

+26
-24
lines changed

1 file changed

+26
-24
lines changed

cores/arduino/USB/SAMD21_USBDevice.h

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,10 @@ void USBDevice_SAMD21G18x::calibrate() {
195195
usb.PADCAL.bit.TRIM = pad_trim;
196196
}
197197

198+
/*
199+
* USB EP generic handlers.
200+
*/
201+
198202
class EPHandler {
199203
public:
200204
virtual void handleEndpoint() = 0;
@@ -218,6 +222,8 @@ class DoubleBufferedEPOutHandler : public EPHandler {
218222
usbd.epBank0SetSize(ep, 64);
219223
usbd.epBank0SetType(ep, 3); // BULK OUT
220224

225+
usbd.epBank0SetAddress(ep, data0);
226+
221227
release();
222228
}
223229

@@ -229,12 +235,12 @@ class DoubleBufferedEPOutHandler : public EPHandler {
229235
}
230236
if (first0 == last0) {
231237
first0 = 0;
232-
last0 = 0;
238+
current = 1;
233239
ready0 = false;
234240
if (notify) {
241+
notify = false;
235242
release();
236243
}
237-
current = 1;
238244
return -1;
239245
}
240246
return data0[first0++];
@@ -244,12 +250,12 @@ class DoubleBufferedEPOutHandler : public EPHandler {
244250
}
245251
if (first1 == last1) {
246252
first1 = 0;
247-
last1 = 0;
253+
current = 0;
248254
ready1 = false;
249255
if (notify) {
256+
notify = false;
250257
release();
251258
}
252-
current = 0;
253259
return -1;
254260
}
255261
return data1[first1++];
@@ -262,16 +268,29 @@ class DoubleBufferedEPOutHandler : public EPHandler {
262268
{
263269
// Ack Transfer complete
264270
usbd.epBank0AckTransferComplete(ep);
271+
//usbd.epBank0AckTransferFailed(ep);
265272

266273
// Update counters and swap banks
267274
if (incoming == 0) {
268275
last0 = usbd.epBank0ByteCount(ep);
269-
ready0 = true;
270276
incoming = 1;
277+
usbd.epBank0SetAddress(ep, data1);
278+
ready0 = true;
279+
if (ready1) {
280+
notify = true;
281+
return;
282+
}
283+
notify = false;
271284
} else {
272285
last1 = usbd.epBank0ByteCount(ep);
273-
ready1 = true;
274286
incoming = 0;
287+
usbd.epBank0SetAddress(ep, data0);
288+
ready1 = true;
289+
if (ready0) {
290+
notify = true;
291+
return;
292+
}
293+
notify = false;
275294
}
276295
release();
277296
}
@@ -295,28 +314,11 @@ class DoubleBufferedEPOutHandler : public EPHandler {
295314
}
296315

297316
void release() {
298-
if (incoming == 0) {
299-
if (ready0) {
300-
notify = true;
301-
return;
302-
}
303-
usbd.epBank0SetAddress(ep, data0);
304-
} else {
305-
if (ready1) {
306-
notify = true;
307-
return;
308-
}
309-
usbd.epBank0SetAddress(ep, data1);
310-
}
311-
usbd.epBank0AckTransferComplete(ep);
312-
//usbd.epBank0AckTransferFailed(ep);
313-
usbd.epBank0EnableTransferComplete(ep);
314-
315317
// Release OUT EP
318+
usbd.epBank0EnableTransferComplete(ep);
316319
usbd.epBank0SetMultiPacketSize(ep, size);
317320
usbd.epBank0SetByteCount(ep, 0);
318321
usbd.epBank0ResetReady(ep);
319-
notify = false;
320322
}
321323

322324
private:

0 commit comments

Comments
 (0)