Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

usb

stm32

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?

like image 698
mmccoo Avatar asked Mar 21 '26 00:03

mmccoo


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().

like image 108
user2686101 Avatar answered Mar 24 '26 22:03

user2686101



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!