cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F405 and HAL USB-CDC strange behavior: Com-Port error & USBD_MAX_STR_DESC_SIZ

Br33
Associate

Hi,

some questions about the HAL USB-CDC (USB_FS) implementation. I use a STM32F405 with TrueStudio and CubeMX (for code generation) and Win10. I changed, as suggested from ST, the heap (0x1000) and stack (0x800) size. The USB CDC user Buffers are 128 bytes each and the USBD_MAX_STR_DESC_SIZ Buffer is 256 bytes.

Questions to begin with:

What is the use case for USBD_MAX_STR_DESC_SIZ Buffer?

Do we store the configured USB-descriptors in this buffer? see https://www.beyondlogic.org/usbnutshell/usb5.shtml#DeviceDescriptors

How big should this Buffer be (minimal)?

My problem:

When i change the Buffersize (USBD_MAX_STR_DESC_SIZ) to lower values than 256 or higher values than 512 bytes the USB device is recognized by the OS (WIN10) but i cannot connect to the device/com-port via HTerm or an application. What could be the reason for this behavior?

2 REPLIES 2

I don't CubeMX so don't know what the generated code does exactly, but from my casual encounter with the examples in Cube, this buffer is used to expand strings to string descriptors (adding "header" and converting characters to 16-bit unicode). Thus, if shorter than twice the length of the longest string plus two, it will overflow in USBD_GetString(). I am not sure why longer would be a problem, maybe you've run out of total RAM space?

JW

Br33
Associate

Thank you,

the fix for my Problem was a temporary buffer in CDC_SET_LINE_CODING / CDC_GET_LINE_CODING. Well Windows 10 uses CDC-Commands..

/* USER CODE
static uint8_t tempbuf[7] 
*/
 
    case CDC_SET_LINE_CODING:
    	tempbuf[0] = pbuf[0];
    	tempbuf[1] = pbuf[1];
    	tempbuf[2] = pbuf[2];
    	tempbuf[3] = pbuf[3];
    	tempbuf[4] = pbuf[4];
    	tempbuf[5] = pbuf[5];
    	tempbuf[6] = pbuf[6];
 
    break;
 
    case CDC_GET_LINE_CODING:
      	pbuf[0] = tempbuf[0];
      	pbuf[1] = tempbuf[1];
      	pbuf[2] = tempbuf[2];
      	pbuf[3] = tempbuf[3];
      	pbuf[4] = tempbuf[4];
      	pbuf[5] = tempbuf[5];
      	pbuf[6] = tempbuf[6];
 
    break;

After this i could use any String Descriptor buffer size i wanted.

For everone else please note you should increase your heap size, change the CDC-TX/RX-Buffers to 64 Byte (if you use USB_FS) and check your autogenerated Device Descritors in usbd_desc.c (bDeviceClass, bDeviceSubClass, BDeviceProtocol )

further reading:

https://community.st.com/s/question/0D50X00009Xke3ESAR/usb-vcp-windows-10

https://community.st.com/s/feed/0D50X00009XkYSESA3

https://blog.brichacek.net/wp-content/uploads/2015/10/STM32F4-and-USB.pdf