AnsweredAssumed Answered

STM32F3Discovery USB CDC Receive loosing data

Question asked by Torben Hansen on Oct 18, 2017
Latest reply on Dec 6, 2017 by haselwood.don

I'm trying to make my STM32F3Dsicovery just loop back some data over USB however with code below I loosing approximate every 2nd character. If I enable the #if clause the missing characters drop to approximately 2%.

I'm using minicom as terminal

$ minicom -D /dev/ttyACM0

and the linux kernel 4.4.87 stock cdc_acm driver

I'm pasting 50 characters into the terminal. They seems to be sent as 50 single character packages.

Apart from the code below it's as generated from CubeMX, imported in eclipse as Makefile-project.

 

Any idea what I'm doing wrong?

 

I have the following in usb_cdc_if.c

 

/* USER CODE BEGIN PRIVATE_VARIABLES */
#define USER_DATA_SIZE (1<<8)
uint8_t ReadBuffer[USER_DATA_SIZE];
uint32_t ptrIn=0,ptrOut=0;
/* USER CODE END PRIVATE_VARIABLES */

 ...........snip ....

static int8_t CDC_Receive_FS (uint8_t* Buf, uint32_t *Len)
{
  /* USER CODE BEGIN 6 */
    uint32_t ptrCDC=0;
    while (ptrCDC<(*Len))
    {
        if (((ptrIn+1) & (USER_DATA_SIZE-1))!=ptrOut)
        {
            ReadBuffer[ptrIn++]=Buf[ptrCDC++];
            ptrIn&=(USER_DATA_SIZE-1);
        }
        else
        {
            return(USBD_FAIL);
        }
    }
#if 0
    for (int i=0;i<50;i++)
        ;
#endif
  USBD_CDC_SetRxBuffer(&hUsbDeviceFS, &Buf[0]);
  USBD_CDC_ReceivePacket(&hUsbDeviceFS);
  return (USBD_OK);
  /* USER CODE END 6 */
}

 

 

and in main()

  /* USER CODE BEGIN WHILE */
  while (1)
  {
  /* USER CODE END WHILE */

 

  /* USER CODE BEGIN 3 */
      uint8_t tBuffer[100];
      uint32_t rLen = CDC_Read(tBuffer,100);
      if (rLen>0)
          CDC_Transmit_FS(tBuffer, rLen);

 

  }
  /* USER CODE END 3 */

Attachments

Outcomes