@@ -251,14 +251,12 @@ int USB_Recv(u8 ep)
251
251
}
252
252
253
253
// Space in send EP
254
- u8 USB_SendSpace (u8 ep)
254
+ int8_t USB_SendSpace (u8 ep)
255
255
{
256
256
LockEP lock (ep);
257
257
if (!ReadWriteAllowed ())
258
- return 0 ;
259
- // subtract 1 from the EP size to never send a full packet,
260
- // this avoids dealing with ZLP's in USB_Send
261
- return USB_EP_SIZE - 1 - FifoByteCount ();
258
+ return -1 ;
259
+ return USB_EP_SIZE - FifoByteCount ();
262
260
}
263
261
264
262
// Blocking Send of data to an endpoint
@@ -273,12 +271,15 @@ int USB_Send(u8 ep, const void* d, int len)
273
271
}
274
272
275
273
int r = len;
274
+
275
+ bool sendZlp = (len % USB_EP_SIZE) == 0 ;
276
+
276
277
const u8* data = (const u8*)d;
277
278
u8 timeout = 250 ; // 250ms timeout on send? TODO
278
- while (len)
279
+ while (len || sendZlp )
279
280
{
280
- u8 n = USB_SendSpace (ep);
281
- if (n == 0 )
281
+ int8_t n = USB_SendSpace (ep);
282
+ if (n < 0 )
282
283
{
283
284
if (!(--timeout))
284
285
return -1 ;
@@ -293,6 +294,13 @@ int USB_Send(u8 ep, const void* d, int len)
293
294
// Frame may have been released by the SOF interrupt handler
294
295
if (!ReadWriteAllowed ())
295
296
continue ;
297
+
298
+ if (len == 0 && sendZlp) {
299
+ // empty transfer sent
300
+ Send8 (0 );
301
+ sendZlp = false ;
302
+ }
303
+
296
304
len -= n;
297
305
if (ep & TRANSFER_ZERO)
298
306
{
@@ -475,7 +483,7 @@ static
475
483
bool SendConfiguration (int maxlen)
476
484
{
477
485
// Count and measure interfaces
478
- InitControl (0 );
486
+ InitControl (0 );
479
487
u8 interfaces = SendInterfaces ();
480
488
ConfigDescriptor config = D_CONFIG (_cmark + sizeof (ConfigDescriptor),interfaces);
481
489
0 commit comments