cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F103 USB cdc receiver is very slow

SS.Sagar
Associate III

i am using stm32f103 for one project. Previously we were using UART interface to transfer data but now we want to use USB CDC interface for the same.

i configured the usb in cdc mode and it is working also.

I am facing one issue like data receiving is very slow. If i send 20-30 bytes characters then i am getting only 2-3 characters on the screen.

Followign are the changes i have done in usbd_cdc_if.c

static int8_t CDC_Receive_FS(uint8_t* Buf, uint32_t *Len)
{
  /* USER CODE BEGIN 6 */
  USBD_CDC_SetRxBuffer(&hUsbDeviceFS, &Buf[0]);
  USBD_CDC_ReceivePacket(&hUsbDeviceFS);
 
 
  memset (usb_cdc_buffer, '\0', 64);  // clear the buffer
  uint8_t len = (uint8_t)*Len;
  memcpy(usb_cdc_buffer, Buf, len);  // copy the data to the buffer
  memset(Buf, '\0', len);   // clear the Buf also
  usb_cdc_rx_flag = 1;
  
  return (USBD_OK);
  /* USER CODE END 6 */
}

and in while loop of the main function i am checking for the flag and transmitting it like follow,

if(usb_cdc_rx_flag)
	  {
		  CDC_Transmit_FS(usb_cdc_buffer,64);
		  usb_cdc_rx_flag = 0;
	  }

Any help will be appreciated.

3 REPLIES 3
TDK
Guru

> If i send 20-30 bytes characters then i am getting only 2-3 characters on the screen.

In your example, you're sending 64 bytes. As long as you're sending 64 bytes or less, they all go in the same packet and either all of them will be received, or none of them.

Using DMA and sending more than 64 bytes at a time will speed things up.

If you feel a post has answered your question, please click "Accept as Solution".
SS.Sagar
Associate III

Yes, Here i am copying only 64 bytes. This callback gets hit whenever USB receives any data. So even if send 20-30 bytes then i am getting few of the bytes.

Sure i will check with DMA as well.

TDK
Guru

It just doesn't add up. USB isn't like UART where characters are sent out one by one. USB uses packets with a payload of length 64 (or less). Either all characters in the packet are received, or none are.

If you feel a post has answered your question, please click "Accept as Solution".