Skip to content

Commit

Permalink
Fix race condition in USB CDC transmit
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
pbrook authored and cmaglie committed May 23, 2014
1 parent b822091 commit 13c0db5
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion hardware/arduino/cores/arduino/USBCore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -290,9 +290,12 @@ int USB_Send(u8 ep, const void* d, int len)

if (n > len)
n = len;
len -= n;
{
LockEP lock(ep);
// Frame may have been released by the SOF interrupt handler
if (!ReadWriteAllowed())
continue;
len -= n;
if (ep & TRANSFER_ZERO)
{
while (n--)
Expand Down

0 comments on commit 13c0db5

Please sign in to comment.