plusk01 / airdamon_f3

Be the Matt Damon of the skies
2 stars 1 forks source link

don't try and print if usb not connected for vcp #2

Open plusk01 opened 6 years ago

plusk01 commented 6 years ago

Currently, if using VCP and trying to write, but no one is consuming the data on the other side, there is a timeout and it can take block, particularly when using printf (which isn't a huge problem as printf is only for debugging). There is also a barely-noticeable delay when using the VCP::write() method.

It seems that it is the fault of the following USB_TIMEOUT code:

void VCP::write(uint8_t* ch, uint8_t len)
{
  if (!usbIsConnected() || !usbIsConfigured()) return;

  uint32_t start = millis();
  while (len > 0)
  {
    uint32_t num_bytes_sent = CDC_Send_DATA(ch, len);
    len -= num_bytes_sent;
    ch += num_bytes_sent;

    if (len == 0 || millis() > (start + USB_TIMEOUT))
      break;
  }
}

It would be better if there was a way to say "If the host USB device is not ready to consume data, don't even try and send." Although I'm not sure if there is an easy way to know that (but I feel like there should be).