Skip to content

Commit 13c0db5

Browse files
pbrookcmaglie
authored andcommitted
Fix race condition in USB CDC transmit
If the Start of Frame interrupt triggers just after the call to USB_SendSpace in USB_Send then we can get data loss. When the first bank is full and the second partially full, the SOF handler will release the second bank via USB_Flush. Data is then lost due to overflow as USB_Send continues writing data to the now-closed bank. Fix this by re-checking the FIFO status inside LockEP, immediately before doing the data write. Signed-off-by: Paul Brook <[email protected]>
1 parent b822091 commit 13c0db5

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

hardware/arduino/cores/arduino/USBCore.cpp

+4-1
Original file line numberDiff line numberDiff line change
@@ -290,9 +290,12 @@ int USB_Send(u8 ep, const void* d, int len)
290290

291291
if (n > len)
292292
n = len;
293-
len -= n;
294293
{
295294
LockEP lock(ep);
295+
// Frame may have been released by the SOF interrupt handler
296+
if (!ReadWriteAllowed())
297+
continue;
298+
len -= n;
296299
if (ep & TRANSFER_ZERO)
297300
{
298301
while (n--)

0 commit comments

Comments
 (0)