cancel
Showing results for 
Search instead for 
Did you mean: 

Issues with STM32F4 USBX supporting both USB CDC and MSC

jason_gu
Associate

 

We are developing a product based on the STM32F469 with Azure RTOS / USBX.
The system requires both USB CDC and MSC device functionality:

  • CDC → serial communication
  • MSC → file transfer

Both operate as USB device classes.

We found that getting CDC and MSC working together on STM32F4 + USBX is difficult, and we have not found any official example demonstrating this.

The STM32F4 Azure RTOS package provides separate examples for CDC and MSC:

https://github.com/STMicroelectronics/x-cube-azrtos-f4/tree/main/Projects/STM32469I-Discovery/Applications/USBX

By following those examples, we can get either:

  • CDC working alone, or
  • MSC working alone

However, we need both classes in the same product.

We investigated two possible approaches:

  1. USB composite device (preferred)
  2. Runtime switching between CDC and MSC

Unfortunately, we have not been able to make either approach work reliably.


1. Composite Device Attempt

For the composite-device approach, we managed to get both classes successfully registered using:

 

 
_ux_device_stack_class_register()
 

Windows Device Manager correctly detects:

  • USB Composite Device
  • USB Mass Storage Device
  • USB Serial Device

However:

  • MSC works correctly
  • CDC does not function

Our suspicion is that the issue may be related to the USB FIFO configuration (HAL_PCDEx_SetRxFiFo() / HAL_PCDEx_SetTxFiFo()).

The STM32 examples only provide FIFO configurations for:

  • CDC alone, or
  • MSC alone

There is no example showing how FIFO sizes should be configured for CDC + MSC together.
We tried multiple FIFO combinations without success.

If anyone has a working FIFO configuration for STM32F469 + USBX CDC/MSC composite device, that would be very helpful.


2. Runtime Switching Attempt

As an alternative, we tried dynamically switching between CDC and MSC modes.

The idea was:

  • Tear down the current USB class
  • Reinitialize USBX
  • Start the other class

Simplified code:

 

 
void USBX_Start_CDC(void)
{
ux_system_initialize(...);

_ux_device_stack_initialize(...);

_ux_device_stack_class_register(
_ux_system_slave_class_cdc_acm_name,
_ux_device_class_cdc_acm_entry,
...);

HAL_PCD_Start(&hpcd_USB_OTG_FS);
}

void USBX_Start_MSC(void)
{
ux_system_initialize(...);

_ux_device_stack_initialize(...);

_ux_device_stack_class_register(
_ux_system_slave_class_storage_name,
_ux_device_class_storage_entry,
...);

HAL_PCD_Start(&hpcd_USB_OTG_FS);
}

void USBX_Switch_Mode(uint8_t new_mode)
{
HAL_PCD_Stop(&hpcd_USB_OTG_FS);

_ux_device_stack_disconnect();
_ux_device_stack_uninitialize();
ux_system_uninitialize();

HAL_Delay(200);

if (new_mode == USB_MODE_CDC)
USBX_Start_CDC();
else
USBX_Start_MSC();
}
 

However, this approach also fails.

It appears that the initialization sequence creates various RTOS objects and allocations:

  • threads
  • event flags
  • mutexes
  • memory allocations
  • etc.

During teardown, many (possibly all) of these resources do not appear to be fully deleted or released.

As a result:

  • The second initialization fails
  • Even reinitializing the same class can fail
  • Errors indicate corrupted/internal state or duplicate object creation

Our current conclusion is that:

USBX device stack initialization is effectively “one-time only” on STM32F4, and full teardown/reinitialization is not supported.


Questions

  1. Is there an official example of STM32F4 + USBX supporting CDC + MSC composite device?
  2. Are there recommended FIFO settings for CDC + MSC together?
  3. Does USBX officially support full device-stack teardown and reinitialization?
  4. If not, what is the recommended approach for products needing both CDC and MSC functionality?
2 REPLIES 2
Gyessine
ST Employee

Hello @jason_gu 
>Is there an official example of STM32F4 + USBX supporting CDC + MSC composite device?

For now, I think there is no official example that establishes your requirements. However, there are several articles that may be helpful for your use case, whether you continue using USBX or ST classic middleware. See article1, article2 and article3 .
Since you are using USBX MSC+CDC: you should ensure that you dedicated at least 11KB of memory for USBX

Gyessine_2-1779703978685.png

>If not, what is the recommended approach for products needing both CDC and MSC functionality?

I recommend using the first method, which is building a composite project. This approach is usually simpler than switching between classes at runtime, as switching often causes memory management issues.
If you still want to try switching classes, you can refer to the attached project.

The attached project is based on STM32U585. It starts as an HID device and then switches to a CDC device after five seconds. You can use the switch logic in app_usbx_device.c and ThreadX memory management in app_azure.c to create your application.
I hope my answer helps you and meets your expectations.
BR
Gyessine

 

 
 







To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

FBL
ST Employee

Hi @jason_gu @Gyessine 

Just to avoid confusion between FIFO configuration and USBX memory pool allocation, you can refer to this article and practical use cases here for fifo sizing.

You can start with article as mentioned @Gyessine , if you have issues integrating both classes feel free to mention the error and attach your project.

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.




Best regards,
FBL