STM32 usb lib: how does USBD_CDC_SetRxBuffer know that buffer size?

Viewed 2548

I'm trying to get some basic USB CDC stuff to work on the stm32f103 blue pill.

In the examples, there are these code snippets

#define APP_RX_DATA_SIZE  8
uint8_t UserRxBufferFS[APP_RX_DATA_SIZE];

static int8_t CDC_Init_FS(void)
{ 
  /* USER CODE BEGIN 3 */ 
  /* Set Application Buffers */
  USBD_CDC_SetTxBuffer(&hUsbDeviceFS, UserTxBufferFS, 0);
  USBD_CDC_SetRxBuffer(&hUsbDeviceFS, UserRxBufferFS);
  return (USBD_OK);
  /* USER CODE END 3 */ 
}

so... the function USBD_CDC_SetRxBuffer doesn't take length as an argument. How can that be?

When I send a dumb string of 144 chars from a terminal, I get the data in chunks of 64.

With the length of 8 above this means the buffer overflows. I read somewhere that USB stuff happens in packet sizes as large as 64. ok.

So on what basis should I set APP_RX_DATA_SIZE? How to I prevent overflows?

Why doesn't USBD_CDC_SetRxBuffer take the buffer size as an argument?

1 Answers

As you got it, data comes in chunks of 64.

So if you have a buffer larger than 64 bytes, it won't overflow by a single packet.

Then, next packet won't come until you call USBD_CDC_ReceivePacket().

You will have enough time to play with the data before calling USBD_CDC_ReceivePacket().

Related