AnsweredAssumed Answered

problems with USB lib CDC example

Question asked by van_thielen.bart on Nov 19, 2012
Latest reply on Feb 18, 2014 by krutecki
I want to use the CDC example to send ADC data to a PC in a fast way.
My problem is that I seem to be losing data erratically, especially if I send more data.

I changed the  usbd_cdc_VCP.c file in the following way (see code below):
- when DataRx is called (when a character comes in) it will set a counter (StartSend)
- I added a SOF callback that is called from usbd_cdc_core.c on each SOF
  In this callback, the counter is decremented and a 250 char block is sent.
  This is repeated this the counter hits zero

If I set StartSend to values below 8 (2000 chars, spread over 8 frames) then I get the right number of characters in the RealTerm terminal program that I am using in 95 % of cases
In the other 5% I first get 976 chars and 3024 on the next character that I send.


If I set StartSend to higher values (9 for example), I get even more problems:
202,202,202,2250,202,202,202,... chars received on each 2250 chars send.

If I set the circular buffer (APP_Rx_Buffer) size from 2048 to 2148 bytes long. then this changes to:
..., 102,102,102,2250,102,102,102,...

So in both cases, the number of disappeared chars seems to be equal to the buffer size (APP_RX_DATA_SIZE)

The code changes are here below:

Does anybody have an idea of what might be going wrong here?




static uint16_t RP700_DataRx (uint8_t* Buf, uint32_t Len)
{
  uint32_t i,n;
  uint8_t Buffer[APP_RX_DATA_SIZE];
 
  STM_EVAL_LEDToggle(LED3);
  StartSend=4;

  return USBD_OK;
}

static uint16_t RP700_DataBufTx (uint8_t* Buf, uint32_t Len)
{
  uint32_t i;
 
  for (i = 0; i < Len; i++)
  {
     APP_Rx_Buffer[APP_Rx_ptr_in] = i;
     APP_Rx_ptr_in++;
     if(APP_Rx_ptr_in == APP_RX_DATA_SIZE) {
       APP_Rx_ptr_in = 0; /* To avoid buffer overflow */
       STM_EVAL_LEDToggle(LED4); // Blue led
      }
  } 
  return USBD_OK;
}


static uint16_t RP700_SOF ()
{
  uint32_t i,n;
  uint8_t Buffer[APP_RX_DATA_SIZE];
   
  STM_EVAL_LEDToggle(LED2);  // top RED LED
  if (StartSend) {  RP700_DataBufTx (Buffer, 250); StartSend--; }  // 20x 250 chars

  return USBD_OK;
}

Outcomes