Skip to main content
ABasi.2
Senior
November 19, 2025
Question

USBX MSC SD card + FILEX SD card problem

  • November 19, 2025
  • 3 replies
  • 414 views

Hi, i'm trying to make a firmware that can read an SD card with filex but that can also connect that SD card to USBX MSC device (no simultaney).

 

i'm able to do the two functionality in two separate firmware, but the problem is to merge.

 

for now is sufficient that the card is read at start up and next i can see the SD card with USBX MSC device

when i can do that the firmware shall evolve to connect the SD card to USBX MSC and after a modification by PC disconnect the SD card the time necessary to see if something is changed and do stuff

 

unfortunately the first step is totally ok, at startUp i can read the SDcard with  FILEX but.. after that USBX wont work

 

i'm using a threadX flag to indicate at the USB that is possible to use the SD card, there is some example about myu goal?

i will attach to this message the relevant files..

 

thank you in advance for any hint.

 

best reguards

 

3 replies

T_Hamdi
ST Employee
November 19, 2025

Hello @ABasi.2

Yes, it is possible to combine both functionalities  SD card access using FileX and exposing the SD card as a USB MSC device with USBX .

The key is to switch between the two modes, ensuring that each mode cleanly closes access to the SD card before switching. For example, you can use a button or a signal to trigger this mode change.

You can refer to the following STM32CubeH5 examples on GitHub:

These two examples can be integrated into the same firmware. Then by implementing a switch (button or signal) according to your use case, you can toggle between the two modes. At each switch, make sure to properly manage closing and reopening the SD card access to avoid conflicts.

 

"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.Hamdi Teyeb"
ABasi.2
ABasi.2Author
Senior
November 19, 2025

Hello T_Hamdi and thank you for your link

BUT , as i said in the message i am already able to make the two separate firmware

the problem is in the switch metod, if you see my file you see that in this case i'm opening the media, do stff, closing the media in the app_filex.c file , after that i set a flag

 

only after the flag setted in the USBX part the firmware is using the SD card (actually i start USB after the flag), so i'm expecting there is no conflict at all.. BUT when i plug in the USB Cable someting goes wrong

 

it seems is not possible to opening the sd media in the USBX file.. and i don't understand why..

 

thank you for any advice

 

best reguards

T_Hamdi
ST Employee
November 20, 2025

Hello @ABasi.2 

Thank you for your explanation. To better understand the issue and provide you with accurate advice, could you please share the main.c file where the application handles the switching between the USBx MSC and Filex ?

 

"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.Hamdi Teyeb"
ABasi.2
ABasi.2Author
Senior
November 20, 2025

Hello T_Hamdi

 

i don't have a switch in the main.c file

 

the "switch" is inside app_filex.c

 

the last line is 

tx_event_flags_set(&basXflags,0x02, TX_OR);

here i set a flag

 

and after that i have the sdio_disk media already closed (in this example i don't need to reopen a file)

 

in app_usbx_device.c 

 

there is :

static VOID app_ux_device_thread_entry(ULONG thread_input)
{
 /* USER CODE BEGIN app_ux_device_thread_entry */
 TX_PARAMETER_NOT_USED(thread_input);

 ULONG flags;

 // Initialization of USB device
 MX_USB_PCD_Init();

 // PMA from 0x00 to 0x3F -> BTABLE
 HAL_PCDEx_PMAConfig(&hpcd_USB_DRD_FS, 0x00, PCD_SNG_BUF, 0x40); // endpoint 0 OUT -> CMD OUT
 HAL_PCDEx_PMAConfig(&hpcd_USB_DRD_FS, 0x80, PCD_SNG_BUF, 0x80); // endpoint 0 IN -> CMD IN
 HAL_PCDEx_PMAConfig(&hpcd_USB_DRD_FS, 0x81, PCD_SNG_BUF, 0xC0); // endpoint 1 IN -> MSC IN
 HAL_PCDEx_PMAConfig(&hpcd_USB_DRD_FS, 0x01, PCD_SNG_BUF, 0x100); // endpoint 1 OUT -> MSC OUT

 _ux_dcd_stm32_initialize((ULONG)USB_DRD_FS, (ULONG)&hpcd_USB_DRD_FS);


 tx_event_flags_get(&basXflags,0x02, TX_OR,&flags, TX_WAIT_FOREVER);



 HAL_PCD_Start(&hpcd_USB_DRD_FS); // Start device USB

 where i wayt forever thiat flag to start the usb device

 

after that in ux_device_msc. this function are called:

 

ULONG USBD_STORAGE_GetMediaLastLba(VOID)
{
 ULONG LastLba = 0U;

 /* USER CODE BEGIN USBD_STORAGE_GetMediaLastLba */

 if(sdio_disk.fx_media_id != FX_MEDIA_ID)
 {
 fx_media_open(&sdio_disk, FX_SD_VOLUME_NAME, fx_stm32_sd_driver, (VOID *)FX_NULL, (VOID *) fx_sd_media_memory, sizeof(fx_sd_media_memory));
 }
 //if(sdio_disk.fx_media_id == FX_MEDIA_ID)
 {
 LastLba = sdio_disk.fx_media_total_sectors - 1;
 }
 /* USER CODE END USBD_STORAGE_GetMediaLastLba */

 return LastLba;
}

/**
 * @brief USBD_STORAGE_GetMediaBlocklength
 * Get Media block length.
 * @param none.
 * @retval block length.
 */
ULONG USBD_STORAGE_GetMediaBlocklength(VOID)
{
 ULONG MediaBlockLen = 0U;

 /* USER CODE BEGIN USBD_STORAGE_GetMediaBlocklength */
 if(sdio_disk.fx_media_id != FX_MEDIA_ID)
 {
 fx_media_open(&sdio_disk, FX_SD_VOLUME_NAME, fx_stm32_sd_driver, (VOID *)FX_NULL, (VOID *) fx_sd_media_memory, sizeof(fx_sd_media_memory));
 }

 //if(sdio_disk.fx_media_id == FX_MEDIA_ID)
 {
 MediaBlockLen = sdio_disk.fx_media_bytes_per_sector;
 }
 /* USER CODE END USBD_STORAGE_GetMediaBlocklength */

 return MediaBlockLen;
}

 but

fx_media_open(&sdio_disk, FX_SD_VOLUME_NAME, fx_stm32_sd_driver, (VOID *)FX_NULL, (VOID *) fx_sd_media_memory, sizeof(fx_sd_media_memory));

fails!

 

and i don't understand why.. the media is closed.. why i can open it in this file?

 

thank you

 

best reguards

T_Hamdi
ST Employee
November 25, 2025

hello @ABasi.2 

To avoid the unexpected behavior caused by USBX MSC functions (USBD_STORAGE_GetMediaLastLba and USBD_STORAGE_GetMediaBlocklength) being called before FileX initialization is complete, I propose modifying the initialization sequence to start USBX MSC only after FileX has fully completed its initialization.

Specifically, this means:

  • Perform all FileX initialization steps (media open, close, ...) in a dedicated thread or task.
  • Once FileX initialization is fully done and the media is ready, signal this event using a ThreadX event flag, 
  • Only after receiving this signal, start the USBX MSC thread or trigger USBX MSC initialization.

This approach ensures that USBX functions querying the media will only be called after FileX has successfully opened and prepared the SD card, preventing premature calls that cause errors or unexpected non-blocking behavior.

"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.Hamdi Teyeb"
ABasi.2
ABasi.2Author
Senior
November 25, 2025

thank you for your response @T_Hamdi 

can you provide a little example of that? 

as i said in my last post the "flag approach" seems have some problem

 

a little example code is really appreciated

 

best reguards