@@ -251,14 +251,12 @@ int USB_Recv(u8 ep)
251251}
252252
253253// Space in send EP
254- u8 USB_SendSpace (u8 ep)
254+ int8_t USB_SendSpace (u8 ep)
255255{
256256 LockEP lock (ep);
257257 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 ();
262260}
263261
264262// Blocking Send of data to an endpoint
@@ -273,12 +271,15 @@ int USB_Send(u8 ep, const void* d, int len)
273271 }
274272
275273 int r = len;
274+
275+ bool sendZlp = (len % USB_EP_SIZE) == 0 ;
276+
276277 const u8 * data = (const u8 *)d;
277278 u8 timeout = 250 ; // 250ms timeout on send? TODO
278- while (len)
279+ while (len || sendZlp )
279280 {
280- u8 n = USB_SendSpace (ep);
281- if (n == 0 )
281+ int8_t n = USB_SendSpace (ep);
282+ if (n < 0 )
282283 {
283284 if (!(--timeout))
284285 return -1 ;
@@ -293,6 +294,13 @@ int USB_Send(u8 ep, const void* d, int len)
293294 // Frame may have been released by the SOF interrupt handler
294295 if (!ReadWriteAllowed ())
295296 continue ;
297+
298+ if (len == 0 && sendZlp) {
299+ // empty transfer sent
300+ Send8 (0 );
301+ sendZlp = false ;
302+ }
303+
296304 len -= n;
297305 if (ep & TRANSFER_ZERO)
298306 {
@@ -475,7 +483,7 @@ static
475483bool SendConfiguration (int maxlen)
476484{
477485 // Count and measure interfaces
478- InitControl (0 );
486+ InitControl (0 );
479487 u8 interfaces = SendInterfaces ();
480488 ConfigDescriptor config = D_CONFIG (_cmark + sizeof (ConfigDescriptor),interfaces);
481489
0 commit comments